Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- Escribir un programa que permita un juego de tatetí entre dos personas. Luego,
- generar un jugador aleatorio que participe en uno de los turnos.
- Reglas:
- - Tablero 3x3
- - Se alternan dos jugadores y colocan símbolos O y X
- - Comienzan los O
- - Los símbolos pueden colocarse en cualquier parte del tablero
- siempre y cuando ese lugar no esté ocupado por un símbolo
- - Gana el primer jugador en obtener tres símbolos propios alineados
- (horizontal, vertical, diagonal)
- | |
- -+-+-
- | |
- -+-+-
- | |
- }
- type
- TTablero = array[1..3, 1..3] of Char;
- // 'O'
- // 'X'
- // ' '
- var
- g_tablero: TTablero;
- g_jugador: Char;
- g_estado: Char;
- procedure inicializar_tablero(var tablero: TTablero);
- var
- i, j: Byte;
- begin
- for i := 1 to 3 do // filas
- for j := 1 to 3 do // columnas
- tablero[i, j] := ' ';
- end;
- procedure mostrar_tablero(tablero: TTablero);
- var
- i, j: Byte;
- begin
- for i := 1 to 3 do
- begin
- for j := 1 to 3 do
- begin
- write(tablero[i, j]);
- if j < 3 then
- write('|');
- end;
- writeln();
- if i < 3 then
- writeln('-+-+-');
- end
- end;
- function posicion_valida(tablero: TTablero; i, j: Byte): Boolean;
- begin
- posicion_valida := (tablero[i, j] = ' ') and
- (i >= 1) and (i <= 3) and (j >= 1) and (j <= 3);
- end;
- function tablero_lleno(tablero: TTablero): Boolean;
- var
- resultado: Boolean;
- i, j: Byte;
- begin
- // tablero está lleno
- resultado := TRUE;
- for i := 1 to 3 do
- for j := 1 to 3 do
- if tablero[i, j] = ' ' then
- resultado := FALSE; // no, no está lleno
- tablero_lleno := resultado;
- {
- resultado := TRUE;
- i := 1;
- j := 1;
- while resultado and (i <= 3) do
- begin
- lleno := tablero[i, j] <> ' ';
- j := j + 1;
- if (j > 3) then
- begin
- j := 1;
- i := i + 1;
- end;
- end;
- tablero_lleno := resultado;
- }
- end;
- function verificar_reglas(tablero: TTablero): Char;
- // O: Ganó O
- // X: Ganó X
- // E: Empate
- // S: Sigue
- begin
- // Primero verificamos si gana O
- // por filas
- if (tablero[1, 1] = 'O') and (tablero[1, 2] = 'O') and (tablero[1, 3] = 'O') or
- (tablero[2, 1] = 'O') and (tablero[2, 2] = 'O') and (tablero[2, 3] = 'O') or
- (tablero[3, 1] = 'O') and (tablero[3, 2] = 'O') and (tablero[3, 3] = 'O') or
- // por columnas
- (tablero[1, 1] = 'O') and (tablero[2, 1] = 'O') and (tablero[3, 1] = 'O') or
- (tablero[1, 2] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 2] = 'O') or
- (tablero[1, 3] = 'O') and (tablero[2, 3] = 'O') and (tablero[3, 3] = 'O') or
- // por diagonales
- (tablero[1, 1] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 3] = 'O') or
- (tablero[1, 3] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 1] = 'O') then
- verificar_reglas := 'O'
- // Verificar si ganó X
- else if (tablero[1, 1] = 'X') and (tablero[1, 2] = 'X') and (tablero[1, 3] = 'X') or
- (tablero[2, 1] = 'X') and (tablero[2, 2] = 'X') and (tablero[2, 3] = 'X') or
- (tablero[3, 1] = 'X') and (tablero[3, 2] = 'X') and (tablero[3, 3] = 'X') or
- // por columnas
- (tablero[1, 1] = 'X') and (tablero[2, 1] = 'X') and (tablero[3, 1] = 'X') or
- (tablero[1, 2] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 2] = 'X') or
- (tablero[1, 3] = 'X') and (tablero[2, 3] = 'X') and (tablero[3, 3] = 'X') or
- // por diagonales
- (tablero[1, 1] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 3] = 'X') or
- (tablero[1, 3] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 1] = 'X') then
- verificar_reglas := 'X'
- else if tablero_lleno(tablero) then
- verificar_reglas := 'E'
- else
- verificar_reglas := 'S';
- end;
- procedure turno(var tablero: TTablero; jugador: Char);
- var
- i, j: Byte;
- begin
- writeln('Turno de ', jugador, ':');
- write('Fila: ');
- readln(i);
- write('Columna: ');
- readln(j);
- while not posicion_valida(tablero, i, j) do
- begin
- writeln('Posición inválida');
- write('Fila: ');
- readln(i);
- write('Columna: ');
- readln(j);
- end;
- tablero[i, j] := jugador
- end;
- procedure turno_aleatorio(var tablero: TTablero; jugador: Char);
- var
- i, j: Byte;
- begin
- i := Random(3) + 1;
- j := Random(3) + 1;
- while not posicion_valida(tablero, i, j) do
- begin
- i := Random(3) + 1;
- j := Random(3) + 1;
- end;
- tablero[i, j] := jugador
- end;
- begin
- randomize();
- inicializar_tablero(g_tablero);
- mostrar_tablero(g_tablero);
- g_jugador := 'O';
- g_estado := verificar_reglas(g_tablero);
- // while not ((alguien gano) or (empate)) do
- while g_estado = 'S' do
- begin
- if g_jugador = 'O' then
- turno(g_tablero, g_jugador)
- else
- turno_aleatorio(g_tablero, g_jugador);
- g_estado := verificar_reglas(g_tablero);
- mostrar_tablero(g_tablero);
- if g_jugador = 'O' then
- g_jugador := 'X'
- else
- g_jugador := 'O';
- end;
- if g_estado = 'O' then
- writeln('Ganó O')
- else if g_estado = 'X' then
- writeln('Ganó X')
- else
- writeln('Empataron');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement