fork download
  1. // Timothy Price CS1A Ch 7 Challenge 15
  2. /*******************************************************************************
  3.  * Run game
  4.  *
  5.  * This program receives inputs to complete a game of tic-tac-toe.
  6.  *
  7.  * Inputs:
  8.  * Coordinates for placement of piece
  9.  *
  10.  * Outputs:
  11.  * Game board
  12.  * Selected placement
  13.  * Win or tie indicator
  14.  ******************************************************************************/
  15.  
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. // Constants for board size
  21. const int SIZE = 3;
  22.  
  23. // Function Prototypes
  24. void initializeBoard(char board[SIZE][SIZE]);
  25. void displayBoard(char board[SIZE][SIZE]);
  26. bool checkWin(char board[SIZE][SIZE], char symbol);
  27. bool isBoardFull(char board[SIZE][SIZE]);
  28.  
  29. int main() {
  30. char board[SIZE][SIZE];
  31.  
  32. // Initialize board with placeholder asterisks
  33. initializeBoard(board);
  34.  
  35. cout << "-------------------------------------" << endl;
  36. cout << " TIC-TAC-TOE (X vs O) " << endl;
  37. cout << "-------------------------------------" << endl;
  38. cout << "Use row and column numbers (0, 1, or 2)." << endl;
  39.  
  40. bool gameRunning = true;
  41. char currentPlayer = 'X'; // Player 1 starts with X
  42.  
  43. while (gameRunning) {
  44. displayBoard(board);
  45.  
  46. int row, col;
  47.  
  48. // Get move from current player
  49. cout << "Player ";
  50. if (currentPlayer == 'X') {
  51. cout << "1 (X)";
  52. } else {
  53. cout << "2 (O)";
  54. }
  55. cout << ", enter your move:" << endl;
  56.  
  57. while (true) {
  58. cout << " Enter row (0-2): ";
  59. cin >> row;
  60. cout << row;
  61. cout << " Enter column (0-2): ";
  62. cin >> col;
  63. cout << col;
  64.  
  65. // Validate input bounds and whether the spot is empty
  66. if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
  67. if (board[row][col] == '*') {
  68. board[row][col] = currentPlayer;
  69. break;
  70. } else {
  71. cout << "Choose another position." << endl;
  72. }
  73. } else {
  74. cout << "Invalid, enter numbers between 0 and 2." << endl;
  75. }
  76. }
  77.  
  78. // Check if current player won
  79. if (checkWin(board, currentPlayer)) {
  80. displayBoard(board);
  81. if (currentPlayer == 'X') {
  82. cout << "Player 1 (X) wins!" << endl;
  83. } else {
  84. cout << "Player 2 (O) wins!" << endl;
  85. }
  86. gameRunning = false;
  87. }
  88. // Check if the board is full (Tie)
  89. else if (isBoardFull(board)) {
  90. displayBoard(board);
  91. cout << "It's a tie! The board is full." << endl;
  92. gameRunning = false;
  93. }
  94. else {
  95. // Switch turns
  96. if (currentPlayer == 'X') {
  97. currentPlayer = 'O';
  98. } else {
  99. currentPlayer = 'X';
  100. }
  101. }
  102. }
  103.  
  104. cout << "-------------------------------------" << endl;
  105. cout << " GAME OVER " << endl;
  106. cout << "-------------------------------------" << endl;
  107.  
  108. return 0;
  109. }
  110.  
  111. // Function to initialize the board with '*'
  112. void initializeBoard(char board[SIZE][SIZE]) {
  113. for (int i = 0; i < SIZE; ++i) {
  114. for (int j = 0; j < SIZE; ++j) {
  115. board[i][j] = '*';
  116. }
  117. }
  118. }
  119.  
  120. // Function to display the game board grid
  121. void displayBoard(char board[SIZE][SIZE]) {
  122. cout << endl << " 0 1 2" << endl;
  123. cout << " -------------";
  124. for (int i = 0; i < SIZE; ++i) {
  125. cout << endl << i << " | ";
  126. for (int j = 0; j < SIZE; ++j) {
  127. cout << board[i][j] << " | ";
  128. }
  129. cout << endl << " -------------";
  130. }
  131. cout << endl << endl;
  132. }
  133.  
  134. // Function to check if a player has won
  135. bool checkWin(char board[SIZE][SIZE], char symbol) {
  136. // Check rows
  137. for (int i = 0; i < SIZE; ++i) {
  138. if (board[i][0] == symbol
  139. && board[i][1] == symbol
  140. && board[i][2] == symbol) {
  141. return true;
  142. }
  143. }
  144.  
  145. // Check columns
  146. for (int j = 0; j < SIZE; j++) {
  147. if (board[0][j] == symbol
  148. && board[1][j] == symbol
  149. && board[2][j] == symbol) {
  150. return true;
  151. }
  152. }
  153.  
  154. // Check diagonals
  155. if (board[0][0] == symbol
  156. && board[1][1] == symbol
  157. && board[2][2] == symbol) {
  158. return true;
  159. }
  160. if (board[0][2] == symbol
  161. && board[1][1] == symbol
  162. && board[2][0] == symbol) {
  163. return true;
  164. }
  165.  
  166. return false;
  167. }
  168.  
  169. // Function to check if the board is completely full (used for ties)
  170. bool isBoardFull(char board[SIZE][SIZE]) {
  171. for (int i = 0; i < SIZE; ++i) {
  172. for (int j = 0; j < SIZE; ++j) {
  173. if (board[i][j] == '*') {
  174. return false; // Found an empty spot, not full yet
  175. }
  176. }
  177. }
  178. return true; // No empty spots left
  179. }
