Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. char Board[9];
  8.  
  9. void showBoard();
  10. bool moveIsValid(int m);
  11. int whoWon();
  12.  
  13. int
  14. main()
  15. {
  16. srand(time(NULL));
  17. string Player_1_Name;
  18. string Player_2_Name;
  19. int Whose_Turn = 1;
  20. int Move;
  21. int Total_Moves = 0;
  22.  
  23. Board[0] = '0';
  24. Board[1] = '1';
  25. Board[2] = '2';
  26. Board[3] = '3';
  27. Board[4] = '4';
  28. Board[5] = '5';
  29. Board[6] = '6';
  30. Board[7] = '7';
  31. Board[8] = '8';
  32.  
  33. cout << "Player 1: Please enter your name." << endl;
  34. cin >> Player_1_Name;
  35. cout << "Player 2: Computer." << endl;
  36. cin >> Player_2_Name;
  37.  
  38. while (whoWon() == 0 && Total_Moves < 9) {
  39. do {
  40. if (Whose_Turn == 1) {
  41. showBoard();
  42.  
  43. cout << Player_1_Name << ": It's your turn." << endl;
  44. cout << "Enter the number of the spot where you'd like to move." << endl;
  45. cin >> Move;
  46.  
  47. } else {
  48. Move = rand() % 9;
  49. }
  50.  
  51. } while (moveIsValid(Move) != true);
  52.  
  53. Total_Moves++;
  54.  
  55. switch (Whose_Turn) {
  56. case (1):
  57. Board[Move] = 'x';
  58. Whose_Turn = 2;
  59. break;
  60. case (2):
  61. Board[Move] = 'o';
  62. Whose_Turn = 1;
  63. }
  64. }
  65. showBoard();
  66. switch (whoWon()) {
  67. case 1:
  68. cout << Player_1_Name << " has won the game!" << endl;
  69. break;
  70. case 2:
  71. cout << Player_2_Name << " has won the game!" << endl;
  72. break;
  73. default:
  74. cout << "It's a tie game!" << endl;
  75. break;
  76. }
  77.  
  78. system("PAUSE");
  79. }
  80.  
  81. void
  82. showBoard()
  83. {
  84. cout << endl;
  85. cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl;
  86. cout << "--+---+--" << endl;
  87. cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl;
  88. cout << "--+---+--" << endl;
  89. cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl;
  90. cout << endl;
  91. }
  92.  
  93. bool
  94. moveIsValid(int m)
  95. {
  96. return (Board[m] != 'x' && Board[m] != 'o');
  97. }
  98.  
  99. int charToWinner(char ch)
  100. {
  101. if (ch == 'x') {
  102. return 1;
  103. } else {
  104. return 2;
  105. }
  106. }
  107.  
  108. int
  109. whoWon()
  110. {
  111. if (Board[0] == Board[1] && Board[1] == Board[2]) {
  112. return charToWinner(Board[0]);
  113. }
  114.  
  115. if (Board[3] == Board[4] && Board[4] == Board[5]) {
  116. return charToWinner(Board[3]);
  117. }
  118.  
  119. if (Board[6] == Board[7] && Board[7] == Board[8]) {
  120. return charToWinner(Board[6]);
  121. }
  122.  
  123. if (Board[0] == Board[3] && Board[3] == Board[6]) {
  124. return charToWinner(Board[0]);
  125. }
  126.  
  127. if (Board[1] == Board[4] && Board[4] == Board[7]) {
  128. return charToWinner(Board[1]);
  129. }
  130. if (Board[2] == Board[5] && Board[5] == Board[8]) {
  131. return charToWinner(Board[2]);
  132. }
  133.  
  134. if (Board[0] == Board[4] && Board[4] == Board[8]) {
  135. return charToWinner(Board[0]);
  136. }
  137.  
  138. if (Board[2] == Board[4] && Board[4] == Board[6]) {
  139. return charToWinner(Board[2]);
  140. }
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement