tic tac toe java用户与计算机

tic tac toe java用户与计算机,java,tic-tac-toe,Java,Tic Tac Toe,我正在为我的班级写一个井字游戏。一切正常,但我不知道如何让我的电脑播放器只选择可用的空间。我的代码有问题,允许计算机选择其他玩家或根本不玩。任何帮助都将不胜感激 import java.util.Random; import java.util.Scanner; public class TicTacToe1 { public static void main(String[] args) { welcome(); initializeBoard(); printBo

我正在为我的班级写一个井字游戏。一切正常,但我不知道如何让我的电脑播放器只选择可用的空间。我的代码有问题,允许计算机选择其他玩家或根本不玩。任何帮助都将不胜感激

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;
}
import java.util.Random;
导入java.util.Scanner;
公共类TictaToe1{
公共静态void main(字符串[]args){
欢迎();
初始化板();
印制板();
而((!checkWin())&&(!checkDraw()){
playerMove();
印制板();
System.out.println();
computerMove();
印制板();
}
System.out.println();
如果(checkWin()==true){
System.out.println(“获胜者是”+currentTurn);
}
如果(checkDraw()==true){
系统输出打印项次(“绘制”);
}
}
私有静态字符串[][]板=新字符串[3][3];
私有静态int行、列;
公共静态扫描仪扫描=新扫描仪(System.in);
公共静态字符串currentTurn=“X”;
//公共静态字符串computerTurn=“O”;
公共静态字符串turn(){
如果(当前匝数=“X”){
currentTurn=“O”;
}否则{
currentTurn=“X”;
}
回流匝数;
}
私有静态void欢迎(){
系统输出打印项次(“Tic Tac Toe”);
System.out.println(“请输入您所在位置行(1-3)列(1-3):”的坐标;
}
public static void initializeBoard(){//initialize tic tac toe
对于(int i=0;i

}问题出在您的计算机移动逻辑中

    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();
}
这应该适合你,只是复制粘贴在你的电脑移动的地方

现在,关于为什么您的代码不起作用:- 您的代码:

    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循环将不再运行,因此您可以将
board[row][columns]=turn()
设置在while循环之外,代码将正常工作

p.S.花了很大的毅力才不让一台机器都起义参考你的

我的代码有问题,允许计算机选择其他玩家或根本不玩

玩得开心:)


~HelpfulStackoverflowCommunity

Np。如果答案有效,请将其标记为完成