- #1
iamjon.smith
- 117
- 3
Assignment:
TicTacToe and TicTacToeGame.
The TicTacToe class contains a 3x3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. It should have a method playerMove that accepts two integers as parameters, the first is the player (1 or 2) and the second is the actual move (1-9). If it is a valid move it changes the value of that square to 1 or 2 (depending on whose turn it is and returns the Boolean value true to indicate a valid move has been completed). If it is not a valid move the board does not change and it returns a Boolean value of false to indicate an invalid move. TicTacToe should also have the method displayBoard that will display the current state of the board. The last method should be determineWinner which returns a 1 if player one won, a 2 if player 2 won, a 0 if it’s a ‘cat’s game’, and -1 if there is no winner yet and the game is not over.
Here is my TicTacToeClass
Here is the main method:
problems I am having:
1. The program accepts user input, determines position, and displays move, but it doesn't switch players. It is always player 1's turn.
2. Instead of displaying a 1 or 2 for the player, I need it to display an X for player one and a O for player 2.
What am I overlooking?
TicTacToe and TicTacToeGame.
The TicTacToe class contains a 3x3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. It should have a method playerMove that accepts two integers as parameters, the first is the player (1 or 2) and the second is the actual move (1-9). If it is a valid move it changes the value of that square to 1 or 2 (depending on whose turn it is and returns the Boolean value true to indicate a valid move has been completed). If it is not a valid move the board does not change and it returns a Boolean value of false to indicate an invalid move. TicTacToe should also have the method displayBoard that will display the current state of the board. The last method should be determineWinner which returns a 1 if player one won, a 2 if player 2 won, a 0 if it’s a ‘cat’s game’, and -1 if there is no winner yet and the game is not over.
Here is my TicTacToeClass
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoetest;
/**
*
* @author Jon and Jessica
*/
public class TicTacToe {
private int player = 1;
private int gameBoard[][] = new int[3][3];
public TicTacToe(){
// Initializes all elements to the array of 0; nested for loop
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
gameBoard[i][j] = 0;
}
}
}
public TicTacToe( int [][] gameBoard){
setGameBoard(gameBoard);
}
/**
* @return the player
*/
public int getPlayer() {
return player;
}
/**
* @param player the player to set
*/
public void setPlayer(int player) {
this.player = player;
}
/**
* @return the gameBoard
*/
public int[][] getGameBoard() {
return gameBoard;
}
/**
* @param gameBoard the gameBoard to set
*/
public void setGameBoard(int[][] gameBoard) {
this.gameBoard = gameBoard;
}
public String gamePiece (int player){
String result = "";
if (player == 1){
result = "X";
}else if (player == 2){
result = "O";
}
return result;
}
// Accepts row and column from player. If move made is invalid, it will still be current player's turn.
public boolean playerMove(int row, int col) {
// Initialize boolean move
boolean move;
// Checks to see if board location is occupied and a move can be made
if (gameBoard[row -1][col -1] == 0)
{
gameBoard[row-1][col-1] = player;
return true;
}
else
{
// The specified cell is already occupied.
move = false;
}
return move;
}
public boolean isGameOver(){
boolean gameOver = true;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (gameBoard[i][j] == 0){
gameOver = false;
}
}
}
return gameOver;
}
// Method to check rows, columns, and diaganals for winner
public int gameWinner(){
int winner = 0;
// check row 1 for winner
if (gameBoard[0][0] == gameBoard[0][1] && gameBoard[0][0] == gameBoard[0][2] && gameBoard[0][0] != 0 ){
winner = gameBoard[0][0];
}
// check row 2 for winner
if (gameBoard[1][0] == gameBoard[1][1] && gameBoard[1][0] == gameBoard[1][2] && gameBoard[1][0] != 0 ){
winner = gameBoard[1][0];
}
// check row 3 for winner
if (gameBoard[2][0] == gameBoard[2][1] && gameBoard[2][0] == gameBoard[2][2] && gameBoard[2][0] != 0 ){
winner = gameBoard[2][0];
}
// check column 1 for winner
if (gameBoard[0][0] == gameBoard[1][0] && gameBoard[0][0] == gameBoard[2][0] && gameBoard[0][0] != 0 ){
winner = gameBoard[0][0];
}
// check column 2 for winner
if (gameBoard[0][1] == gameBoard[1][1] && gameBoard[0][1] == gameBoard[2][1] && gameBoard[0][1] != 0 ){
winner = gameBoard[0][1];
}
// check column 3 for winner
if (gameBoard[0][2] == gameBoard[1][2] && gameBoard[0][2] == gameBoard[2][2] && gameBoard[0][2] != 0 ){
winner = gameBoard[0][2];
}
// check first diagonal row for winner;
if (gameBoard[0][0] == gameBoard[1][1] && gameBoard[0][0] == gameBoard[2][2] && gameBoard[0][0] != 0 ){
winner = gameBoard[0][0];
}
// check second diagonal for winner
if (gameBoard[0][2] == gameBoard[1][1] && gameBoard[0][2] == gameBoard[2][0] && gameBoard[0][2] != 0 ){
winner = gameBoard[0][2];
}
return winner;
}
// Display board after each input
public void showBoard(){
System.out.println("Tic Tac Toe Game\n" + "-----------");
for(int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
System.out.print(gameBoard[i][j] + " | ");
}
System.out.println("\n-----------");
}
}
}
Here is the main method:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoetest;
import java.util.Scanner;
/**
*
* @author Jon and Jessica
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Variable declaration
int row;
int column;
TicTacToe ttt = new TicTacToe();
Scanner input = new Scanner(System.in); // accept user input
System.out.println("Welcome to Tic Tac Toe!\n Player 1 = X\n Player 2 = O");
while(ttt.gameWinner() == 0 && !ttt.isGameOver()){
ttt.showBoard();
// Get next player move
System.out.println("Player " + ttt.getPlayer());
System.out.println("Place your piece!");
System.out.println("Please enter the row (1-3)");
row = input.nextInt();
// Trap for invalid row number
while(row < 1 || row > 3){
System.out.println("That is not a valid row!");
System.out.println("Please re-enter row 1 - 3");
row = input.nextInt();
}
System.out.println("Please Enter Column (1-3)");
column = input.nextInt();
// Trap for invalid column
while(column < 1 || column > 3){
System.out.println("That is not a valid column!");
System.out.println("Please re-enter column 1 - 3");
column = input.nextInt();
}
while(!ttt.playerMove(row,column)){
System.out.println("That is not a valid move... please try again!");
System.out.println("Please enter the row 1-3");
row = input.nextInt();
// Trap for invalid entry
while(row < 1 || row > 3){
System.out.println("That is not a valid row, try again.");
System.out.println("Please enter row 1-3");
row = input.nextInt();
}
System.out.println("Please enter column 1-3");
column = input.nextInt();
// Trap for invalid entry
while (column < 1 || column > 3){
System.out.println("That is not a valid column, try again.");
System.out.println("Please enter column 1-3");
column = input.nextInt();
}
}
}
// If the loop was exited because there are no moves left, it's a tie, or "Cat's Game"
if(ttt.gameWinner() == 0){
System.out.println("Sorry...Cat's Game!");
}
else{
// Since player auto advances, once game is over, winner is previous player
System.out.println("The winner is Player ");
if(ttt.getPlayer() == 1){
System.out.println("2");
}
else{
System.out.println("1");
}
}
}
}
problems I am having:
1. The program accepts user input, determines position, and displays move, but it doesn't switch players. It is always player 1's turn.
2. Instead of displaying a 1 or 2 for the player, I need it to display an X for player one and a O for player 2.
What am I overlooking?