• Lenguaje

    Java

  • 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
import java.util.Random;
import java.util.Scanner;

public class TicTacToe {

    private static Scanner in = new Scanner(System.in);
       
    public static void main (String[] args) {
        Random rand = new Random();
        char[][] tictactoe = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
        char ganador = ' ';
        boolean juega_cpu = rand.nextBoolean();
        int i, j, turnos;
        for (turnos=0; turnos<9 && ganador!='O' && ganador!='X'; turnos++) {
            if (juega_cpu) {
                do {
                    i = rand.nextInt(3);
                    j = rand.nextInt(3);
                } while (tictactoe[i][j] != ' ');
                tictactoe[i][j] = 'O';
            } else {
                imprimirCuadricula(tictactoe);
                do {
                    i = leerNumero("rengl\242n");
                    j = leerNumero("columna");
                    if (tictactoe[i][j] != ' ')
                        System.out.println("La casilla seleccionada ya est\240 ocupada.\n");
                } while (tictactoe[i][j] != ' ');
                tictactoe[i][j] = 'X';
            }
            for (i=0; ganador==' ' && i<3; i++)
                if (tictactoe[i][0] != ' ' && tictactoe[i][0] == tictactoe[i][1] && tictactoe[i][1] == tictactoe[i][2])
                    ganador = tictactoe[i][0];
            for (i=0; ganador==' ' && i<3; i++)
                if (tictactoe[0][i] != ' ' && tictactoe[0][i] == tictactoe[1][i] && tictactoe[1][i] == tictactoe[2][i])
                    ganador = tictactoe[0][i];
            if (ganador == ' ' && tictactoe[0][0] != ' ' && tictactoe[0][0] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][2])
                ganador = tictactoe[0][0];
            if (ganador == ' ' && tictactoe[0][2] != ' ' && tictactoe[0][2] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][0])
                ganador = tictactoe[0][2];
            juega_cpu = !juega_cpu;
        }
        imprimirCuadricula(tictactoe);
        switch (ganador) {
            case 'O': System.out.println("La computadora ha ganado."); break;
            case 'X': System.out.println("El usuario ha ganado.");     break;
            case ' ': System.out.println("Empate.");                   break;
        }
        System.out.println();
    }

    public static void imprimirCuadricula (char[][] tictactoe) {
        int i, j;
        System.out.println("\n\n\n");
        System.out.println("      \311\315\315\315\313\315\315\315\313\315\315\315\273");
        System.out.println("      \272 1 \272 2 \272 3 \272");
        System.out.println("      \310\315\315\315\312\315\315\315\312\315\315\315\274");
        System.out.println();
        System.out.println("\311\315\273   \311\315\315\315\313\315\315\315\313\315\315\315\273");
        for (i=0; i<tictactoe.length; i++) {
            if (i != 0)
                System.out.println("\314\315\271   \314\315\315\315\316\315\315\315\316\315\315\315\271");
            System.out.print("\272" + (i+1) + "\272   \272");
            for (j=0; j<tictactoe[i].length; j++)
                System.out.print(" " + tictactoe[i][j] + " \272");
            System.out.println();
        }
        System.out.println("\310\315\274   \310\315\315\315\312\315\315\315\312\315\315\315\274\n");
    }

    static int leerNumero (String variable) {
        int numero;
        do {
            System.out.print("Seleccione el n\243mero de " + variable + ": ");
            numero = in.nextInt();
            if (numero != 1 && numero != 2 && numero != 3)
                System.out.println("La casilla seleccionada esta fuera del rango.\n");
        } while (numero != 1 && numero != 2 && numero != 3);
        return numero - 1;
    }

}