Untitled


SUBMITTED BY: Guest

DATE: May 21, 2013, 9:03 p.m.

FORMAT: Text only

SIZE: 5.0 kB

HITS: 8237

  1. #include <iostream>
  2. using namespace std;
  3. bool checkWin (char);
  4. //check_win - checks if the player using the given character won
  5. //@param char - the character the player is using
  6. void displayBoard ();
  7. //displayBoard - displays the board to the users
  8. char display [5][5] = { { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' }, { '-', '+', '-', '+', '-' }, { ' ', '|', ' ', '|', ' ' } };
  9. int main ()
  10. {
  11. char board [3][3] = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } },
  12. player[2] = { ' ', ' ' },
  13. again = 'y';
  14. bool gameWon = false;
  15. while (again != 'n')
  16. {
  17. int turns = 0;
  18. cout << "Player 1 enter the character you are going to use (no spaces): ";
  19. cin >> player[0];
  20. while (player[0] == ' ' || player[0] == ' ')
  21. {
  22. cout << "You entered a space (no spaces), re-enter a new character: ";
  23. cin >> player[0];
  24. }
  25. cout << endl << "Player 2 enter the character you are going to use (no spaces): ";
  26. cin >> player[1];
  27. while (player[1] == ' ' || player[1] == ' ')
  28. {
  29. cout << "You entered a space (no spaces), re-enter a new character: ";
  30. cin >> player[1];
  31. }
  32. while (turns != 9 && !gameWon)
  33. {
  34. unsigned short x, y;
  35. cout << endl << endl;
  36. bool p1turn = true, p2turn = true;
  37. while (p1turn && turns != 9)
  38. {
  39. p1turn = false;
  40. displayBoard();
  41. cout << endl << endl << "Player 1: Enter 2 numbers (1-3) to mark off that place: ";
  42. cin >> x >> y;
  43. while (x > 3 || x < 1 || y > 3 || y < 1)
  44. {
  45. cout << "You must enter numbers from 1 to 3, please re-enter them: ";
  46. cin >> x >> y;
  47. }
  48. if (board[x - 1][y - 1] != ' ')
  49. {
  50. p1turn = true;
  51. cout << "You entered a coordinate that was already taken." << endl << endl;
  52. }
  53. else
  54. {
  55. board[x - 1][y - 1] = player[0];
  56. x = (x * 2) - 1;
  57. y = (y * 2) - 1;
  58. display[x - 1][y - 1] = player[0];
  59. if (checkWin(player[0])) gameWon = true;
  60. turns++;
  61. }
  62. }
  63. while (p2turn && turns != 9 && !gameWon)
  64. {
  65. p2turn = false;
  66. cout << endl;
  67. displayBoard();
  68. cout << endl << endl << "Player 2: Enter 2 numbers (1-3) to mark off that place: ";
  69. cin >> x >> y;
  70. while (x > 3 || x < 1 || y > 3 || y < 1)
  71. {
  72. cout << "You must enter numbers from 1 to 3, please re-enter them: ";
  73. cin >> x >> y;
  74. }
  75. if (board[x - 1][y - 1] != ' ')
  76. {
  77. p2turn = true;
  78. cout << "You entered a coordinate that was already taken." << endl << endl;
  79. }
  80. else
  81. {
  82. board[x - 1][y - 1] = player[1];
  83. x = (x * 2) - 1;
  84. y = (y * 2) - 1;
  85. display[x - 1][y - 1] = player[1];
  86. if (checkWin(player[1])) gameWon = true;
  87. turns++;
  88. }
  89. }
  90. }
  91. cout << endl << endl;
  92. displayBoard();
  93. if (checkWin(player[0]))
  94. cout << endl << endl << "And the winner is: Player 1!!!" << endl << endl;
  95. else if (checkWin(player[1]))
  96. cout << endl << endl << "And the winner is: Player 2!!!" << endl << endl;
  97. else cout << endl << endl << "And it looks like we have a draw folks. Cats Game." << endl << endl;
  98. cout << "Play again? (y/n): ";
  99. cin >> again;
  100. if (again != 'n')
  101. {
  102. for (int x = 1; x <= 3; x++)
  103. for (int y = 1; y <= 3; y++)
  104. {
  105. board[x - 1][y - 1] = ' ';
  106. display[(x * 2) - 2][(y * 2) - 2] = ' ';
  107. }
  108. }
  109. }
  110. cout << endl << endl;
  111. system("Pause");
  112. return 0;
  113. }
  114. bool checkWin (char player)
  115. {
  116. bool result = false;
  117. for (int x = 0; x < 5; x += 2)
  118. {
  119. bool thisResult = true;
  120. for (int y = 0; y < 5; y += 2)
  121. thisResult = thisResult && display[x][y] == player;
  122. result = result || thisResult;
  123. }
  124. for (int x = 0; x < 5; x += 2)
  125. {
  126. bool thisResult = true;
  127. for (int y = 0; y < 5; y += 2)
  128. thisResult = thisResult && display[y][x] == player;
  129. result = result || thisResult;
  130. }
  131. bool thisResult[2] = { true, true };
  132. int y;
  133. for (int x = 0; x < 5; x += 2)
  134. {
  135. y = 4 - x;
  136. thisResult[0] = thisResult[0] && display[x][y] == player;
  137. }
  138. result = result || thisResult[0];
  139. for (int x = 0; x < 5; x += 2)
  140. {
  141. y = x;
  142. thisResult[0] = thisResult[0] && display[x][y] == player;
  143. }
  144. result = result || thisResult[0];
  145. return result;
  146. }
  147. void displayBoard ()
  148. {
  149. cout << " y:1 2 3" << endl << "x:" << endl;
  150. for (int x = 0; x < 5; x++)
  151. {
  152. if (x != 1 && x != 3) cout << (x + 2) / 2 << ". ";
  153. else cout << " ";
  154. for (int y = 0; y < 5; y++)
  155. cout << display[x][y];
  156. cout << endl;
  157. }
  158. }

comments powered by Disqus