井字游戏预测平局(平局)

时间:2019-04-24 10:54:17

标签: java

我正在为自己的作业打井字游戏,老师告诉我要打领带时要打领带。 (假设玩家不聪明),例如

x o x
- - -
o x o

只能导致平局,所以当发生这种情况时,我们应该以“这是平局”结束游戏。

我已经完成了用char [] []数组对井字游戏的编码,但是我不知道如何预测抽奖。

public class Ttt22 {

    private static int spacesLe

    private static char[][] board;

    public static void main (String[] args) {
        // the first move belongs to X.
        System.out.println("Welcome to Tic Tac Toe");
        board = new char[3][3];
        initializeBoard();
        firstDraw();
        char mark = 'X';

        while (true) {
            int square = getLegalMove(mark);
            move(square, mark);
            draw();
            if (is3InRow(mark)) {
                System.out.println(mark + " wins!");
                break;
            }

            if (isBoardFull()) {
                System.out.println("Tie game!");
                break;
            }

            if (mark == 'X') {
                mark = 'O';
            }
            else {
                mark = 'X';
            }
        }
    }

    public static int getLegalMove (char mark) {
        java.util.Scanner console = new java.util.Scanner(System.in);
        while (true) {
            System.out.println(mark + "'s next move: ");
            int square = console.nextInt();
            if ((square >= 1) &&
                (square <= 9) &&
                (isSquareEmpty(square))) {
                return square;
            }
            System.out.println("\nIllegal move, try again\n");
        }
    }

    public static void initializeBoard () {
        spacesLeft = 9;
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                board[i][j] = ' ';
            }
        }
    }

    public static void firstDraw () {
        System.out.println();
        System.out.println("   |   |   ");
        System.out.println(" " + 1 + " | " + 2 + " | " + 3);
        System.out.println("   |   |   ");
        System.out.println("---+---+---");
        System.out.println("   |   |   ");
        System.out.println(" " + 4 + " | " + 5 + " | " + 6);
        System.out.println("   |   |   ");
        System.out.println("---+---+---");
        System.out.println("   |   |   ");
        System.out.println(" " + 7 + " | " + 8 + " | " + 9);
        System.out.println("   |   |   ");
        System.out.println();
    }

    public static void draw () {
        System.out.println();
        System.out.println("   |   |   ");
        System.out.println(" " + board[0][0] + " | "
                           + board[0][1] + " | " + board[0][2]);
        System.out.println("   |   |   ");
        System.out.println("---+---+---");
        System.out.println("   |   |   ");
        System.out.println(" " + board[1][0] + " | "
                           + board[1][1] + " | " + board[1][2]);
        System.out.println("   |   |   ");
        System.out.println("---+---+---");
        System.out.println("   |   |   ");
        System.out.println(" " + board[2][0] + " | "
                           + board[2][1] + " | " + board[2][2]);
        System.out.println("   |   |   ");
        System.out.println();
    }

    public static void move (int square, char mark) {
        if (isSquareEmpty(square)) {
            spacesLeft = spacesLeft - 1;
        }
        int row = (square - 1) / 3;
        int column = (square - 1) % 3;
        board[row][column] = mark;
    }

    public static boolean isSquareEmpty (int square) {
        int row = (square - 1) / 3;
        int column = (square - 1) % 3;
        return (board[row][column] == ' ');
    }

    public static boolean is3InRow (char mark) {            
        return
            (board[0][0] == mark && board[0][1] == mark && board[0][2] == mark) ||
            (board[1][0] == mark && board[1][1] == mark && board[1][2] == mark) ||
            (board[2][0] == mark && board[2][1] == mark && board[2][2] == mark) ||
            (board[0][0] == mark && board[1][0] == mark && board[2][0] == mark) ||
            (board[0][1] == mark && board[1][1] == mark && board[2][1] == mark) ||
            (board[0][2] == mark && board[1][2] == mark && board[2][2] == mark) ||
            (board[0][0] == mark && board[1][1] == mark && board[2][2] == mark) ||
            (board[0][2] == mark && board[1][1] == mark && board[2][0] == mark);
    }

    public static boolean isBoardFull () {
        return spacesLeft == 0;
    }
}

预期的结果是游戏是

x o x
- - -
o x o

它打印“并列游戏!”游戏结束

2 个答案:

答案 0 :(得分:2)

嗯,简单地说,在“玩家” 的每一步之后,启动一个自动过程,该过程将从该特定位置开始计算游戏的所有结果。如果所有这些都以平局结束,那么您可以放心声明最终结果是平局。当然,这是我首先想到的第一个,根本没有优化的解决方案。如果您有兴趣,可以在此基础上进行一些优化。

答案 1 :(得分:0)

对于这类任务,有一些算法,建议您阅读有关Minimax