Jgrasp Tic Tac Toe 搜索二维数组
Jgrasp Tic Tac Toe Searching A Two Dimensional array
今天我要在 Java 中使用二维数组创建井字棋程序。我已经编写了大部分代码并将 "X" 和 "O" 的值设置在数组中。我似乎无法弄清楚的是如何搜索数组以测试是否有赢家。我目前的方法是:
if(board[0][0] && board[0][1] && board[0][2] == x)
{
//Some player wins
}
当然这并没有给我带来我希望的结果。我希望得到有关如何检查我的数组并调用获胜方法的解释。我恳请不要为我完成它,虽然这绝对太仁慈了,但也不允许我进一步了解我的知识。非常感谢,我希望能尽快收到回复!
节目:
import java.util.*;
import java.io.*;
import static java.lang.System.*;
public class TicTacToe
{
private String[][]board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
public TicTacToe()
{
board = new String[ROWS][COLUMNS];
for(int r=0; r<ROWS; r++)
for(int c = 0; c<COLUMNS; c++)
board[r][c] = " "; //fill array with spaces
}
public void set(int r, int c, String player)
{
if (board[r][c].equals(" "))
board[r][c] = player; //place the "x" or "o"
}
/* toString() creates a String representation of board, for example,
|x o|
| x |
| o|
*/
public String toString()
{
String d = ""; //d is the display
for(int r=0; r<ROWS; r++)
{
d = d + "|";
for(int c = 0; c<COLUMNS; c++)
d = d+board[r][c];
d = d + "|\n";
}
return d;
}
/*PseudoCode for winner:
If three of same type match in diagonal, row, column, then return a winner based on what varibale
EX: [0][0] [0][1] [0][2] all are X, therefore player with X wins
*/
public boolean winner(String player)
{
//Return Winner
}
public class TicTacToeDriver
{
public void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
String player = "x"; //first player
TicTacToe game = new TicTacToe();
boolean done = false;
System.out.println(game);
while (!done)
{
System.out.print("Row for " + player + " (-1 TO EXIT): ");
int row = keyboard.nextInt();
if (row<0) //user wants to end the game
done = true;
else
{
System.out.print("Column for " + player + ": " );
int col = keyboard.nextInt();
game.set(row, col, player);//update board
done = game.winner(player); //check for winner
if(done)
System.out.println("Player " + player + " is the winner");
if(player.equals("x")) //change player
player = "o";
else
player = "x";
}
System.out.println(game); //game over
}
}
}
}
您必须创建一个 table 或某种光栅。
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}
今天我要在 Java 中使用二维数组创建井字棋程序。我已经编写了大部分代码并将 "X" 和 "O" 的值设置在数组中。我似乎无法弄清楚的是如何搜索数组以测试是否有赢家。我目前的方法是:
if(board[0][0] && board[0][1] && board[0][2] == x)
{
//Some player wins
}
当然这并没有给我带来我希望的结果。我希望得到有关如何检查我的数组并调用获胜方法的解释。我恳请不要为我完成它,虽然这绝对太仁慈了,但也不允许我进一步了解我的知识。非常感谢,我希望能尽快收到回复!
节目:
import java.util.*;
import java.io.*;
import static java.lang.System.*;
public class TicTacToe
{
private String[][]board;
private static final int ROWS = 3;
private static final int COLUMNS = 3;
public TicTacToe()
{
board = new String[ROWS][COLUMNS];
for(int r=0; r<ROWS; r++)
for(int c = 0; c<COLUMNS; c++)
board[r][c] = " "; //fill array with spaces
}
public void set(int r, int c, String player)
{
if (board[r][c].equals(" "))
board[r][c] = player; //place the "x" or "o"
}
/* toString() creates a String representation of board, for example,
|x o|
| x |
| o|
*/
public String toString()
{
String d = ""; //d is the display
for(int r=0; r<ROWS; r++)
{
d = d + "|";
for(int c = 0; c<COLUMNS; c++)
d = d+board[r][c];
d = d + "|\n";
}
return d;
}
/*PseudoCode for winner:
If three of same type match in diagonal, row, column, then return a winner based on what varibale
EX: [0][0] [0][1] [0][2] all are X, therefore player with X wins
*/
public boolean winner(String player)
{
//Return Winner
}
public class TicTacToeDriver
{
public void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
String player = "x"; //first player
TicTacToe game = new TicTacToe();
boolean done = false;
System.out.println(game);
while (!done)
{
System.out.print("Row for " + player + " (-1 TO EXIT): ");
int row = keyboard.nextInt();
if (row<0) //user wants to end the game
done = true;
else
{
System.out.print("Column for " + player + ": " );
int col = keyboard.nextInt();
game.set(row, col, player);//update board
done = game.winner(player); //check for winner
if(done)
System.out.println("Player " + player + " is the winner");
if(player.equals("x")) //change player
player = "o";
else
player = "x";
}
System.out.println(game); //game over
}
}
}
}
您必须创建一个 table 或某种光栅。
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html
public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
return (board[currentRow][0] == theSeed // 3-in-the-row
&& board[currentRow][1] == theSeed
&& board[currentRow][2] == theSeed
|| board[0][currentCol] == theSeed // 3-in-the-column
&& board[1][currentCol] == theSeed
&& board[2][currentCol] == theSeed
|| currentRow == currentCol // 3-in-the-diagonal
&& board[0][0] == theSeed
&& board[1][1] == theSeed
&& board[2][2] == theSeed
|| currentRow + currentCol == 2 // 3-in-the-opposite-diagonal
&& board[0][2] == theSeed
&& board[1][1] == theSeed
&& board[2][0] == theSeed);
}