Skip to content

Instantly share code, notes, and snippets.

@zodman
Last active January 10, 2022 23:19
Show Gist options
  • Save zodman/7207927 to your computer and use it in GitHub Desktop.
Save zodman/7207927 to your computer and use it in GitHub Desktop.
juego de ajedrez en pascal
from colorconsole import terminal
BLANCO = 'blanco'
NEGRO = 'negro'
VACIO = ""
TRenglon = range(0,9)
TColumna = ["A","B","C","D","E","F","G","H"]
screen = terminal.get_terminal()
class TTablero(object):
__fJaqueMate = False
__t = {} # TPieza
def __init__(self):
screen.clear()
self.__fJaqueMate = False
for i in TRenglon:
for j in TColumna:
self.__t[(i,j)] = None
t = self.__t
t[(1,"A")] = TTorre(BLANCO,self)
t[(1,"B")] = TCaballo(BLANCO, self)
t[(1,"C")] = TAlfil(BLANCO, self)
t[(1,"D")] = TReina(BLANCO,self)
t[(1,"E")] = TRey(BLANCO,self)
t[(1,"F")] = TAlfil(BLANCO, self)
t[(1,"G")] = TCaballo(BLANCO,self)
t[(1,"H")] = TTorre(BLANCO,self)
for j in TColumna:
t[(2,j)] = TPeon(BLANCO,self)
t[(8,"A")] = TTorre(NEGRO,self)
t[(8,"B")] = TCaballo(NEGRO,self)
t[(8,"C")] = TAlfil(NEGRO,self)
t[(8,"D")] = TReina(NEGRO,self)
t[(8,"E")] = TRey(NEGRO,self)
t[(8,"F")] = TAlfil(NEGRO,self)
t[(8,"G")] = TCaballo(NEGRO,self)
t[(8,"H")] = TTorre(NEGRO,self)
for j in TColumna:
t[(7,j)] = TPeon(NEGRO,self)
def RenglonVacio(self, columna_c, renglon_r0, renglon_rf):
pass
def ColumnaVacia(self, renglon_r, columna_c0, columna_cf):
pass
def DiagonalVacia(self, renglon_r0, columna_c0, renglon_rf, columna_cf):
pass
def PiezaEn(self, renglon_i, columna_j):
pass
def MoverPieza(self, renglon_i0, columna_j0, renglon_it, columna_jt):
pass
def ComerPieza(self, renglon_i0, columna_j0, renglon_it, columna_jt):
pass
def JaqueMate(self):
return self.__fJaqueMate
def MovimientoJugador(self, jugador):
pass
def Desplegar(self):
screen.clear()
for j in TColumna:
for i in TRenglon:
if self.__t[(i,j)] == None:
screen.print_at(ord(j) +1, i,"_")
else:
self.__t[(i,j)].Desplegar(i,j)
class TPieza(object):
__c = None # TColorPieza
__t = None # TTablero
def __init__(self, colorpieza_col, tablero_tab):
self.__c = colorpieza_col
self.__t = tablero_tab
def Desplegar(self, renglon_i, columna_j):
self.x = renglon_i
self.y = columna_j
#screen.print_at((ord(columna_j) +1),renglon_i,self.p)
def Color(self):
pass
def Mover(self, renglon_i0, columna_j0, renglon_it, columna_jt):
pass
class TPeon(TPieza):
p= "P"
def Desplegar(self, renglon_i, columna_j):
screen.print_at(self.x, self.y,self.p)
class TTorre(TPieza):
p = "T"
class TCaballo(TPieza):
p = "C"
class TAlfil(TPieza):
p ="A"
class TRey(TPieza):
p = "R"
class TReina(TPieza):
p = "R"
if __name__== "__main__":
t = TTablero()
#while not t.JaqueMate():
t.Desplegar()
t.MovimientoJugador(BLANCO)
t.Desplegar()
t.MovimientoJugador(NEGRO)
screen.reset()
program ajedrez_oo;
uses
crt; {Cathode Ray Tube}
type
TColorPieza = (NEUTRO, BLANCO, NEGRO);
TRenglon = 1..8;
TColumna = (A, B, C, D, E, F, G, H);
TPieza = class; {Declaración "adelantada" (forward)}
TTablero = class
private
t : array[TRenglon,TColumna] of TPieza;
fJaqueMate : boolean;
public
constructor Create;
function RenglonVacio(c : TColumna; r0, rf : TRenglon): boolean;
function ColumnaVacia(r : TRenglon; c0, cf : TColumna): boolean;
function PiezaEn(i : TRenglon; j : TColumna): TPieza;
procedure MoverPieza(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
procedure ComerPieza(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
function JaqueMate: boolean;
procedure Desplegar;
procedure MovimientoJugador(jug : TColorPieza);
end;
TPieza = class
private
c : TColorPieza;
t : TTablero;
public
constructor Create(col : TColorPieza; tab : TTablero);
procedure Desplegar(i : TRenglon; j : TColumna); virtual;
function Color: TColorPieza;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); virtual; abstract;
end;
TPeon = class(TPieza)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
TTorre = class(TPieza)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
TCaballo = class(TPieza)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
TAlfil = class(TPieza)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
TRey = class(TPieza)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
TReina = class(TPieza) // class(TTorre, TCaballo)
procedure Desplegar(i : TRenglon; j : TColumna); override;
procedure Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna); override;
end;
constructor TTablero.Create;
var
i : TRenglon;
j : TColumna;
begin
inherited Create;
fJaqueMate := false;
{Inicializar el tablero vacío}
for i := 1 to 8 do
for j := A to H do
t[i,j] := nil;
{Inicializar las piezas blancas}
t[1,A] := TTORRE.Create(BLANCO, self);
t[1,B] := TCABALLO.Create(BLANCO, self);
t[1,C] := TALFIL.Create(BLANCO, self);
t[1,D] := TREINA.Create(BLANCO, self);
t[1,E] := TREY.Create(BLANCO, self);
t[1,F] := TALFIL.Create(BLANCO, self);
t[1,G] := TCABALLO.Create(BLANCO, self);
t[1,H] := TTORRE.Create(BLANCO, self);
for j := A to H do
t[2,j] := TPEON.Create(BLANCO, self);
{Inicializar las piezas negras}
t[8,A] := TTORRE.Create(NEGRO, self);
t[8,B] := TCABALLO.Create(NEGRO, self);
t[8,C] := TALFIL.Create(NEGRO, self);
t[8,D] := TREINA.Create(NEGRO, self);
t[8,E] := TREY.Create(NEGRO, self);
t[8,F] := TALFIL.Create(NEGRO, self);
t[8,G] := TCABALLO.Create(NEGRO, self);
t[8,H] := TTORRE.Create(NEGRO, self);
for j := A to H do
t[7,j] := TPEON.Create(NEGRO, self);
end;
procedure TTablero.Desplegar;
var
i : TRenglon;
j : TColumna;
begin
clrscr;
for j := A to H do
for i := 1 to 8 do
if t[i, j] = NIL then
begin
gotoxy(2 * (ord(j) + 1), 2 * i);
write('_');
end
else
t[i,j].Desplegar(i, j); {Despacho polimórfico del mensaje Desplegar}
writeln;
end;
procedure TTablero.MovimientoJugador(jug : TColorPieza);
var
i0, it : TRenglon;
j0, jt : TColumna;
begin
writeln('Movimiento del Jugador ', jug);
write('Renglón origen: '); readln(i0);
write('Columna origen: '); readln(j0);
write('Renglón destino: '); readln(it);
write('Columna destino: '); readln(jt);
if t[i0, j0] <> nil then
t[i0, j0].Mover(i0, j0, it, jt) {Despacho polimórfico del mensaje Mover}
else
writeln('Error: No hay pieza en esa posición');
end;
function TTablero.RenglonVacio(c : TColumna; r0, rf : TRenglon): boolean;
var
r : TRenglon;
temp : TRenglon;
begin
if rf < r0 then
begin
temp := rf;
rf := r0;
r0 := temp;
end;
r := succ(r0);
while (r < rf) and (t[r, c].Color = NEUTRO) do
r := r + 1;
if r = rf then
Result := true
else
Result := false;
end;
function TTablero.ColumnaVacia(r : TRenglon; c0, cf : TColumna): boolean;
var
c : TColumna;
temp : TColumna;
begin
if cf < c0 then
begin
temp := cf;
cf := c0;
c0 := temp;
end;
c := succ(c0);
while (c < cf) and (t[r, c].Color = NEUTRO) do
c := succ(c);
if c = cf then
Result := true
else
Result := false;
end;
procedure TTablero.MoverPieza(i0 : TRenglon;
j0 : TColumna;
it : TRenglon;
jt : TColumna);
begin
t[it, jt] := t[i0, j0];
t[i0, j0] := NIL;
end;
procedure TTablero.ComerPieza(i0 : TRenglon;
j0 : TColumna;
it : TRenglon;
jt : TColumna);
begin
{NOTA: Faltaría agregar al score la pieza comida antes de destruirla}
t[it, jt].Free;
self.MoverPieza(i0, j0, it, jt);
end;
function TTablero.PiezaEn(i : TRenglon; j : TColumna): TPieza;
begin
Result := t[i, j];
end;
function TTablero.JaqueMate: boolean;
begin
Result := fJaqueMate;
end;
constructor TPieza.Create(col : TColorPieza; tab : TTablero);
begin
inherited Create;
c := col;
t := tab; {Asociación de TPieza al tablero}
end;
procedure TPieza.Desplegar(i : TRenglon; j : TColumna);
begin
gotoxy(2 * (ord(j) + 1), 2 * i);
end;
function TPieza.Color: TColorPieza;
begin
Result := c;
end;
procedure TPeon.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('P');
end;
procedure TPeon.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
case Color of
NEGRO: begin
{El peón sólo puede avanzar 1 fila}
if it = i0 - 1 then
if j0 = jt then {el peón sólo puede moverse en su columna}
if t.PiezaEn(it, jt) = NIL then
t.MoverPieza(i0, j0, it, jt)
else
{¿Estamos comiendo?}
if (ord(jt) = ord(j0) - 1) or (ord(jt) = ord(j0) + 1) then
if t.PiezaEn(it, jt).Color = BLANCO then
t.ComerPieza(i0, j0, it, jt)
else
writeln('No es posible moverse a más de 1 columna de distancia')
else
writeln('No es posible avanzar más de 1 renglón');
end;
BLANCO: begin
{El peón sólo puede avanzar 1 fila}
if it = i0 + 1 then
if j0 = jt then {el peón sólo puede moverse en su columna}
if t.PiezaEn(it, jt) = NIL then
t.MoverPieza(i0, j0, it, jt)
else
{¿Estamos comiendo?}
if (ord(jt) = ord(j0) - 1) or (ord(jt) = ord(j0) + 1) then
if t.PiezaEn(it, jt).Color = BLANCO then
t.ComerPieza(i0, j0, it, jt)
else
writeln('No es posible moverse a más de 1 columna de distancia')
else
writeln('No es posible avanzar más de 1 renglón');
end;
end; {case}
end;
procedure TTorre.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('T');
end;
procedure TTorre.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
if t.PiezaEn(it,jt).Color = t.PiezaEn(i0,j0).Color then
writeln('Animal, no te puedes comer a los tuyos')
else
if ((i0 = it) and (j0 <> jt)) or ((j0 = jt) and (i0 <> it)) then
if i0 = it then
if t.ColumnaVacia(i0, j0, jt) then
t.MoverPieza(i0, j0, it, jt)
else
writeln('El renglón no está vacío')
else
if j0 = jt then
if t.RenglonVacio(j0, i0, it) then
t.MoverPieza(i0, j0, it, jt)
else
writeln('La columna no está vacía')
else
writeln('No es posible moverse entre renglones y columnas simultáneamente');
end;
procedure TCaballo.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('C');
end;
procedure TCaballo.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
end;
procedure TAlfil.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('A');
end;
procedure TAlfil.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
end;
procedure TRey.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('K');
end;
procedure TRey.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
end;
procedure TReina.Desplegar(i : TRenglon; j : TColumna);
begin
inherited Desplegar(i, j);
write('Q');
end;
procedure TReina.Mover(i0 : TRenglon; j0 : TColumna; it : TRenglon; jt : TColumna);
begin
end;
var
t : TTablero;
{Programa principal}
begin
t := TTablero.Create;
repeat
t.Desplegar;
t.MovimientoJugador(BLANCO);
t.Desplegar;
t.MovimientoJugador(NEGRO);
until t.JaqueMate;
end.
@TheBlackCat778
Copy link

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment