Is there a reason why this error is occurring? I can't identify what's causing it to happen.
Java Code:
package tictac1;
import java.util.*;
public class TicTacToe{
//Instance variables
private char[][] board; //2D array of chars for the board
public TicTacToe(){ //creates a board where each space holds whitespace
board = new char[3][3];
for (int row = 0; row < 3; row ++){
for (int col = 0; col < 3; col++){
board[row][col] = ' ';
} // end of inner loop
} // end of outer loop
}
// Methods
public boolean set(int row, int col, char c){ //sets (row, col) to c
if (row >= 3 || row < 0)
return false;
if (col >= 3 || col < 0)
return false;
if (board[row][col] != ' ')
if ( !(c == 'X' || c == 'O'))
return false;
// assertion: row, col, c are valid
board[row][col] = c;
return true;
}
/*if this returns true, the character c (X or O) is put
* at the board's [row][col] position.
* this happens only if the variables row, col and c are valid, and
* the place where [row][col] refers to is not already
* occupied (already assigned a value),
* other than the default whitespace.
* this returns false if not true. */
public char get(int row, int col){
return board[row][col];
}
/*this is a getter method that
* retrieves the character assigned to (rol,col)
* the variable row is an int from 0 to board.length.
* the variable col is an int from 0 to board[0].length
* this returns the character that is at the referred position*/
public boolean isFull(){
for (int row = 0; row < 3; row ++){
for (int col = 0; col < 3; col ++){
if (board[row][col] != ' '){
return false;
}
}
}
return true;
}
/*this is a method that returns whether or not the board is fully occupied.
*this returns false if any space in the board is whitespace. otherwise it is true.
**/
public boolean isWinner(int row, int col){
String Player = Character.toString(board[row][col]);
boolean onDiagonal = (row ==col)||(col== -1* row+ (board.length-1));
boolean HorizontalWin = true;
boolean VerticalWin = true;
boolean DiagonalWinOne = true;
boolean DiagonalWinTwo = true;
for (int n = 0; n<board.length; n++){ //Checks rows and columns
if(!Character.toString(board[row][n]).equals(Player))
HorizontalWin = false;
if(!Character.toString(board[n][col]).equals(Player))
VerticalWin = false;
}
if(onDiagonal){ //Checks only if move was on a diagonal
//Checks the diagonals
for(int n = 0; n < board.length; n++){
if(!Character.toString(board[n][n]).equals(Player))
DiagonalWinOne = false;
if(!Character.toString(board[n][-1*n+(board.length-1)]).equals(Player))
DiagonalWinTwo = false;
}
}
else{
DiagonalWinOne = false;
DiagonalWinTwo = false;
}
boolean hasWon = (HorizontalWin || VerticalWin || DiagonalWinOne || DiagonalWinTwo);
return hasWon;
}
/*This method takes the row and column coordinates of the last move made and checks to see
* if that move caused the player to win. This method checks the row, column and diagonal
* (if the move was made on a diagonal) that the move was played on to see if
* the winning condition was met there.
*/
public boolean whoIsWinner(int row, int col, boolean hasWon){
boolean xWon = false;
boolean oWon = false;
if (hasWon == true)
if (board[row][col] == 'X')
xWon = true;
if (board[row][col] == 'O')
oWon = true;
if (xWon = true)
return xWon;
if (oWon = true);
return oWon;
}
[B] private char winner(boolean hasWon, boolean xWon, boolean oWon){
if (hasWon != false)
if (xWon == true)
return 'X';
if (oWon == true)
return 'O';
else
return ' ';[/B]
}
public void play(){
Scanner input = new Scanner(System.in);
int row, col;
char currPlayer = 'X';
while(!isFull() && winner() != 'X' && winner() != 'O'){
try{
print();
System.out.println("Player "+currPlayer+"'s Turn");
System.out.print("Enter your move. (e.g. 1,2):");
Scanner tokens = new Scanner(input.nextLine());
tokens.useDelimiter(",");
row = tokens.nextInt();
col = tokens.nextInt();
if (!set(row,col,currPlayer)){
throw new Exception("Error Setting.");
}
}catch(Exception error){
if(error instanceof InputMismatchException){
input.nextLine();
}else{
System.out.println("Bad input");
}
if(currPlayer == 'X'){
currPlayer = 'O';
}
else{
currPlayer = 'X';
}
}
if(currPlayer == 'X'){
currPlayer = 'O';
}
else{
currPlayer = 'X';
}
}
input.close();
print();
if (winner() == 'X'){
System.out.println("Player X wins!!");
}
else if (winner() == 'O'){
System.out.println("Player O wins!!");
}
else{
System.out.println("Everybody wins!! (Sort of)");
}
}
/*this method plays a full game of tic-tac-toe. The method assumes the board is empty.
* (the variables are initialized, but no moves have been made.)
*/
public void print(){ //this method prints the current state of the board
int col;
System.out.print(" col");
System.out.print(" ");
for (col = 0; col < 3; col++){
}
System.out.println();
for (int row = 0; row < 3; row ++){
if(row == 0){
System.out.println(" +-+-+-+");
System.out.println("row "+ "|" +" ");
System.out.print(" +-+-+-+");
}
else {
System.out.println(" "+"|" +" ");
System.out.print(" +-+-+-+");
}
for (col = 0; col < 3; col++){
System.out.print(board[row][col] + " ");
} // end of inner loop
System.out.println();
} // end of outer loop
}
}
No comments:
Post a Comment