我试图让我的代码检查我的Tic Tac Toe游戏的胜利和关系。到目前为止,胜利检查仅适用于对角线,但它仍然有问题。领带检查根本不起作用。我需要关于我的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;
}
}
答案 0 :(得分:0)
这看起来像是一个练习,所以我只是给出一个答案的大纲。但是如果你想简化检查逻辑,我会写一个类似于
的方法static boolean checkLine(int[][] array, int startRow, int startCol, int rowIncrement, int colIncrement) {
...
}
如果数组的一条直线上的所有内容相等(而不是空方格),则返回true
,否则返回false
。对于您已编码的对角线,您需要调用
win = checkLine(array, 0, 0, 1, 1);
因为您将开始查看array[0][0]
,并且您检查的每个方格将位于比前一行更多的第1行,而第1列比前一列更多(因此{{1} },array[1][1]
)。
要查看底行,您要查看array[2][2]
,array[2][0]
,array[2][1]
的位置,您可以这样称呼它:
array[2][2]
表示从第2行和第0列开始,然后检查其他方块,在行号中加0(因为你停留在同一行),列号为1。
您可以了解如何为每行,每列和对角线执行此操作。你打电话win = checkLine(array, 2, 0, 0, 1);
8次(3行,3列,2个对角线)。