Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help 2048game in pascal

berytaberyta argentinaMember Posts: 1
edited June 2015 in Pascal

This is a game i need to present for my programming class.
It is the almighty 2048 game..
http://gabrielecirulli.github.io/2048/
and we have to do a simpler version in pascal language.
This is the code i've made till now..

program game2048;
uses crt;
const max = 4;
type matriz = array [1..max,1..max] of integer;
var tabla:matriz;
key:char;

procedure random2 (var k:matriz);
var n1,n2:integer;
begin
repeat
randomize;
n1:=random(3)+1;
n2:=random(3)+1
until k[n1,n2]=0;
k[n1,n2]:=2;
end;

procedure cargaMatriz (var s:matriz);
var i,j:integer;
begin
for i:=1 to max do
begin
for j:=1 to max do
begin
s[i,j]:=0;
end;
end;
random2(s);
random2(s);
end;

function check0 (s:matriz):boolean;
var i,j,c:integer;
begin
c:=0;
for i:=1 to max do
begin
for j:= 1 to max do
begin
if s[i,j]=0 then
c:=c+1;
end;
end;
if c=0 then
check0:=false
else
check0:=true;
end;

procedure printM (var s:matriz);
var i,j:integer;
begin
textbackground(lightgray);
textcolor(black);
clrScr;
for i:= 1 to max do
begin
for j:= 1 to max do
begin
if (check0(s)) then
write(s[i,j],' ')
else
write('c*est fini');
end;
writeln;
end;
end;

procedure movr(var s:matriz);
var i,j,k,aux:integer;
begin
for i:= 1 to max do
for j:= max downto 1 do
for K:= j downto 1 do
begin
if ((s[i,j]=0) and (s[i,k]<>0)) then
begin
aux:=s[i,j];
s[i,j]:=s[i,k];
s[i,k]:=aux;
end;
end;
end;

procedure merger(var s:matriz);
var i,j: integer;
begin
for i:=1 to max do
for j:= 2 to max do
if (s[i,j]=s[i,j-1]) then
begin
s[i,j-1]:=s[i,j-1]+s[i,j];
s[i,j]:=0;
end;
end;

procedure movl(var s:matriz);
var i,j,k,aux:integer;
begin
clrScr;
for i:= 1 to max do
for j:= 1 to max do
for K:= j to max do
begin
if ((s[i,j]=0) and (s[i,k]<>0)) then
begin
aux:=s[i,j];
s[i,j]:=s[i,k];
s[i,k]:=aux;
end;
end;
end;

procedure mergel(var s:matriz);
var i,j: integer;
begin
for i:=1 to max do
for j:= 1 to max-1 do
if (s[i,j]=s[i,j+1]) then
begin
s[i,j]:=s[i,j]+s[i,j+1];
s[i,j+1]:=0;
end;
end;

procedure movu(var s:matriz);
var i,j,k,aux:integer;
begin
clrScr;
for i:= 1 to max do
for j:= 1 to max do
for K:= i to max do
begin
if ((s[i,j]=0) and (s[k,j]<>0)) then
begin
aux:=s[i,j];
s[i,j]:=s[k,j];
s[k,j]:=aux;
end;
end;
end;

procedure mergeu(var s:matriz);
var i,j:integer;
begin
for j:= 1 to max do
for i:=1 to max-1 do
if (s[i,j]=s[i+1,j]) then
begin
s[i,j]:=s[i+1,j]+s[i,j];
s[i+1,j]:=0;
end;
end;

procedure movd(var s:matriz);
var i,j,k,aux:integer;
begin
clrScr;
for i:= max downto 1 do
for j:= 1 to max do
for K:= i downto 1 do
begin
if ((s[i,j]=0) and (s[k,j]<>0)) then
begin
aux:=s[i,j];
s[i,j]:=s[k,j];
s[k,j]:=aux;
end;
end;
end;

procedure merged(var s:matriz);
var i,j:integer;
begin
for j:= 1 to max do
for i:= max downto 2 do
if (s[i,j]=s[i-1,j]) then
begin
s[i,j]:=s[i-1,j]+s[i,j];
s[i-1,j]:=0;
end;
end;

begin
cargamatriz(tabla);
printM(tabla);
writeln('Bienvenido, presione Arriba/aBajo/Izquierda/Derecha o Esc');
repeat
key:=readkey;
begin
case key of
'I','i' : begin
mergel(tabla);
movl(tabla);
random2(tabla);
printm(tabla);
end;
'A','a' : begin
mergeu(tabla);
movu(tabla);
random2(tabla);
printm(tabla);
end;
'D','d' : begin
merger(tabla);
movr(tabla);
random2(tabla);
printm(tabla);
end;
'B','b' : begin
merged(tabla);
movd(tabla);
random2(tabla);
printm(tabla);
end;
end;
end;
until key=#27;
readkey;
end.

one of the thing i don't understand is why the 'random2' procedure doesn't seem to work in the general program, even though it works well when i call it from the procedure 'cargarMatriz'.. and also when i execute it the program it work fine for a while, but after a certain point just it stops displaying.. before ending the game..

Well, i'm a fresher so any hints on a more appropriate aproach will be helpfull..

Tagged:
Sign In or Register to comment.