Question

In: Computer Science

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]);

How it should look:

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!

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

Solutions

Expert Solution

C++ Program:

#include <iostream>

using namespace std;

// input prompts the user for a board and inputs it
// into the given array
void input (char board [3][3])
{
    int i, j;
    char ch;

    //Prompting user
    cout << "\n Enter your board as characters (x, o or .): \n";

    //Outer loop for rows
    for(i=0; i<3; i++)
    {
        //Inner loop for columns
        for(j=0; j<3; j++)
        {
            //Reading character
            cin >> ch;

            //Adding character to board
            if(ch == '.')
            {
                board[i][j] = ' ';
            }
            else
            {
                board[i][j] = ch;
            }
        }
    }
}


// print prints the board in nice format
void print (char board [3][3])
{
    int i, j;

    cout << "\n\n The board is: \n";

    cout << "\n |---|---|---| \n";

    //Outer loop for rows
    for(i=0; i<3; i++)
    {
        //Inner loop for columns
        for(j=0; j<3; j++)
        {
            //Printing character
            cout << " | " << board[i][j];
        }

        cout << " |";

        //Printing footer
        cout << "\n |---|---|---| \n";
    }
}

// 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)
{
    return (board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
           (board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
           (board[2][0] == player && board[2][1] == player && board[2][2] == player) ||
           (board[0][0] == player && board[1][0] == player && board[2][0] == player) ||
           (board[0][1] == player && board[1][1] == player && board[2][1] == player) ||
           (board[0][2] == player && board[1][2] == player && board[2][2] == player) ||
           (board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
           (board[0][2] == player && board[1][1] == player && board[2][0] == player);

}


//Main function
int main()
{
    char userChoice;

    //Board
    char board[3][3];

    //Loop till user wants to quit
    do
    {
        //Reading board
        input(board);

        //Printing board
        print(board);

        //Deciding winner
        if(win(board, 'x'))
        {
            cout << "\n X wins! \n";
        }
        else if(win(board, 'o'))
        {
            cout << "\n o wins! \n";
        }
        else
        {
            cout << "\n No Winner! \n";
        }

        //Prompting user
        cout << "\n\n Would you like to do another (y or n)? ";
        cin >> userChoice;

    }while(userChoice == 'y' || userChoice == 'Y');

    cout << "\n Thanks for playing! \n";
    return 0;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking 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 in the grid have been filled with tokens and neither player has...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
I need a good pseudocode for C++ for a tic tac toe program.
I need a good pseudocode for C++ for a tic tac toe program.
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your...
In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 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 program should also announce if one of the players wins or if a draw...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe...
Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 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 program should also announce if one of the players wins...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O) 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 players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT