fork download
  1. //Devin Scheu CS1A Chapter 7, P. 448, #15
  2. //
  3. /**************************************************************
  4. *
  5. * PLAY TIC-TAC-TOE GAME
  6. * ____________________________________________________________
  7. * This program allows two players to play tic-tac-toe, displaying
  8. * the board, handling moves, and determining the winner or tie.
  9. * ____________________________________________________________
  10. * INPUT
  11. * row : The row for the player's move
  12. * col : The column for the player's move
  13. *
  14. * OUTPUT
  15. * gameBoard : The current state of the tic-tac-toe board
  16. * gameResult : The result of the game (win or tie)
  17. *
  18. **************************************************************/
  19.  
  20. #include <iostream>
  21.  
  22. using namespace std;
  23.  
  24. int main () {
  25.  
  26. //Variable Declarations
  27. char board[3][3] = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}};
  28. int turn = 1; // 1 for X, 2 for O
  29. bool gameOver = false;
  30.  
  31. while (!gameOver) {
  32. // Display board
  33. cout << "\nCurrent Board:" << endl;
  34. for (int i = 0; i < 3; i++) {
  35. for (int j = 0; j < 3; j++) {
  36. cout << board[i][j] << " ";
  37. }
  38. cout << endl;
  39. }
  40.  
  41. // Player turn
  42. char player = (turn == 1) ? 'X' : 'O';
  43. int row, col;
  44. cout << "Player " << turn << " (" << player << "), enter row (1-3): ";
  45. cin >> row;
  46. cout << row << endl;
  47. while (row < 1 || row > 3) {
  48. cout << "Invalid row. Enter row (1-3): ";
  49. cin >> row;
  50. cout << row << endl;
  51. }
  52. cout << "Enter column (1-3): ";
  53. cin >> col;
  54. cout << col << endl;
  55. while (col < 1 || col > 3) {
  56. cout << "Invalid column. Enter column (1-3): ";
  57. cin >> col;
  58. cout << col << endl;
  59. }
  60. row--; col--; // Adjust to 0-based index
  61. while (board[row][col] != '*') {
  62. cout << "Spot taken. Enter row (1-3): ";
  63. cin >> row;
  64. cout << row << endl;
  65. while (row < 1 || row > 3) {
  66. cout << "Invalid row. Enter row (1-3): ";
  67. cin >> row;
  68. cout << row << endl;
  69. }
  70. cout << "Enter column (1-3): ";
  71. cin >> col;
  72. cout << col << endl;
  73. while (col < 1 || col > 3) {
  74. cout << "Invalid column. Enter column (1-3): ";
  75. cin >> col;
  76. cout << col << endl;
  77. }
  78. row--; col--;
  79. }
  80. board[row][col] = player;
  81.  
  82. // Check for win
  83. bool win = false;
  84. // Rows
  85. for (int i = 0; i < 3; i++) {
  86. if (board[i][0] == player && board[i][1] == player && board[i][2] == player) win = true;
  87. }
  88. // Columns
  89. for (int i = 0; i < 3; i++) {
  90. if (board[0][i] == player && board[1][i] == player && board[2][i] == player) win = true;
  91. }
  92. // Diagonals
  93. if (board[0][0] == player && board[1][1] == player && board[2][2] == player) win = true;
  94. if (board[0][2] == player && board[1][1] == player && board[2][0] == player) win = true;
  95.  
  96. if (win) {
  97. // Display final board
  98. cout << "\nFinal Board:" << endl;
  99. for (int i = 0; i < 3; i++) {
  100. for (int j = 0; j < 3; j++) {
  101. cout << board[i][j] << " ";
  102. }
  103. cout << endl;
  104. }
  105. // Separator and Output Section
  106. cout << "-------------------------------------------------------" << endl;
  107. cout << "OUTPUT:" << endl;
  108. cout << "Player " << turn << " wins!" << endl;
  109. gameOver = true;
  110. return 0;
  111. }
  112.  
  113. // Check for tie
  114. bool tie = true;
  115. for (int i = 0; i < 3; i++) {
  116. for (int j = 0; j < 3; j++) {
  117. if (board[i][j] == '*') tie = false;
  118. }
  119. }
  120. if (tie) {
  121. // Display final board
  122. cout << "\nFinal Board:" << endl;
  123. for (int i = 0; i < 3; i++) {
  124. for (int j = 0; j < 3; j++) {
  125. cout << board[i][j] << " ";
  126. }
  127. cout << endl;
  128. }
  129. // Separator and Output Section
  130. cout << "-------------------------------------------------------" << endl;
  131. cout << "OUTPUT:" << endl;
  132. cout << "It's a tie!" << endl;
  133. gameOver = true;
  134. return 0;
  135. }
  136.  
  137. // Switch turn
  138. turn = (turn == 1) ? 2 : 1;
  139. }
  140.  
  141. } //end of main()
Success #stdin #stdout 0.01s 5284KB
stdin
1
1
1
3
2
2
3
2
2
1
2
3
1
2
3
1
3
2
3
3
stdout
Current Board:
* * * 
* * * 
* * * 
Player 1 (X), enter row (1-3): 1
Enter column (1-3): 1

Current Board:
X * * 
* * * 
* * * 
Player 2 (O), enter row (1-3): 1
Enter column (1-3): 3

Current Board:
X * O 
* * * 
* * * 
Player 1 (X), enter row (1-3): 2
Enter column (1-3): 2

Current Board:
X * O 
* X * 
* * * 
Player 2 (O), enter row (1-3): 3
Enter column (1-3): 2

Current Board:
X * O 
* X * 
* O * 
Player 1 (X), enter row (1-3): 2
Enter column (1-3): 1

Current Board:
X * O 
X X * 
* O * 
Player 2 (O), enter row (1-3): 2
Enter column (1-3): 3

Current Board:
X * O 
X X O 
* O * 
Player 1 (X), enter row (1-3): 1
Enter column (1-3): 2

Current Board:
X X O 
X X O 
* O * 
Player 2 (O), enter row (1-3): 3
Enter column (1-3): 1

Current Board:
X X O 
X X O 
O O * 
Player 1 (X), enter row (1-3): 3
Enter column (1-3): 2
Spot taken. Enter row (1-3): 3
Enter column (1-3): 3

Final Board:
X X O 
X X O 
O O X 
-------------------------------------------------------
OUTPUT:
Player 1 wins!