fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6.  
  7. void Print_now (std::vector<std::vector<char>>& square)
  8. {
  9. for (int i = 0; i != 3; ++i)
  10. {
  11. for (int j = 0; j != 3; ++j)
  12. {
  13. std::cout << square[i][j] << " ";
  14. }
  15. std::cout << "\n";
  16. }
  17. }
  18.  
  19.  
  20.  
  21. bool draw (std::vector<std::vector<bool>>& is_there)
  22. {
  23. for (int i = 0; i != 3; ++i)
  24. {
  25. for (int j = 0; j != 3; ++j)
  26. {
  27. if (!is_there[i][j])
  28. {
  29. return false;
  30. }
  31. }
  32. }
  33. return true;
  34. }
  35.  
  36. bool Vruyr_wins (std::vector<std::vector<char>>& square)
  37. {
  38. int count = 0;
  39. for (int i = 0; i != 3; ++i)
  40. {
  41. for (int j = 0; j != 3; ++j)
  42. {
  43. if (square[i][j] == 'O')
  44. {
  45. ++count;
  46. }
  47. }
  48. if (count == 3)
  49. {
  50. return true;
  51. }
  52. }
  53. count = 0;
  54. for (int i = 0; i != 3; ++i)
  55. {
  56. for (int j = 0; j != 3; ++j)
  57. {
  58. if (square[j][i] == 'O')
  59. {
  60. ++count;
  61. }
  62. }
  63. if (count == 3)
  64. {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70.  
  71.  
  72. bool Player_wins (std::vector<std::vector<char>>& square)
  73. {
  74. int count = 0;
  75. for (int i = 0; i != 3; ++i)
  76. {
  77. for (int j = 0; j != 3; ++j)
  78. {
  79. if (square[i][j] == 'X')
  80. {
  81. ++count;
  82. }
  83. }
  84. if (count == 3)
  85. {
  86. return true;
  87. }
  88. }
  89. count = 0;
  90. for (int i = 0; i != 3; ++i)
  91. {
  92. for (int j = 0; j != 3; ++j)
  93. {
  94. if (square[j][i] == 'X')
  95. {
  96. ++count;
  97. }
  98. }
  99. if (count == 3)
  100. {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. bool there_is_possibility_to_lose (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
  112. {
  113. int count = 0;
  114. for (int i = 0; i != 3; ++i)
  115. {
  116. for (int j = 0; j != 3; ++j)
  117. {
  118. if (square[i][j] == 'X')
  119. {
  120. ++count;
  121. }
  122. }
  123. if(count == 2)
  124. {
  125. for(int j = 0; j != 3; ++j)
  126. {
  127. if ((square[i][j] != 'X') && (square[i][j] != 'O'))
  128. {
  129. square[i][j] = 'O';
  130. return true;
  131. }
  132. }
  133. }
  134. }
  135. count = 0;
  136.  
  137. for (int i = 0; i != 3; ++i)
  138. {
  139. for (int j = 0; j != 3; ++j)
  140. {
  141. if (square[j][i] == 'X')
  142. {
  143. ++count;
  144. }
  145. }
  146. if(count == 2)
  147. {
  148. for(int j = 0; j != 3; ++j)
  149. {
  150. if ((square[j][i] != 'X') && (square[j][i] != 'O'))
  151. {
  152. square[j][i] = 'O';
  153. return true;
  154. }
  155. }
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. void Vruyrs_turn (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
  166. {
  167. if (there_is_possibility_to_lose(square, is_there))
  168. {
  169. return;
  170. }
  171. int com = rand()%9;
  172. if (!is_there[(com-1)/3][(com-1)%3])
  173. {
  174. is_there[(com-1)/3][(com-1)%3] = true;
  175. square[(com-1)/3][(com-1)%3] = 'O';
  176. return;
  177. }
  178. else
  179. {
  180. Vruyrs_turn(square, is_there);
  181. }
  182. }
  183.  
  184.  
  185. void Players_turn (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
  186. {
  187. int com;
  188. std::cin >> com;
  189. square[(com-1)/3][(com-1)%3] = 'X';
  190. is_there[(com-1)/3][(com-1)%3] = true;
  191. }
  192.  
  193.  
  194. void start (std::vector<std::vector<char>>& square, std::vector<std::vector<bool>>& is_there)
  195. {
  196. if(draw(is_there))
  197. {
  198. std::cout << "It's Draw, try one more time";
  199. return;
  200. }
  201. std::cout << "It's Your turn" << "\n";
  202. Players_turn (square, is_there);
  203. Print_now (square);
  204. if(Player_wins(square))
  205. {
  206. std::cout << "You WIN!!!!";
  207. return;
  208. }
  209. std::cout << "Vruyr's turn" << "\n";
  210. Vruyrs_turn (square, is_there);
  211. Print_now (square);
  212. if(Vruyr_wins(square))
  213. {
  214. std::cout << "You Lose";
  215. return;
  216. }
  217. start (square, is_there);
  218. }
  219.  
  220. int main()
  221. {
  222. srand(time(nullptr));
  223. std::vector<std::vector<char>> square (3, std::vector<char>(3, '*'));
  224. std::vector<std::vector<bool>> is_there (3, std::vector<bool>(3, false));
  225. std::cout << "Hi, You are playing with Vruyr" << "\n";
  226. start (square, is_there);
  227. }
  228.  
Runtime error #stdin #stdout 0s 4536KB
stdin
4
stdout
Hi, You are playing with Vruyr
It's Your turn