fork download
  1. //Diego Martinez CSC5 Chapter 7, P.492, #18
  2. /*******************************************************************************
  3. * PLAY TIC-TAC-TOE
  4. * ______________________________________________________________________________
  5. * This program allows two players to play a game of tic-tac-toe, taking turns
  6. * placing Xs and Os. It determines a winner or if it ends in a tie.
  7. *
  8. * Computation is based on the Formula:
  9. * Row match
  10. * Column match
  11. * Diagonal Match
  12. *______________________________________________________________________________
  13. * INPUT
  14. * Row and column positions (0-2)
  15. *
  16. * OUTPUT
  17. * Invalid moves
  18. * Winning Player
  19. * Tie game messsage
  20. *
  21. *******************************************************************************/
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. const int SIZE = 3;
  26.  
  27. // ===== FUNCTION PROTOTYPES =====
  28. void initializeBoard(char board[SIZE][SIZE]);
  29. void displayBoard(char board[SIZE][SIZE]);
  30. bool placeMove(char board[SIZE][SIZE], int row, int col, char player);
  31. bool checkWin(char board[SIZE][SIZE], char player);
  32. bool checkTie(char board[SIZE][SIZE]);
  33.  
  34. // ===== MAIN FUNCTION =====
  35. int main()
  36. {
  37. // ===== DATA DICTIONARY =====
  38. char board[SIZE][SIZE]; // game board
  39. int row, col; // player input
  40. char currentPlayer = 'X'; // Player 1 starts
  41. bool gameOver = false;
  42.  
  43. initializeBoard(board);
  44.  
  45. // ===== GAME LOOP =====
  46. while (!gameOver)
  47. {
  48. displayBoard(board);
  49.  
  50. cout << "\nPlayer " << currentPlayer << " turn\n";
  51. cout << "Enter row and column (0-2): ";
  52. cin >> row >> col;
  53.  
  54. if (!placeMove(board, row, col, currentPlayer))
  55. {
  56. cout << "Invalid move. Try again.\n";
  57. continue;
  58. }
  59.  
  60. if (checkWin(board, currentPlayer))
  61. {
  62. displayBoard(board);
  63. cout << "\nPlayer " << currentPlayer << " wins!\n";
  64. gameOver = true;
  65. }
  66. else if (checkTie(board))
  67. {
  68. displayBoard(board);
  69. cout << "\nIt's a tie!\n";
  70. gameOver = true;
  71. }
  72. else
  73. {
  74. // switch player
  75. if (currentPlayer == 'X')
  76. currentPlayer = 'O';
  77. else
  78. currentPlayer = 'X';
  79. }
  80. }
  81.  
  82. return 0;
  83. }
  84.  
  85. // ===== FUNCTION DEFINITIONS =====
  86.  
  87. // Initialize board with '*'
  88. void initializeBoard(char board[SIZE][SIZE])
  89. {
  90. for (int i = 0; i < SIZE; i++)
  91. {
  92. for (int j = 0; j < SIZE; j++)
  93. {
  94. board[i][j] = '*';
  95. }
  96. }
  97. }
  98.  
  99. // Display board
  100. void displayBoard(char board[SIZE][SIZE])
  101. {
  102. cout << "\nTic-Tac-Toe Board:\n";
  103. for (int i = 0; i < SIZE; i++)
  104. {
  105. for (int j = 0; j < SIZE; j++)
  106. {
  107. cout << board[i][j] << " ";
  108. }
  109. cout << endl;
  110. }
  111. }
  112.  
  113. // Place move if valid
  114. bool placeMove(char board[SIZE][SIZE], int row, int col, char player)
  115. {
  116. if (row < 0 || row >= SIZE || col < 0 || col >= SIZE)
  117. return false;
  118.  
  119. if (board[row][col] != '*')
  120. return false;
  121.  
  122. board[row][col] = player;
  123. return true;
  124. }
  125.  
  126. // Check win condition
  127. bool checkWin(char board[SIZE][SIZE], char player)
  128. {
  129. // rows & columns
  130. for (int i = 0; i < SIZE; i++)
  131. {
  132. if (board[i][0] == player && board[i][1] == player && board[i][2] == player)
  133. return true;
  134.  
  135. if (board[0][i] == player && board[1][i] == player && board[2][i] == player)
  136. return true;
  137. }
  138.  
  139. // diagonals
  140. if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
  141. return true;
  142.  
  143. if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
  144. return true;
  145.  
  146. return false;
  147. }
  148.  
  149. // Check tie condition
  150. bool checkTie(char board[SIZE][SIZE])
  151. {
  152. for (int i = 0; i < SIZE; i++)
  153. {
  154. for (int j = 0; j < SIZE; j++)
  155. {
  156. if (board[i][j] == '*')
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
Success #stdin #stdout 0.01s 5320KB
stdin
0 0
0 1
1 0
1 1
2 2
2 1
stdout
Tic-Tac-Toe Board:
* * * 
* * * 
* * * 

Player X turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X * * 
* * * 
* * * 

Player O turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X O * 
* * * 
* * * 

Player X turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X O * 
X * * 
* * * 

Player O turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X O * 
X O * 
* * * 

Player X turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X O * 
X O * 
* * X 

Player O turn
Enter row and column (0-2): 
Tic-Tac-Toe Board:
X O * 
X O * 
* O X 

Player O wins!