Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中Tic Tac Toe的赢/平检查_Java_Tic Tac Toe - Fatal编程技术网

Java中Tic Tac Toe的赢/平检查

Java中Tic Tac Toe的赢/平检查,java,tic-tac-toe,Java,Tic Tac Toe,我正试图让我的代码检查我的井字游戏的胜利和平局。到目前为止,赢支票只适用于对角线,但它仍然是有缺陷的。领带检查根本不起作用。我需要winCheck方法的帮助,并让它检查横向、垂直和对角的胜利。我还需要帮助打印赢得比赛的球员。这是我的密码 import java.util.Scanner; import java.util.StringTokenizer; public class TicTacToe { public static void main(String[] args){

我正试图让我的代码检查我的井字游戏的胜利和平局。到目前为止,赢支票只适用于对角线,但它仍然是有缺陷的。领带检查根本不起作用。我需要winCheck方法的帮助,并让它检查横向、垂直和对角的胜利。我还需要帮助打印赢得比赛的球员。这是我的密码

import java.util.Scanner;
import java.util.StringTokenizer;

public class TicTacToe {
public static void main(String[] args){

    Scanner console = new Scanner(System.in);
    System.out.print("Enter board size (e.g. 5): ");
    int size = 0;
    if(console.hasNextInt()){   //If the console has an integer, sets the size
        size = console.nextInt();
    } else {
        while(!console.hasNextInt()){   //If no integer
            console.next(); //Discards bad input
            System.out.print("Invalid input. Enter a whole positive integer: ");
        }
        size = console.nextInt();   //Sets integer once valid input is entered
    }

    int[][] array = new int[size][size];    //Sets the board size by the input (input x input)
    int i=0;    //Initializing "i" for the loops

    do{
        i=1;    //Resets "i" to one
        for(; i<=2; i++){   //For loop repeating for both players 1 and 2
            System.out.print("Player " + i + "'s Move (row,column): ");
            String plyrInput = console.next();
            //errorCheck(plyrInput, console);
            inputParse(plyrInput, i, array);    //Sends player's coordinates, player number, and the array
            if(winCheck(array, i)==true){
                System.out.print("Game over.");
                break;
            }
        }

    }while(winCheck(array, i)==false);
    console.close();
}

public static String errorCheck(String input, Scanner console){
    while(!input.contains(",")){
        console.next();
        System.out.print("Please re-enter in proper format; row,column: ");
    }
    return console.next();
}



public static void inputParse(String input, int player, int[][] array){
    StringTokenizer tokens = new StringTokenizer(input, ",");   //Parses input by comma

    String row = tokens.nextToken();    //First part of input
    String column = tokens.nextToken(); //Second part of input

    int rowNum = Integer.parseInt(row)-1;   //Decreases the inputs by one for the array
    int colNum = Integer.parseInt(column)-1;

    //System.out.println(rowNum);
    //System.out.println(colNum);

    if(player==1){  //If the player is player one, INCREASES the array location by 1
        ++array[rowNum][colNum];
    } else {        //If the player is player two, DECREASES the array location by 1
        --array[rowNum][colNum];
    }


    for(int x=0; x<array[0].length; x++){
        for(int y=0; y<array.length; y++){
            System.out.print(array[x][y]);
        }
        System.out.println();
    }

    boardPrint(array);
}

public static void boardPrint(int[][] array){
    System.out.print("+");
    for(int size=0; size<array.length; size++){ //Prints the "+-+-+-+" part of board according to given size
        System.out.print("-+");
    }
    System.out.println();

    for(int row=0; row<array[0].length; row++){
        for(int column=0; column<array.length; column++){
            System.out.print("|");
            if(array[row][column]==1){  //If the array index has 1, prints out "X" (for player 1)
                System.out.print("X");
            } else if(array[row][column]==-1){  //If negative 1, prints out "O" (for player 2)
                System.out.print("O");
            } else {    
                System.out.print(" ");  //Otherwise if array something else, prints out a regular space
            }
        }
        System.out.println("|");

        System.out.print("+");
        for(int size=0; size<array.length; size++){
            System.out.print("-+");
        }
        System.out.println();
    }
}

public static boolean winCheck(int[][] array, int player){
    int counter = 1;
    for(int i=0; i<array.length-1; i++){
        if(array[i][i] == array[i+1][i+1]){
            counter++;
        }
        if(counter==array.length){
            System.out.println("Player " + player + " wins.");
            return true;
        }
    }
    System.out.println(counter);
    return false;
}
}
import java.util.Scanner;
导入java.util.StringTokenizer;
公共类Tictatcoe{
公共静态void main(字符串[]args){
扫描仪控制台=新扫描仪(System.in);
系统输出打印(“输入电路板尺寸(如5):”;
int size=0;
if(console.hasNextInt()){//如果控制台有一个整数,则设置大小
size=console.nextInt();
}否则{
而(!console.hasnetint()){//如果没有整数
console.next();//丢弃错误的输入
System.out.print(“输入无效。输入一个正整数:”);
}
size=console.nextInt();//输入有效输入后设置整数
}
int[][]数组=新int[size][size];//通过输入设置电路板大小(输入x输入)
int i=0;//初始化循环的“i”
做{
i=1;//将“i”重置为1

对于(;i这看起来像一个练习,所以我只想给出一个答案的概要。但是如果你想简化检查逻辑,我会写一个类似的方法

static boolean checkLine(int[][] array, int startRow, int startCol, int rowIncrement, int colIncrement) { 
    ...
}
如果数组的一条直线上的所有元素都相等(而不是空的正方形),则返回
true
,否则返回
false
。对于已经编码的对角线,可以调用

win = checkLine(array, 0, 0, 1, 1);
因为您将要开始查看
数组[0][0]
,并且您检查的每个方块在第1行比前一行多,第1列比前一列多(因此
数组[1][1]
数组[2][2]

要检查最下面一行,您将在那里查看
array[2][0]
array[2][1]
array[2][2]
,您可以这样称呼它:

win = checkLine(array, 2, 0, 0, 1);
这意味着您从第2行和第0列开始,然后要检查其他方块,请将0添加到行号(因为您位于同一行),将1添加到列号


您可以为每一行、每一列和每一对角线计算如何执行此操作。您可以调用
checkLine
8次(3行、3列、2对角线)。

您的对角线检查检查
array[0][0]==array[1][1]
array[1][1]=array[2][2]
。太好了。你需要做什么比较来检查一行呢?一列呢?另一条对角线呢?写出这些比较,你可能会发现一些模式,你可以用它们来确定要编码什么。这是我一直坚持的部分。我知道我可以重复另一条对角线的代码,但我很抱歉试图找到一个更简单的方法。这就是我需要帮助的地方。