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
XXX XOX
OXO XOO
OOX OOX
XXO XOO
OXX XOX
OOX OXX
** – 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.
#include <iostream>
using namespace std;
void initializeBoard(char board[][3]);
void displayBoard(char board[][3]);/*gets input from user returns character number of location chosen by user*/
void getInput(char board[][3], char marker,int& x,int& y);/* mark character marker of player on chosen character pos of board*/
void markBoard(char board[][3],int x,int y, char marker);/*checks to see if someone has won returns true or false*/
bool gameOver(char board[][3]); /*checks to see if someone won on rows of board*/
bool checkHorizontal(char board[][3], char marker);/*checks to see if someone won on columns of board*/
bool checkVertical(char board[][3], char marker);/*checks to see if someone won on diagonal of board*/
bool checkDiagonal(char board[][3], char marker);/*checks to see if players have tied*/
bool checkTie(char board[][3]);/*prints winner as marker or ties*/
void printWinner(char marker);/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/
bool validMove(char board[][3], int x,int y);
int main()
{ srand(time(0));
int start=rand()%2;
char board[3][3];
char exit;
char f,s;
int x,y;
f='X';
s='O';
initializeBoard(board);
displayBoard(board);
while(true)
{getInput(board,f,x,y);
markBoard(board, x,y, f);
displayBoard(board);
if (gameOver(board))
break;
getInput(board,s,x,y);
markBoard(board, x,y, s);
displayBoard(board);
if (gameOver(board))
break;
}
system("pause");
return 0;
}
void initializeBoard(char board[][3])
{
char count = '1';
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++){
board[i][j]= count;
count++;
}
}
}
bool validMove(char board[][3], int x, int y)
{if(x<0||x>2||y<0||y>2)
return false;
if(board[x][y]=='X'||board[x][y]=='O')
return false;
else
return true;
}
void displayBoard(char board[][3])
{ int t;
for(t=0; t<3; t++) {
cout<<" "<<board[t][0]<<" | "<< board[t][1]<<" | "<< board[t][2];
if(t!=2) cout<<"\n---|---|---\n";
}
cout<<"\n";
}
void getInput(char board[][3], char marker,int& x,int& y)
{for(;;)
{cout << "Player "<< marker << " Enter a Row and Column (between 1-3): ";
cin >> x>>y;
x--;
y--;
if (validMove(board,x,y))
return;
cout << "Invalid Move: Please Try Again\n\n";
}
}
void markBoard(char board[][3], int x,int y, char marker)
{ board[x][y]=marker;
}
bool gameOver(char board[][3])
{if (checkHorizontal(board,'X'))
{printWinner('X');
return true;
}
if (checkVertical(board, 'X'))
{printWinner('X');
return true;
}
if (checkDiagonal(board, 'X'))
{ printWinner('X');
return true;
}
if (checkHorizontal(board,'O'))
{printWinner('O');
return true;
}
if (checkVertical(board, 'O'))
{printWinner('O');
return true;
}
if (checkDiagonal(board, 'O'))
{printWinner('O');
return true;
}
if (checkTie(board))
{printWinner('T');
return true;
}
return false;
}
bool checkHorizontal(char board[][3], char marker)
{int i,j,count;
for(i=0; i<3; i++)
{count=0;
for(j=0;j<3;j++)
if(board[i][j]==marker)
count++;
if(count==3)
return true;
}
return false;
}
bool checkVertical(char board[][3], char marker)
{int i,j,count;
for(i=0; i<3; i++)
{count=0;
for(j=0;j<3;j++)
if(board[j][i]==marker)
count++;
if(count==3)
return true;
}
return false;
}
bool checkDiagonal(char board[][3], char marker)
{if(board[0][0]==board[1][1] && board[1][1]==board[2][2]&& board[0][0]==marker)
return true;
if(board[0][2]==board[1][1] && board[1][1]==board[2][0]&& board[0][2]==marker)
return true;
return false;
}
bool checkTie(char board[][3])
{int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(isdigit(board[i][j]))
return false;
return true;
}
void printWinner(char marker)
{ if(marker=='T')
cout<<"TIE GAME!\n";
else
cout<<"The winner is "<<marker<<"!!!\n";
}
================================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
The object ticTacToeBoard is initialized to indicate if the computer is the player 1 or player...
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...
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...
Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction - This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...
Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...
(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...
In C++. Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member called gameState that holds one of the following values: X_WON, O_WON, or UNFINISHED - use an enum type for this, not string (the...
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: 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...
1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...
Please modify the following code to NOT have "TicTacToe extends Board" (I don't want any extending). Please ensure the resulting code completes EACH part of the following prompt: "Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data......