井字游戏数组。获胜者方法且无平方
我不知道我在这里做错了什么。 谁能告诉我 checkWinner
和 isSquareFree
方法中的 checkRow
代码有什么问题?
这是我的代码:
public class TicTacToe
{
/**
* intance variables to hold the data's state for the game.
*/
public static final int GRIDSIZE = 3;
public static final char BLANK = ' ';
public static final char TIE = 'T';
public static final char NOUGHT = 'O';
public static final char CROSS = 'X';
private char[][] grid;
private char WhoseTurn;
/**
* Construct a tic tac toe grid ready to play a new game.
* The game grid should be GRIDSIZE by GRIDSIZE spaces
* all containing the BLANK character.
* Initially, the starting player is not decided,
* indicated by setting whoseturn as BLANK.
*/
public TicTacToe()
{
this.WhoseTurn = BLANK;
this.grid = new char[GRIDSIZE][GRIDSIZE];
for (int r = 0; r < grid.length; r++)
{
for ( int c = 0; c < grid[r].length; c++)
{
this.grid[r][c] = BLANK;
}
}
}
/**
* Reset the tic tac toe game ready to play again.
* Conditions for play are the same as for the constructor.
*/
public void newGame()
{
char[][] boardToClear = getGrid();
final int sizeOfBoard = grid.length;
for ( int row = 0; row < grid.length; row++)
{
for ( int col = 0; col < grid.length; col++)
{
grid[row][col] = BLANK;
}
}
}
public char[][] getGrid()
{
int gridLen = grid.length;
char[][] gridCopy = new char[gridLen][];
for ( int r = 0; r < gridCopy.length; r++)
{
gridCopy[r] = new char[gridCopy.length];
for ( int c = 0; c < gridCopy.length; c++)
{
gridCopy[r][c] = grid[r][c];
}
}
return gridCopy;
}
public char getWhoseTurn()
{
return WhoseTurn;
}
/**
* printGrid() displays the current state of the game grid
* on the console for debugging.
* It uses the form feed character \u000C to clear the console before
* printing the current grid.
*/
private void printGrid()
{
System.out.print('\u000C'); // clear the console window
for (int x = 0; x < GRIDSIZE-1; x++) {
System.out.print(grid[x][0] + "|" +
grid[x][1] + "|" + grid[x][2]);
System.out.println("\n-----"); //
System.out.print(grid[GRIDSIZE-1][0] + "|" +
grid[GRIDSIZE-1][1] + "|" +
grid[GRIDSIZE-1][2]);
}
}
// Now print last row (with no bottom edge)
private boolean checkIfGridFull()
{
char[][] board = getGrid();
int size = grid.length;
for ( int row = 0; row < size; row++)
{
for ( int col = 0; col < board[row].length; col++)
{
if ( grid[row][col] == BLANK)
{
return false;
}
}
}
return true;
}
public boolean move(char player, int row, int col)
{
char[][] boardToPlay = getGrid();
int size = grid.length;
char x = player;
if ( (player == NOUGHT) || ( player == CROSS))
{
if ( (x == WhoseTurn) || (WhoseTurn == BLANK))
{
if ((checkIfGridFull() == false) && ( boardToPlay[row][col] == BLANK))
{
if( (row < size) && ( col < size))
{
boardToPlay[row][col] = player;
if ( player == CROSS)
{
WhoseTurn = NOUGHT;
}
if ( player == NOUGHT)
{
WhoseTurn = CROSS;
}
return true;
}
}
}
}
return false;
}
public boolean isSquareFree( int row, int col)
{
if ((grid[row][col] == BLANK))
{
return true;
}
return false
;
}
public char checkWinner()
{
int countNought;
int countCross ;
int size = grid.length;
for ( int row = 0; row < size; row++)
{
countNought = 0;
countCross = 0;
for ( int col = 0; col < size; col++)
{
if ( grid[row][col] == CROSS)
{
countCross++;
}
if ( grid[row][col] == NOUGHT)
{
countNought++;
}
if ( countNought == size)
{
return NOUGHT;
}
if ( countCross == size)
{
return CROSS;
}
}
}
return BLANK;
}
}
I dont know what I'm doing wrong here.
Can anyone tell me what's wrong with my checkRow
code in checkWinner
and isSquareFree
method?
Here's my code:
public class TicTacToe
{
/**
* intance variables to hold the data's state for the game.
*/
public static final int GRIDSIZE = 3;
public static final char BLANK = ' ';
public static final char TIE = 'T';
public static final char NOUGHT = 'O';
public static final char CROSS = 'X';
private char[][] grid;
private char WhoseTurn;
/**
* Construct a tic tac toe grid ready to play a new game.
* The game grid should be GRIDSIZE by GRIDSIZE spaces
* all containing the BLANK character.
* Initially, the starting player is not decided,
* indicated by setting whoseturn as BLANK.
*/
public TicTacToe()
{
this.WhoseTurn = BLANK;
this.grid = new char[GRIDSIZE][GRIDSIZE];
for (int r = 0; r < grid.length; r++)
{
for ( int c = 0; c < grid[r].length; c++)
{
this.grid[r][c] = BLANK;
}
}
}
/**
* Reset the tic tac toe game ready to play again.
* Conditions for play are the same as for the constructor.
*/
public void newGame()
{
char[][] boardToClear = getGrid();
final int sizeOfBoard = grid.length;
for ( int row = 0; row < grid.length; row++)
{
for ( int col = 0; col < grid.length; col++)
{
grid[row][col] = BLANK;
}
}
}
public char[][] getGrid()
{
int gridLen = grid.length;
char[][] gridCopy = new char[gridLen][];
for ( int r = 0; r < gridCopy.length; r++)
{
gridCopy[r] = new char[gridCopy.length];
for ( int c = 0; c < gridCopy.length; c++)
{
gridCopy[r][c] = grid[r][c];
}
}
return gridCopy;
}
public char getWhoseTurn()
{
return WhoseTurn;
}
/**
* printGrid() displays the current state of the game grid
* on the console for debugging.
* It uses the form feed character \u000C to clear the console before
* printing the current grid.
*/
private void printGrid()
{
System.out.print('\u000C'); // clear the console window
for (int x = 0; x < GRIDSIZE-1; x++) {
System.out.print(grid[x][0] + "|" +
grid[x][1] + "|" + grid[x][2]);
System.out.println("\n-----"); //
System.out.print(grid[GRIDSIZE-1][0] + "|" +
grid[GRIDSIZE-1][1] + "|" +
grid[GRIDSIZE-1][2]);
}
}
// Now print last row (with no bottom edge)
private boolean checkIfGridFull()
{
char[][] board = getGrid();
int size = grid.length;
for ( int row = 0; row < size; row++)
{
for ( int col = 0; col < board[row].length; col++)
{
if ( grid[row][col] == BLANK)
{
return false;
}
}
}
return true;
}
public boolean move(char player, int row, int col)
{
char[][] boardToPlay = getGrid();
int size = grid.length;
char x = player;
if ( (player == NOUGHT) || ( player == CROSS))
{
if ( (x == WhoseTurn) || (WhoseTurn == BLANK))
{
if ((checkIfGridFull() == false) && ( boardToPlay[row][col] == BLANK))
{
if( (row < size) && ( col < size))
{
boardToPlay[row][col] = player;
if ( player == CROSS)
{
WhoseTurn = NOUGHT;
}
if ( player == NOUGHT)
{
WhoseTurn = CROSS;
}
return true;
}
}
}
}
return false;
}
public boolean isSquareFree( int row, int col)
{
if ((grid[row][col] == BLANK))
{
return true;
}
return false
;
}
public char checkWinner()
{
int countNought;
int countCross ;
int size = grid.length;
for ( int row = 0; row < size; row++)
{
countNought = 0;
countCross = 0;
for ( int col = 0; col < size; col++)
{
if ( grid[row][col] == CROSS)
{
countCross++;
}
if ( grid[row][col] == NOUGHT)
{
countNought++;
}
if ( countNought == size)
{
return NOUGHT;
}
if ( countCross == size)
{
return CROSS;
}
}
}
return BLANK;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此类项目中跟踪错误的一种好方法是使用打印行语句。尝试这样做:在您认为问题所在的位置附近放置一条打印行,并打印出一些局部变量的值。如果您看到不应该看到的值,那么您就知道您的数据在代码的早期部分遇到了一些问题。在这种情况下,请将打印行移回流程中并重试。继续这样做,直到您注意到值从好到坏(或从坏到好):此时您已经找到了包含错误的代码。
一旦完成此操作,您通常只需考虑一个小函数,并且更容易理解您做错了什么。如果您仍然找不到问题,请在此处发布代码片段,有人可能会帮助您。
One good method for tracking down a bug in a project like this is to use print line statements. Try this: put a print line somewhere near where you think the problem is, and print out the values of some of the local variables. If you see values that you shouldn't then you know that your data has come into some trouble earlier on in the code. In that case, move the print lines further back in the flow and try again. Keep doing this until you notice values going from good to bad (or bad to good): at that point you have tracked down the code that contains the bug.
Once you've done this, you will usually have only a small function to think about, and it will be much easier to understand what you've done wrong. If you still can't find the problem, then post the code snippet here and someone will probably be able to help you.
checkWinner() 是错误的,你没有检查对角线,只是写了八个获胜组合
checkWinner() is wrong you're not checking diagonals, just write the eights winning combinations