資源描述:
《Pascal版2048》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、programg2048;constdx:array[1..4]ofinteger=(1,0,-1,0);dy:array[1..4]ofinteger=(0,1,0,-1);vara:array[0..5,0..5]ofinteger;spacei,spacej:array[1..16]ofinteger;i,j,z:integer;step0:longint;q:boolean;procedureprint;vark1,k2:integer;beginfork1:=1to4dobeginfork2:=1to4dowrite(a[k1,k2
2、]:4);writeln;end;end;functionremove:boolean;varz1,z2,z3:integer;beginremove:=false;forz1:=1to4doforz2:=1to4doforz3:=1to4doifa[z1,z2]=a[z1+dx[z3],z2+dy[z3]]thenbeginremove:=true;exit;end;end;functionover:boolean;vark1,k2:integer;beginover:=false;fork1:=1to4dofork2:=1to4doif(
3、a[k1,k2]=0)orremovethenbeginover:=true;exit;end;end;functionjumpw(i1,i2,i3:integer):boolean;vark1:integer;beginjumpw:=true;fork1:=i3+1toi1-1doifa[k1,i2]<>0thenjumpw:=false;end;functionjumps(i1,i2,i3:integer):boolean;vark1:integer;beginjumps:=true;fork1:=i1+1toi3-1doifa[k1,i
4、2]<>0thenjumps:=false;end;functionjumpa(i1,i2,i3:integer):boolean;vark1:integer;beginjumpa:=true;fork1:=i3+1toi2-1doifa[i1,k1]<>0thenjumpa:=false;end;functionjumpd(i1,i2,i3:integer):boolean;vark1:integer;beginjumpd:=true;fork1:=i2+1toi3-1doifa[i1,k1]<>0thenjumpd:=false;end;
5、procedureborn;vark1,k2,k3,ra:integer;beginfork1:=1to4dofork2:=1to4doifa[k1,k2]=0thenbegininc(step0);spacei[step0]:=k1;spacej[step0]:=k2;end;ifstep0=1thena[spacei[1],spacej[1]]:=2elsebeginra:=random(step0-1)+1;a[spacei[ra],spacej[ra]]:=2;end;print;q:=over;end;proceduretry;va
6、ri1,i2,i3:integer;ch:char;move:boolean;beginch:='#';move:=false;while(ch<>'a')and(ch<>'s')and(ch<>'w')and(ch<>'d')doread(ch);casechof'w':beginfori1:=2to4dofori2:=1to4dofori3:=1toi1-1dobeginifa[i1,i2]<>0thenbeginif(a[i3,i2]=0)and(jumpw(i1,i2,i3))thenbegina[i3,i2]:=a[i1,i2];a
7、[i1,i2]:=0;move:=true;break;end;if(a[i3,i2]=a[i1,i2])and(jumpw(i1,i2,i3))thenbegina[i3,i2]:=2*a[i1,i2];a[i1,i2]:=0;move:=true;ifa[i3,i2]=2048thenbeginwriteln('Yeah!Iwin!233');readln;halt;endelsebreak;end;end;end;end;'s':beginfori1:=3downto1dofori2:=1to4dofori3:=4downtoi1+1d
8、obeginifa[i1,i2]<>0thenbeginif(a[i3,i2]=0)and(jumps(i1,i2,i3))thenbegina[i3,i2]:=a