Advertisement
Guest User

Untitled

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