Java中Tic Tac Toe游戏的赢条件检查和cpu输入有问题

Java中Tic Tac Toe游戏的赢条件检查和cpu输入有问题,java,arrays,arraylist,multidimensional-array,Java,Arrays,Arraylist,Multidimensional Array,每当连续有3个(无论是X还是O),它都会返回我赢得比赛(例如X | O | X)。我认为这与我的placePiece方法有关,但我不能完全确定。在我再把头发扯下来之前,有人能帮我找出我的逻辑出了什么问题吗 我有时也会遇到另一个问题,当它是我的第一个回合,我输入一个数字的地方,计算机也会输入相同的数字,并把它放在我的上面。然而,这只发生在第一次转弯时,不会再发生。我想这是因为在游戏开始时,playerPositions ArrayList中没有任何内容。计算机输入和用户输入必须同时发生,或者可能是

每当连续有3个(无论是X还是O),它都会返回我赢得比赛(例如X | O | X)。我认为这与我的placePiece方法有关,但我不能完全确定。在我再把头发扯下来之前,有人能帮我找出我的逻辑出了什么问题吗

我有时也会遇到另一个问题,当它是我的第一个回合,我输入一个数字的地方,计算机也会输入相同的数字,并把它放在我的上面。然而,这只发生在第一次转弯时,不会再发生。我想这是因为在游戏开始时,playerPositions ArrayList中没有任何内容。计算机输入和用户输入必须同时发生,或者可能是计算机在用户之前实际输入?我不太确定,但我已经被这些问题困扰了很长一段时间了。请帮助我:(

公共类TicTacToe{
静态ArrayList playerPositions=新建ArrayList();
静态ArrayList cpuppositions=新ArrayList();
公共静态void main(字符串[]args){
//初始化游戏板
char[][]游戏板={
{' ', '|', ' ', '|', ' '},
{'-', '+', ' ', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', ' ', '+', '-'},
{' ', '|', ' ', '|', ' '}
};
//打印接受字符数组作为参数的游戏板
印刷游戏板(游戏板);
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您想成为X还是O?”);
char user=scan.nextLine().charAt(0);
字符cpu=“”;
如果(用户=='x'){
cpu='o';
}否则{
cpu='x';
}
//while(true)无限期地循环代码
while(true){
扫描=新扫描仪(System.in);
System.out.println(“您的移动!(1-9):”;
int pos=scan.nextInt();
//检查您输入的pos是否已在阵列列表中或
//如果您正试图采取计算机已经采取的立场
while(playerPositions.contains(pos)| | cpuPositions.contains(playerPositions)){
System.out.println(“位置已被占据…请输入另一个位置:”);
pos=scan.nextInt();
}
//placepiece,获取初始字符[],从扫描仪获取int pos,并从扫描仪获取字符用户
placePiece(游戏板、pos、用户);
//放置工件后检查结果
字符串结果=winnerWinnerChickenDinner();
//检查每次放置工件后是否有结果
if(result.length()>0){
系统输出打印项次(结果);
打破
}
Random rand=新的Random();
int cpuPos=rand.nextInt(9)+1;
placePiece(游戏板、cpuPos、cpu);
//检查计算机生成的号码是否已由您使用,或者计算机是否已生成该位置
while(playerPositions.contains(cpuPos)| | cpuppositions.contains(cpuPos)){
cpuPos=rand.nextInt(9)+1;
}
印刷游戏板(游戏板);
//cpu放置工件后检查结果
结果=WinnerWinnerWinnerChickendInner();
//检查是否有结果或只是一个空字符串
if(result.length()>0){
系统输出打印项次(结果);
打破
}
}
}
公共静态void打印游戏板(char[][]游戏板){
//对于游戏板中的每一行
用于(字符[]行:游戏板){
//对于行中的每个字符
for(字符c:行){
系统输出打印(c);
}
//为每行打印一行
System.out.println();
}
}
公共静态无效placePiece(char[][]游戏板、int-pos、char-user){
//初始化字符
字符符号=“”;
//检查此人是否选择了x
如果(用户=='x'){
符号='X';
playerPositions.add(位置);
}否则{
cpuppositions.add(pos);
}
如果(用户=='o'){
符号='O';
playerPositions.add(位置);
}否则{
cpuppositions.add(pos);
}
开关(pos){
案例1:
游戏板[0][0]=符号;
打破
//[0][1]是行“|”的空格
案例2:
游戏板[0][2]=符号;
打破
案例3:
游戏板[0][4]=符号;
打破
案例4:
游戏板[2][0]=符号;
打破
案例5:
游戏板[2][2]=符号;
打破
案例6:
游戏板[2][4]=符号;
打破
案例7:
游戏板[4][0]=符号;
打破
案例8:
游戏板[4][2]=符号;
打破
案例9:
游戏板[4][4]=符号;
打破
违约:
打破
}
}
公共静态字符串winnerWinnerChickenDinner(){
List topRow=Arrays.asList(1,2,3);
listmidrow=Arrays.asList(4,5,6);
List botRow=Arrays.asList(7,8,9);
//列表从左到右,所以必须使它们从上到下
List leftCol=Arrays.asList(1,4,7);
List midCol=Arrays.asList(2,5,8);
List rightCol=Arrays.asList(3,6,9);
//对角线也一样
List leftRightDia=Arrays.asList(1,5,9);
listRightLeftDia=Arrays.asList(3,5,7);
//列表列表…将上面的列表添加到wincon列表
List winCon=new ArrayList();
winCon.add(顶行);
winCon.add(中行);
winCon.add(botRow);
winCon.add(leftCol);
winCon.add(midCol);
winCon.add(右列);
winCon.add(左-右直径);
winCon.add(右左直径);
用于(列表l:winCon){
if(播放位置。包含所有(l)){
返回“你赢了!”;
}else if(cpuppositions.containsAll(l)){
return“随机生成数字的计算机”
public class TicTacToe {
static ArrayList<Integer> playerPositions = new ArrayList<>();
static ArrayList<Integer> cpuPositions = new ArrayList<>();
public static void main (String[] args) {
    //initializing the gameboard
    char[][] gameBoard = {
            {' ', '|', ' ', '|', ' '},
            {'-', '+', ' ', '+', '-'},
            {' ', '|', ' ', '|', ' '},
            {'-', '+', ' ', '+', '-'},
            {' ', '|', ' ', '|', ' '}
    };
    //printing the gameboard which takes in a char array as the parameters
    printGameBoard(gameBoard);
    Scanner scan = new Scanner(System.in);
    System.out.println("Would you like to be X or O?");
    char user = scan.nextLine().charAt(0);
    char cpu = ' ';
    if (user == 'x') {
        cpu = 'o';
    } else {
        cpu = 'x';
    }
    // while (true) loops code indefinitely
    while (true) {
        scan = new Scanner(System.in);

        System.out.println("Your move! (1-9): ");
        int pos = scan.nextInt();
        //checking if the pos you entered is already in the array list or
        // if you are trying to take a position taken by the computer already
        while(playerPositions.contains(pos) || cpuPositions.contains(playerPositions)) {
            System.out.println("Position is already taken... Enter another position: ");
            pos = scan.nextInt();
        }
        //placepiece by taking in the initial char[][], getting the int pos from scanner, and getting the char user from scanner
        placePiece(gameBoard, pos, user);
        //checking the result after you place a piece
        String result = winnerWinnerChickenDinner();
        //checking if there even is a result after every time you place a piece
        if (result.length()>0) {
            System.out.println(result);
            break;
        }


        Random rand = new Random();
        int cpuPos = rand.nextInt(9) + 1;
        placePiece(gameBoard, cpuPos, cpu);
        //checking if the number the computer generated is already taken by you or if the computer already generated that position
        while(playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
            cpuPos = rand.nextInt(9) + 1;
        }

        printGameBoard(gameBoard);
        //checking result after cpu places a piece
        result = winnerWinnerChickenDinner();
        //checking if there is a result or just an empty string
        if (result.length()>0) {
            System.out.println(result);
            break;
        }
    }
}
public static void printGameBoard(char[][] gameBoard){
    //for each row in the gameboard
    for(char[] row : gameBoard) {
        //for each character in the row
        for(char c : row) {
            System.out.print(c);
        }
        //printing a line for each row
        System.out.println();
    }
}
public static void placePiece(char[][] gameBoard, int pos, char user) {
    //initializing the char
    char symbol = ' ';
    //checking if the person chose x or not
    if(user == 'x') {
        symbol = 'X';
        playerPositions.add(pos);
    } else {
        cpuPositions.add(pos);
    }
    if (user == 'o') {
        symbol = 'O';
        playerPositions.add(pos);
    } else {
        cpuPositions.add(pos);
    }
    switch (pos) {
        case 1:
            gameBoard[0][0] = symbol;
            break;
            // [0][1] is the space for the line '|'
        case 2:
            gameBoard[0][2] = symbol;
            break;
        case 3:
            gameBoard[0][4] = symbol;
            break;
        case 4:
            gameBoard[2][0] = symbol;
            break;
        case 5:
            gameBoard[2][2] = symbol;
            break;
        case 6:
            gameBoard[2][4] = symbol;
            break;
        case 7:
            gameBoard[4][0] = symbol;
            break;
        case 8:
            gameBoard[4][2] = symbol;
            break;
        case 9:
            gameBoard[4][4] = symbol;
            break;
        default:
            break;
    }
}
public static String winnerWinnerChickenDinner(){
    List topRow = Arrays.asList(1, 2, 3);
    List midRow = Arrays.asList(4, 5, 6);
    List botRow = Arrays.asList(7, 8, 9);
    //list goes from left to right so have to make them top to bottom
    List leftCol = Arrays.asList(1, 4, 7);
    List midCol = Arrays.asList(2, 5, 8);
    List rightCol = Arrays.asList(3, 6, 9);
    //same for diagonals
    List leftRightDia = Arrays.asList(1, 5, 9);
    List rightLeftDia = Arrays.asList(3, 5, 7);
    //List of a list... adding lists above to wincon list
    List<List> winCon = new ArrayList<List>();
    winCon.add(topRow);
    winCon.add(midRow);
    winCon.add(botRow);
    winCon.add(leftCol);
    winCon.add(midCol);
    winCon.add(rightCol);
    winCon.add(leftRightDia);
    winCon.add(rightLeftDia);
    for (List l : winCon) {
        if (playerPositions.containsAll(l)) {
            return "You won!";
        } else if (cpuPositions.containsAll(l)) {
            return "A computer that randomly generates numbers won...";
        } else if (playerPositions.size() + cpuPositions.size() == 9){
            return "Tie";
        }
    }
    return "";
}
// Change the method signature so it includes a boolean describing who is using the method
public static void placePiece(char[][] gameBoard, int pos, char symbol, boolean isUser) {
    // Change the symbol to the right character
    if (symbol == 'x') {
        symbol = 'X';
    } else if (symbol == 'o') {
        symbol = 'O';
    } else {
        symbol = ' ';
    }
    // Now put the position in the right list
    if (isUser) {
        playerPositions.add(pos);
    } else {
        cpuPositions.add(pos);
    }
    // Keep the other code the same
    switch(pos):
        case 1:
            gameBoard[0][0] = symbol;
    ...
    Random rand = new Random();
    int cpuPos = rand.nextInt(9) + 1;
    //checking if the number the computer generated is already taken by you or if the computer already generated that position
    while (playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
        cpuPos = rand.nextInt(9) + 1;
    }
    // calling this after cpuPos is updated (note this uses the updated placePiece method signature)
    placePiece(gameBoard, cpuPos, cpu, false); // false signifies this is for the CPU
if (user == 'o') {
    symbol = 'O';
        playerPositions.add(pos);
} else {
        cpuPositions.add(pos);
} 
if (user == 'x') .... etc.
char symbol = Character.toUpperCase(playerSymbol);
if (user.equals("user")) {
        playerPositions.add(pos);
    } else {
        cpuPositions.add(pos);
} 
// rest of the method 
placePiece(gameBoard, cpuPos, cpu);
while (playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
      cpuPos = rand.nextInt(9) + 1;
}
for (List l : winCon) {
     if (playerPositions.containsAll(l)) {
         return "You won!";
     } else if (cpuPositions.containsAll(l)) {
         return "A computer that randomly generates numbers won...";
     }
}
if (playerPositions.size() + cpuPositions.size() == 9) {
    return "Tie";
}
return "";