如何使我的tictactoe程序可扩展

时间:2015-11-11 16:48:02

标签: java arrays

我必须做这个tic tac toe程序,可以从3x3缩放到10x10。 我在教程的帮助下掌握了游戏的基础知识,但没有成功地找到如何使其可扩展。

程序是玩家与计算机,计算机试图阻止玩家赢得游戏。游戏规则是基本的tictactoe / fiveinarow / sixinarowow等等。

public class TicTacToe {

    static int A1, A2, A3, B1, B2, B3, C1, C2, C3; // Board spots.

    static Scanner sc = new Scanner(System.in);

    /**
     *
     *
     *
     */
    public static void main(String[] args) {
        String prompt = "Make you move with the letters A-C and numbers 1-3"
                + "\nYou are \"X\" and the computer is \"O\"\n";

        String yourTurn; // player's playturn
        String computerTurn; // computer's playturn

        boolean gameIsWon = false; // Boolean to game win/lose

        // There are a maximum of nine plays, so a for loop keeps track of
        // the number of plays. The game is over after the ninth play.
        // Each time through the loop, both the human and the computer play.
        // So i is incremented in the body of the loop after the computer plays.
        for (int i = 1; i <= 9; i++) {

            // Player
            yourTurn = getMove(prompt);
            updateBoard(yourTurn, 1);

            displayBoard();             // shows the board 
            if (isGameWon()) {
                System.out.println("YOU WIN!");
                gameIsWon = true;
                break;
            }

            // Computer 
            if (i < 9) {
                computerTurn = computerTurn();
                System.out.println("Computer's turn: " + computerTurn);
                updateBoard(computerTurn, 2);

                displayBoard();
                if (isGameWon()) {
                    System.out.println("YOU LOSE!");
                    gameIsWon = true;
                    break;
                }
                prompt = "Your turn: ";
                i++;
            }
        }
        if (!gameIsWon) {
            System.out.println("NO WINNER! DRAW!");
        }
    }

    /**
     *
     *
     *
     * @param prompt
     * @return 
     */
    public static String getMove(String prompt) {
        String play;
        System.out.print(prompt);
        do {
            play = sc.nextLine();
            if (!isValidPlay(play)) {
                System.out.println("Error! Use only combination of A-C and 1-3!"
                        + " For Example: A1"
                        + "\nIf you used the right combination "
                        + "check if you or the computer do not already have "
                        + "\"X\" or \"O\" there.");
            }
        } while (!isValidPlay(play));
        return play;
    }

    /**
     * Checks if the play is possible.
     *
     *
     * @param play
     * @return
     */
    public static boolean isValidPlay(String play) {
        if (play.equalsIgnoreCase("A1") & A1 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("A2") & A2 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("A3") & A3 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("B1") & B1 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("B2") & B2 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("B3") & B3 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("C1") & C1 == 0) {
            return true;
        }
        if (play.equalsIgnoreCase("C2") & C2 == 0) {
            return true;
        }
        return play.equalsIgnoreCase("C3") & C3 == 0;
    }

    /**
     *
     *
     *
     * @param play
     * @param player
     */
    public static void updateBoard(String play, int player) {
        if (play.equalsIgnoreCase("A1")) {
            A1 = player;
        }
        if (play.equalsIgnoreCase("A2")) {
            A2 = player;
        }
        if (play.equalsIgnoreCase("A3")) {
            A3 = player;
        }
        if (play.equalsIgnoreCase("B1")) {
            B1 = player;
        }
        if (play.equalsIgnoreCase("B2")) {
            B2 = player;
        }
        if (play.equalsIgnoreCase("B3")) {
            B3 = player;
        }
        if (play.equalsIgnoreCase("C1")) {
            C1 = player;
        }
        if (play.equalsIgnoreCase("C2")) {
            C2 = player;
        }
        if (play.equalsIgnoreCase("C3")) {
            C3 = player;
        }
    }

    /**
     *
     *
     *
     */
    public static void displayBoard() {
        String line;
        System.out.println();
        line = " " + getXO(A1) + " | " + getXO(A2) + " | " + getXO(A3);
        System.out.println(line);
        System.out.println("-----------");
        line = " " + getXO(B1) + " | " + getXO(B2) + " | " + getXO(B3);
        System.out.println(line);
        System.out.println("-----------");
        line = " " + getXO(C1) + " | " + getXO(C2) + " | " + getXO(C3);
        System.out.println(line);
        System.out.println();
    }

    public static String getXO(int square) {
        if (square == 1) {
            return "X";
        }
        if (square == 2) {
            return "O";
        }
        return " ";
    }

    /**
     *
     *
     *
     * @return
     */
    public static String computerTurn() {
        if (A1 == 0) {
            return "A1";
        }
        if (A2 == 0) {
            return "A2";
        }
        if (A3 == 0) {
            return "A3";
        }
        if (B1 == 0) {
            return "B1";
        }
        if (B2 == 0) {
            return "B2";
        }
        if (B3 == 0) {
            return "B3";
        }
        if (C1 == 0) {
            return "C1";
        }
        if (C2 == 0) {
            return "C2";
        }
        if (C3 == 0) {
            return "C3";
        }
        return "";
    }

    /**
     *
     *
     *
     * @return
     */
    public static boolean isGameWon() {
        if (isRowWon(A1, A2, A3)) {
            return true;
        }
        if (isRowWon(B1, B2, B3)) {
            return true;
        }
        if (isRowWon(C1, C2, C3)) {
            return true;
        }
        if (isRowWon(A1, B1, C1)) {
            return true;
        }
        if (isRowWon(A2, B2, C2)) {
            return true;
        }
        if (isRowWon(A3, B3, C3)) {
            return true;
        }
        if (isRowWon(A1, B2, C3)) {
            return true;
        }
        return isRowWon(A3, B2, C1);
    }

    /**
     *
     *
     *
     * @param a
     * @param b
     * @param c
     * @return
     */
    public static boolean isRowWon(int a, int b, int c) {
        return ((a == b) & (a == c) & (a != 0));
    }
}

1 个答案:

答案 0 :(得分:3)

编写这个程序的方法真的很长,但有些提示:

static int A1, A2, A3, B1, B2, B3, C1, C2, C3; // Board spots.

实际上这是模拟

static int board[][] = new int[3][3];

通过

扩展程序替换变量声明
static int size;
static int board[][];

询问用户的尺寸和

System.out.print("Insert size of the board : ");
size = c.nextInt();
board = new int[size][size];

在此之后,相应地更新您的displayBoardisValidPlayupdateBoard

注意如果您不想使用逗号,我希望像1,1而不是A1一样进行移动,在0开始登录你可以玩01

例如

(你会看到现在的方法要短得多)。

public static boolean isValidPlay(String play) {  // play = 1,1 or 3,1 
    int x = Integer.valueOf(play.split(",")[0]); 
    int y = Integer.valueOf(play.split(",")[0]);

    // check if move is inside the board
    if (x > size || y > size) return false;

    // check if given position is empty and return accordingly
    if (board[x][y] == 0) return true;
    else return false;
}