Write a c program to play tictactoe Use a twodimensional arr

Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns).

X goes first. The user will enter a row and column number to enter an X. If the position is not in use, put an X there. If there is already an X or O, tell the user to try another combination. Then O will go and do the same thing.

After X or O enters a legal move (you must check it’s one of the 9 allowed row/col combinations), see if the person who just made a move won. To win, you must have three of that mark (X or O) in any column, row or diagonal. Write a function to test if the player won. Pass it your symbol and return a boolean. If the palyer did not win, the next player makes a move and the program will check if there is a win. Continue to do this until either someone wins or there is no where to move.

0,0

0,1

0,2

1,0

1,1

1,2

2,0

2,1

2,2

The board will start out looking like this

(remember rows and columns start at 0)

0,0

0,1

0,2

1,0

1,1

X

2,0

2,1

2,2

If X selects row 1 column 2 the board will look like this

0,0

0,1

0,2

1,0

1,1

X

2,0

O

2,2

Since X didn’t win, it is now O’s turn. After selecting row 2 column 1, the board will look like this

Continue until someone wins (state whether it is X or O) or it’s a stalemate and no one wins. Ask if the players want another game. If yes, clear the board and start over otherwise stop the program

Some suggestions for doing the assignment:

Code the board as a two dimensional array and test that entries appear in the proper place. You’ll need a function to print the board.

Put in the win rules for three in a row and test

Put in the win rules for three in a column and diaganols and test

Include a feature that checks if the board is full in which case the game is a draw

Have the game start over if there is a win or draw, ask if the user wants to play again

For those who like a challenge – generate a random number between 0 and 2 for the row and column and use that as input to the game. Run a large number of games and see how many have been won (count by X and O) and how many draws.

0,0

0,1

0,2

1,0

1,1

1,2

2,0

2,1

2,2

Solution


//Tic-Tac-Toe c++ game
//Create a empty project in visual studio c++ and create two files called as
//TicTacToe.h(header file)
//TicTacToe.cpp(implementaion file)
//main.cpp(driver file)
------------------------------------------------------------------------------------------------
//TicTacToe.h header file
//Class declaration of TicTacToe.h
#ifndef TICTACTTOE_H
#define TICTACTTOE_H
//gloabal size of the board
const int SIZE=3;
class TicTacToe
{
   //private members of class TicTacToe
private:
   char currentPlayer;
   char board[SIZE][SIZE];
public:
   //default constructor
   TicTacToe();
   //To print board status
   void print();
   //Returns whose turn x or O
   char getCurrentPlayer();
   //returns true if player won the game
   bool isDone();
   //Returns the winner of the game
   char getWinner();
   //checks if the move is valid
   bool isValidMove(int row,int col);
   //To make a move in the board
   void makeMove(int row,int col);

};

#endif TICTACTOE_H
-----------------------------------------------------
//TicTacToe.cpp
//TicTacToe.h implementation file
#include<iostream>
//Include TicTacToe header file
#include \"TicTacToe.h\"
using namespace std;

//Constructor that sets the default \'-\' values
//and starting position is set to \'X\'-player
TicTacToe::TicTacToe()
{
   //set starting player to \'X\'
   //set remaining to empty
  
   for(int i=0;i<SIZE;i++)
       for(int j=0;j<SIZE;j++)
           board[i][j]=\'-\';

   //set starting player X
   board[0][0]=\'X\';
   //Next , set currentPlayer to \'O\'
   currentPlayer=\'O\';
}

//Print the board status
void TicTacToe::print()
{
   cout<<\"TIC-TAC-TOE BOARD\"<<endl;
   cout<<\"-------------------\"<<endl;
   for(int i=0;i<SIZE;i++)
   {
       for(int j=0;j<SIZE;j++)
           cout<<board[i][j]<<\"\\t\";
       cout<<endl;
   }
}

//Method that makeMove that accepts the row and colum
//number and checks the validmove and set the currentPlayer
//move .If move is invalid then print a message \"invalid move\"
void TicTacToe::makeMove(int row,int col)
{
   if(isValidMove(row,col))
   {
       board[row][col]=getCurrentPlayer();
       //change the currentPlayer to X
       if(currentPlayer==\'X\')
           currentPlayer=\'O\';
       else
           currentPlayer=\'X\';
   }
   else
       cout<<\"Invalid move\"<<endl;

}


//The metod isValidMove that accepts the row and column
//and returns true of board position is empty \'-\'(Empty value)
//otherwiser returns false
bool TicTacToe::isValidMove(int row,int col)
{
   return board[row][col]==\'-\';
}
//Returns the currentPlayer board to play
char TicTacToe:: getCurrentPlayer()
{
   return currentPlayer;
}

//The method isDone that returns true
//if the any of the possible validations are true
bool TicTacToe:: isDone()
{

   //horizontal checking for x -win
   if(board[0][0]==\'X\' &&board[0][1]==\'X\' &&board[0][2]==\'X\' )
       return true;
   else if(board[1][0]==\'X\' &&board[1][1]==\'X\' &&board[1][2]==\'X\' )
       return true;
   else if(board[2][0]==\'X\' &&board[2][1]==\'X\' &&board[2][2]==\'X\' )
       return true;
   //vertical checking for x -win
   else if(board[0][0]==\'X\' &&board[1][0]==\'X\' &&board[2][0]==\'X\' )
       return true;
   else if(board[0][1]==\'X\' &&board[1][1]==\'X\' &&board[2][1]==\'X\' )
       return true;
   else if(board[0][2]==\'X\' &&board[1][2]==\'X\' &&board[2][2]==\'X\' )
       return true;

   //Diagonal checking for x -win
   else if(board[0][0]==\'X\' &&board[1][1]==\'X\' &&board[2][2]==\'X\' )
       return true;
   else if(board[2][0]==\'X\' &&board[1][1]==\'X\' &&board[0][2]==\'X\' )
       return true;

   //horizontal checking for O -win
   if(board[0][0]==\'O\' &&board[0][1]==\'O\' &&board[0][2]==\'O\' )
       return true;
   else if(board[1][0]==\'O\' &&board[1][1]==\'O\' &&board[1][2]==\'O\' )
       return true;
   else if(board[2][0]==\'O\' &&board[2][1]==\'O\' &&board[2][2]==\'O\' )
       return true;
   //vertical checking for O -win
   else if(board[0][0]==\'O\' &&board[1][0]==\'O\' &&board[2][0]==\'O\' )
       return true;
   else if(board[0][1]==\'O\' &&board[1][1]==\'O\' &&board[2][1]==\'O\' )
       return true;
   else if(board[0][2]==\'O\' &&board[1][2]==\'O\' &&board[2][2]==\'O\' )
       return true;

   //Diagonal checking for O -win
   else if(board[0][0]==\'O\' &&board[1][1]==\'O\' &&board[2][2]==\'O\' )
       return true;
   else if(board[2][0]==\'O\' &&board[1][1]==\'O\' &&board[0][2]==\'O\' )
       return true;
   else
       //otherwise no x or O not win
       return false;
}
-------------------------------------------------------------------------
//main.cpp
//main method that test the class
//by implementing the TicTacToe class
#include<iostream>
#include \"TicTacToe.h\"
using namespace std;

int main()
{
   //Make a tictactoe object
   TicTacToe game;  
   //print board status
   game.print();
   cout<<\"Current Player : \"<<game.getCurrentPlayer()<<endl;
   //calling makeMove
   game.makeMove(2,2);
   game.print();
   cout<<\"Current Player : \"<<game.getCurrentPlayer()<<endl;
   //calling makeMove
   game.makeMove(0,2);
   game.print();
   cout<<\"Current Player : \"<<game.getCurrentPlayer()<<endl;
   //calling makeMove
   game.makeMove(1,1);
   game.print();

   char player=game.getCurrentPlayer();
   cout<<\"Current Player : \"<<player<<endl;
   //calling makeMove
   game.makeMove(0,1);
   game.print();

   //calling isDone method to check if the current player won the game
   if(game.isDone())
       cout<<player<<\" player won the game\"<<endl;

   //pause the program output
   system(\"pause\");
   return 0;
}
--------------------------------------------------------------
Sample output:

sample run1:
Welcome to Tic-Tac-Toe. Please enter your first move: A1

X |   |
-----------
   |   |
-----------
   |   |

I will play at A2

X | O |
-----------
   |   |
-----------
   |   |

Please enter your next move: B2

X | O |
-----------
   | X |
-----------
   |   |

I will play at A3

X | O | O
-----------
   | X |
-----------
   |   |

Please enter your next move: C3

X | O | O
-----------
   | X |
-----------
   |   | X

You beat me!

sample run2:
Welcome to Tic-Tac-Toe. Please enter your first move: B1

   |   |
-----------
X |   |
-----------
   |   |

I will play at A1

O |   |
-----------
X |   |
-----------
   |   |

Please enter your next move: B2

O |   |
-----------
X | X |
-----------
   |   |

I will play at A2

O | O |
-----------
X | X |
-----------
   |   |

Please enter your next move: B3

O | O |
-----------
X | X | X
-----------
   |   |

You beat me!


sample run3:

Welcome to Tic-Tac-Toe. Please enter your first move: C1

   |   |
-----------
   |   |
-----------
X |   |

I will play at A1

O |   |
-----------
   |   |
-----------
X |   |

Please enter your next move: C2

O |   |
-----------
   |   |
-----------
X | X |

I will play at A2

O | O |
-----------
   |   |
-----------
X | X |

Please enter your next move: C3

O | O |
-----------
   |   |
-----------
X | X | X

You beat me!

Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente
Write a c++ program to play tic-tac-toe. Use a two-dimensional array to store the 9 positions of the board (3 rows, 3 columns). X goes first. The user will ente

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site