Question

Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

Can somebody help me with this coding

the program allow 2 players play tic Tac Toe.

however, mine does not take a turn. after player 1 input the tow and column the program eliminated.

I want this program run until find a winner.

also can somebody add function 1 player vs computer mode as well?

Thanks!

>>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>>

#include "myheader.h"

int main()

{

const int NUM_ROWS = 3;

const int NUM_COLS = 3;

// Variables

bool again;

bool won;

char board[NUM_ROWS][NUM_COLS];

char token;

string player1;

string player2;

won = false;

again = true;

InitBoard(board);

OutputInstruct();

GetPlayers(player1, player2);

token = 'X';

do {

while(!won)

{

GetAndCheckInp(board, token, player1, player2);

if (CheckWin(board) != 'N')

{

won = true;

}

DisplayBoard(board);

SwitchToken(token);

}

} while(again);

return 0;

}

///////////////////HEADER //////////////////////

#ifndef MYHEADER_H_

#define MYHEADER_H_

#include<iostream>

#include<iomanip>

#include<cstdlib>

using namespace std;

const int NUM_ROWS = 3;

const int NUM_COLS = 3;

int sum;

void OutputInstruct()

{

cout << "Play the game"<<endl;

}

/******************************************************************************

* InitBoard

* This function initializes each spot in the board to a space ' '.

* RETURNS: Board initialized with all spaces

*****************************************************************************/

void InitBoard(char board[][3]) // tic tac toe board – OUT

{

for(int i = 0; i <= NUM_ROWS; i++)

for(int j = 0; j <= 3; j++)

board[i][j] = ' ';

}

/******************************************************************************

* DisplayBoard

* This function outputs the tic tac toe board including the tokens

* played in the proper format (as described below).

1 2 3

1[1][1]. [1][2]. [1][3]

2[2][1]. [2][2]. [2][3]

3[3][1]. [3][2]. [3][3]

* RETURNS: nothing

* Outputs the current state of the board

*****************************************************************************/

void DisplayBoard(const char board[][3]) // tic tac toe board

{

cout << setw(10) << "1" << setw(8) << "2" << setw(9) << "3\n";

for (int i=0; i < 3; i++)

{

cout << setw(7)<< "[" << i+1 << "][1] | " << "[" << i+1;

cout << "][2] | " << "[" << i+1 << "][3]" << endl;

cout << setw(14)<< "|" << setw(9) << "|" << endl;

for (int j = 0; j < 3; j++)

{

switch(j)

{

case 0: cout << i + 1 << setw(9) << board[i][j];

cout << setw(4) << "|";

break;

case 1: cout << setw(4) << board[i][j];

cout << setw(5) << "|";

break;

case 2: cout << setw(4) << board[i][j] << endl;

break;

default: cout <<"ERROR!\n\n";

}

}

cout << setw(14)<< "|" << setw(10) << "|\n";

if (i != 2)

{

cout << setw(32) << "--------------------------\n";

}

}

cout << endl << endl;

}

/******************************************************************************

* GetPlayers

* This function prompts the user and gets the input for the players’ names.

* player1 contains the name of the player that is using the X token.

* player2 contains the name of the player that is using the O token.

*

* RETURNS: the token opposite of the one in which it receives.

*****************************************************************************/

void GetPlayers(string& player1, string& player2)

{

cout << "Enter the name of player 1(X) : ";

getline (cin, player1);

cout << "Enter the name of player 2(O) : ";

getline (cin, player2);

}

void GetAndCheckInp(char board[][3], char token, string player1, string player2)

{

int row;

int col;

bool valid;

do

{

if (token == 'X')

{

cout << player1 << "'s turn What's your play? : "<<endl;

cout << "Row? : "<<endl;

cin >> row;

cout << "Column? : "<<endl;

cin >> col;

}

else if (token == 'O')

{

cout << player2 << "'s turn: What's your play?"<<endl;

cout << "Row? : "<<endl;

cin >> row;

cout << "Column? : "<<endl;

cin >> col;

}

row--;

col--;

if ((row > NUM_ROWS || row < 0) || (col > 3 || col < 0))

{

valid = false;

cout << "Please chose a space on the board." << endl;

}

else if (board[row][col] != ' ')

{

valid = false;

cout << "That space is already taken, choose another"<<endl;

}

else

{

valid = true;

board[row][col] = token;

sum++;

}

} while (!valid);

}

/******************************************************************************

* SwitchToken

*

* RETURNS: the token opposite of the one in which it receives.

*****************************************************************************/

char SwitchToken(char token) // current player’s token ('X' or 'O') - IN

{

if (toupper(token) == 'X')

{

token = 'O';

}

else if (toupper(token) == 'O')

{

token = 'X';

}

}

/******************************************************************************

* CheckWin

* This function checks to see if either player has won.

* RETURNS the character value of the player that won or T if it was a tie

* or N if no won

*****************************************************************************/

char CheckWin(const char board[][3]) // tic tac toe board - IN

{

int win = 0;

// Checks for three same value horizental

for (int i = 0; i < 3; i++)

if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) win = board[i][0];

// Checks for three same value vertical

for (int i = 0; i < 3; i++)

if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) win = board[0][i];

// Checks for three same value diagnal

if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))

win = board[1][1];

return win;

}

/******************************************************************************

* OutputWinner

*

* RETURNS: nothing

* Displays the winner’s name

*****************************************************************************/

void OutputWinner(char whoWon, string player1, string player2)

{

if (whoWon == 'X')

{

cout << player1 << " has won the game"<<endl;

}

else if (whoWon == 'O')

{

cout << player2 << " has won the game"<<endl;;

}

else if (whoWon == 'T')

{

cout << player1 << " and " << player2 << " have tied"<<endl;;

}

}

#endif

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

After fixing all the bugs and making your code working is:

///////////////////HEADER //////////////////////​​​​​​​

#ifndef MYHEADER_H_
#define MYHEADER_H_
#include<iostream>
#include<iomanip>
#include<cstdlib>

using namespace std;

const int NUM_ROWS = 3;
const int NUM_COLS = 3;

int sum;

void OutputInstruct()
{
   cout << "\nPlay the game\n"<<endl;
}

/******************************************************************************
* InitBoard
* This function initializes each spot in the board to a space ' '.
* RETURNS: Board initialized with all spaces
*****************************************************************************/

void InitBoard(char board[][3]) // tic tac toe board – OUT
{
   for(int i = 0; i <= NUM_ROWS; i++)
       for(int j = 0; j <= 3; j++)
           board[i][j] = ' ';
}

/******************************************************************************
* DisplayBoard
* This function outputs the tic tac toe board including the tokens
* played in the proper format (as described below).

   1        2        3
1[1][1]. [1][2]. [1][3]
2[2][1]. [2][2]. [2][3]
3[3][1]. [3][2]. [3][3]

* RETURNS: nothing
* Outputs the current state of the board
*****************************************************************************/

void DisplayBoard(const char board[][3]) // tic tac toe board
{
   cout << setw(10) << "1" << setw(8) << "2" << setw(9) << "3\n";
   for (int i=0; i < 3; i++)
   {
       cout << setw(7)<< "[" << i+1 << "][1] | " << "[" << i+1;
       cout << "][2] | " << "[" << i+1 << "][3]" << endl;
       cout << setw(14)<< "|" << setw(9) << "|" << endl;

       for (int j = 0; j < 3; j++)
       {
           switch(j)
           {
               case 0: cout << i + 1 << setw(9) << board[i][j];
               cout << setw(4) << "|";
               break;

               case 1: cout << setw(4) << board[i][j];
               cout << setw(5) << "|";
               break;

               case 2: cout << setw(4) << board[i][j] << endl;
               break;

               default: cout <<"ERROR!\n\n";
           }
       }

       cout << setw(14)<< "|" << setw(10) << "|\n";
       if (i != 2)
       {
       cout << setw(32) << "--------------------------\n";
       }
   }
   cout << endl << endl;
}

/******************************************************************************
* GetPlayers
* This function prompts the user and gets the input for the players’ names.
* player1 contains the name of the player that is using the X token.
* player2 contains the name of the player that is using the O token.
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/

void GetPlayers(string& player1, string& player2)
{
   cout << "\nEnter the name of player 1(X) : ";
   getline (cin, player1);
   cout << "Enter the name of player 2(O) : ";
   getline (cin, player2);
}

void GetAndCheckInp(char board[][3], char token, string player1, string player2)
{
   int row;
   int col;
   bool valid;

   do
   {
       if (token == 'X')
       {
           cout << player1 << "'s turn What's your play? : "<<endl;

           cout << "Row? : "<<endl;
           cin >> row;

           cout << "Column? : "<<endl;
           cin >> col;
       }

       else if (token == 'O')
       {
           cout << player2 << "'s turn: What's your play?"<<endl;

           cout << "Row? : "<<endl;
           cin >> row;

           cout << "Column? : "<<endl;
           cin >> col;
       }

       row--;
       col--;

       if ((row > NUM_ROWS || row < 0) || (col > 3 || col < 0))
       {
           valid = false;
           cout << "Please chose a space on the board." << endl;
       }

       else if (board[row][col] != ' ')
       {
           valid = false;
           cout << "That space is already taken, choose another"<<endl;
       }

       else
       {
           valid = true;
           board[row][col] = token;
           sum++;
       }

   } while (!valid);
}

/******************************************************************************
* SwitchToken
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/

char SwitchToken(char token) // current player’s token ('X' or 'O') - IN
{
   if (toupper(token) == 'X')
   {
       token = 'O';
   }

   else if (toupper(token) == 'O')
   {
       token = 'X';
   }

   return token;
}

/******************************************************************************
* CheckWin
* This function checks to see if either player has won.
* RETURNS the character value of the player that won or T if it was a tie
* or N if no won
*****************************************************************************/

char CheckWin(const char board[][3]) // tic tac toe board - IN
{
   char win = ' ';

   // Checks for three same value horizental
   for (int i = 0; i < 3; i++)
       if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return board[i][0];

   // Checks for three same value vertical
   for (int i = 0; i < 3; i++)
       if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return board[0][i];

   // Checks for three same value diagnal
   if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
       return board[1][1];

   // Checks if there is any square left
   for (int i = 0; i < 3; i++)
       for (int j = 0; j < 3; j++)
           if (board[i][j] == ' ')
               return ' ';

   // returns draw
   return 'T';
}

/******************************************************************************
* OutputWinner
*
* RETURNS: nothing
* Displays the winner’s name
*****************************************************************************/

void OutputWinner(char whoWon, string player1, string player2)
{
   if (whoWon == 'X')
   {
       cout << player1 << " has won the game"<<endl;
   }

   else if (whoWon == 'O')
   {
       cout << player2 << " has won the game"<<endl;;
   }

   else if (whoWon == 'T')
   {
       cout << player1 << " and " << player2 << " have tied"<<endl;;
   }
}

#endif

>>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>>

#include "myheader.h"

int main()
{
   const int NUM_ROWS = 3;
   const int NUM_COLS = 3;

   // Variables
   string player1;
   string player2;

   GetPlayers(player1, player2);

   bool again;

   do
   {
       // Variables
       bool won;
       char board[NUM_ROWS][NUM_COLS];
       char token;
       char input;

       won = false;
       again = true;

       InitBoard(board);
       OutputInstruct();

       token = 'O';

       while(!won)
       {
           token = SwitchToken(token);
           GetAndCheckInp(board, token, player1, player2);

           if (CheckWin(board) != ' ')
           {
               won = true;
           }

           DisplayBoard(board);
       }

       OutputWinner(CheckWin(board), player1, player2);

       //asking user to play again
       cout<<"\nPlay again? (Y/N) : ";
       cin>>input;

       again = toupper(input)=='Y';

   } while(again);

   return 0;
}

MYHEADER ProgramScreenshots:

MAIN Program Screenshots:

Output Screenshots:

Add a comment
Know the answer?
Add Answer to:
Can somebody help me with this coding the program allow 2 players play tic Tac Toe....
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
  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a 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 draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    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 in c#

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

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

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

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking 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 draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

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