Advertisement
r_russo

Tatetí

Nov 9th, 2021
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.57 KB | None | 0 0
  1. {
  2.     Escribir un programa que permita un juego de tatetí entre dos personas. Luego,
  3.     generar un jugador aleatorio que participe en uno de los turnos.
  4.    
  5.     Reglas:
  6.         - Tablero 3x3
  7.         - Se alternan dos jugadores y colocan símbolos O y X
  8.         - Comienzan los O
  9.         - Los símbolos pueden colocarse en cualquier parte del tablero
  10.         siempre y cuando ese lugar no esté ocupado por un símbolo
  11.         - Gana el primer jugador en obtener tres símbolos propios alineados
  12.         (horizontal, vertical, diagonal)
  13.  
  14.      | |            
  15.     -+-+-
  16.      | |            
  17.     -+-+-
  18.      | |            
  19. }
  20.  
  21. type
  22.     TTablero = array[1..3, 1..3] of Char;
  23.     // 'O'
  24.     // 'X'
  25.     // ' '
  26.    
  27. var
  28.     g_tablero: TTablero;
  29.     g_jugador: Char;
  30.     g_estado: Char;
  31.    
  32. procedure inicializar_tablero(var tablero: TTablero);
  33. var
  34.     i, j: Byte;
  35. begin
  36.     for i := 1 to 3 do  // filas
  37.         for j := 1 to 3 do  // columnas
  38.             tablero[i, j] := ' ';
  39. end;
  40.  
  41. procedure mostrar_tablero(tablero: TTablero);
  42. var
  43.     i, j: Byte;
  44. begin
  45.     for i := 1 to 3 do
  46.     begin
  47.         for j := 1 to 3 do
  48.         begin
  49.             write(tablero[i, j]);
  50.             if j < 3 then
  51.                 write('|');
  52.         end;
  53.         writeln();
  54.         if i < 3 then
  55.             writeln('-+-+-');
  56.     end
  57. end;
  58.  
  59. function posicion_valida(tablero: TTablero; i, j: Byte): Boolean;
  60. begin
  61.     posicion_valida := (tablero[i, j] = ' ') and
  62.         (i >= 1) and (i <= 3) and (j >= 1) and (j <= 3);
  63. end;
  64.  
  65. function tablero_lleno(tablero: TTablero): Boolean;
  66. var
  67.     resultado: Boolean;
  68.     i, j: Byte;
  69. begin
  70.     // tablero está lleno
  71.     resultado := TRUE;
  72.     for i := 1 to 3 do
  73.         for j := 1 to 3 do
  74.             if tablero[i, j] = ' ' then
  75.                 resultado := FALSE;  // no, no está lleno
  76.    
  77.     tablero_lleno := resultado;
  78.    
  79.     {
  80.     resultado := TRUE;
  81.     i := 1;
  82.     j := 1;
  83.     while resultado and (i <= 3) do
  84.     begin
  85.         lleno := tablero[i, j] <> ' ';
  86.         j := j + 1;
  87.         if (j > 3) then
  88.         begin
  89.             j := 1;
  90.             i := i + 1;
  91.         end;
  92.     end;
  93.    
  94.     tablero_lleno := resultado;
  95.     }
  96. end;
  97.  
  98. function verificar_reglas(tablero: TTablero): Char;
  99. // O: Ganó O
  100. // X: Ganó X
  101. // E: Empate
  102. // S: Sigue
  103. begin
  104.     // Primero verificamos si gana O
  105.     // por filas
  106.     if (tablero[1, 1] = 'O') and (tablero[1, 2] = 'O') and (tablero[1, 3] = 'O') or
  107.         (tablero[2, 1] = 'O') and (tablero[2, 2] = 'O') and (tablero[2, 3] = 'O') or
  108.         (tablero[3, 1] = 'O') and (tablero[3, 2] = 'O') and (tablero[3, 3] = 'O') or
  109.     // por columnas
  110.         (tablero[1, 1] = 'O') and (tablero[2, 1] = 'O') and (tablero[3, 1] = 'O') or
  111.         (tablero[1, 2] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 2] = 'O') or
  112.         (tablero[1, 3] = 'O') and (tablero[2, 3] = 'O') and (tablero[3, 3] = 'O') or
  113.     // por diagonales
  114.         (tablero[1, 1] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 3] = 'O') or
  115.         (tablero[1, 3] = 'O') and (tablero[2, 2] = 'O') and (tablero[3, 1] = 'O') then
  116.             verificar_reglas := 'O'
  117.     // Verificar si ganó X
  118.     else if (tablero[1, 1] = 'X') and (tablero[1, 2] = 'X') and (tablero[1, 3] = 'X') or
  119.         (tablero[2, 1] = 'X') and (tablero[2, 2] = 'X') and (tablero[2, 3] = 'X') or
  120.         (tablero[3, 1] = 'X') and (tablero[3, 2] = 'X') and (tablero[3, 3] = 'X') or
  121.     // por columnas
  122.         (tablero[1, 1] = 'X') and (tablero[2, 1] = 'X') and (tablero[3, 1] = 'X') or
  123.         (tablero[1, 2] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 2] = 'X') or
  124.         (tablero[1, 3] = 'X') and (tablero[2, 3] = 'X') and (tablero[3, 3] = 'X') or
  125.     // por diagonales
  126.         (tablero[1, 1] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 3] = 'X') or
  127.         (tablero[1, 3] = 'X') and (tablero[2, 2] = 'X') and (tablero[3, 1] = 'X') then
  128.             verificar_reglas := 'X'
  129.     else if tablero_lleno(tablero) then
  130.         verificar_reglas := 'E'
  131.     else
  132.         verificar_reglas := 'S';
  133. end;
  134.  
  135. procedure turno(var tablero: TTablero; jugador: Char);
  136. var
  137.     i, j: Byte;
  138. begin
  139.     writeln('Turno de ', jugador, ':');
  140.  
  141.     write('Fila: ');
  142.     readln(i);
  143.     write('Columna: ');
  144.     readln(j);
  145.    
  146.     while not posicion_valida(tablero, i, j) do
  147.     begin
  148.         writeln('Posición inválida');
  149.         write('Fila: ');
  150.         readln(i);
  151.         write('Columna: ');
  152.         readln(j);
  153.     end;
  154.     tablero[i, j] := jugador
  155. end;
  156.  
  157. procedure turno_aleatorio(var tablero: TTablero; jugador: Char);
  158. var
  159.     i, j: Byte;
  160. begin
  161.     i := Random(3) + 1;
  162.     j := Random(3) + 1;
  163.    
  164.     while not posicion_valida(tablero, i, j) do
  165.     begin
  166.         i := Random(3) + 1;
  167.         j := Random(3) + 1;
  168.     end;
  169.     tablero[i, j] := jugador
  170. end;
  171.  
  172. begin          
  173.     randomize();
  174.     inicializar_tablero(g_tablero);
  175.     mostrar_tablero(g_tablero);
  176.     g_jugador := 'O';
  177.     g_estado := verificar_reglas(g_tablero);
  178.    
  179.     // while not ((alguien gano) or (empate)) do
  180.     while g_estado = 'S' do
  181.     begin
  182.         if g_jugador = 'O' then
  183.             turno(g_tablero, g_jugador)
  184.         else
  185.             turno_aleatorio(g_tablero, g_jugador);
  186.        
  187.         g_estado := verificar_reglas(g_tablero);
  188.         mostrar_tablero(g_tablero);
  189.        
  190.         if g_jugador = 'O' then
  191.             g_jugador := 'X'
  192.         else
  193.             g_jugador := 'O';
  194.     end;
  195.  
  196.     if g_estado = 'O' then
  197.         writeln('Ganó O')
  198.     else if g_estado = 'X' then
  199.         writeln('Ganó X')
  200.     else
  201.         writeln('Empataron');
  202. end.
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement