Question: Write a program with a class to play Tic-Tac-Toe. Here is the structure of the program: Create a char type named constant named NOBODY with
Write a program with a class to play Tic-Tac-Toe. Here is the structure of the program:
Create a char type named constant named NOBODY with a value of '\0'.
Create 9 char fields initialized to '1' through '9'
Create a player field initialized to 'X'
Create a play field initialized to NOBODY
Create a winner field initialized to NOBODY
Create an move field initialized to 1.
public void input() will print out the board like the following, but using the 9 fields in place of the digits.
1|2|3 -+-+- 4|5|6 -+-+- 7|8|9
Then, prompt the user for a number and input that as play
public void makeMove() will check if any of the fields is equal to the input playchar and set that field to the current player char if it does match. If none of the fields match, output "Illegal move" and quit.
public void checkForWin() will see if the fields in any row, column or diagonal are all equal to the current player, and if so, set winner equal to player
public void takeTurn() should call input(), makeMove() and checkForWin(), then, if the winner is still NOBODY, count the move and change player from 'X' to 'O' or from 'O' to 'X'.
public boolean over() will return true if the winner is not NOBODY or if the move is greater than 9.
public char getWinner() will return winner
I'm trying to determine if the checkforwin method i have will be able to work. This is what i have so far
private static final char NOBODY = '0'; private char player = 'X'; private char player2 = 'O'; private char play = NOBODY; private char winner = NOBODY; private char one = '1'; private char two = '2'; private char three = '3'; private char four = '4'; private char five = '5'; private char six = '6'; private char seven = '7'; private char eight = '8'; private char nine = '9'; private int move = 1; public static void main(String[] args) { Scanner sc = new Scanner(System.in); TicTacToe game = new TicTacToe(); while (!game.over()) { game.takeTurn(); } if (game.getWinner()==NOBODY) System.out.println(" \tIt was a cat's game! "); else System.out.println(" \tPlayer " + game.getWinner() + " wins!!! "); } private static Scanner sc = new Scanner(System.in);
public void input(){ System.out.println(one + "|" + two + "|"+ three); System.out.println("-+-+-"); System.out.println(four + "|" + five + "|"+ six); System.out.println("-+-+-"); System.out.println(seven + "|" + eight + "|"+ nine); System.out.println("Enter a number"); play= sc.next().charAt(0); }
public char getWinner() { return winner; }
public void takeTurn() { input(); makeMove(); checkforwin(); return ; }
public boolean over() { return false; } public void checkforwin(){ if (one==player && two==player && three==player || one==player && four==player && seven==player || two==player && five==player && eight==player || three==player && six==player && nine==player || four==player && five==player && six==player || seven==player && eight==player && nine==player || one==player && five==player && nine==player || three==player && five==player && seven==player){ winner=player; } return; } public void makeMove(){ if (play == one){ one = player; } else if(play==two){ two=player; } else if (play==three){ three=player; } else if (play==four){ four=player; } else if (play==five){ five=player; } else if (play==six){ six=player; } else if (play==seven){ seven=player; } else if (play==eight){ eight=player; } else if (play==nine){ nine=player; } else { System.out.println("illegal Move:"); System.exit(0); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
