如何确定我的Tic Tac Toe计划的获胜者

时间:2015-11-16 14:59:53

标签: java arrays tic-tac-toe

早些时候发帖:How do I make my tictactoe program scalable

我试图使Tic Tac Toe程序(人类与计算机)可扩展(可以更改电路板尺寸)。我早些时候遇到了重大问题,但修复了大部分问题。

游戏的规则是基本的Tic Tac Toe,但是一个不同的规则是,无论棋盘有多大(当>= 5时),玩家或计算机只需连续赢得五个分数。

现在我的计划唯一的游戏破解问题是确定谁赢了比赛。只有游戏才能结束“平局”。在这一刻。 (我还没有实现" >= 5"还有)。

具体的问题解释是我需要确定胜利者和结束屏幕,例如" computer wins"和/或" player wins"。

package tictactoe;

import java.util.Scanner;
import java.util.Random;

public class TicTacToe {

    public static int size;
    public static char[][] board;
    public static int score = 0;
    public static Scanner scan = new Scanner(System.in);

    /**
     * Creates base for the game.
     * 
     * @param args the command line parameters. Not used.
     */
    public static void main(String[] args) {

        System.out.println("Select board size");
        System.out.print("[int]: ");
        size = Integer.parseInt(scan.nextLine());

        board = new char[size][size];
        setupBoard();

        int i = 1;

        while (true) {
            if (i % 2 == 1) {
                displayBoard();
                getMove();
            } else {
                computerTurn();
            }

            // isWon()
            if (isDraw()) {
                System.err.println("Draw!");
                break;
            }

            i++;
        }

    }

    /**
     * Checks for draws.
     *
     * @return if this game is a draw
     */
    public static boolean isDraw() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (board[i][j] == ' ') {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Displays the board.
     * 
     * 
     */
    public static void displayBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.printf("[%s]", board[i][j]);
            }

            System.out.println();
        }
    }

    /**
     * Displays the board.
     * 
     * 
     */
    public static void setupBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i][j] = ' ';
            }
        }
    }

    /*
     * Checks if the move is allowed. 
     *
     *
     */
    public static void getMove() {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.printf("ROW: [0-%d]: ", size - 1);
            int x = Integer.parseInt(sc.nextLine());
            System.out.printf("COL: [0-%d]: ", size - 1);
            int y = Integer.parseInt(sc.nextLine());

            if (isValidPlay(x, y)) {
                board[x][y] = 'X';
                break;
            }
        }
    }

    /*
     * Randomizes computer's turn - where it inputs the mark 'O'.
     *
     *
     */
    public static void computerTurn() {
        Random rgen = new Random();  // Random number generator                        

        while (true) {
            int x = (int) (Math.random() * size);
            int y = (int) (Math.random() * size);

            if (isValidPlay(x, y)) {
                board[x][y] = 'O';
                break;
            }
        }
    }

    /**
     * Checks if the move is possible.
     * 
     * @param inX
     * @param inY
     * @return 
     */
    public static boolean isValidPlay(int inX, int inY) {

        // Play is out of bounds and thus not valid.
        if ((inX >= size) || (inY >= size)) {
            return false;
        }

        // Checks if a play have already been made at the location,
        // and the location is thus invalid.  
        return (board[inX][inY] == ' ');
    }
}

1 个答案:

答案 0 :(得分:2)

你已经有了循环播放,因此,在每次迭代中,你都会以同样的方式检查游戏isDraw(),同时检查一些玩家是否赢了:

while (true) {
    if (i % 2 == 1) {
        displayBoard();
        getMove();
    } else {
        computerTurn();
    }

    // isWon()
    if (isDraw()) {
        System.err.println("Draw!");
        break;
    } else if (playerHasWon()){
        System.err.println("YOU WIN!");
        break;
    } else if (computerHasWon()) {
        System.err.println("Computer WINS!\nYOU LOOSE!!");
        break;
    }

    i++;
}

创建所需方法后:

public static boolean playerHasWon() {
    boolean hasWon = false;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
              // check if 5 in a line
        }
    }

    return hasWon ;
}

public static boolean computerHasWon() {
    boolean hasWon = false;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
              // check if 5 in a line
        }
    }

    return hasWon ;
}

接下来的问题当然是我如何创建这种方法?如果你有这个问题,不知道怎么办,但要快速检查here here和{{3你会发现一些想法。

ADD ON:

为了澄清,我会创建一个函数返回int而不是booleans,以检查游戏是否使用了一些常量:

private final int DRAW = 0;
private final int COMPUTER = 1;
private final int PLAYER = 2;

private int isGameFinished() {
    if (isDraw()) return DRAW;
    else if (computerHasWon()) return COMPUTER;
    else if (playerHasWon()) return PLAYER;
}

然后只需使用开关案例here

进行检查
loop: while (true) {
    // other stufff
    switch (isGameFinished()) {
    case PLAYER:
        System.err.println("YOU WIN!");
        break loop;
    case COMPUTER:
        System.err.println("Computer WINS!\nYOU LOOSE!!");
        break loop;
    case DRW:       
        System.err.println("IT'S A DRAW");
        break loop;
}