Question

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 marks in a row, in a column, or on a diagonal. A draw occurs when the board is full and no one has won.

Each player's position should be input as indexes into the tic-tac-toe board – that is, a row number, a space, and a column number. Make the program user-friendly.

After each game, print out a diagram of the board showing the ending positions. Keep a count of the number of games each player has won and the number of draws. Before the beginning of each game, ask each player if he or she wishes to continue. If either player wishes to quit, print out the statistics and stop.

Draw a flow-chart or give a set of steps in pseudo-code for the design of your solution before implementing it and include your design. Then implement your program. Call you program tictactoe.cpp.

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

#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
int player1 = 0, player2 = 0;
char playersMark[2] = {'O','X'};
char board[3][3];

void showBoard(){ /* to prety print the board */
cout << " | | " << endl;
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;

cout << " | | " << endl << endl;
}
bool checkinput(int row,int col){ /*check if the position is empty or not*/
return (board[row-1][col-1] == '-');
}

bool checkwin(){/*check for winner*/
if(board[0][0] != '-' && board[0][1] != '-' && board[0][2] != '-' &&board[0][0] == board[0][1] && board[0][1] == board[0][2]){
return true;
}
else if(board[1][0] != '-' && board[1][1] != '-' && board[1][2] != '-' && board[1][0] == board[1][1] && board[1][1] == board[1][2]){
return true;
}
else if(board[2][0] != '-' && board[2][1] != '-' && board[2][2] != '-' && board[2][0] == board[2][1] && board[2][1] == board[2][2]){
return true;
}
else if(board[0][0] != '-' && board[1][0] != '-' && board[2][0] != '-' && board[0][0] == board[1][0] && board[1][0] == board[2][0]){
return true;
}
else if(board[0][1] != '-' && board[1][1] != '-' && board[2][1] != '-' && board[0][1] == board[1][1] && board[1][1] == board[2][1]){
return true;
}
else if(board[0][2] != '-' && board[1][2] != '-' && board[2][2] != '-' && board[0][2] == board[1][2] && board[1][2] == board[2][2]){
return true;
}
else if(board[0][0] != '-' && board[1][1] != '-' && board[2][2] != '-' && board[0][0] == board[1][1] && board[1][1] == board[2][2]){
return true;
}
else if(board[0][2] != '-' && board[1][1] != '-' && board[2][0] != '-' && board[0][2] == board[1][1] && board[1][1] == board[2][0]){
return true;
}
else
{
return false;
}
}
void newGame(){/*to start a new game*/
int row=0, col=0;
int turn = 0;
int choice;
int numberOfTurns = 0;
cout<<"\t\t\t********NEW GAME STARTS********\n"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j] = '-';
}
}
showBoard();
while(true){
numberOfTurns += 1 ;
cout<< "Player" << (turn+1) <<"'s turn it is... \n";
turn = !turn;
do{
cout<<"Input Row : ";
cin>>row;
cout<<endl;
cout<<"Input Column : ";
cin>>col;
}while(checkinput(row,col)==false);
board[row-1][col-1] = playersMark[!turn];
showBoard();
if(numberOfTurns>=3 && checkwin()){
if(turn==0){
cout<<"Player2 wins"<<endl;
player2 += 1;
}
else{
cout<<"Player1 wins"<<endl;
player1 += 1;
}
cout<<"If player1 wants to continue Press 1..."<<endl;
cin>>choice;

if(choice == 1){
cout<<"If player2 wants to continue Press 2..."<<endl;
cin>>choice;
if (choice == 2)
{
system("clear");
newGame();
}
else
break;
}
else{
return;
}
}
else if(numberOfTurns == 9){
cout<<"Game is Drawn "<<endl;
cout<<"If player1 wants to continue Press 1..."<<endl;
cin>>choice;

if(choice == 1){
cout<<"If player2 wants to continue Press 2..."<<endl;
cin>>choice;
if (choice == 2)
{
system("clear");
newGame();
}
else
{
break;
}
}
else{
return;
}
}
else{
continue;
}
}
}
int main() {
cout<<"*********************Welcome to Tic Tac Toe*********************"<<endl;
cout<<"\t\t\t\t\t PLAYER1 : O\n";
cout<<"\t\t\t\t\t PLAYER2 : X\n\n";
newGame();/* start game */
/* row and column start from 1 and end at 3 for example if user want to enter in first block he need toi enter 1 1 in row and column respectively*/
system("clear");
cout<<"\t\t\t\t\t **************GAME OVER**************\n\n"; /*game over show the result*/
cout<<"\t\t\t\t\t\t\t Players 1:" <<player1<<endl;
cout<<"\t\t\t\t\t\t\t Players 2:" <<player2<<endl;
cout<<"\n\t\t\t\t\t ***********THANKS ! VISIT AGAIN *********";
}

#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
int player1 = 0, player2 = 0;
char playersMark[2] = {'O','X'};
char board[3][3];

void showBoard(){ /* to prety print the board */
cout << " | | " << endl;
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;

cout << " | | " << endl << endl;
}
bool checkinput(int row,int col){ /*check if the position is empty or not*/
return (board[row-1][col-1] == '-');
}

bool checkwin(){/*check for winner*/
if(board[0][0] != '-' && board[0][1] != '-' && board[0][2] != '-' &&board[0][0] == board[0][1] && board[0][1] == board[0][2]){
return true;
}
else if(board[1][0] != '-' && board[1][1] != '-' && board[1][2] != '-' && board[1][0] == board[1][1] && board[1][1] == board[1][2]){
return true;
}
else if(board[2][0] != '-' && board[2][1] != '-' && board[2][2] != '-' && board[2][0] == board[2][1] && board[2][1] == board[2][2]){
return true;
}
else if(board[0][0] != '-' && board[1][0] != '-' && board[2][0] != '-' && board[0][0] == board[1][0] && board[1][0] == board[2][0]){
return true;
}
else if(board[0][1] != '-' && board[1][1] != '-' && board[2][1] != '-' && board[0][1] == board[1][1] && board[1][1] == board[2][1]){
return true;
}
else if(board[0][2] != '-' && board[1][2] != '-' && board[2][2] != '-' && board[0][2] == board[1][2] && board[1][2] == board[2][2]){
return true;
}
else if(board[0][0] != '-' && board[1][1] != '-' && board[2][2] != '-' && board[0][0] == board[1][1] && board[1][1] == board[2][2]){
return true;
}
else if(board[0][2] != '-' && board[1][1] != '-' && board[2][0] != '-' && board[0][2] == board[1][1] && board[1][1] == board[2][0]){
return true;
}
else
{
return false;
}
}
void newGame(){/*to start a new game*/
int row=0, col=0;
int turn = 0;
int choice;
int numberOfTurns = 0;
cout<<"\t\t\t********NEW GAME STARTS********\n"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
board[i][j] = '-';
}
}
showBoard();
while(true){
numberOfTurns += 1 ;
cout<< "Player" << (turn+1) <<"'s turn it is... \n";
turn = !turn;
do{
cout<<"Input Row : ";
cin>>row;
cout<<endl;
cout<<"Input Column : ";
cin>>col;
}while(checkinput(row,col)==false);
board[row-1][col-1] = playersMark[!turn];
showBoard();
if(numberOfTurns>=3 && checkwin()){
if(turn==0){
cout<<"Player2 wins"<<endl;
player2 += 1;
}
else{
cout<<"Player1 wins"<<endl;
player1 += 1;
}
cout<<"If player1 wants to continue Press 1..."<<endl;
cin>>choice;

if(choice == 1){
cout<<"If player2 wants to continue Press 2..."<<endl;
cin>>choice;
if (choice == 2)
{
system("clear");
newGame();
}
else
break;
}
else{
return;
}
}
else if(numberOfTurns == 9){
cout<<"Game is Drawn "<<endl;
cout<<"If player1 wants to continue Press 1..."<<endl;
cin>>choice;

if(choice == 1){
cout<<"If player2 wants to continue Press 2..."<<endl;
cin>>choice;
if (choice == 2)
{
system("clear");
newGame();
}
else
{
break;
}
}
else{
return;
}
}
else{
continue;
}
}
}
int main() {
cout<<"*********************Welcome to Tic Tac Toe*********************"<<endl;
cout<<"\t\t\t\t\t PLAYER1 : O\n";
cout<<"\t\t\t\t\t PLAYER2 : X\n\n";
newGame();/* start game */
/* row and column start from 1 and end at 3 for example if user want to enter in first block he need toi enter 1 1 in row and column respectively*/
system("clear");
cout<<"\t\t\t\t\t **************GAME OVER**************\n\n"; /*game over show the result*/
cout<<"\t\t\t\t\t\t\t Players 1:" <<player1<<endl;
cout<<"\t\t\t\t\t\t\t Players 2:" <<player2<<endl;
cout<<"\n\t\t\t\t\t ***********THANKS ! VISIT AGAIN *********";
}

Add a comment
Know the answer?
Add Answer to:
Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...
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
  • 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)    ...

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

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

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

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

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

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

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

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