Advertisement
Guest User

Untitled

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