Enhance and correct a Tic Tac Toe game. The game is intended to be played against a computer.
The object TicTacToeBoard is initialized to indicate if the computer is the player 1 or player 2. Your task is to allow turns between the player and computer and to determine if a winner exists.
Keyboard input implies using the Scanner object.
Review Section 7.7 in the textbook.
Ideally, do 1 task at a time and test the results. The tasks are arranged so that the previous work will help with your testing.
The TicTacToeBoard class uses a Character class which is a wrapper class for char. It allows for the value null. Thus, the board values can be ‘X’,’O’ or null.
Deliverables
Submit the modified and corrected TicTacToe class and TicTacToeBoard class.
Be sure to submit both files. If you neglect to include a file, you must email me the missing file before the due date.
The file GuiBoard.java does not need to be included as there are no required changes.
Program Assumptions
The TicTacToeBoard is instantiated with the Computer as player 2. This setting also determines who is ‘X’ and who is ‘O’. ‘X’ always goes first.
In the TicTacToe board will have null values in the ‘squares’ when started.
Program Requirements
XXX XOX
OXO XOO
OOX OOX
It does not check diagonal values.
XXO XOO
OXX XOX
OOX OXX
5 points Extra Credit – Change the valid input range from 0 -2 to 1-3 for the row and column indicators as part of the processing in the TicTacToe class. Do not change the setValue method in the TicTacToeBoard class. All other work must be completed to receive extra credit.
///////////////////////////////////////////////////////// TicTacToeBoard.java
import java.util.Random;
public class TicTacToeBoard {
private Character [][] board;// to hold all character
private Character player1;/// player 1
private Character player2;// player 2
public TicTacToeBoard(){
board=new Character[3][3];
// initialize board
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
this.board[i][j]=null;
}
}
// set computer as 2nd player
this.player1='X';
this.player2='O';
}
// display
void display(){
for(int i=0;i<3;i++){
System.out.print(" ");
for(int j=0;j<3;j++){
if(this.board[i][j]==null){
System.out.print(" ");
}
else{
System.out.print(this.board[i][j]);
}
if(j<2){
System.out.print(" | ");
}
}
if(i<2){
System.out.println("\n--------------");
}
System.out.println();
}
}
// set computer as first player
void setPlayer(){
this.player1='O';
this.player2='X';
}
// set for user move
boolean setValue(int i,int j){
boolean ret=false;
// out of the index list
if((i<0 || i>2) && (j<0 || j>2)){
ret=false;
}
else{
// if space available
if(hasSquaresAvailable(i,j)==true){
this.board[i][j]=this.player1;
ret=true;
}
}
return(ret);
}
// check space is occupied or not
public boolean hasSquaresAvailable(int i,int j){
return(this.board[i][j]==null);
}
// retun winner or not
public boolean hasWinner(){
/* return win result */
return(this.checkForDiagonalWin() ||
this.checkRowOrColumnForWinner());
}
// get winner name
public Character theWinnerIs(){
Character ret=' ';
int i=0;
/* check each row */
/* if each block in a row are same */
for(i=0;i<3;i++){
if(this.board[i][0]!=null &&
this.board[i][0]==this.board[i][1] &&
this.board[i][1]==this.board[i][2]){
ret=this.board[i][0];
break;
}
}
/* check each col */
/* if each block in a col are same */
for(i=0;i<3;i++){
if(ret==' ' && this.board[0][i]!=null &&
this.board[0][i]==this.board[1][i] &&
this.board[1][i]==this.board[2][i]){
ret=this.board[0][i];
break;
}
}
/* if each block in 1st digonal are same */
if(ret==' ' && this.board[0][0]!=null
&& this.board[0][0]==this.board[1][i] &&
this.board[1][1]==this.board[2][2]){
ret=this.board[0][0];
}
/* if each block in 2nd digonal are same */
if(ret==' ' && this.board[2][0]!=null
&& this.board[2][0]==this.board[1][i] &&
this.board[1][1]==this.board[0][2]){
ret=this.board[2][0];
}
/* return win result */
return(ret);
}
// return computer turn
public boolean ComputersTurn(){
boolean ret=false;
// checking any space is available or not
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(this.board[i][j]==null){
ret=true;
break;
}
}
}
if(ret==false){
return(false);
}
// random turn
Random r=new Random();
int row=r.nextInt(3);
int col=r.nextInt(3);
// if move not available
while(this.hasSquaresAvailable(row,col)==false){
row=r.nextInt(3);
col=r.nextInt(3);
}
this.board[row][col]=this.player2;
return(true);
}
// if first player is user then return false;
// otherwise true
boolean checkFirstPlay(){
if(this.player1=='X'){
return(false);
}
else{
return(true);
}
}
// each digonal check
private boolean checkForDiagonalWin(){
boolean ret=false;
/* if each block in 1st digonal are same */
if(this.board[0][0]!=null &&
this.board[0][0]==this.board[1][1] &&
this.board[1][1]==this.board[2][2]){
ret=true;
}
/* if each block in 2nd digonal are same */
if(ret==false && this.board[2][0]!=null
&& this.board[2][0]==this.board[1][1] &&
this.board[1][1]==this.board[0][2]){
ret=true;
}
return(ret);
}
// each row and column check for winner
private boolean checkRowOrColumnForWinner(){
boolean ret=false;
int i=0;
/* check each row */
/* if each block in a row are same */
for(i=0;i<3;i++){
if(this.board[i][0]!=null &&
this.board[i][0]==this.board[i][1] &&
this.board[i][1]==this.board[i][2]){
ret=true;
break;
}
}
/* check each col */
/* if each block in a col are same */
for(i=0;i<3;i++){
if(ret==false && this.board[0][i]!=null &&
this.board[0][i]==this.board[1][i] &&
this.board[1][i]==this.board[2][i]){
ret=true;
break;
}
}
return(ret);
}
}
////////////////////// TicTacToe.java
import java.util.Scanner;
public class TicTacToe {
private TicTacToeBoard board;
private long securityCode;
public TicTacToe(){
// sequrity code
this.securityCode=2910_3970_1482_7531L;
board=new TicTacToeBoard();
this.game();
}
void game(){
Scanner sc=new Scanner(System.in);
// user choice for compute first or not
System.out.print("Choose Player1 [C for computer /U for user] :
");
String inp=sc.next();
// user set for player
if(inp.charAt(0)=='C' || inp.charAt(0)=='c'){
this.board.setPlayer();
}
int row,col;
// check for computer is first player or not
if(this.board.checkFirstPlay()==true){
this.board.ComputersTurn();
}
while(true){
// display board
this.board.display();
// user move
System.out.print("\nYour Turn\nEnter Row Number [0-2] : ");
row=sc.nextInt();
System.out.print("Enter column Number [0-2] : ");
col=sc.nextInt();
// check user move is correct or not
// if ok , go to next step
if((row>=0 && row<=2) && (col>=0
&& col<=2)){
// set user move
if(this.board.setValue(row, col)==true){
// check winner status for user
if(this.board.hasWinner()==true){
this.board.display();
System.out.println("\nThe Winner is
"+this.board.theWinnerIs());
break;
}
else{
// if not win computer move
// if not move possible . break loop
if(this.board.ComputersTurn()==false){
this.board.display();
// if no computer move available break loop , and it is draw
System.out.println("\nNo Available spaces”");
System.out.println("The computer has forfeited");
break;
}
// move possible
else{
// check computer won the game or not
if(this.board.hasWinner()==true){
this.board.display();
System.out.println("\nThe Winner is
"+this.board.theWinnerIs());
break;
}
}
}
}
// wrong bad user move
else{
System.out.println("Space is already occupied .Try Again.");
}
}
else{
System.out.println("Wrong Input , please choose correct
inputs.");
}
}
System.out.println("Game Over.");
sc.close();
}
}
//////////////////////////////////// GuiBoard.java
public class GuiBoard {
public static void main(String[] args) {
TicTacToe t=new TicTacToe();
}
}
Enhance and correct a Tic Tac Toe game. The game is intended to be played against...
The object ticTacToeBoard is initialized to indicate if the computer is the player 1 or player 2. Your task is to allow turns between the player and computer and to determine if a winner exists. The TicTacToeBoard class uses a Character class which is a wrapper class for char. It allows for the value null. Thus, the board values can be ‘X’,’O’ or null. Program Requirements In the TicTacToe class, set the existing Long variable named securityCode with a value...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...
(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...
I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....