Success #stdin #stdout 0.01s 5316KB
stdin
0
0
0
1
0
2
1
0
1
1
1
2
2
0
2
1
2
2
stdout
-------------------------------------
       TIC-TAC-TOE (X vs O)          
-------------------------------------
Use row and column numbers (0, 1, or 2).

     0   1   2
   -------------
0 | * | * | * | 
   -------------
1 | * | * | * | 
   -------------
2 | * | * | * | 
   -------------

Player 1 (X), enter your move:
  Enter row (0-2): 0  Enter column (0-2): 0
     0   1   2
   -------------
0 | X | * | * | 
   -------------
1 | * | * | * | 
   -------------
2 | * | * | * | 
   -------------

Player 2 (O), enter your move:
  Enter row (0-2): 0  Enter column (0-2): 1
     0   1   2
   -------------
0 | X | O | * | 
   -------------
1 | * | * | * | 
   -------------
2 | * | * | * | 
   -------------

Player 1 (X), enter your move:
  Enter row (0-2): 0  Enter column (0-2): 2
     0   1   2
   -------------
0 | X | O | X | 
   -------------
1 | * | * | * | 
   -------------
2 | * | * | * | 
   -------------

Player 2 (O), enter your move:
  Enter row (0-2): 1  Enter column (0-2): 0
     0   1   2
   -------------
0 | X | O | X | 
   -------------
1 | O | * | * | 
   -------------
2 | * | * | * | 
   -------------

Player 1 (X), enter your move:
  Enter row (0-2): 1  Enter column (0-2): 1
     0   1   2
   -------------
0 | X | O | X | 
   -------------
1 | O | X | * | 
   -------------
2 | * | * | * | 
   -------------

Player 2 (O), enter your move:
  Enter row (0-2): 1  Enter column (0-2): 2
     0   1   2
   -------------
0 | X | O | X | 
   -------------
1 | O | X | O | 
   -------------
2 | * | * | * | 
   -------------

Player 1 (X), enter your move:
  Enter row (0-2): 2  Enter column (0-2): 0
     0   1   2
   -------------
0 | X | O | X | 
   -------------
1 | O | X | O | 
   -------------
2 | X | * | * | 
   -------------

Player 1 (X) wins!
-------------------------------------
           GAME OVER                 
-------------------------------------