Question

Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won.

The board will be read in as 3x3 array of characters (x, o or . for a blank spot).

You are required to write the following functions to help solve the problem:

// input prompts the user for a board and inputs it

// into the given array

void input (char board [3][3]);

// print prints the board in nice format

void print (char board [3][3]);

// win returns true if the given player has won on the

// given board, else it returns false

bool win (char board [3][3], char player);

You are required to use nested loops to write the functions input and print.

The back of this page shows a sample run of the program (input in bold). Your output should look at least as nice as that shown. You may assume that the board will be legal and there will be at most one winner.

Enter your board as characters (x, o or .):

o.x

..o

.x.

The board is:

o |   | x

-----------

   |   | o

-----------

   | x |

No winner!

Would you like to do another (y or n)? y

Enter your board as characters (x, o or .):

oxo

ox.

.xo

The board is:

o | x | o

-----------

o | x |

-----------

   | x | o

X wins!

Would you like to do another (y or n)? y

Enter your board as characters (x, o or .):

xoo

xox

ox.

The board is:

x | o | o

-----------

x | o | x

-----------

o | x |

O wins!

Would you like to do another (y or n)? n

Thanks for playing!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
#include<cstdlib>
using namespace std;
 
class TicTacToe{
      public:
             void print();
             void play();
             char determine();
             TicTacToe();
 
      private:
              int p1x,p1y;
              int p2x,p2y;
              char TicTacToeBoard[3][3];
      };
 
 
 
      TicTacToe::TicTacToe(){
               char TicTacToeBoard[3][3] = {{'!','!','!'},{'!','!','!'},{'!','!','!'} }; // fill board
               TicTacToeBoard[p1x][p1y] = 'X'; // put x where p1 has coordinates
               TicTacToeBoard[p2x][p2y] = 'O'; // put y where p1 has coordinates
                             }
 
int main(){
 
          TicTacToe playgame,winner; //create objects of type TicTacToe
          playgame.play(); // play game
          winner.determine(); // determine winner
 
          system("pause");
          return 0;
          }    
 
 
void TicTacToe::print(){ // for printing board
 
     int row;
     int col;
     cout << "----------------------" << endl;
     for(row = 0;row < 3; row++){
             for(col = 0;col < 3; col++){
                     cout << TicTacToeBoard[row][col];
                     }
                     cout << endl;
                     }
     cout << "----------------------" << endl;                
}
 
void TicTacToe::play(){ // play game
 
     TicTacToe showboard;
     int turn = 1;
     while(turn <= 9){ 
 
     // let player 1 enter coordinates
     cout << "Player 1" << endl;
     cout << "X:";
     cin >> p1x;
 
     cout << "Y:";
     cin >> p1y;
     showboard.print(); // print board
     cout << endl;
 
     turn++;
     // let player 2 enter coordinates
     cout << "Player 2" << endl;
     cout << "X:";
     cin >> p2x;
 
     cout << "Y:";
     cin >> p2y;
     showboard.print(); // print board
     cout << endl;
 
     turn++;
 
 
 
     }
 
     }
 
char TicTacToe::determine(){ // determine the winner might be incorrect
 
    if( TicTacToeBoard[0][0]==TicTacToeBoard[0][1] && TicTacToeBoard[0][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[0][0];
        }
        else if( TicTacToeBoard[1][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[1][0];
        }
        else if( TicTacToeBoard[2][0]==TicTacToeBoard[2][1] && TicTacToeBoard[2][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[2][0];
        }
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][0] && TicTacToeBoard[1][0] == TicTacToeBoard[2][0] ){
        return TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][1]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][1] ){
        return TicTacToeBoard[0][1];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][2] && TicTacToeBoard[1][2] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][2];
        } 
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][2];
        }
    else return ' ';    
 
}
Add a comment
Know the answer?
Add Answer to:
Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...
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
  • 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...

  • Objective: To write a program to allow a game of Tic Tac Toe to be played,...

    Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...

  • Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...

    Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...

  • Please write a Python program to check a tic-tac-toe game and show its winning result in...

    Please write a Python program to check a tic-tac-toe game and show its winning result in detail. This is an application program of a 3-dimensional list or array. Your complete test run output must look as follows. This test really checks to make sure your program is performing perfectly to check every possible winning situation for X and O. GAME 0 is as follows: OOO OOO OOO O won by row 1 O won by row 2 O won by...

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

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

  • 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 the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

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

  • 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