井字游戏Java用户与计算机

问题描述 投票:0回答:1

我正在为我的课堂写井字游戏。一切正常,但我无法弄清楚如何使计算机播放器仅选择可用空间。我的代码出现故障,并允许计算机选择其他玩家空间或根本不玩。任何帮助将不胜感激。

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

public class TicTacToe1 {

public static void main(String[] args) {
    welcome();
    initializeBoard();
    printBoard();

    while ((!checkWin()) && (!checkDraw())) {
        playerMove();
        printBoard();
        System.out.println();
        computerMove();
        printBoard();
    }
    System.out.println();
    if (checkWin() == true) {
        System.out.println("The winner is " + currentTurn);
    }
    if (checkDraw() == true) {
        System.out.println("Draw");
    }
}

private static String[][] board = new String[3][3];

private static int row, column;

public static Scanner scan = new Scanner(System.in);

public static String currentTurn = "X";

// public static String computerTurn = "O";

public static String turn() {
    if (currentTurn == "X") {
        currentTurn = "O";
    } else {
        currentTurn = "X";
    }
    return currentTurn;
}

private static void welcome() {
    System.out.println("Tic Tac Toe");
    System.out.println("Please enter your coordinates for your location row (1-3) column (1-3):");
}

public static void initializeBoard() { // initialize tic tac toe
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            board[i][j] = "-";
        }
    }
}

public static void printBoard() {

    for (int i = 0; i < board.length; i++) {
        System.out.println();
        for (int j = 0; j < board.length; j++) {
            if (j == 0) {
                System.out.print("| ");
            }
            System.out.print(board[i][j] + " | ");
        }
    }
}

public static void playerMove() {
    System.out.println();
    System.out.println("Your Move: ");
    row = scan.nextInt() - 1;
    column = scan.nextInt() - 1;
    if (board[row][column] == "-") {
        board[row][column] = turn();
    } else {
        System.out.println("Invalid entry. Please go again");
        row = scan.nextInt() - 1;
        column = scan.nextInt() - 1;
        board[row][column] = turn();
    }

}

// public static void computerMove() {
// Random computerMove = new Random();
// row = computerMove.nextInt(3);
// column = computerMove.nextInt(3);
// if (board[row][column] == "-") {
// board[row][column] = turn();
// } else {

// }

// }

public static void computerMove() {
    Random computerMove = new Random();
    row = computerMove.nextInt(3);
    column = computerMove.nextInt(3);
    while (board[row][column] != "-") {
        // Random computerMove = new Random();
        // row = computerMove.nextInt(3);
        // column = computerMove.nextInt(3);
        if (board[row][column] == "-") {
            board[row][column] = turn();
        } else {
            row = computerMove.nextInt(3);
            column = computerMove.nextInt(3);
            board[row][column] = turn();
        }

    }

}

public static boolean checkWin() {
    return (checkDiagonalWin() || checkHorizontalWin() || checkVerticalWin());

}

public static boolean checkDiagonalWin() {
    if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[1][1] != "-")) {
        return true;
    }
    if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[1][1] != "-")) {
        return true;
    }
    return false;
}

public static boolean checkHorizontalWin() {
    // for (int i = 0; i < board.length; i++) {
    if ((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]) && (board[0][0] != "-")) {
        return true;
    }
    if ((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]) && (board[1][0] != "-")) {
        return true;
    }
    if ((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]) && (board[2][0] != "-")) {
        return true;
    }
    // }
    return false;
}

public static boolean checkVerticalWin() {
    // for (int j = 0; j < board.length; j++) {
    if ((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]) && (board[0][0] != "-")) {
        return true;
    }
    if ((board[0][1] == board[1][1]) && (board[0][1] == board[2][1]) && (board[0][1] != "-")) {
        return true;
    }
    if ((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]) && (board[0][2] != "-")) {
        return true;
    }
    // }
    return false;
}

public static boolean checkDraw() {
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board.length; j++) {
            if (board[i][j] == "-") {
                return false;
            }
        }
    }
    return true;
}

}

java tic-tac-toe
1个回答
0
投票

问题出在您的计算机上。移动逻辑。

    public static void computerMove() {
    Random computerMove = new Random();
    row = computerMove.nextInt(3);
    column = computerMove.nextInt(3);
    while (board[row][column] != "-") {
        row = computerMove.nextInt(3);
        column = computerMove.nextInt(3);
    }
    board[row][column] = turn();
}

这应该适合您,只需将其复制粘贴到您的computerMove位置即可。

现在,为什么您的代码无法正常工作:-您的代码:

    while (board[row][column] != "-") {

    if (board[row][column] == "-") {
        board[row][column] = turn();
    } else {
        row = computerMove.nextInt(3);
        column = computerMove.nextInt(3);
        board[row][column] = turn();
    }

}

while循环查看该位置,发现没有'-',因此运行。然后在while循环中,您有一个if语句,该语句检查是否在该位置有“-”。这永远不可能成立,因为我们的while循环将无法运行。

最好的想法是让您的代码不断更改行和列,直到获得带有'-'的位置为止,并使用while循环来执行此操作。一旦获得“-”,您的while循环将不再运行,因此您只需在while循环外设置board[row][columns] = turn(),您的代码将可以正常工作。

P.S。尽了很多不制造机器的意愿,这是对您的

的参考

我的代码出现故障,使计算机可以选择其他玩家空间或完全不玩游戏

与您的程序一起玩耍:)

〜HelpfulStackoverflowCommunity

© www.soinside.com 2019 - 2024. All rights reserved.