Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. char board[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} }; //stores the values on the board.
  5. char turn = 'X'; //determines whether it is "X"'s turn or "O"'s turn.
  6. int choice; //used for user input.
  7. int fullCheck = 0; //checks how many spaces are filled on the board. When this value is equal to 9, the board is full, so the program announces a tie and terminates the program.
  8. bool isOver = false; //a boolean for whether the game is over or not. Once this value is true, the program terminates.
  9.  
  10. void switchTurn()
  11. {
  12. if (turn == 'X')
  13. {
  14. turn = 'O';
  15. }
  16. else
  17. {
  18. turn = 'X';
  19. }
  20. }
  21. void drawBoard()
  22. {
  23.  
  24. for (int horizontal = 0; horizontal < 3; ++horizontal)
  25. {
  26. cout << "|";
  27. for (int vertical = 0; vertical < 3; ++vertical)
  28. {
  29. cout << board[horizontal][vertical] << "|";
  30. }
  31. if (horizontal < 2)
  32. {
  33. cout << "\n|-+-+-|\n";
  34. }
  35. }
  36.  
  37. }
  38. void translateChoice()
  39. {
  40. int rowPick = 0;
  41. int columnPick = 0;
  42. choice -= 1;
  43. while (choice > 2)
  44. {
  45. rowPick += 1;
  46. choice -= 3;
  47. }
  48. columnPick = choice;
  49. if (board[rowPick][columnPick] != 'X' && board[rowPick][columnPick] != 'O' && rowPick <= 2)
  50. {
  51. board[rowPick][columnPick] = turn;
  52. switchTurn();
  53. fullCheck += 1;
  54. }
  55. else
  56. {
  57. cout << "Sorry, that square is taken. Try again";
  58. }
  59. }
  60. void winProcedure()
  61. {
  62. switchTurn();
  63. drawBoard();
  64. cout << "\n\nyou win, " << turn << "!!\n\n";
  65. isOver = true;
  66. }
  67. void winCheck()
  68. {
  69. if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[2][0] == board[1][1] && board[1][1] == board[0][2]))
  70. {
  71. winProcedure();
  72. }
  73. for (int i = 0; i < 3; ++i)
  74. {
  75. if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]))
  76. {
  77. winProcedure();
  78. }
  79. }
  80. }
  81. int main()
  82. {
  83. cout << "Welcome to Tic Tac Toe!\n\n";
  84. while (isOver == false)
  85. {
  86. drawBoard();
  87. cout << "\n\nPick your square, " << turn << "\n\nChoice: ";
  88. cin >> choice;
  89. system("CLS");
  90. translateChoice();
  91. cout << "\n\n";
  92. winCheck();
  93. if (fullCheck == 9 && isOver == false)
  94. {
  95. cout << "It's a tie!\n\n";
  96. isOver = true;
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement