You are on page 1of 7

{

_ _ _ _ _ _ _ _
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | |o|x| | | |
| | | |x|o| | | |
| | | | | | | | |
| | | | | | | | |
|_|_|_|_|_|_|_|_|

}
Program otelo_dos_jugadores;
Uses Crt;
Const
{valores con los cuales llenar la matriz}
VACIO = 0;
BLANCO = 1;
NEGRO = 2; {COMPUTADORA- aleatorio}
{s¡mbolos que representan cada ficha}
FICHA : array[BLANCO..NEGRO] of Char = ('o','x'); {eleg¡ blanco: color blanco
negro : color azul }

{L¡mites (gr€fico del tablero)}


IZQ = 20; {23}
DER = 44;
ARRIBA = 3; {3}
ABAJO = 24;

{mensajes de error}
ERRORES: array[0..2] of string = ('Esa posici¢n no est€ en el tablero',
'Casillero ocupado ',
'Esa posici¢n no es v€lida');

Var
casillero : array[1..8, 1..8] of byte; {tablero}
coordxchar: char; {coordenadas para posicionar la ficha}
coordx,coordy: byte;
turno: byte; {de qui‚n es el turno}
v,b,n,c: byte; {contadores: vac¡os, blancas, negras y posibles}
msj_error: string;

{##############################################################}
{# INICIALIZACIàN DE LA MATRIZ #}
{##############################################################}

Procedure Inicializar_Tablero;
Var i,j: byte;
Begin
{Limpia el tablero}
For i:=1 to 8 Do
For j:=1 to 8 Do
begin
casillero[i,j] := 0;
end;
{Posiciona las fichas de apertura}
casillero[4,4]:= BLANCO; casillero[5,5]:= BLANCO;
casillero[4,5]:= NEGRO; casillero[5,4]:= NEGRO;
End;

{##############################################################}
{# RUTINAS DE GRµFICAS e INICIALES #}
{##############################################################}

Procedure Dibujar_Tablero;
Var i: byte;
Begin
Writeln;
Writeln(' A B C D E F G H ');
Writeln(' ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»');
For i:=1 to 7 Do
Begin
Writeln(' ',i,' º ³ ³ ³ ³ ³ ³ ³ º');
Writeln(' ºÄÄÅÄÄÅÄÄÅÄÄÅÄÄÅÄÄÅÄÄÅÄĺ');
End;
Writeln(' ',8,' º ³ ³ ³ ³ ³ ³ ³ º');
Writeln(' ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ');
End;

Procedure Dibujar_Fichas;
Var i,j: byte;
Begin
For i:=1 to 8 Do
For j:=1 to 8 Do
Begin
GotoXY(IZQ+i*3, 2+j*2); {(trat‚ de pasar la matriz a las coordenadas
de la pantalla en modo texto)}
Case casillero[i,j] of
BLANCO : Begin
TextColor(White);
Write(ficha[BLANCO]);
End;
NEGRO : Begin
TextColor(Blue);
Write(ficha[NEGRO]);
End;
End;
TextColor(lightgray);
End;
End;

{##############################################################}
{# RUTINAS DE DETECCIàN DE ERRORES #}
{##############################################################}

Function EstaVacio(x,y: byte): Boolean;


Begin
EstaVacio:= False;{predeterminado}
If (casillero[x,y]=BLANCO) OR (casillero[x,y]=NEGRO) then
EstaVacio:= False
Else If (casillero[x,y]=VACIO) then
EstaVacio:= True;
End;

Function EsLegal(x,y: byte): Boolean;


Var i,j: byte;
a,b: shortint;
tuyo, elotro: byte;
Begin
tuyo:= turno;
Case tuyo of
BLANCO: elotro:= NEGRO;
NEGRO : elotro:= BLANCO;
End;
EsLegal:= False;
i:= x; j:= y;
{analiza cada casillero contiguo}
For a:=-1 to 1 Do
For b:=-1 to 1 Do
Begin
If (a=0)AND(b=0) Then Continue; {(excepto el actual)}
If (x+a>8)OR(x+a<1)OR(y+b>8)OR(y+b<1) then Continue;{(y descarta si sale de
l tablero)}
{si encuentra una ficha del otro jugador}
If (casillero[x+a,y+b]= elotro) then
Begin
{contin£a en esa direcci¢n}
While (casillero[i+a,j+b]= elotro) Do
Begin
i:=i+a;
j:=j+b;
End;
{hasta encontrar una ficha propia (evita errores ocasionados
por valores fuera de la matriz, que se hallan en memoria)}
If (casillero[i+a,j+b]=tuyo)AND(i+a>=1)AND(i+a<=8)AND(j+b>=1)AND(j+b<=8)t
hen
Begin
EsLegal:= True;
Exit;
End;
End;
i:= x; j:= y; {reinicia la posici¢n actual y prueba con otra casilla}
End;
End;

{###############################################################}
{# RUTINA DE CONVERSIàN DE FICHAS #}
{###############################################################}

(* Diferencias con EsLegal:


- comprueba cada casilla
- si encuentra una l¡nea convertible, marca el inicio y el fin
y realiza los cambios en pantalla y en matriz, de las fichas
que se encuentran entre medio
*)

Procedure Convertir_Fichas(x,y: byte);


Var i,j: byte;
a,b: shortint;
xi,xf,yi,yf: byte;
tuyo, elotro: byte;
Begin
tuyo:= turno;
Case tuyo of
BLANCO: elotro:= NEGRO;
NEGRO : elotro:= BLANCO;
End;
i:= x; j:= y;
For a:=-1 to 1 Do
For b:=-1 to 1 Do
Begin
If (a=0)AND(b=0) then Continue;
If (x+a>8)OR(x+a<1)OR(y+b>8)OR(y+b<1) then Continue;
If (casillero[x+a,y+b]= elotro) then
Begin
While (casillero[i+a,j+b]= elotro) Do
Begin
i:=i+a;
j:=j+b;
End;
If (casillero[i+a,j+b]=tuyo)AND(i+a>=1)AND(i+a<=8)AND(j+b>=1)AND(j+b<=8)
then
Begin
xi:= x+a; yi:= y+b;
xf:= i+a; yf:= j+b;
{/-----En est€ secci¢n, los cambia en matriz y los muestra-----/}
While (xi<>xf)OR(yi<>yf)Do
Begin
casillero[xi,yi]:= tuyo;
Sound(700); Delay(1500); NoSound;{es para hacerlo agradable;)}
Dibujar_Fichas;
Delay(1500);
xi:= xi+a; yi:= yi+b;
End;
End;
End;
i:= x; j:= y;
End;

End;

(*##############################################################)
(##############################################################*)
Procedure Leer_Datos;
Begin
{Limpiar errores}
msj_error:='';
GotoXY(6,24); DelLine;
GotoXY(6,24); Write('Ubicar ficha en: ');
Readln(coordxchar,coordy);
{cambia valor ASCII a n£mero}
coordx:= Ord(Upcase(coordxchar))-64;
{si no est€ en el tablero}
If (coordx>8)OR(coordx<1)OR(coordy>8)OR(coordy<1) then
msj_error:= ERRORES[0]
{si el lugar no est€ vac¡o}
Else If Not(EstaVacio(coordx,coordy)) then
msj_error:= ERRORES[1]
{si no es un movimiento legal}
Else If Not(EsLegal(coordx,coordy)) then
msj_error:= ERRORES[2];
GotoXY(6,22); Write(' ');
TextColor(magenta);
GotoXY(6,22); Write(msj_error);
TextColor(lightgray);
End;

Procedure Leer_Datos_Maquina;
Var error: Boolean;
Begin
error:= TRUE;
Randomize;
While error=TRUE Do
Begin
coordx:= Random(8)+1;
coordy:= Random(8)+1;
error:=FALSE;
If (coordx>8)OR(coordx<1)OR(coordy>8)OR(coordy<1) then
error:= TRUE
Else If Not(EstaVacio(coordx,coordy)) then
error:= TRUE
Else If Not(EsLegal(coordx,coordy)) then
error:= TRUE;
End;
End;

Procedure Mostrar_Posibles;
Var ip,jp: byte;
Begin
c:=0;
{para cada casillero y el jugador de turno, eval£a si no hay errores}
For ip:=1 to 8 Do
For jp:=1 to 8 Do
If EstaVacio(ip,jp) then
If EsLegal(ip,jp) then
Begin
GotoXY(IZQ+ip*3, 2+jp*2);
Write('°°');
c:=c+1;
End
Else continue;
End;

{Cuenta cu€ntas fichas tiene cada jugador }


Procedure Tabla_de_Fichas;
Var fi,fj: byte;
Begin
v:=0; b:=0; n:=0;
For fi:=1 to 8 do
For fj:=1 to 8 do
Case casillero[fi,fj] of
BLANCO: b:=b+1;
NEGRO : n:=n+1;
End;
v:=64-(b+n);
GotoXY(65,2); Write(FICHA[BLANCO],' = ',b);
GotoXY(65,3); Write(FICHA[NEGRO],' = ',n);
GotoXY(64,5); Write('Turno: ',FICHA[turno]);
End;

(*************************************************************)
(*************************************************************)
{ PROGRAMA PRINCIPAL }
(*************************************************************)
(*************************************************************)
Begin
Clrscr;
Inicializar_Tablero;
Dibujar_Tablero;
Dibujar_Fichas;
turno:= BLANCO;
Tabla_de_Fichas;
Repeat
{Muestra los lugares posibles}
Mostrar_Posibles; If c=0 then {si no los hay, pasa el turno}
Begin
Case turno of
BLANCO: turno:= NEGRO;
NEGRO : turno:= BLANCO;
End;
Tabla_de_Fichas;
Mostrar_Posibles;
End;
{Lee d¢nde quiere ubicar la ficha}
If turno=BLANCO then
Begin
Leer_Datos;
While Length(msj_error)>0 Do
Leer_Datos;
End
Else if turno=NEGRO then {<========MAQUINA}
Begin
Delay(25000);
Leer_Datos_Maquina;
End;

{ubica la ficha (en la matriz de datos)}


casillero[coordx,coordy]:= turno;
{dibuja la ficha (pero sin realizar la conversi¢n)}
Clrscr;
Dibujar_Tablero;
Dibujar_Fichas;
{Ahora s¡: empieza a convertir las fichas del otro jugador}
Convertir_Fichas(coordx,coordy);

{cambia de turno}
Case turno of
BLANCO: turno:= NEGRO;
NEGRO : turno:= BLANCO;
End;
{muestra "puntuaci¢n" de cada uno"}
Tabla_de_Fichas;
Until v=0;
GotoXY(6,24); TextColor(White);
If b=n Then Writeln('Empate');
If b>n Then Writeln('Ganador: ',FICHA[BLANCO]);
If b<n Then Writeln('Ganador: ',FICHA[NEGRO]);
Readkey;
End.

You might also like