Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Tic Tac Toe Game using C++

Tic-Tac-Toe Game
Write a program that allows two players to play  a game of tic-tac-roc. Use a two dimensional
char army with three rows and three columns as the game board. Each
element of the array should be initialized with an asterisk (“). The program should fll n 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
ask the user to enter the row and column number.
Allows player 2 to select a location on the board for an O. The program should
ask the user to enter the row and column number.
Determines whether a p layer has won, or a tic has occurred. If a player has won,
the program should declare that player the winner and end. If a tie has occurred,
the pro~ram should say so and end .

Player 1 wins when there are three XS in a rowan the game board. The XS can appear
in a row, in a column, or diagonally across the board. A tic occurs when all of the
locations on the board are full, but there is no winner.

Answer:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//  Tic-Tac-Toe Game
#include <iostream>
using namespace std;
 
// Constants for the rows and columns
const int ROWS = 3;
const int COLS = 3;
 
// Function prototypes
void displayBoard(char [][COLS]);
void playerTurn(char [][COLS], char);
bool gameOver(char [][COLS]);
bool playerWins(char [][COLS], char);
bool playerCanWin(char [][COLS], char);
void displayWinner(char [][COLS]);
 
int main()
{
   // Array for the game board.
   char gameBoard[ROWS][COLS] = { '*', '*', '*',
                                  '*', '*', '*',
                                  '*', '*', '*'
                                 };
    
   do
   {
      // Display the game board.
      displayBoard(gameBoard);
       
      // Let player 1 have a turn.
      playerTurn(gameBoard, 'X');
       
      // Display the game board again.
      displayBoard(gameBoard);
       
      // If the game is not over, let
      // player 2 have a turn.
      if (!gameOver(gameBoard))
         playerTurn(gameBoard, 'O');
          
   } while (!gameOver(gameBoard));
    
   // Display the board one last time.
   displayBoard(gameBoard);
    
   // Display the winner.
   displayWinner(gameBoard);
 
   return 0;
}
 
//******************************************************
// The displayBoard function displays the contents of  *
// the board.                                          *
//******************************************************
 
void displayBoard(char board[][COLS])
{
   // Display the column headings.
   cout << "       Columns\n";
   cout << "        1 2 3\n";
    
   // Display the rows.
   for (int row = 0; row < ROWS; row++)
   {
      // Row label.
      cout << "Row " << (row + 1)<< ":  ";
       
      // Row contents.
      for (int col = 0; col < COLS; col++)
      {
         cout << board[row][col] << " ";
      }
      cout << endl;
   }
}
 
//******************************************************
// The playerTurn function allows a player (X or O) to *
// take a turn.                                        *
//******************************************************
 
void playerTurn(char board[][COLS], char symbol)
{
   // The isAvailable flag is set to true when the
   // player selects a location on the board that
   // is available.
   bool isAvailable = false;
    
   int row;  // Row to place symbol
   int col;  // column to place symbol
    
   // Prompt the player to enter a location.
   cout << "Player " << symbol << "'s turn.\n";
   cout << "Enter a row and column to place an "
        << symbol << ".\n";
    
   // Get and validate the location.
   while (!isAvailable)
   {
      // Get the row.
      cout << "Row: "; cin >> row;
       
      // Validate the row.
      while (row < 1 || row > ROWS)
      {
         cout << "Invalid Row!\n";
         cout << "Row: "; cin >> row;
      }
       
      // Get the column.
      cout << "Column: "; cin >> col;
    
      // Validate the column.
      while (col < 1 || col > COLS)
      {
         cout << "Invalid Column!\n";
         cout << "Column: "; cin >> col;
      }
       
      // Determine whether the selected
      // cell is available.
      if (board[row - 1][col - 1] == '*')
         isAvailable = true;
      else
      {
         cout << "That location is not available. "
              << "Select another location.\n";
      }
   }
    
   // Place the player's symbol on the board
   // at the selected location.
   board[row - 1][col - 1] = symbol;
}
 
//****************************************************
// The gameOver function returns true if the game    *
// is over. This is the case when either player has  *
// already won, or there is a tie.                   *
//****************************************************
 
bool gameOver(char board[][COLS])
{
   // If either player has already won, game over.
   if (playerWins(board, 'X') || playerWins(board, 'O'))
      return true;
       
   // Otherwise, if either player CAN STILL win, game
   // NOT over.
   else if (playerCanWin(board, 'X') || playerCanWin(board, 'O'))
      return false;
    
   // Otherwise, it's a tie. Game over.
   else
      return true;
}
 
//***********************************************************
// The playerWins function accepts the game board and       *
// a player symbol (X or O) as arguments. It returns        *
// true if the player has won.                              *
//***********************************************************
 
bool playerWins(char board[][COLS], char symbol)
{
   // Check the first horizontal row.
   if (board[0][0] == symbol && board[0][1] == symbol &&
       board[0][2] == symbol)
      return true;
 
   // Check the second horizontal row.
   if (board[1][0] == symbol && board[1][1] == symbol &&
       board[1][2] == symbol)
      return true;
 
   // Check the third horizontal row.
   if (board[2][0] == symbol && board[2][1] == symbol &&
       board[2][2] == symbol)
      return true;
       
   // Check the first column.
   if (board[0][0] == symbol && board[1][0] == symbol &&
       board[2][0] == symbol)
      return true;
    
   // Check the second column.
   if (board[0][1] == symbol && board[1][1] == symbol &&
       board[2][1] == symbol)
      return true;
 
   // Check the third column.
   if (board[0][2] == symbol && board[1][2] == symbol &&
       board[2][2] == symbol)
      return true;
 
   // Check the diagonal
   if (board[0][0] == symbol && board[1][1] == symbol &&
       board[2][2] == symbol)
      return true;
    
   // If we make it this far, the player did not win.
   return false;
}
 
//*****************************************************
// The playerCanWin function returns true if the      *
// player (X or O) can still win.                     *
//*****************************************************
 
bool playerCanWin(char board[][COLS], char symbol)
{
   // Check the first horizontal row for a possibility.
   if ((board[0][0] == symbol || board[0][0] == '*') &&
       (board[0][1] == symbol || board[0][1] == '*') &&
       (board[0][2] == symbol || board[0][2] == '*'))
      return true;
 
   // Check the second horizontal row for a possibility.
   if ((board[1][0] == symbol || board[1][0] == '*') &&
       (board[1][1] == symbol || board[1][1] == '*') &&
       (board[1][2] == symbol || board[1][2] == '*'))
      return true;
 
   // Check the third horizontal row for a possibility.
   if ((board[2][0] == symbol || board[2][0] == '*') &&
       (board[2][1] == symbol || board[2][1] == '*') &&
       (board[2][2] == symbol || board[2][2] == '*'))
      return true;
 
   // Check the first column for a possibility.
   if ((board[0][0] == symbol || board[0][0] == '*') &&
       (board[1][0] == symbol || board[1][0] == '*') &&
       (board[2][0] == symbol || board[2][0] == '*'))
      return true;
 
   // Check the second column for a possibility.
   if ((board[0][1] == symbol || board[0][1] == '*') &&
       (board[1][1] == symbol || board[1][1] == '*') &&
       (board[2][1] == symbol || board[2][1] == '*'))
      return true;
 
   // Check the third column for a possibility.
   if ((board[0][2] == symbol || board[0][2] == '*') &&
       (board[1][2] == symbol || board[1][2] == '*') &&
       (board[2][2] == symbol || board[2][2] == '*'))
      return true;
 
   // Check the diagonal for a possibility.
   if ((board[0][0] == symbol || board[0][0] == '*') &&
       (board[1][1] == symbol || board[1][1] == '*') &&
       (board[2][2] == symbol || board[2][2] == '*'))
      return true;
 
   // If we make it this far, the player cannot win.
   return false;
}
 
//*****************************************************
// The displayWinner function displays the winner.    *
//*****************************************************
 
void displayWinner(char board[][COLS])
{
   if (playerWins(board, 'X'))
      cout << "Player 1 (X) WINS!!!!!\n\n";
   else if (playerWins(board, 'O'))
      cout << "Player 2 (O) WINS!!!!!\n\n";
   else
      cout << "Game Over... it's a TIE.\n\n";
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!