Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.56 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. char square[9], squareSave[9];
  8. char player, ai;
  9. int winner = 0, winnerSave = 0, currentStep, aiPick = -1, chosen[9];
  10.  
  11. void clrscr() {
  12. system("@cls||clear");
  13. }
  14.  
  15.  
  16. void outPut() {
  17. int line = 0;
  18. printf("\n------------------------------------------------\n");
  19. for (int i = 0; i != 9; i++) {
  20. if (i / 3 != line) {
  21. printf("\n\n");
  22. line++;
  23. }
  24. printf("| %c |", square[i]);
  25. if ((i + 1) % 3 == 0) {
  26. printf(" |(%c)|(%c)|(%c)|", line * 3 + 1 + '0', line * 3 + 2 + '0', line * 3 + 3 + '0');
  27. }
  28. }
  29. printf("\n------------------------------------------------\n");
  30. }
  31.  
  32. void outPutSave() {
  33. int line = 0;
  34. printf("\n------------------------------------------------\n");
  35. for (int i = 0; i != 9; i++) {
  36. if (i / 3 != line) {
  37. printf("\n\n");
  38. line++;
  39. }
  40. printf("| %c |", squareSave[i]);
  41. }
  42. printf("\n------------------------------------------------\n");
  43. }
  44. void set0() {
  45. for (int i = 0; i != 9; i++) {
  46. square[i] = '.';
  47. chosen[i] = 9;
  48. }
  49. }
  50.  
  51. void set0Save() {
  52. for (int i = 0; i != 9; i++) {
  53. squareSave[i] = '.';
  54. }
  55. }
  56.  
  57. void save() {
  58. for (int i = 0; i != 9; i++) {
  59. squareSave[i] = square[i];
  60. }
  61. }
  62.  
  63. void load() {
  64. for (int i = 0; i != 9; i++) {
  65. square[i] = squareSave[i];
  66. }
  67. }
  68.  
  69. void playerStep() {
  70. int playerPick = -1, checker = 0;
  71. char playerPickChar[1];
  72. printf("\nPick a free square(1-9) to place %c: ", player);
  73. do {
  74. int res;
  75. do {
  76. res = scanf("%d", &playerPick);
  77. while (getchar() != '\n');
  78. if (res != 1) printf("\nError. Pick a free square(1-9) to place %c: ", player);
  79. } while (res != 1);
  80. if (playerPick < 1 || playerPick > 9 || square[playerPick - 1] != '.') {
  81. printf("\nError. Pick a free square(1-9) to place %c: ", player);
  82. }
  83. else checker = 1;
  84. } while (checker == 0);
  85. square[playerPick - 1] = player;
  86. chosen[currentStep - 1] = playerPick - 1;
  87. }
  88.  
  89. void playerChoise() {
  90. int playerD = -1, playerDReserv;
  91. char playerDChar;
  92. clrscr();
  93. printf("\nChose your fighter(X = 1 | O = 0): ");
  94. int checker = 0;
  95. do {
  96. int res;
  97. do {
  98. res = scanf("%d", &playerD);
  99. while (getchar() != '\n');
  100. if (res != 1) {
  101. clrscr();
  102. printf("\nChose your fighter(X = 1 | O = 0): ");
  103. }
  104. } while (res != 1);
  105. if (playerD == 0 || playerD == 1) {
  106. checker = 1;
  107. break;
  108. }
  109. else {
  110. clrscr();
  111. printf("\nChose your fighter(X = 1 | O = 0): ");
  112. }
  113. } while (checker == 0);
  114. if (playerD == 1) {
  115. player = 'X';
  116. ai = 'O';
  117. }
  118. else {
  119. player = 'O';
  120. ai = 'X';
  121. }
  122. return;
  123. }
  124.  
  125. void winCheck() {
  126. winner = 0;
  127. if (square[0] == square[1] && square[1] == square[2]) {
  128. if (square[0] == 'X') winner = 1;
  129. }
  130. if (square[3] == square[4] && square[4] == square[5]) {
  131. if (square[3] == 'X') winner = 1;
  132. }
  133. if (square[6] == square[7] && square[7] == square[8]) {
  134. if (square[6] == 'X') winner = 1;
  135. }
  136. if (square[0] == square[3] && square[3] == square[6]) {
  137. if (square[0] == 'X') winner = 1;
  138. }
  139. if (square[1] == square[4] && square[4] == square[7]) {
  140. if (square[1] == 'X') winner = 1;
  141. }
  142. if (square[2] == square[5] && square[5] == square[8]) {
  143. if (square[2] == 'X') winner = 1;
  144. }
  145. if (square[0] == square[4] && square[4] == square[8]) {
  146. if (square[0] == 'X') winner = 1;
  147. }
  148. if (square[2] == square[4] && square[4] == square[6]) {
  149. if (square[2] == 'X') winner = 1;
  150. }
  151. if (square[0] == square[1] && square[1] == square[2]) {
  152. if (square[0] == 'O') winner = 2;
  153. }
  154. if (square[3] == square[4] && square[4] == square[5]) {
  155. if (square[3] == 'O') winner = 2;
  156. }
  157. if (square[6] == square[7] && square[7] == square[8]) {
  158. if (square[6] == 'O') winner = 2;
  159. }
  160. if (square[0] == square[3] && square[3] == square[6]) {
  161. if (square[0] == 'O') winner = 2;
  162. }
  163. if (square[1] == square[4] && square[4] == square[7]) {
  164. if (square[1] == 'O') winner = 2;
  165. }
  166. if (square[2] == square[5] && square[5] == square[8]) {
  167. if (square[2] == 'O') winner = 2;
  168. }
  169. if (square[0] == square[4] && square[4] == square[8]) {
  170. if (square[0] == 'O') winner = 2;
  171. }
  172. if (square[2] == square[4] && square[4] == square[6]) {
  173. if (square[2] == 'O') winner = 2;
  174. }
  175. }
  176.  
  177. void winCheckSave() {
  178. winnerSave = 0;
  179. if (squareSave[0] == squareSave[1] && squareSave[1] == squareSave[2]) {
  180. if (squareSave[0] == 'X') winnerSave = 1;
  181. }
  182. if (squareSave[3] == squareSave[4] && squareSave[4] == squareSave[5]) {
  183. if (squareSave[3] == 'X') winnerSave = 1;
  184. }
  185. if (squareSave[6] == squareSave[7] && squareSave[7] == squareSave[8]) {
  186. if (squareSave[6] == 'X') winnerSave = 1;
  187. }
  188. if (squareSave[0] == squareSave[3] && squareSave[3] == squareSave[6]) {
  189. if (squareSave[0] == 'X') winnerSave = 1;
  190. }
  191. if (squareSave[1] == squareSave[4] && squareSave[4] == squareSave[7]) {
  192. if (squareSave[1] == 'X') winnerSave = 1;
  193. }
  194. if (squareSave[2] == squareSave[5] && squareSave[5] == squareSave[8]) {
  195. if (squareSave[2] == 'X') winnerSave = 1;
  196. }
  197. if (squareSave[0] == squareSave[4] && squareSave[4] == squareSave[8]) {
  198. if (squareSave[0] == 'X') winnerSave = 1;
  199. }
  200. if (squareSave[2] == squareSave[4] && squareSave[4] == squareSave[6]) {
  201. if (squareSave[2] == 'X') winnerSave = 1;
  202. }
  203. if (squareSave[0] == squareSave[1] && squareSave[1] == squareSave[2]) {
  204. if (squareSave[0] == 'O') winnerSave = 2;
  205. }
  206. if (squareSave[3] == squareSave[4] && squareSave[4] == squareSave[5]) {
  207. if (squareSave[3] == 'O') winnerSave = 2;
  208. }
  209. if (squareSave[6] == squareSave[7] && squareSave[7] == squareSave[8]) {
  210. if (squareSave[6] == 'O') winnerSave = 2;
  211. }
  212. if (squareSave[0] == squareSave[3] && squareSave[3] == squareSave[6]) {
  213. if (squareSave[0] == 'O') winnerSave = 2;
  214. }
  215. if (squareSave[1] == squareSave[4] && squareSave[4] == squareSave[7]) {
  216. if (squareSave[1] == 'O') winnerSave = 2;
  217. }
  218. if (squareSave[2] == squareSave[5] && squareSave[5] == squareSave[8]) {
  219. if (squareSave[2] == 'O') winnerSave = 2;
  220. }
  221. if (squareSave[0] == squareSave[4] && squareSave[4] == squareSave[8]) {
  222. if (squareSave[0] == 'O') winnerSave = 2;
  223. }
  224. if (squareSave[2] == squareSave[4] && squareSave[4] == squareSave[6]) {
  225. if (squareSave[2] == 'O') winnerSave = 2;
  226. }
  227. }
  228.  
  229. void aiStepX() {
  230. save();
  231. int squareScore[9], scoreBuffer, circleChecker, squareScoreMustWin[9], squareScoreMustLose[9];
  232. for (int i = 0; i != 9; i++) {
  233. squareScore[i] = 0;
  234. squareScoreMustWin[i] = 0;
  235. squareScoreMustLose[i] = 0;
  236. }
  237. for (int q = chosen[0] % 9; q != chosen[0] + 1; q++) {
  238. for (int w = chosen[1] % 9; w != chosen[1] + 1; w++) {
  239. for (int e = chosen[2] % 9; e != chosen[2] + 1; e++) {
  240. for (int r = chosen[3] % 9; r != chosen[3] + 1; r++) {
  241. for (int t = chosen[4] % 9; t != chosen[4] + 1; t++) {
  242. for (int y = chosen[5] % 9; y != chosen[5] + 1; y++) {
  243. for (int u = chosen[6] % 9; u != chosen[6] + 1; u++) {
  244. for (int i = chosen[7] % 9; i != chosen[7] + 1; i++) {
  245. for (int o = 0; o != 9; o++) {
  246. if (q != 10 && w != 10 && e != 10 && r != 10 && t != 10 && y != 10 && u != 10 && i != 10 && o != 10 && q != 9 && w != 9 && e != 9 && r != 9 && t != 9 && y != 9 && u != 9 && i != 9 && o != 9 &&
  247. w != q && e != q && r != q && t != q && y != q && u != q && i != q && o != q &&
  248. e != w && r != w && t != w && y != w && u != w && i != w && o != w &&
  249. r != e && t != e && y != e && u != e && i != e && o != e &&
  250. t != r && y != r && u != r && i != r && o != r &&
  251. y != t && u != t && i != t && o != t &&
  252. u != y && i != y && o != y &&
  253. i != u && o != u &&
  254. o != i) {
  255. set0Save();
  256. scoreBuffer = 0;
  257. circleChecker = 0;
  258. winnerSave = 0;
  259. if (squareSave[q] == '.') squareSave[q] = 'X'; else break;
  260. if (squareSave[w] == '.') squareSave[w] = 'O'; else break;
  261. if (squareSave[e] == '.') squareSave[e] = 'X'; else break;
  262. if (squareSave[r] == '.') squareSave[r] = 'O'; else break;
  263. if (squareSave[t] == '.') squareSave[t] = 'X'; else break;
  264. winCheckSave();
  265. if (winnerSave != 0) {
  266. circleChecker = 1;
  267. switch (winnerSave) {
  268. case 0: break;
  269. case 1: if (currentStep == 5) squareScoreMustWin[t]++; scoreBuffer++; break;
  270. default: break;
  271. }
  272. }
  273. if (squareSave[y] == '.') squareSave[y] = 'O'; else break;
  274. winCheckSave();
  275. if (winnerSave != 0 && circleChecker == 0) {
  276. circleChecker = 1;
  277. switch (winnerSave) {
  278. case 0: break;
  279. case 2: if (currentStep == 5) squareScoreMustLose[y]++; scoreBuffer--; break;
  280. default: break;
  281. }
  282. }
  283. if (squareSave[u] == '.') squareSave[u] = 'X'; else break;
  284. winCheckSave();
  285. if (winnerSave != 0 && circleChecker == 0) {
  286. switch (winnerSave) {
  287. case 0: break;
  288. case 1: if (currentStep == 7) squareScoreMustWin[u]++; scoreBuffer++; break;
  289. default: break;
  290. }
  291. }
  292. if (squareSave[i] == '.') squareSave[i] = 'O'; else break;
  293. winCheckSave();
  294. if (winnerSave != 0 && circleChecker == 0) {
  295. switch (winnerSave) {
  296. case 0: break;
  297. case 2: if (currentStep == 7) squareScoreMustLose[i]++; scoreBuffer--; break;
  298. default: break;
  299. }
  300. }
  301. if (squareSave[o] == '.') squareSave[o] = 'X'; else break;
  302. winCheckSave();
  303. if (winnerSave != 0 && circleChecker == 0) {
  304. circleChecker = 1;
  305. switch (winnerSave) {
  306. case 0: break;
  307. case 1: scoreBuffer++; break;
  308. default: break;
  309. }
  310. }
  311. switch (currentStep) {
  312. case 3: squareScore[e] = scoreBuffer + squareScore[e]; break;
  313. case 5: squareScore[t] = scoreBuffer + squareScore[t]; break;
  314. case 7: squareScore[u] = scoreBuffer + squareScore[u]; break;
  315. case 9: squareScore[o] = scoreBuffer + squareScore[o]; break;
  316. default: break;
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. int minScore = 999999;
  329. for (int i = 0; i != 9; i++) {
  330. if (square[i] == '.') {
  331. printf("\nScore for square[%d] score: %d | mustLose: %d | mustWin: %d", i + 1, squareScore[i], squareScoreMustLose[i], squareScoreMustWin[i]);
  332. }
  333. if (i == 0) {
  334. minScore = squareScore[i];
  335. }
  336. else {
  337. if (squareScore[i] < minScore) {
  338. minScore = squareScore[i];
  339. }
  340. }
  341. }
  342. for (int i = 0; i != 9; i++) {
  343. if (squareScore[i] > minScore) {
  344. if (square[i] == '.') {
  345. minScore = squareScore[i];
  346. aiPick = i;
  347. }
  348. }
  349. int randomCount = rand() % 2;
  350. if (squareScore[i] == minScore && randomCount == 1 && square[i] == '.') {
  351. if (square[i] == '.') {
  352. minScore = squareScore[i];
  353. aiPick = i;
  354. }
  355. }
  356. }
  357. int checkerMust = 0;
  358. for (int i = 0; i != 9; i++) {
  359. if (squareScoreMustWin[i] > 0 && square[i] == '.') {
  360. aiPick = i;
  361. checkerMust = 1;
  362. }
  363. }
  364. int countFree = 0;
  365. for (int i = 0; i != 9; i++) {
  366. if (square[i] == '.') {
  367. countFree++;
  368. }
  369. }
  370. if (countFree == 1) {
  371. for (int i = 0; i != 9; i++) {
  372. if (square[i] == '.') {
  373. aiPick = i;
  374. }
  375. }
  376. }
  377. printf("\nBot picked %d\n", aiPick + 1);
  378. square[aiPick] = ai;
  379. chosen[currentStep - 1] = aiPick;
  380. }
  381.  
  382. void aiStepO() {
  383. srand(time(NULL));
  384. save();
  385. int squareScore[9], scoreBuffer, circleChecker, squareScoreMustWin[9], squareScoreMustLose[9];
  386. for (int i = 0; i != 9; i++) {
  387. squareScore[i] = 0;
  388. squareScoreMustWin[i] = 0;
  389. squareScoreMustLose[i] = 0;
  390. }
  391. for (int q = chosen[0] % 9; q != chosen[0] + 1; q++) {
  392. for (int w = chosen[1] % 9; w != chosen[1] + 1; w++) {
  393. for (int e = chosen[2] % 9; e != chosen[2] + 1; e++) {
  394. for (int r = chosen[3] % 9; r != chosen[3] + 1; r++) {
  395. for (int t = chosen[4] % 9; t != chosen[4] + 1; t++) {
  396. for (int y = chosen[5] % 9; y != chosen[5] + 1; y++) {
  397. for (int u = chosen[6] % 9; u != chosen[6] + 1; u++) {
  398. for (int i = chosen[7] % 9; i != chosen[7] + 1; i++) {
  399. for (int o = 0; o != 9; o++) {
  400. if (q != 10 && w != 10 && e != 10 && r != 10 && t != 10 && y != 10 && u != 10 && i != 10 && o != 10 && q != 9 && w != 9 && e != 9 && r != 9 && t != 9 && y != 9 && u != 9 && i != 9 && o != 9 &&
  401. w != q && e != q && r != q && t != q && y != q && u != q && i != q && o != q &&
  402. e != w && r != w && t != w && y != w && u != w && i != w && o != w &&
  403. r != e && t != e && y != e && u != e && i != e && o != e &&
  404. t != r && y != r && u != r && i != r && o != r &&
  405. y != t && u != t && i != t && o != t &&
  406. u != y && i != y && o != y &&
  407. i != u && o != u &&
  408. o != i) {
  409. set0Save();
  410. scoreBuffer = 0;
  411. circleChecker = 0;
  412. winnerSave = 0;
  413. if (squareSave[q] == '.') squareSave[q] = 'X'; else break;
  414. if (squareSave[w] == '.') squareSave[w] = 'O'; else break;
  415. if (squareSave[e] == '.') squareSave[e] = 'X'; else break;
  416. if (squareSave[r] == '.') squareSave[r] = 'O'; else break;
  417. if (squareSave[t] == '.') squareSave[t] = 'X'; else break;
  418. winCheckSave();
  419. if (winnerSave != 0) {
  420. circleChecker = 1;
  421. switch (winnerSave) {
  422. case 0: break;
  423. case 1: if (currentStep == 4) squareScoreMustLose[t]++; scoreBuffer--; break;
  424. default: break;
  425. }
  426. }
  427. if (squareSave[y] == '.') squareSave[y] = 'O'; else break;
  428. winCheckSave();
  429. if (winnerSave != 0 && circleChecker == 0) {
  430. circleChecker = 1;
  431. switch (winnerSave) {
  432. case 0: break;
  433. case 2: if (currentStep == 6) squareScoreMustWin[y]++; scoreBuffer++; break;
  434. default: break;
  435. }
  436. }
  437. if (squareSave[u] == '.') squareSave[u] = 'X'; else break;
  438. winCheckSave();
  439. if (winnerSave != 0 && circleChecker == 0) {
  440. switch (winnerSave) {
  441. case 0: break;
  442. case 1: if (currentStep == 6) squareScoreMustLose[u]++; scoreBuffer--; break;
  443. default: break;
  444. }
  445. }
  446. if (squareSave[i] == '.') squareSave[i] = 'O'; else break;
  447. winCheckSave();
  448. if (winnerSave != 0 && circleChecker == 0) {
  449. switch (winnerSave) {
  450. case 0: break;
  451. case 2: if (currentStep == 8) squareScoreMustWin[i]++; scoreBuffer++; break;
  452. default: break;
  453. }
  454. }
  455. if (squareSave[o] == '.') squareSave[o] = 'X'; else break;
  456. winCheckSave();
  457. if (winnerSave != 0 && circleChecker == 0) {
  458. circleChecker = 1;
  459. switch (winnerSave) {
  460. case 0: break;
  461. case 1: if (currentStep == 8) squareScoreMustLose[o]++; scoreBuffer--; break;
  462. default: break;
  463. }
  464. }
  465. winCheckSave();
  466. switch (currentStep) {
  467. case 2: squareScore[w] = scoreBuffer + squareScore[w]; break;
  468. case 4: squareScore[r] = scoreBuffer + squareScore[r]; break;
  469. case 6: squareScore[y] = scoreBuffer + squareScore[y]; break;
  470. case 8: squareScore[i] = scoreBuffer + squareScore[i]; break;
  471. default: break;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483. int minScore = 999999;
  484. for (int i = 0; i != 9; i++) {
  485. if (square[i] == '.') {
  486. printf("\nScore for square[%d] score: %d | mustLose: %d | mustWin: %d", i + 1, squareScore[i], squareScoreMustLose[i], squareScoreMustWin[i]);
  487. }
  488. if (i == 0) {
  489. minScore = squareScore[i];
  490. }
  491. else {
  492. if (squareScore[i] < minScore) {
  493. minScore = squareScore[i];
  494. }
  495. }
  496. }
  497. for (int i = 0; i != 9; i++) {
  498. if (squareScore[i] > minScore && square[i] == '.') {
  499. if (square[i] == '.') {
  500. minScore = squareScore[i];
  501. aiPick = i;
  502. }
  503. }
  504. int randomCount = rand() % 2;
  505. if (squareScore[i] == minScore && randomCount == 1 && square[i] == '.') {
  506. if (square[i] == '.') {
  507. minScore = squareScore[i];
  508. aiPick = i;
  509. }
  510. }
  511. }
  512. int checkerMust = 0;
  513. for (int i = 0; i != 9; i++) {
  514. if (squareScoreMustWin[i] > 0 && square[i] == '.') {
  515. aiPick = i;
  516. checkerMust = 1;
  517. }
  518. }
  519. for (int i = 0; i != 9; i++) {
  520. if (squareScoreMustLose[i] > 0 && checkerMust == 0 && square[i] == '.') {
  521. aiPick = i;
  522. checkerMust = 1;
  523. }
  524. }
  525. if (currentStep == 2 && square[4] != '.') {
  526. int pickRand;
  527. pickRand = rand() % 4;
  528. if (pickRand == 0) aiPick = 0;
  529. if (pickRand == 1) aiPick = 2;
  530. if (pickRand == 2) aiPick = 6;
  531. if (pickRand == 3) aiPick = 8;
  532. }
  533. if (currentStep == 2) {
  534. if (square[0] != '.' || square[2] != '.' || square[6] != '.' || square[8] != '.') aiPick = 4;
  535. }
  536. if (currentStep == 4) {
  537. if (square[0] == 'X' && square[8] == 'X' || square[2] == 'X' && square[6] == 'X') {
  538. int pickRand;
  539. pickRand = rand() % 4;
  540. if (pickRand == 0) aiPick = 1;
  541. if (pickRand == 1) aiPick = 3;
  542. if (pickRand == 2) aiPick = 5;
  543. if (pickRand == 3) aiPick = 7;
  544. }
  545. }
  546. printf("\nBot picked %d\n", aiPick + 1);
  547. square[aiPick] = ai;
  548. chosen[currentStep - 1] = aiPick;
  549. }
  550.  
  551. int main() {
  552. int gameChecker = 1;
  553. do {
  554. playerChoise();
  555. set0();
  556. for (int q = 1; q != 10; q++) {
  557. currentStep = q;
  558. winCheck();
  559. if (winner != 0) break;
  560. if (player == 'X') {
  561. if (q % 2 == 1) {
  562. if (currentStep == 1) {
  563. outPut();
  564. playerStep();
  565. outPut();
  566. }
  567. else {
  568. playerStep();
  569. outPut();
  570. }
  571. }
  572. else {
  573. aiStepO();
  574. outPut();
  575. }
  576. }
  577. else {
  578. if (q % 2 == 1) {
  579. if (q == 1) {
  580. square[4] = 'X';
  581. chosen[0] = 4;
  582. outPut();
  583. }
  584. else {
  585. aiStepX();
  586. outPut();
  587. }
  588. }
  589. else {
  590. playerStep();
  591. }
  592. }
  593. }
  594. winCheck();
  595. if (winner == 0) printf("\nFinished as Draw!\n");
  596. else {
  597. if (winner == 1) {
  598. printf("\nFinished, X is winner!");
  599. }
  600. else {
  601. printf("\nFinished, O is winner!");
  602. }
  603. }
  604. int checkerG = 0;
  605. printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
  606. do {
  607. int res;
  608. do {
  609. res = scanf("%d", &gameChecker);
  610. while (getchar() != '\n');
  611. if (res != 1) {
  612. printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
  613. }
  614. } while (res != 1);
  615. if (gameChecker == 0 || gameChecker == 1) checkerG = 1;
  616. else {
  617. printf("\nDo you want to play again? 1 - YES; 0 - NO: ");
  618. }
  619. } while (checkerG == 0);
  620. } while (gameChecker != 0);
  621. return 0;
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement