Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. #pragma region
  5. void print(string text);
  6. void print(string text, int num);
  7. void print(string text, int num, string text2);
  8. void printBoard(int arr[][3], int sizeX, int sizeY);
  9. void drawBoard(char arr[][3]);
  10. void start(char ans);
  11. bool logic(int _input, int logicBoard[][3], int _pl, int _round);
  12. void boardManager(int type);
  13. void visualMng(int _input, char _board[][3], char _pl, int _round);
  14. #pragma endregion
  15. int main() {
  16. char answer;
  17. print("Do you wish to start the game ? y/n ");
  18. cin >> answer;
  19. system("cls");
  20. start(answer);
  21.  
  22.  
  23.  
  24.  
  25. cin.get();
  26. cin.get();
  27. }
  28.  
  29. #pragma region
  30. void print(string text)
  31. {
  32. cout << text << endl;
  33. }
  34. void print(string text, int num)
  35. {
  36. cout << text << " " << num << endl;
  37. }
  38. void print(string text, int num, string text2)
  39. {
  40. cout << text << " " << num << " " << text2 << endl;
  41. }
  42. void printBoard(int arr[][3], int sizeX, int sizeY)
  43. {
  44. for (int y = 0; y < sizeY; y++)
  45. {
  46. for (int x = 0; x < sizeX; x++)
  47. {
  48. cout << arr[x][y] << " ";
  49. }
  50. cout << endl;
  51. }
  52. }
  53. void drawBoard(char arr[][3]) {
  54. print(" | | ");
  55. cout << " " << arr[0][0] << " | " << arr[1][0] << " | " << arr[2][0] << endl;
  56. print("_____|_____|_____");
  57. print(" | | ");
  58. cout << " " << arr[0][1] << " | " << arr[1][1] << " | " << arr[2][1] << endl;
  59. print("_____|_____|_____");
  60. print(" | | ");
  61. cout << " " << arr[0][2] << " | " << arr[1][2] << " | " << arr[2][2] << endl;
  62. print(" | | ");
  63. }
  64. #pragma endregion
  65.  
  66. void start(char ans) {
  67. char answers;
  68. switch (ans)
  69. {
  70. case'y':
  71. print("Choose which side you want to play on : X / O");
  72. cin >> answers;
  73. if (answers == 'x' || answers == 'X')
  74. boardManager(1);
  75. else if (answers == 'o' || answers == 'O')
  76. boardManager(2);
  77. else
  78. print("Incorrect answer");
  79. break;
  80. case'n':
  81. print("Okay bb");
  82. break;
  83. default:
  84. print("Incorrect answer");
  85. break;
  86. }
  87.  
  88. }
  89.  
  90. bool logic(int _input, int logicBoard[][3], int _pl, int _round)
  91. {
  92. bool gameOn = true;
  93. int xmatcher;
  94. int ymatcher;
  95. int zmatcher;
  96.  
  97. switch (_input)
  98. {
  99. case 1:
  100. if (_round % 2 == 0)
  101. {
  102. if (logicBoard[0][2] != 1 || logicBoard[0][2] != 2)
  103. logicBoard[0][2] = 1;
  104. break;
  105. }
  106. else {
  107. if (logicBoard[0][2] != 1 || logicBoard[0][2] != 2)
  108. logicBoard[0][2] = 2;
  109. break;
  110. }
  111. case 2:
  112. if (_round % 2 == 0)
  113. {
  114. if (logicBoard[1][2] != 1 || logicBoard[1][2] != 2)
  115. logicBoard[1][2] = 1;
  116. break;
  117. }
  118. else {
  119. if (logicBoard[1][2] != 1 || logicBoard[1][2] != 2)
  120. logicBoard[1][2] = 2;
  121. break;
  122. }
  123. case 3:
  124. if (_round % 2 == 0)
  125. {
  126. if (logicBoard[2][2] != 1 || logicBoard[2][2] != 2)
  127. logicBoard[2][2] = 1;
  128. break;
  129. }
  130. else {
  131. if (logicBoard[2][2] != 1 || logicBoard[2][2] != 2)
  132. logicBoard[2][2] = 2;
  133. break;
  134. }
  135. case 4:
  136. if (_round % 2 == 0)
  137. {
  138. if (logicBoard[0][1] != 1 || logicBoard[0][1] != 2)
  139. logicBoard[0][1] = 1;
  140. break;
  141. }
  142. else {
  143. if (logicBoard[0][1] != 1 || logicBoard[0][1] != 2)
  144. logicBoard[0][1] = 2;
  145. break;
  146. }
  147. case 5:
  148. if (_round % 2 == 0)
  149. {
  150. if (logicBoard[1][1] != 1 || logicBoard[1][1] != 2)
  151. logicBoard[1][1] = 1;
  152. break;
  153. }
  154. else {
  155. if (logicBoard[1][1] != 1 || logicBoard[1][1] != 2)
  156. logicBoard[1][1] = 2;
  157. break;
  158. }
  159. case 6:
  160. if (_round % 2 == 0)
  161. {
  162. if (logicBoard[2][1] != 1 || logicBoard[2][1] != 2)
  163. logicBoard[2][1] = 1;
  164. break;
  165. }
  166. else {
  167. if (logicBoard[2][1] != 1 || logicBoard[2][1] != 2)
  168. logicBoard[2][1] = 2;
  169. break;
  170. }
  171. case 7:
  172. if (_round % 2 == 0)
  173. {
  174. if (logicBoard[0][0] != 1 || logicBoard[0][0] != 2)
  175. logicBoard[0][0] = 1;
  176. break;
  177. }
  178. else {
  179. if (logicBoard[0][0] != 1 || logicBoard[0][0] != 2)
  180. logicBoard[0][0] = 2;
  181. break;
  182. }
  183. case 8:
  184. if (_round % 2 == 0)
  185. {
  186. if (logicBoard[1][0] != 1 || logicBoard[1][0] != 2)
  187. logicBoard[1][0] = 1;
  188. break;
  189. }
  190. else {
  191. if (logicBoard[1][0] != 1 || logicBoard[1][0] != 2)
  192. logicBoard[1][0] = 2;
  193. break;
  194. }
  195. case 9:
  196. if (_round % 2 == 0)
  197. {
  198. if (logicBoard[2][0] != 1 || logicBoard[2][0] != 2)
  199. logicBoard[2][0] = 1;
  200. break;
  201. }
  202. else {
  203. if (logicBoard[2][0] != 1 || logicBoard[2][0] != 2)
  204. logicBoard[2][0] = 2;
  205. break;
  206. }
  207. default:
  208. break;
  209. }
  210.  
  211.  
  212. if (_round > 4)
  213. {
  214. if (_round % 2 == 0) {
  215. zmatcher = logicBoard[0][0] + logicBoard[1][1] + logicBoard[2][2];
  216. if (zmatcher < 3)
  217. zmatcher = 0;
  218. zmatcher = logicBoard[0][2] + logicBoard[1][1] + logicBoard[2][0];
  219. if (zmatcher < 3)
  220. zmatcher = 0;
  221. for (int y = 0; y < 3; y++)
  222. {
  223. ymatcher = logicBoard[0][y] + logicBoard[1][y] + logicBoard[2][y];
  224. }
  225. if (ymatcher < 3)
  226. ymatcher = 0;
  227. for (int x = 0; x < 3; x++)
  228. {
  229. xmatcher = logicBoard[x][0] + logicBoard[x][1] + logicBoard[x][2];
  230. }
  231. if (xmatcher < 3)
  232. xmatcher = 0;
  233.  
  234. if (xmatcher == 3 || ymatcher == 3 || zmatcher == 3) {
  235. print("Game Over, Player2 won the game.");
  236. gameOn = false;
  237. }
  238. }
  239. else {
  240. zmatcher = logicBoard[0][0] + logicBoard[1][1] + logicBoard[2][2];
  241. if (zmatcher < 6)
  242. zmatcher = 0;
  243. zmatcher = logicBoard[0][2] + logicBoard[1][1] + logicBoard[2][0];
  244. if (zmatcher < 6)
  245. zmatcher = 0;
  246. for (int y = 0; y < 3; y++)
  247. {
  248. ymatcher = logicBoard[0][y] + logicBoard[1][y] + logicBoard[2][y];
  249. }
  250. if (ymatcher < 6)
  251. ymatcher = 0;
  252. for (int x = 0; x < 3; x++)
  253. {
  254. xmatcher = logicBoard[x][0] + logicBoard[x][1] + logicBoard[x][2];
  255. }
  256. if (xmatcher < 6)
  257. xmatcher = 0;
  258.  
  259. if (xmatcher == 6 || ymatcher == 6 || zmatcher == 6) {
  260. print("Game Over, Player1 won the game.");
  261. gameOn = false;
  262. }
  263.  
  264. }
  265. }
  266. else if (_round == 9) {
  267. print("Game over, play again?");
  268. }
  269. return gameOn;
  270. }
  271.  
  272. void visualMng(int _input, char _board[][3],char _pl, int _round)
  273. {
  274. int endRound = 1;
  275. bool nextRound = true;
  276.  
  277. while (nextRound) {
  278. switch (_input)
  279. {
  280. case 1:
  281. if (_round > 1) {
  282. if (_board[0][2] != ' ')
  283. {
  284. print("Position already taken :) Enter different position: ");
  285. nextRound = false;
  286. }
  287. else {
  288. _board[0][2] = _pl;
  289. system("cls");
  290. endRound++;
  291. }
  292. }
  293. else {
  294. _board[0][2] = _pl;
  295. system("cls");
  296. endRound++;
  297. }
  298. break;
  299. case 2:
  300. if (_round > 1) {
  301. if (_board[1][2] != ' ')
  302. {
  303. print("Position already taken :) Enter different position: ");
  304. nextRound = false;
  305. }
  306. else {
  307. _board[1][2] = _pl;
  308. system("cls");
  309. endRound++;
  310. }
  311. }
  312. else {
  313. _board[1][2] = _pl;
  314. system("cls");
  315. endRound++;
  316. }
  317. break;
  318. case 3:
  319. if (_round > 1) {
  320. if (_board[2][2] != ' ')
  321. {
  322. print("Position already taken :) Enter different position: ");
  323. nextRound = false;
  324. }
  325. else {
  326. _board[2][2] = _pl;
  327. system("cls");
  328. endRound++;
  329. }
  330. }
  331. else {
  332. _board[2][2] = _pl;
  333. system("cls");
  334. endRound++;
  335. }
  336. break;
  337. case 4:
  338. if (_round > 1) {
  339. if (_board[0][1] != ' ')
  340. {
  341. print("Position already taken :) Enter different position: ");
  342. nextRound = false;
  343. }
  344. else {
  345. _board[0][1] = _pl;
  346. system("cls");
  347.  
  348. endRound++;
  349. }
  350. }
  351. else {
  352. _board[0][1] = _pl;
  353. system("cls");
  354.  
  355. endRound++;
  356. }
  357. break;
  358. case 5:
  359. if (_round > 1) {
  360. if (_board[1][1] != ' ')
  361. {
  362. print("Position already taken :) Enter different position: ");
  363. nextRound = false;
  364. }
  365. else {
  366. _board[1][1] = _pl;
  367. system("cls");
  368.  
  369. endRound++;
  370. }
  371. }
  372. else {
  373. _board[1][1] = _pl;
  374. system("cls");
  375.  
  376. endRound++;
  377. }
  378. break;
  379. case 6:
  380. if (_round > 1) {
  381. if (_board[2][1] != ' ')
  382. {
  383. print("Position already taken :) Enter different position: ");
  384. nextRound = false;
  385. }
  386. else {
  387. _board[2][1] = _pl;
  388. system("cls");
  389.  
  390. endRound++;
  391. }
  392. }
  393. else {
  394. _board[2][1] = _pl;
  395. system("cls");
  396.  
  397. endRound++;
  398. }
  399. break;
  400. case 7:
  401. if (_round > 1) {
  402. if (_board[0][0] != ' ')
  403. {
  404. print("Position already taken :) Enter different position: ");
  405. nextRound = false;
  406. }
  407. else {
  408. _board[0][0] = _pl;
  409. system("cls");
  410. endRound++;
  411. }
  412. }
  413. else {
  414. _board[0][0] = _pl;
  415. system("cls");
  416.  
  417. endRound++;
  418. }
  419. break;
  420. case 8:
  421. if (_round > 1) {
  422. if (_board[1][0] != ' ')
  423. {
  424. print("Position already taken :) Enter different position: ");
  425. nextRound = false;
  426. }
  427. else {
  428. _board[1][0] = _pl;
  429. system("cls");
  430.  
  431. endRound++;
  432. }
  433. }
  434. else {
  435. _board[1][0] = _pl;
  436. system("cls");
  437.  
  438. endRound++;
  439. }
  440. break;
  441. case 9:
  442. if (_round > 1) {
  443. if (_board[2][0] != ' ')
  444. {
  445. print("Position already taken :) Enter different position: ");
  446. nextRound = false;
  447. }
  448. else {
  449. _board[2][0] = _pl;
  450. system("cls");
  451.  
  452. endRound++;
  453. }
  454. }
  455. else {
  456. _board[2][0] = _pl;
  457. system("cls");
  458.  
  459. endRound++;
  460. }
  461. break;
  462.  
  463. default:
  464. break;
  465. }
  466. if (nextRound = false)
  467. {
  468. cin >> _input;
  469. }
  470. }
  471. }
  472.  
  473. void boardManager(int type) {
  474. char visualBoard[3][3];
  475. int logicBoard[3][3];
  476. char p1;
  477. char p2;
  478. int logicalP1;
  479. int logicalP2;
  480. int input;
  481. int round = 0;
  482. bool gameState = true;
  483. //creates visual board
  484. for (int x = 0; x < 3; x++)
  485. {
  486. for (int y = 0; y < 3; y++)
  487. {
  488. visualBoard[x][y] = ' ';
  489. }
  490. }
  491. //creates logical board
  492. for (int x = 0; x < 3; x++)
  493. {
  494. for (int y = 0; y < 3; y++)
  495. {
  496. logicBoard[x][y] = 0;
  497. }
  498. }
  499.  
  500. //chooses player side
  501. switch (type)
  502. {
  503. case 1:
  504. print("Player 1 is X you may begin to play now.");
  505. p1 = 'X';
  506. p2 = 'O';
  507. logicalP1 = 1;
  508. logicalP2 = 2;
  509. break;
  510. case 2:
  511. print("Player 1 is O you may begin to play now.");
  512. p1 = 'O';
  513. p2 = 'X';
  514. logicalP1 = 2;
  515. logicalP2 = 1;
  516. break;
  517. default:
  518. break;
  519. }
  520.  
  521. system("cls");
  522. drawBoard(visualBoard);
  523. print("Use numpad on your keyboard to play the game, when you choose your destination press enter.");
  524. while (gameState)
  525. {
  526.  
  527. cin >> input;
  528. if (round % 2 == 0) {
  529. print("Player 1's turn:");
  530. visualMng(input, visualBoard, p1, round);
  531. logic(input, logicBoard, logicalP1 , round);
  532. }
  533. else {
  534. print("Player 2's turn");
  535. visualMng(input, visualBoard, p2, round);
  536. logic(input, logicBoard, logicalP2, round);
  537. }
  538. drawBoard(visualBoard);
  539. print("Use numpad on your keyboard to play the game, when you choose your destination press enter.");
  540. round++;
  541. gameState = logic(input, logicBoard, p1, round);
  542. }
  543. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement