Question

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 th

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

/*in c++*/

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
   char Board[10] = {'o','1','2','3','4','5','6','7','8','9'};
   int playerTurn= 1;
   int choice;
   int win = -1;
   do
   {
  
  
   cout<<"\t Welcome to Tic Toc Toe Game"<<endl<<endl;
   cout<<"player 1 (X) || player 2 (0)"<<endl;
  
       cout << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[1] << " | " << Board[2] << " | " << Board[3] << endl;
   cout << "_____|_____|_____" << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[4] << " | " << Board[5] << " | " << Board[6] << endl;
   cout << "_____|_____|_____" << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[7] << " | " << Board[8] << " | " << Board[9] << endl;
   cout << "     |     |     " << endl << endl;
  
   cout<<"(player "<<playerTurn<<" Turn) Enter a number: ";
   cin>>choice;
  
   if(playerTurn== 1)
   {
       switch(choice)
       {
           case 1:
           if(Board[1]=='X'||Board[1]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[1] = 'X';
              playerTurn= 2;  
           }
           break;
          
           case 2:
           if(Board[2]=='X'||Board[2]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[2] = 'X';
              playerTurn= 2;  
           }
           break;
          
           case 3:
           if(Board[3]=='X'||Board[3]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[3] = 'X';
              playerTurn= 2;  
           }
           break;
           case 4:
           if(Board[4]=='X'||Board[4]=='O')
           {
               cout<<" Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[4] = 'X';
              playerTurn= 2;  
           }
           break;
           case 5:
           if(Board[5]=='X'||Board[5]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[5] = 'X';  
              playerTurn= 2;
           }
           break;
           case 6:
           if(Board[6]=='X'||Board[6]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[6] = 'X';
              playerTurn= 2;  
           }
           break;
           case 7:
           if(Board[7]=='X'||Board[7]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[7] = 'X';
              playerTurn= 2;  
           }
           break;
           case 8:
           if(Board[8]=='X'||Board[8]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[8] = 'X';  
              playerTurn= 2;
           }
           break;
           case 9:
           if(Board[9]=='X'||Board[9]=='O')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[9] = 'X';
              playerTurn= 2;  
           }
           break;
          
           default:cout<<"Error Invalid Move"<<endl;
           system("pause");
           break;
       }
   }
   else
   {
       switch(choice)
       {
           case 1:
           if(Board[1]=='O'||Board[1]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[1] = 'O';
              playerTurn= 1;  
           }
           break;
          
           case 2:
           if(Board[2]=='O'||Board[2]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[2] = 'O';  
              playerTurn= 1;  
           }
           break;
          
           case 3:
           if(Board[3]=='O'||Board[3]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[3] = 'O';  
              playerTurn= 1;  
           }
           break;
           case 4:
           if(Board[4]=='O'||Board[4]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[4] = 'O';  
              playerTurn= 1;  
           }
           break;
           case 5:
           if(Board[5]=='O'||Board[5]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[5] = 'O';
              playerTurn= 1;      
           }
           break;
           case 6:
           if(Board[6]=='O'||Board[6]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[6] = 'O';
              playerTurn= 1;      
           }
           break;
           case 7:
           if(Board[7]=='O'||Board[7]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[7] = 'O';  
              playerTurn= 1;  
           }
           break;
           case 8:
           if(Board[8]=='O'||Board[8]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[8] = 'O';  
              playerTurn= 1;  
           }
           break;
           case 9:
           if(Board[9]=='O'||Board[9]=='X')
           {
               cout<<"Error Invalid Move"<<endl;
               system("pause");
           }
           else
           {
              Board[9] = 'O';  
              playerTurn= 1;  
           }
           break;
          
           default:cout<<"Error Invalid Move"<<endl;
           system("pause");
           break;
       }
   }
  
  
  
  
  
   if (Board[1] == Board[2] && Board[2] == Board[3])
       win = 1;
   else if (Board[4] == Board[5] && Board[5] == Board[6])
       win = 1;
   else if (Board[7] == Board[8] && Board[8] == Board[9])
       win = 1;
   else if (Board[1] == Board[4] && Board[4] == Board[7])
       win = 1;
   else if (Board[2] == Board[5] && Board[5] == Board[8])
       win = 1;
   else if (Board[3] == Board[6] && Board[6] == Board[9])
       win = 1;
   else if (Board[1] == Board[5] && Board[5] == Board[9])
       win = 1;
   else if (Board[3] == Board[5] && Board[5] == Board[7])
       win = 1;
   else if (Board[1] != '1' && Board[2] != '2' && Board[3] != '3' && Board[4] != '4' &&

    Board[5] != '5' && Board[6] != '6' && Board[7] != '7' && Board[8] != '8' && Board[9] != '9')
       win = 0;
   else
       win = -1;
  
  
   system("cls");
   }
   while(win == -1);
  
       cout<<"\t Welcome to Tic Toc Toe Game"<<endl<<endl;
   cout<<"player 1 Mark (X) || player 2 Mark (0)"<<endl;
  
           cout << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[1] << " | " << Board[2] << " | " << Board[3] << endl;
   cout << "_____|_____|_____" << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[4] << " | " << Board[5] << " | " << Board[6] << endl;
   cout << "_____|_____|_____" << endl;
   cout << "     |     |     " << endl;
   cout << " " << Board[7] << " | " << Board[8] << " | " << Board[9] << endl;
   cout << "     |     |     " << endl << endl;
  
   if(win==1)
   {
       if(playerTurn== 1)
       {
           playerTurn= 2;
       }
       else if(playerTurn== 2)
       {
           playerTurn= 1;
       }
       cout<<"\a player"<<playerTurn<<" win "<<endl;  
   }
   else
       cout<<"\aGame Tie"<<endl;
  
   return 0;
}

/*

output

   Welcome to Tic Toc Toe Game

player 1 (X) || player 2 (0)

     |     |   
1 | 2 | 3
_____|_____|_____
     |     |   
4 | 5 | 6
_____|_____|_____
     |     |   
7 | 8 | 9
     |     |   

(player 1 Turn) Enter a number: 1
sh: 1: cls: not found
   Welcome to Tic Toc Toe Game

player 1 (X) || player 2 (0)

     |     |   
X | 2 | 3
_____|_____|_____
     |     |   
4 | 5 | 6
_____|_____|_____
     |     |   
7 | 8 | 9
     |     |   

(player 2 Turn) Enter a number: 2
sh: 1: cls: not found
   Welcome to Tic Toc Toe Game

player 1 (X) || player 2 (0)

     |     |   
X | O | 3
_____|_____|_____
     |     |   
4 | 5 | 6
_____|_____|_____
     |     |   
7 | 8 | 9
     |     |   

(player 1 Turn) Enter a number: 5
sh: 1: cls: not found
   Welcome to Tic Toc Toe Game

player 1 (X) || player 2 (0)

     |     |   
X | O | 3
_____|_____|_____
     |     |   
4 | X | 6
_____|_____|_____
     |     |   
7 | 8 | 9
     |     |   

(player 2 Turn) Enter a number: 3
sh: 1: cls: not found
   Welcome to Tic Toc Toe Game

player 1 (X) || player 2 (0)

     |     |   
X | O | O
_____|_____|_____
     |     |   
4 | X | 6
_____|_____|_____
     |     |   
7 | 8 | 9
     |     |   

(player 1 Turn) Enter a number: 9
sh: 1: cls: not found
   Welcome to Tic Toc Toe Game

player 1 Mark (X) || player 2 Mark (0)

     |     |   
X | O | O
_____|_____|_____
     |     |   
4 | X | 6
_____|_____|_____
     |     |   
7 | 8 | X
     |     |   

player1 win

*/

Add a comment
Know the answer?
Add Answer to:
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...
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
  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. 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 Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String 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: a. Displays the contents of the board array. b. Allows player 1 to select a location...

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

  • Use Java language to create this program Write a program that allows two players to play...

    Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...

  • Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I...

    Please help me write a Pseudocode (please do NOT answer in JAVA or Python - I will not be able to use those). Please ALSO create a flowchart using Flowgarithm program. Thank you so much, if answer is provided in Pseudocode and Flowchart, I will provide good feedback and thumbs up to the resonder. Thank you! 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...

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

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

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

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

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