Guest User

Untitled

a guest
May 21st, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void main() {
  6. char cSquare1('1');
  7. char cSquare2('2');
  8. char cSquare3('3');
  9. char cSquare4('4');
  10. char cSquare5('5');
  11. char cSquare6('6');
  12. char cSquare7('7');
  13. char cSquare8('8');
  14. char cSquare9('9');
  15. int iPlayerTurn(1);
  16. bool bGameOver(true); // Variables are initialised to begin with
  17.  
  18. // Here is the main do-while loop that the game consists of
  19. do {
  20. // Functions to display an acsii tic tac toe board
  21. cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
  22. cout << "-+-+-"<< endl;
  23. cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
  24. cout << "-+-+-"<< endl;
  25. cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl;
  26.  
  27. // Sets the player marker: Player 1 uses X and Player 2 uses O
  28. char cPlayerMark;
  29. if (iPlayerTurn == 1) {
  30. cPlayerMark = 'X';
  31. } else {
  32. cPlayerMark = 'O';
  33. }
  34.  
  35. // Prompt the player for a move
  36. cout << "Player" << iPlayerTurn << "'s move:" << endl;
  37. bool bValidMove;
  38. // Loop until we get a valid move
  39. do {
  40. char cNextMove;
  41. cin >> cNextMove;
  42. bValidMove = true;
  43.  
  44. // Check for a valid move
  45. if (cNextMove == '1' && cSquare1 == '1') {
  46. cSquare1 = cPlayerMark;
  47. } else if (cNextMove == '2' && cSquare2 == '2') {
  48. cSquare2 = cPlayerMark;
  49. } else if (cNextMove == '3' && cSquare3 == '3') {
  50. cSquare3 = cPlayerMark;
  51. } else if (cNextMove == '4' && cSquare4 == '4') {
  52. cSquare4 = cPlayerMark;
  53. } else if (cNextMove == '5' && cSquare5 == '5') {
  54. cSquare5 = cPlayerMark;
  55. } else if (cNextMove == '6' && cSquare6 == '6') {
  56. cSquare6 = cPlayerMark;
  57. } else if (cNextMove == '7' && cSquare7 == '7') {
  58. cSquare7 = cPlayerMark;
  59. } else if (cNextMove == '8' && cSquare8 == '8') {
  60. cSquare8 = cPlayerMark;
  61. } else if (cNextMove == '9' && cSquare9 == '9') {
  62. cSquare9 = cPlayerMark;
  63. } else {
  64. cout << "Cannot go there, try again." << endl;
  65. bValidMove = false;
  66. }
  67. } while (!bValidMove);
  68.  
  69. bGameOver = false;
  70. bool bWinGame = true;
  71. // Check for winning conditions
  72. if (cSquare1 != '1') {
  73. if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
  74. bGameOver = true;
  75. }
  76. if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
  77. bGameOver = true;
  78. }
  79. }
  80. if (cSquare5 != '5') {
  81. if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
  82. bGameOver = true;
  83. }
  84. if (cSquare2 == cSquare5 && cSquare8 == cSquare5) {
  85. bGameOver = true;
  86. }
  87. if (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
  88. bGameOver = true;
  89. }
  90. if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
  91. bGameOver = true;
  92. }
  93. }
  94. if (cSquare9 != '9') {
  95. if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
  96. bGameOver = true;
  97. }
  98. if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
  99. bGameOver = true;
  100. }
  101. }
  102. // Need to check the board full (no-win condition)
  103. if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
  104. cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
  105. cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver)
  106. {
  107. bGameOver = true;
  108. bWinGame = false;
  109. }
  110.  
  111. if (bGameOver) {
  112. if (bWinGame) {
  113. cout << "Player" << iPlayerTurn << " is victorious!" << endl;
  114. }
  115. // Print ending board
  116. cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
  117. cout << "-+-+-"<< endl;
  118. cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
  119. cout << "-+-+-"<< endl;
  120. cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl;
  121.  
  122. cout << "Game Over" << endl;
  123. cout << "Rematch (y/n)?" << endl;
  124. char cPlayAgain;
  125. cin >> cPlayAgain;
  126.  
  127. if (cPlayAgain == 'y') {
  128. bGameOver = false;
  129. // Clear the board
  130. cSquare1 = '1';
  131. cSquare2 = '2';
  132. cSquare3 = '3';
  133. cSquare4 = '4';
  134. cSquare5 = '5';
  135. cSquare6 = '6';
  136. cSquare7 = '7';
  137. cSquare8 = '8';
  138. cSquare9 = '9';
  139. }
  140. iPlayerTurn = 1;
  141. } else {
  142. // Alternate player turns
  143. if (iPlayerTurn == 1) {
  144. iPlayerTurn = 2;
  145. } else {
  146. iPlayerTurn = 1;
  147. }
  148. }
  149. } while (!bGameOver);
  150. }
Add Comment
Please, Sign In to add comment