PDA

Ver la Versión Completa : Problema uso de tag


Neg90
27-03-2013, 08:13:28
Buenas, ando en busca de ayuda. Estoy haciendo un TATETI entre dos jugadores humanos, y me surgió un problema al momento de verificar quien gana. el código es este ..


begin
aux:= aux+1;
if (aux mod 2 <> 0) then
begin
(sender as timage).picture:= form2.image1.Picture;
(sender as timage).enabled:= false;
if tag = 1 then
matriz[1,1]:= 1
else
if tag = 1 then
matriz[1,2]:=1
else
if tag =3 then
matriz[1,3]:=1
else
if tag = 4 then
matriz[2,1]:=1
else
if tag = 5 then
matriz[2,2]:=1
else
if tag = 6 then
matriz[2,3]:=1
else
if tag = 7 then
matriz[3,1]:= 1
else
if tag = 8 then
matriz[3,2]:=1
else
matriz[3,3]:=1;

for i:=8 to 248 do
begin
sleep(1);
refresh;
shape1.top:=i;
end;
end
else
begin
(sender as timage).picture:= form2.image2.Picture;
(sender as timage).enabled:= false;
if tag = 1 then
showmessage('Hola')
else
if tag = 2 then
matriz[1,2]:=2
else
if tag =3 then
matriz[1,3]:=2
else
if tag = 4 then
matriz[2,1]:=2
else
if tag = 5 then
matriz[2,2]:=2
else
if tag = 6 then
matriz[2,3]:=2
else
if tag = 7 then
matriz[3,1]:= 2
else
if tag = 8 then
matriz[3,2]:=2
else
matriz[3,3]:=2;
for i := 248 downto 8 do
begin
sleep(1);
refresh;
shape1.Top:=i;
end;


end;
end;

tengo 9 imágenes y cada ves que uno de los jugadores hace click en una de las imágenes en teoría carga un valor en la matriz, 1 si es el jugador 1 o 2 si es el jugador 2. Lo que había hecho es ponerle a cada una de las imágenes ponerle un tag de 1 a 9, pero me di cuenta de que el problema esta en que no cargo la matriz con ningún valor. Gracias de antemano.

Pd: el cartel de hola esta de mas pero era para ver si me cargaba la matriz .

TOPX
27-03-2013, 14:19:34
Buenas,

Si entiendo bien, al evaluar la propiedad Tag de uno de los TImage en ese evento debería ser con algo como:
if (Sender as TImage).Tag = 10 then

Como soy criticón, creería que ese código se puede mejorar cambiando la cascada de if por un case of (http://www.delphibasics.co.uk/RTL.asp?Name=Case), quitándole el sleep y ... en fin, son gustos.
-

Neg90
27-03-2013, 17:42:58
Muchas gracias me solucionaste el problema , te comento pensé en hacerlo con un case pero tenia problemas de sintaxis así que preferí anidar if con tal de que funcionara, igualmente ya solucione lo del case y ahora funciona perfecto. Lo que me decís del sleep, no se que decirte por que es algo decorativo, es un shape que se mueve de una posición a otra y como era muy brusco el movimiento le puse el sleep para que se notara el movimiento, pero es muy probable que halla una manera mejor de hacerlo si te interesa coméntame que te parece. Gracias me sirvió tu ayuda.

cloayza
27-03-2013, 19:17:48
Creo que aquí hay un problema...:D

if (aux mod 2 <> 0) then
begin
(sender as timage).picture:= form2.image1.Picture;
(sender as timage).enabled:= false;
if tag = 1 then
matriz[1,1]:= 1
else
if tag = 1 then
matriz[1,2]:=1
else

Saludos cordiales...

cloayza
27-03-2013, 19:49:22
Amigo te propongo esta modificación a tu algoritmo...

{
APos[Tag].X =>Indice de la matriz
APos[Tag].Y =>Indice de la matriz

Tag Posicion Matriz
--------------------
1 =>matriz [1,1]
2 =>matriz [1,2]
3 =>matriz [1,3]
4 =>matriz [2,1]
5 =>matriz [2,2]
6 =>matriz [2,3]
7 =>matriz [3,1]
8 =>matriz [3,2]
9 =>matriz [3,3]

matriz [ APos[Tag].X, APos[Tag].Y ]
}

Const
APos:Array[1..9] Of TPoint=((X:1; Y:1),(X:1;Y:2),(X:1;Y:3),(X:2;Y:1),(X:2;Y:2),(X:2;Y:3),(X:3;Y:1),(X:3;Y:2),(X:3;Y:3));
{APos Vector con las coordenadas de x e y de la matriz...}

var i, aux:Integer;
matriz:array[1..3,1..3] of Integer;
begin
aux:= aux+1;

if (aux mod 2 <> 0) then
begin
(sender as timage).picture:= form2.image1.Picture;
(sender as timage).enabled:= false;

Matriz[ APos[Tag].X, APos[Tag].Y]:= 1;

{ if tag = 1 then
matriz[1,1]:= 1
else
if tag = 1 then
matriz[1,2]:=1
else
if tag =3 then
matriz[1,3]:=1
else
if tag = 4 then
matriz[2,1]:=1
else
if tag = 5 then
matriz[2,2]:=1
else
if tag = 6 then
matriz[2,3]:=1
else
if tag = 7 then
matriz[3,1]:= 1
else
if tag = 8 then
matriz[3,2]:=1
else
matriz[3,3]:=1;
}
for i:=8 to 248 do
begin
sleep(1);
refresh;
shape1.top:=i;
end;
end
else
begin
(sender as timage).picture:= form2.image2.Picture;
(sender as timage).enabled:= false;

Matriz[ APos[Tag].X, APos[Tag].Y]:= 1;

{if tag = 1 then
showmessage('Hola')
else
if tag = 2 then
matriz[1,2]:=2
else
if tag =3 then
matriz[1,3]:=2
else
if tag = 4 then
matriz[2,1]:=2
else
if tag = 5 then
matriz[2,2]:=2
else
if tag = 6 then
matriz[2,3]:=2
else
if tag = 7 then
matriz[3,1]:= 2
else
if tag = 8 then
matriz[3,2]:=2
else
matriz[3,3]:=2;
}
for i := 248 downto 8 do
begin
sleep(1);
refresh;
shape1.Top:=i;
end;
end;


Saludos cordiales

Neg90
28-03-2013, 01:47:55
A mira esta buena esa, ahora que la veo capas que me sirve mas que la que implemente yo, por que me quedo medio feo (o del todo :P) el tema de ver quien gana o si empatan


if (((matriz[1,1] <>0) and (matriz[1,2] <>0)) and ((matriz[1,3] <> 0)) and
((matriz[2,1] <>0) and (matriz[2,2] <>0)) and ((matriz[2,3] <> 0)) and
((matriz[3,1] <>0) and (matriz[3,2] <>0)) and ((matriz[3,3] <> 0))
and (no2 =true ) and (no1 = true)) then
showmessage('Emptaron');
no1:=true;
no2:=true;
aux:= aux+1;
if (aux mod 2 <> 0) then
begin
(sender as timage).picture:= form2.image1.picture;
(sender as timage).enabled:= false;
case (sender as timage).Tag of
1:matriz[1,1]:=1;
2:matriz[1,2]:=1;
3:matriz[1,3]:=1;
4:matriz[2,1]:=1;
5:matriz[2,2]:=1;
6:matriz[2,3]:=1;
7:matriz[3,1]:=1;
8:matriz[3,2]:=1;
9:matriz[3,3]:=1;
end;
for i:=8 to 248 do
begin
sleep(1);
refresh;
shape1.top:=i;
end;
if ((matriz[1,1] =1) and (matriz[1,2] =1)) and ((matriz[1,3] = 1)) or
((matriz[2,1] =1) and (matriz[2,2] =1)) and ((matriz[2,3] = 1)) or
((matriz[3,1] =1) and (matriz[3,2] =1)) and ((matriz[3,3] = 1)) or
((matriz[1,1] =1) and (matriz[2,1] =1)) and ((matriz[3,1] = 1)) or
((matriz[1,2] =1) and (matriz[2,2] =1)) and ((matriz[3,2] = 1)) or
((matriz[1,3] =1) and (matriz[2,3] =1)) and ((matriz[3,3] = 1)) or
((matriz[1,1] =1) and (matriz[2,2] =1)) and ((matriz[3,3] = 1)) or
((matriz[3,1] =1) and (matriz[2,2] =1)) and ((matriz[1,3] = 1)) then
begin
showmessage('Gana '+ label1.Caption);
no1:=true;
end;
end
else
begin
(sender as timage).picture:= form2.image2.Picture;
(sender as timage).enabled:= false;
case (sender as timage).Tag of
1:matriz[1,1]:=2;
2:matriz[1,2]:=2;
3:matriz[1,3]:=2;
4:matriz[2,1]:=2;
5:matriz[2,2]:=2;
6:matriz[2,3]:=2;
7:matriz[3,1]:=2;
8:matriz[3,2]:=2;
9:matriz[3,3]:=2;
end;
for i := 248 downto 8 do
begin
sleep(1);
refresh;
shape1.Top:=i;
end;
if ((matriz[1,1] =2) and (matriz[1,2] =2)) and ((matriz[1,3] = 2)) or
((matriz[2,1] =2) and (matriz[2,2] =2)) and ((matriz[2,3] = 2)) or
((matriz[3,1] =2) and (matriz[3,2] =2)) and ((matriz[3,3] = 2)) or
((matriz[1,1] =2) and (matriz[2,1] =2)) and ((matriz[3,1] = 2)) or
((matriz[1,2] =2) and (matriz[2,2] =2)) and ((matriz[3,2] = 2)) or
((matriz[1,3] =2) and (matriz[2,3] =2)) and ((matriz[3,3] = 2)) or
((matriz[1,1] =2) and (matriz[2,2] =2)) and ((matriz[3,3] = 2)) or
((matriz[3,1] =2) and (matriz[2,2] =2)) and ((matriz[1,3] = 2)) then
begin
showmessage('Gana ' + label2.Caption);
no2:=true;
end;
end;


Voy a ver que hago si encuentro una forma mas practica de encontrar ganador/empate en mi algoritmo o implemento el que vos decís y pruebo. escucho cualquier sugerencia :p estoy complicado con el tema, hace poco arranque a estudiar delphi así que todo me sirve. Les agradezco por ayudarme desde ya.

cloayza
28-03-2013, 14:54:47
Te propongo este código...espero te sirva...


{
APos[Tag].X =>Indice de la matriz
APos[Tag].Y =>Indice de la matriz

Tag Posicion Matriz
--------------------
1 =>matriz [1,1]
2 =>matriz [1,2]
3 =>matriz [1,3]
4 =>matriz [2,1]
5 =>matriz [2,2]
6 =>matriz [2,3]
7 =>matriz [3,1]
8 =>matriz [3,2]
9 =>matriz [3,3]

matriz [ APos[Tag].X, APos[Tag].Y ]
}

Const
APos:Array[1..9] Of TPoint=((X:1; Y:1),(X:1;Y:2),(X:1;Y:3),(X:2;Y:1),(X:2;Y:2),(X:2;Y:3),(X:3;Y:1),(X:3;Y:2),(X:3;Y:3));
{APos Vector con las coordenadas de x e y de la matriz...}
Size_Matriz=3;
type
TMatriz=array[1..Size_Matriz,1..Size_Matriz] of Integer;

var i, aux:Integer;
matriz:TMatriz;
no1, no2:Boolean;
Key:Integer;

function CheckHorizontal(Values:TMatriz; Key:Integer):Boolean;
var
i,j, Count:Integer;
begin
Result:=False;
for i := 1 to Size_Matriz do
begin
Count:=0;
for j:=1 to Size_Matriz do
begin
If Values[i,j]=Key then
Inc(Count);
end;

Result:=Count=Size_Matriz;

if Result then
Break;
end;
end;

function CheckVertical(Values:TMatriz; Key:Integer):Boolean;
var
i,j, Count:Integer;
begin
Result:=False;
for j := 1 to Size_Matriz do
begin
Count:=0;
for i:=1 to Size_Matriz do
begin
If Values[i,j]=Key then
Inc(Count);
end;

Result:=(Count=Size_Matriz);

if Result then
Break;
end;
end;

function CheckDiagonalPrincipal(Values:TMatriz; Key:Integer):Boolean;
var
i,j, Count:Integer;
begin
Result:=False;
Count :=0;
for i := 1 to Size_Matriz do
begin
{ i,i coordenadas de la diagonal principal 1,1 2,2 3,3 }
If Values[i,i]=Key then
Inc(Count);
end;

Result:=(Count=Size_Matriz);
end;

function CheckDiagonalSecundaria(Values:TMatriz; Key:Integer):Boolean;
var
i,j, Count:Integer;
begin
Result:=False;
Count :=0;
j:=1;
for i := Size_Matriz downto 1 do
begin
{ i,j coordenadas de la diagonal secundaria 3,1 2,2 1,3 }
If Values[i,j]=Key then
Inc(Count);
Inc(j);
end;

Result:=(Count=Size_Matriz);
end;
begin
aux:= aux+1;

if (aux mod 2 <> 0) then
begin
(Sender as TImage).Picture:= Self.Image1.Picture;
(Sender as TImage).Enabled:= False;

Key:=1;
end
else
begin
(Sender as TImage).picture:= Self.Image2.Picture;
(Sender as TImage).enabled:= False;

Key:=2;
end;

//Asignas el valor 1 o 2 que esta en la Variable KEY (Integer)...
Matriz[ APos[Tag].X, APos[Tag].Y]:= Key;

For i := 248 downto 8 do
begin
sleep(1);
refresh;
shape1.Top:=i;
end;

//Check el valor de la matriz de acuerdo a el valor de Key
if CheckHorizontal(matriz, Key) or
CheckVertical(Matriz, Key) or
CheckDiagonalPrincipal(Matriz, Key) Or
CheckDiagonalSecundaria(Matriz, Key) then
begin
showmessage('Gana ' + label2.Caption);

no1:=Key=1;
no2:=Key=2;
end;



Saludos cordiales

Neg90
29-03-2013, 10:20:36
Muchas gracias por tu ayuda, ya esta andando perfecto el tateti, gracias por la explicación!