Question

The object ticTacToeBoard is initialized to indicate if the computer is the player 1 or player...

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

  1. In the TicTacToe class, set the existing Long variable named securityCode with a value as defined below. The TicTacToeBoard instance will do these validations for you. The program must simply load a number that qualifies. You may need to test this several times to create a qualifying security code.
    1. securityCode must be a 16 digit number.
    2. securityCode must start with the digit 2.
    3. securityCode must not have sequential repeating digits.
      1. Such as ‘4’ in 271744.
    4. securityCode must not have sequential incrementing or decrementing digits. The next digit in the code cannot be 1 digit higher or 1 digit lower
      1. Such as ‘45’ in 27145.
      2. Such as ‘54’ in 27154.
    5. securityCode must contain every digit (0-9)
    6. Being that securityCode is a Long variable type, it must be defined with a following L.
      1. Long securityCode = 1234L.
    7. You may optionally define the security code value with underscores for readability.
      1. Long securityCode = 1234_4444_4444_4444L;
    8. Do not prompt for a security code, it must be coded inside the TicTacToe class.

  1. The TicTacToeBoard is instantiated with the Computer as player 2.
    1. This setting also determines who is ‘X’ and who is ‘O’. ‘X’ always goes first.
    2. This task is informational only. The provided program does this.

  1. In the TicTacToe class, call the method showBoard() of the TicTacToeBoard instance. This will have blank values in the ‘squares’ when done here as it is done before entries are loaded.

  1. After step 4, create a loop in the TicTacToe class to play the game. This will be referred to as the ‘main’ loop for this process.
    1. Create an int variable for row position. This declaration should be done only once and before the loop starts.
    2. Create an int variable for column position. This declaration should be done before the loop starts.
    3. Inside the loop, do the following tasks
      1. Prompt to Enter a row number (0-2) to place your TicTacToe value.
      2. Prompt to Enter a column number (0-2) to place your TicTacToe value.
      3. Call the setValue() method of the TicTacToeBoard instance with the input given.
        1. setValue requires 3 parameters, Row, Column, and player.
        2. When you call setValue, specify player as TicTacToeBoard.PLAYER_1
        3. setValue is a Boolean method. It generates a true or false result. If the setValue method returns false, it was unable to process the request. When this occurs, display “Try again” and resume the loop at the beginning.
      4. Call the method theComputersTurn() of the TicTacToeBoard instance. If this method returns a false value, display that “The computer has forfeited” and exit the loop.
      5. Call the method showBoard() of the TicTacToeBoard instance.
      1. Call the method hasWinner() of the TicTacToeBoard instance.
        1. If hasWinner() returns true then
          1. Call the method theWinnerIs() of the TicTacToeBoard instance and display the value returned as in the example below.
          2. i.e. “The winner is X”
          3. Exit the loop.
    1. After the loop is completed – display “Game over”.

  1. The method setValue() can be called with out of range numeric values and create a runtime error. Modify the setValue ‘process’ so that it returns false if data passed is not in the range of possible values. (0 – 2).

  1. The TicTacToeBoard class does not have a way to indicate that all entries are loaded. Create a public method inside the TicTacToe class that will report true if there are no empty board areas or false is empty board area(s) exist.   
    1. Call this new method from the TicTacToe class inside the main loop.
      1. Do this before calling setValue().
      2. Do this before calling theComputersTurn().
      3. Display “No Available spaces”.
      4. Terminate the loop.

  1. The TicTacToeBoard class only currently only checks rows and columns. It does not check diagonal values.

          XXX           XOX

        OXO           XOO

        OOX           OOX

  1. Create a private method inside the TicTacToeBoard class named as checkForDiagonalWin that will check for a possible diagonal win. This method does not need internal loops. Study how the private method checkRowOrColumnForWinner works to understand what to look for. Be careful to check for null values before doing other compares as this can result in a null pointer exception.

         XXO         XOO

        OXX          XOX

        OOX         OXX

  1. Call the method created above from the theWinnerIs method in the TicTacToeBoard class as appropriate.

** – 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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.

Add a comment
Know the answer?
Add Answer to:
The object ticTacToeBoard is initialized to indicate if the computer is the player 1 or player...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Enhance and correct a Tic Tac Toe game. The game is intended to be played against...

    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...

    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...

    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....

    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...

    (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 i...

    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...

    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....

    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...

  • Java Object Array With 2 Elements In 1 Object

    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 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......

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT