Advertisement
DarkMaster007

Noob Tic-Tac-Toe Code

Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <time.h>
  6. using namespace std;
  7.  
  8. int aivall, aivaln;
  9. char p1 = ' ', p2 = ' ', p3 = ' ', p4 = ' ', p5 = ' ', p6 = ' ', p7 = ' ', p8 =' ', p9 = ' ', l;
  10. int n, endg = 0, again;
  11.  
  12. int ai(int a, int b) {
  13. if (a == 1) {
  14. switch (b) {
  15. case 1: {if (p1 == 88 || p1 == 79) {again = 1;} else {p1 = 79;}} break;
  16. case 2: {if (p2 == 88 || p2 == 79) {again = 1;} else {p2 = 79;}} break;
  17. case 3: {if (p3 == 88 || p3 == 79) {again = 1;} else {p3 = 79;}} break;
  18. }}
  19. if (a == 2) {
  20. switch (b) {
  21. case 1: {if (p4 == 88 || p4 == 79) {again = 1;} else {p4 = 79;}} break;
  22. case 2: {if (p5 == 88 || p5 == 79) {again = 1;} else {p5 = 79;}} break;
  23. case 3: {if (p6 == 88 || p6 == 79) {again = 1;} else {p6 = 79;}} break;
  24. }}
  25. if (a == 3) {
  26. switch (b) {
  27. case 1: {if (p7 == 88 || p7 == 79) {again = 1;} else {p7 = 79;}} break;
  28. case 2: {if (p8 == 88 || p8 == 79) {again = 1;} else {p8 = 79;}} break;
  29. case 3: {if (p9 == 88 || p9 == 79) {again = 1;} else {p9 = 79;}} break;
  30. }}
  31.  
  32. }
  33.  
  34. char xno(char p1, char p2, char p3, char p4, char p5, char p6, char p7, char p8, char p9) {
  35. cout << " 1 2 3 " << endl;
  36. cout << endl;
  37. cout << " A. " << p1 << " " << p2 << " " << p3 << endl;
  38. cout << endl;
  39. cout << " B. " << p4 << " " << p5 << " " << p6 << endl;
  40. cout << endl;
  41. cout << " C. " << p7 << " " << p8 << " " << p9 << endl;
  42. cout << endl;}
  43.  
  44. int triplecheckx() {
  45. //vertical
  46. if (p1 == 88 && p4 == 88 && p7 == 88) {
  47. endg = 1;
  48. system("cls");
  49. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  50. cout << "Player Won!!!";
  51. }
  52. if (p2 == 88 && p5 == 88 && p8 == 88) {
  53. endg = 1;
  54. system("cls");
  55. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  56. cout << "Player Won!!!";
  57. }
  58. if (p3 == 88 && p6 == 88 && p9 == 88) {
  59. endg = 1;
  60. system("cls");
  61. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  62. cout << "Player Won!!!";
  63. }
  64.  
  65. //Horizontal
  66. if (p1 == 88 && p2 == 88 && p3 == 88) {
  67. endg = 1;
  68. system("cls");
  69. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  70. cout << "Player Won!!!";
  71. }
  72. if (p4 == 88 && p5 == 88 && p6 == 88) {
  73. endg = 1;
  74. system("cls");
  75. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  76. cout << "Player Won!!!";
  77. }
  78. if (p7 == 88 && p8 == 88 && p9 == 88) {
  79. endg = 1;
  80. system("cls");
  81. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  82. cout << "Player Won!!!";
  83. }
  84.  
  85. //Cross
  86. if (p1 == 88 && p5 == 88 && p9 == 88) {
  87. endg = 1;
  88. system("cls");
  89. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  90. cout << "Player Won!!!";
  91. }
  92. if (p3 == 88 && p5 == 88 && p7 == 88) {
  93. endg = 1;
  94. system("cls");
  95. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  96. cout << "Player Won!!!";
  97. }
  98. }
  99.  
  100. int triplechecko() {
  101. //vertical
  102. if (p1 == 79 && p4 == 79 && p7 == 79) {
  103. endg = 1;
  104. system("cls");
  105. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  106. cout << "AI Won!!!";
  107. }
  108. if (p2 == 79 && p5 == 79 && p8 == 79) {
  109. endg = 1;
  110. system("cls");
  111. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  112. cout << "AI Won!!!";
  113. }
  114. if (p3 == 79 && p6 == 79 && p9 == 79) {
  115. endg = 1;
  116. system("cls");
  117. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  118. cout << "AI Won!!!";
  119. }
  120.  
  121. //Horizontal
  122. if (p1 == 79 && p2 == 79 && p3 == 79) {
  123. endg = 1;
  124. system("cls");
  125. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  126. cout << "AI Won!!!";
  127. }
  128. if (p4 == 79 && p5 == 79 && p6 == 79) {
  129. endg = 1;
  130. system("cls");
  131. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  132. cout << "AI Won!!!";
  133. }
  134. if (p7 == 79 && p8 == 79 && p9 == 79) {
  135. endg = 1;
  136. system("cls");
  137. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  138. cout << "AI Won!!!";
  139. }
  140.  
  141. //Cross
  142. if (p1 == 79 && p5 == 79 && p9 == 79) {
  143. endg = 1;
  144. system("cls");
  145. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  146. cout << "AI Won!!!";
  147. }
  148. if (p3 == 79 && p5 == 79 && p7 == 79) {
  149. endg = 1;
  150. system("cls");
  151. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  152. cout << "AI Won!!!";
  153. }
  154. }
  155.  
  156. int checkalltie(char p1,char p2,char p3,char p4,char p5,char p6,char p7,char p8,char p9){
  157. if(p1 != 32 && p2 != 32 && p3 != 32 && p4 != 32 && p5 != 32 && p6 != 32 && p7 != 32 && p8 != 32 && p9 != 32 ) {
  158. endg = 1;
  159. system("cls");
  160. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  161. cout<<"TIE!!!";}
  162. }
  163.  
  164. int numberpick() {
  165. if (l == 'A') {
  166. switch (n) {
  167. case 1:
  168. p1 = 88;
  169. break;
  170. case 2:
  171. p2 = 88;
  172. break;
  173. case 3:
  174. p3 = 88;
  175. break;
  176. }
  177. }
  178. if (l == 'B') {
  179. switch (n) {
  180. case 1:
  181. p4 = 88;
  182. break;
  183. case 2:
  184. p5 = 88;
  185. break;
  186. case 3:
  187. p6 = 88;
  188. break;
  189. }
  190. }
  191. if (l == 'C') {
  192. switch (n) {
  193. case 1:
  194. p7 = 88;
  195. break;
  196. case 2:
  197. p8 = 88;
  198. break;
  199. case 3:
  200. p9 = 88;
  201. }
  202. }
  203. }
  204.  
  205. int main() {
  206. ifendg1:
  207. checkalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  208.  
  209. while (endg == 0) {
  210. srand(time(0));
  211. xno(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  212. cout << "Enter the position: ";
  213. cin >> l >> n;
  214.  
  215. switch (l) {
  216. case 'A':
  217. numberpick();
  218. case 'B':
  219. numberpick();
  220. case 'C':
  221. numberpick();
  222. }
  223.  
  224. checkalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  225. triplecheckx();
  226. if(endg==1) goto ifendg1;
  227.  
  228. repeat:
  229.  
  230. again = 0;
  231. aivall = (rand() % 3) + 1;
  232. aivaln = (rand() % 3) + 1;
  233. ai(aivall, aivaln);
  234. triplechecko();
  235. if(endg==1) goto ifendg1;
  236. if (again == 1) {
  237. goto repeat;
  238. }
  239. dcheckalltie(p1,p2,p3,p4,p5,p6,p7,p8,p9);
  240. system("cls");
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement