• Lenguaje

    Pascal

  • Descripción

    Juego conocido como TicTacToe, Gato, Triqui, Tres en Raya o TaTeTi. Usario juega con el caracter 'X' vs Computadora que juega con el caracter 'O'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
program prog_tictactoe;
uses crt;

type matriz = array[0..2,0..2] of char;

procedure imprimir_cuadricula (tictactoe : matriz);
    var i, j : integer;
    begin
        clrscr;
        writeln ('      '#201#205#205#205#203#205#205#205#203#205#205#205#187);
        writeln ('      '#186' 1 '#186' 2 '#186' 3 '#186);
        writeln ('      '#200#205#205#205#202#205#205#205#202#205#205#205#188);
        writeln;
        writeln (#201#205#187'   '#201#205#205#205#203#205#205#205#203#205#205#205#187);
        for i := 0 to 2 do
            begin
                if i <> 0 then
                    writeln (#204#205#185'   '#204#205#205#205#206#205#205#205#206#205#205#205#185);
                write (#186, i+1, #186'   '#186);
                for j := 0 to 2 do
                    write (' ', tictactoe[i, j], ' '#186);
                writeln;
            end;
        writeln (#200#205#188'   '#200#205#205#205#202#205#205#205#202#205#205#205#188#10#13);
    end;

function leer_numero (variable : string) : integer;
    var tecla : char;
    begin
        write ('Seleccione el n'#163'mero de ', variable, ': ');
        repeat
            tecla := readkey;
        until (tecla = '1') or (tecla = '2') or (tecla = '3');
        writeln (tecla);
        leer_numero := ord (tecla) - ord ('0') - 1;
    end;

var tictactoe : matriz;
var ganador : char;
var juega_cpu : boolean;
var i, j, turnos : integer;
begin
    for i:=0 to 2 do
        for j:=0 to 2 do
            tictactoe[i,j] := ' ';
    ganador := ' ';
    turnos := 0;
    randomize;
    juega_cpu := random (2) = 1;
    while (turnos<9) and (ganador <> 'O') and (ganador <> 'X') do
        begin
            if juega_cpu then
                begin
                    repeat
                        i := random (3);
                        j := random (3);
                    until tictactoe[i,j] = ' ';
                    tictactoe[i,j] := 'O';
                end
            else
                begin
                    imprimir_cuadricula(tictactoe);
                    repeat
                        i := leer_numero('rengl'#162'n');
                        j := leer_numero('columna');
                        if tictactoe[i,j] <> ' ' then
                            writeln ('La casilla seleccionada ya est'#160' ocupada.'#10#13);
                    until tictactoe[i,j] = ' ';
                    tictactoe[i,j] := 'X';
                end;
            i := 0;
            while (ganador = ' ') and (i < 3) do
                begin
                    if (tictactoe[i,0] <> ' ') and (tictactoe[i,0] = tictactoe[i,1]) and (tictactoe[i,1] = tictactoe[i,2]) then
                        ganador := tictactoe[i,0];
                    inc (i);
                end;
            i := 0;
            while (ganador = ' ') and (i < 3) do
                begin
                    if (tictactoe[0,i] <> ' ') and (tictactoe[0,i] = tictactoe[1,i]) and (tictactoe[1,i] = tictactoe[2,i]) then
                        ganador := tictactoe[0,i];
                    inc (i);
                end;
            if (ganador = ' ') and (tictactoe[0,0] <> ' ') and (tictactoe[0,0] = tictactoe[1,1]) and
                (tictactoe[1,1] = tictactoe[2,2]) then
                ganador := tictactoe[0,0];
            if (ganador = ' ') and (tictactoe[0,2] <> ' ') and (tictactoe[0,2] = tictactoe[1,1]) and
                (tictactoe[1,1] = tictactoe[2,0]) then
                ganador := tictactoe[0,2];
            juega_cpu := not juega_cpu;
            inc (turnos);
        end;
    imprimir_cuadricula(tictactoe);
    case ganador of
        'O': writeln ('La computadora ha ganado.');
        'X': writeln ('El usuario ha ganado.');
        ' ': writeln ('Empate.');
    end;
    write (#10#13'Presione una tecla para terminar . . . ');
    ganador := readkey;
end.