问题
I am stuck on my tictactoe problem. Define a calss called TicTacToe. An object of type TicTacToe is a single game of TicTacToe. Store the game board as a single 2d array of base type char that has three rows and three columns. Include methods to add a move, display the board, to tell whose turn it is, to tell whether there is a winnner, to say who the winner is, and to restart the game to the beginning. Write a main method for the class that will allow two players to enter their moves in turn at the same keyboard.
I have some of my methods written and have been testing as I go. When I test my code I either get it to place a mark but also print out invalid entry or it will continuously loop through asking for a move and then saying the space is occupied. I can't figure out how to fix that. I'm sure it has something to do with my do while loop and the boolean methods for isEmpty and notValid. Also I'm stuck on how to implement a counter for each player's win.
Here is my code:
public void addMove()
{
checkTurn();
int row, col;
do
{
System.out.println("Enter a row (1-3): ");
row = in.nextInt() - 1; //Array index starts at 0.
System.out.println("Enter a column (1-3): ");
col = in.nextInt() - 1;
if (row>=0 && row<ROWS)
if(col>=0 && col<COLUMNS)
if (playerX)
{
gameBoard[row][col] = player1Move;
}
else
{
gameBoard[row][col] = player2Move;
}
checkForWin();
changePlayer();
}while (notValid(row,col));
System.out.println("Invaild Entry.");
//System.exit(0);
//checkForWin();
//changePlayer();
}
public boolean notValid(int row, int col)
{
if (row < 0 || row > ROWS )
return true;
if (col < 0 || col > COLUMNS)
return true;
if (!isEmpty(row,col))
return true;
return false;
}
public boolean isEmpty(int row, int col)
{
if(gameBoard[row][col]==' ')
return true;
else
{
System.out.println("Space is already occupied.");
return false;
}
}
}
Here is my testing class:
public class TicTacToe
{
public static void main(String[] args)
{
TicTacToeClass game = new TicTacToeClass();
game.addMove();
game.printBoard();
}
}
回答1:
I will let you handle the multiple game part. This plays one game and exits.
import java.util.Scanner;
public class TicTacToe
{
private final static int ROWS = 3;
private final static int COLUMNS = 3;
private char[][] gameBoard;
private int player1WinCount = 0;
private int player2WinCount = 0;
private char player1Move = 'X', player2Move = 'O';
private boolean playerX = true;
Scanner in = new Scanner(System.in);
public TicTacToe()
{
gameBoard = new char [ROWS][COLUMNS];
playerX = true;
startGame();
}
//Initiate the game board with all empty spaces.
public void startGame()
{
for (int row = 0; row < ROWS; row++) //Loop through rows.
for(int col = 0; col < COLUMNS; col++) //Loop through columns.
gameBoard[row][col]= ' ';
}
public boolean checkTurn()
{
if (playerX)
{
System.out.println("Player X's turn.");
}
else
{
System.out.println("Player O's turn.");
}
return playerX;
}
public void addMove()
{
int row, col;
do
{
checkTurn();
System.out.println("Enter a row (1-3): ");
row = in.nextInt() - 1; //Array index starts at 0.
System.out.println("Enter a column (1-3): ");
col = in.nextInt() - 1;
if(notValid(row,col)){
// do not proceed
System.out.println("Invalid Entry.");
continue;
}
if (row>=0 && row<ROWS)
if(col>=0 && col<COLUMNS)
if (playerX)
{
gameBoard[row][col] = player1Move;
}
else
{
gameBoard[row][col] = player2Move;
}
boolean hasWon = checkForWin();
if(hasWon)
{
System.out.println("You won");
if(playerX)
{
player1WinCount++;
}
else
{
player2WinCount++;
}
break;
}
changePlayer();
}while (true);
}
public boolean notValid(int row, int col)
{
if (row < 0 || row > (ROWS - 1))
return true;
if (col < 0 || col > (COLUMNS - 1))
return true;
if (!isEmpty(row,col))
return true;
return false;
}
public boolean isEmpty(int row, int col)
{
if(gameBoard[row][col]==' ')
return true;
else
{
System.out.println("Space is already occupied.");
return false;
}
}
public void changePlayer()
{
if (playerX)
{
playerX = false;
}
else
{
playerX = true;
}
}
public void printBoard()
{
for (int row = 0; row < ROWS; row++){
for (int col = 0; col < COLUMNS; col++)
{
System.out.print("" + gameBoard[row][col]);
if(col == 0 || col == 1)
System.out.print("|");
}
if (row ==0 || row ==1)
System.out.print("\n-----\n");
}
}
/**
* This method checks to see if a winner.
* return true is there is a winner.
*/
public boolean checkForWin()
{
//checks rows for win
for(int row = 0; row < ROWS; row ++)
{
if (gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1]==gameBoard[row][2] && gameBoard[row][0]!= ' ')
return true;
}
//checks columns for wins.
for (int col = 0; col < COLUMNS; col++)
{
if (gameBoard[0][col] == gameBoard[1][col]&& gameBoard[1][col]==gameBoard[2][col] && gameBoard[0][col]!= ' ')
return true;
}
//check the diagonals for wins.
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2] && gameBoard[0][0]!= ' ')
return true;
if (gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2] && gameBoard[0][2]!= ' ')
return true;
return false;
}
public static void main(String args[])
{
TicTacToe game = new TicTacToe();
game.addMove();
game.printBoard();
}
}
来源:https://stackoverflow.com/questions/36848939/tictactoe-problems