Advertisement
szmelu

XO

May 11th, 2020
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void print(int board[][3]);
  6. void choice(int board[][3], char name[99]);
  7. void check(int board[][3], bool &isEnd);
  8.  
  9. char name1[99];
  10. char name2[99];
  11. int counter = 0;
  12.  
  13. int main()
  14. {
  15. cout << "GRA W KOLKO I KRZYZYK" << endl << endl;
  16. int board[3][3] = { 0 };
  17. int queue = 0;
  18. bool isEnd = false;
  19. cout << "Imie gracza 1: " << endl;
  20. cin.getline(name1, 99);
  21. cout << "Imie gracza 2: " << endl;
  22. cin.getline(name2, 99);
  23. cout << name1 << " " << name2 << endl;
  24.  
  25. while (!isEnd)
  26. {
  27. if (queue == 0)
  28. {
  29. choice(board, name1);
  30. print(board);
  31. check(board, isEnd);
  32. if (isEnd == true)
  33. {
  34. if (counter == 9)
  35. {
  36. cout << "REMIS" << endl;
  37. }
  38. else
  39. {
  40. cout << "Wygral gracz: " << name1 << endl;
  41. }
  42. }
  43. else
  44. queue++;
  45. }
  46. else if (queue == 1)
  47. {
  48. choice(board, name2);
  49. print(board);
  50. check(board, isEnd);
  51. if (isEnd == true)
  52. {
  53. if (counter == 9)
  54. {
  55. cout << "REMIS" << endl;
  56. }
  57. else
  58. {
  59. cout << "Wygral gracz: " << name2 << endl;
  60. }
  61. }
  62. else
  63. queue--;
  64. }
  65.  
  66. }
  67. system("pause");
  68. }
  69.  
  70. void print(int board[][3])
  71. {
  72. cout << " 0 1 2" << endl << endl;
  73. for (int i = 0; i < 3; i++)
  74. {
  75. cout << i << " ";
  76. for (int j = 0; j < 3; j++)
  77. {
  78. cout << "|";
  79. if (board[i][j] == 0) cout << "_";
  80. else if (board[i][j] == 1) cout << "0";
  81. else if (board[i][j] == 2) cout << "X";
  82. }
  83. cout << "|" << endl;
  84. }
  85. cout << counter << endl;
  86. }
  87.  
  88. void choice(int board[][3], char name[99])
  89. {
  90. int row;
  91. int column;
  92. while (true)
  93. {
  94. cout << "Gracz " << name << " wybiera wiersz: ";
  95. cin >> row;
  96. cout << "Gracz " << name << " wybiera kolumne: ";
  97. cin >> column;
  98. if (row < 3 && column < 3)
  99. {
  100. break;
  101. }
  102. else
  103. {
  104. cout << "Wiersz lub kolumna rozna od 0, 1 lub 2, wybierz ponownie!" << endl;
  105. }
  106. }
  107. if (board[row][column] == 0)
  108. {
  109. if(name == name1)
  110. {
  111. board[row][column] = 1;
  112. }
  113. else if (name == name2)
  114. {
  115. board[row][column] = 2;
  116. }
  117. counter++;
  118. }
  119. else
  120. {
  121. cout << "Pole zajete, sprobuj jeszcze raz!" << endl;
  122. choice(board, name);
  123. }
  124. }
  125.  
  126. void check(int board[][3], bool &isEnd)
  127. {
  128. if (counter == 9)
  129. {
  130. isEnd = true;
  131. return;
  132. }
  133. int count0 = 0;
  134. int countX = 0;
  135. for (int i = 0; i < 3; i++)
  136. {
  137. for (int j = 0; j < 3; j++)
  138. {
  139. if (board[i][j] == 1) count0++;
  140. else if (board[i][j] == 2) countX++;
  141. if (count0 == 3 || countX == 3)
  142. {
  143. isEnd = true;
  144. return;
  145. }
  146. }
  147. count0 = 0;
  148. countX = 0;
  149. }
  150.  
  151. for (int i = 0; i < 3; i++)
  152. {
  153. for (int j = 0; j < 3; j++)
  154. {
  155. if (board[j][i] == 1) count0++;
  156. else if (board[j][i] == 2) countX++;
  157. if (count0 == 3 || countX == 3)
  158. {
  159. isEnd = true;
  160. return;
  161. }
  162. }
  163. count0 = 0;
  164. countX = 0;
  165. }
  166. if ((board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) || (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1))
  167. {
  168. isEnd = true;
  169. return;
  170. }
  171. else if ((board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2) || (board[0][2] == 2 && board[1][1] == 2 && board[2][0] == 2))
  172. {
  173. isEnd = true;
  174. return;
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement