Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char board[10] = {'0','1','2','3','4','5','6','7','8','9'};
  6. int win = 0;
  7. int c;
  8. int choice;
  9. int turn = 1;
  10.  
  11. void display_board(){
  12.  
  13. cout << "-------------" << endl;
  14.  
  15. cout << "|" << " " << board[1] << " " << "|" << " " << board[2] << " " << "|" << " " << board[3] << " " << "|" << endl;
  16. cout << "|---|---|---|" << endl;
  17.  
  18. cout << "|" << " " << board[4] << " " << "|" << " " << board[5] << " " << "|" << " " << board[6] << " " << "|" << endl;
  19. cout << "|---|---|---|" << endl;
  20.  
  21. cout << "|" << " " << board[7] << " " << "|" << " " << board[8] << " " << "|" << " " << board[9] << " " << "|" << endl;
  22.  
  23. cout << "-------------" << endl;
  24. }
  25.  
  26. bool check_winner(){
  27.  
  28. for (int i = 1; i <=3; i++){
  29. if(board[i] == board[i+1] && board[i+1] == board[i+2]) return true;
  30. }
  31. for (int i = 1; i <=3; i++){
  32. if(board[i] == board[i+3] && board[i+3] == board[i+6]) return true;
  33. }
  34. if (board[1] == board[5] && board[5] == board[9]) return true;
  35.  
  36. else if (board[3] == board[5] && board[5] == board[7]) return true;
  37.  
  38. else return false;
  39. }
  40.  
  41. void player_move(){
  42.  
  43. cout << "Podaj gdzie chcesz wykonac ruch: ";
  44. cin >> choice;
  45.  
  46. c = check_winner();
  47.  
  48. if(board[choice] == 'X' || board[choice] == 'O'){
  49. cout << "te miejsce jest juz zajete!" << endl;
  50. cout << "Podaj gdzie chcesz wykonac ruch: ";
  51. cin >> choice;
  52. }
  53.  
  54. else if (choice >0 && choice < 10/*choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6 || choice == 7 || choice == 8 || choice == 9*/){
  55. board[choice] = 'X';
  56. check_winner();
  57. display_board();
  58. }
  59. else{
  60. cout << "Podales bledne miejsce" << endl;
  61. }
  62. }
  63.  
  64. void body(int i){
  65. board[i] = 'O';
  66. cout << "AI wybral pole nr: " << i << endl;
  67. display_board();
  68. }
  69. void vertical(){
  70. int i = 0;
  71. while(i<9){
  72. string temp_str_h = "";
  73. temp_str_h = temp_str_h + board[i];
  74. temp_str_h = temp_str_h + board[i + 1];
  75. temp_str_h = temp_str_h + board[i + 2];
  76.  
  77. if(temp_str_h == "XX3") {
  78. body(3);
  79. }
  80. else if(temp_str_h == "X2X") {
  81. body(2);
  82. }
  83. else if(temp_str_h == "1XX") {
  84. body(1);
  85. }
  86. else if(temp_str_h == "XX6") {
  87. body(6);
  88. }
  89. else if(temp_str_h == "X5X") {
  90. body(5);
  91. }
  92. else if(temp_str_h == "4XX") {
  93. body(4);
  94. }
  95. else if(temp_str_h == "XX9") {
  96. body(9);
  97. }
  98. else if(temp_str_h == "X8X") {
  99. body(8);
  100. }
  101. else if(temp_str_h == "7XX") {
  102. body(7);
  103. }
  104. i++;
  105. }
  106. }
  107. void horizontal(){
  108. int i=0;
  109. while(i<9){
  110. string temp_str_h = "";
  111. temp_str_h = temp_str_h + board[i];
  112. temp_str_h = temp_str_h + board[i + 3];
  113. temp_str_h = temp_str_h + board[i + 6];
  114.  
  115. if(temp_str_h == "XX7") {
  116. body(7);
  117. }
  118. else if(temp_str_h == "X4X") {
  119. body(4);
  120. }
  121. else if(temp_str_h == "1XX") {
  122. body(1);
  123. }
  124. else if(temp_str_h == "XX8") {
  125. body(8);
  126. }
  127. else if(temp_str_h == "X5X") {
  128. body(5);
  129. }
  130. else if(temp_str_h == "2XX") {
  131. body(2);
  132. }
  133. else if(temp_str_h == "XX9") {
  134. body(9);
  135. }
  136. else if(temp_str_h == "X6X") {
  137. body(6);
  138. }
  139. else if(temp_str_h == "3XX") {
  140. body(3);
  141. }
  142. i++;
  143. }
  144. }
  145. void diagonal_rd(){
  146. string temp_str_h = "";
  147. temp_str_h = temp_str_h + board[1] + board[5] + board[9];
  148.  
  149. if(temp_str_h == "XX9") {
  150. body(9);
  151. }
  152. else if(temp_str_h == "X5X") {
  153. body(5);
  154. }
  155. else if(temp_str_h == "1XX") {
  156. body(1);
  157. }
  158. }
  159. void diagonal_ld(){
  160. string temp_str_h = "";
  161. temp_str_h = temp_str_h + board[3] + board[5] + board[7];
  162.  
  163. if(temp_str_h == "XX7") {
  164. body(7);
  165. }
  166. else if(temp_str_h == "X5X") {
  167. body(5);
  168. }
  169. else if(temp_str_h == "3XX") {
  170. body(3);
  171. }
  172. }
  173. void ai_move(){
  174.  
  175. vertical();
  176. horizontal();
  177. diagonal_rd();
  178. diagonal_ld();
  179.  
  180. }
  181.  
  182. void game(){
  183. srand( time( NULL ) );
  184.  
  185. display_board();
  186.  
  187. while(c == false){
  188.  
  189. if(check_winner() == true){
  190. cout << "KONIEC GRY";
  191. break;
  192. }
  193. player_move();
  194.  
  195. turn++;
  196. if(turn == 2){
  197. int ai_choice = (rand() % 9) + 1;
  198.  
  199. while(board[ai_choice - 1] == 'O'){
  200. if(board[ai_choice - 1] == 'X' || board[ai_choice - 1] == 'O'){
  201. ai_choice = (rand() % 9) + 1;
  202.  
  203. }
  204. }
  205. board[ai_choice] = 'O';
  206. cout << "AI wybral pole nr: " << ai_choice << endl;
  207. display_board();
  208. }
  209. if(turn % 2 == 1){
  210. ai_move();
  211. }
  212.  
  213. }
  214.  
  215. }
  216.  
  217. int main(){
  218.  
  219. game();
  220. return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement