Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. int Welcome();
  7. int ModeChoice();
  8. int Putdata(short arr[], short i, short j, char smybol);
  9. int WritePlayerName(short gamemode, string players[]);
  10. int Multiplayer(short gamemode, string players[]);
  11. int GameTable(short arr[]);
  12. int Game();
  13. int PuskAtma();
  14. int ChoiceSymbol(char playersymbol[], short a, short gamemode);
  15. int CheckTable(short arr[], short playersirasi);
  16. int main()
  17. {
  18. Game();
  19. }
  20.  
  21.  
  22. int Game()
  23. {
  24. string players[2];
  25. Welcome();
  26.  
  27. short gamemode = ModeChoice();
  28.  
  29.  
  30. cout << "Mode: ";
  31. if (gamemode == 1)
  32. {
  33. cout << "Single player\n";
  34. }
  35. else if (gamemode == 2)
  36. {
  37. cout << "Two players\n";
  38. Multiplayer(gamemode, players);
  39. }
  40. else
  41. {
  42. assert("Error!");
  43. }
  44.  
  45.  
  46.  
  47.  
  48. return 0;
  49. }
  50.  
  51. int ChoiceSymbol(char playersymbol[], short a, short gamemode)
  52. {
  53. char symbol;
  54. if (gamemode == 1)
  55. {
  56. if (a == 1)
  57. {
  58. short random = PuskAtma();
  59. if (random == 0)
  60. {
  61. playersymbol[1] = 'X';
  62. }
  63. else
  64. {
  65. playersymbol[1] = 'O';
  66. }
  67.  
  68. if (playersymbol[1] || playersymbol[1] == 'x')
  69. {
  70. playersymbol[0] = 'O';
  71. }
  72. else
  73. {
  74. playersymbol[0] = 'X';
  75. }
  76. }
  77. else
  78. {
  79. cout << "X or O?\nPlayer1: ";
  80.  
  81. do
  82. {
  83. cin >> symbol;
  84. } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
  85. playersymbol[0] = symbol;
  86. if (playersymbol[0] == 'X' || playersymbol[0] == 'x')
  87. {
  88. playersymbol[1] = 'O';
  89. }
  90. else
  91. {
  92. playersymbol[1] = 'X';
  93. }
  94. }
  95. }
  96. else if (gamemode == 2)
  97. {
  98. if (a == 1)
  99. {
  100. cout << "X or O?\nPlayer2: ";
  101.  
  102. do
  103. {
  104. cin >> symbol;
  105. } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
  106. playersymbol[1] = symbol;
  107. if (playersymbol[1] == 'X' || playersymbol[1] == 'x')
  108. {
  109. playersymbol[0] = 'O';
  110. }
  111. else
  112. {
  113. playersymbol[0] = 'X';
  114. }
  115. }
  116. else
  117. {
  118. cout << "X or O?\nPlayer1: ";
  119.  
  120. do
  121. {
  122. cin >> symbol;
  123. } while (symbol != 'X' && symbol != 'x' && symbol != 'O' && symbol != 'o');
  124. playersymbol[0] = symbol;
  125. if (symbol == 'X' || symbol == 'x')
  126. {
  127. playersymbol[1] = 'O';
  128. }
  129. else
  130. {
  131. playersymbol[1] = 'X';
  132. }
  133. }
  134. }
  135. return 0;
  136. }
  137. int PuskAtma()
  138. {
  139. srand(time(NULL));
  140.  
  141. return rand() % 2;
  142. }
  143.  
  144. int Multiplayer(short gamemode, string players[])
  145. {
  146. enum symbol
  147. {
  148. X = 0, O
  149. };
  150. WritePlayerName(gamemode, players);
  151.  
  152. char playersymbol[2];
  153.  
  154. short oyunabaslayan = PuskAtma();
  155.  
  156. ChoiceSymbol(playersymbol, oyunabaslayan, gamemode);
  157.  
  158. const short size = 3;
  159. short table[3][3]{};
  160.  
  161.  
  162. short index = 0;
  163. short oyunsiras = oyunabaslayan;
  164. while (true)
  165. {
  166. GameTable(table[0]);
  167. cout << players[oyunsiras] << " >> ";
  168. cin >> index;
  169. table[0][index] = oyunsiras + 1;
  170. short status = CheckTable(table[0], oyunsiras + 1);
  171.  
  172. if (status == 0)
  173. {
  174. cout << "Winner Player is " << players[0] << endl;
  175. return 0;
  176. }
  177. else if (status == 1)
  178. {
  179. cout << "Winner Player is " << players[1] << endl;
  180. return 0;
  181. }
  182. }
  183.  
  184. return -1;
  185.  
  186. }
  187. int Welcome()
  188. {
  189. cout << "\t\t\tWelcome!\n";
  190. cout << "\tTic Tac Toe\n";
  191. cout << endl << endl;
  192.  
  193. cout << "Mode:\n";
  194. cout << "1. Single player\n";
  195. cout << "2. Two players\n";
  196. cout << endl;
  197.  
  198. return 0;
  199. }
  200.  
  201. int ModeChoice()
  202. {
  203. short choice = 0;
  204.  
  205.  
  206. do
  207. {
  208. cout << ">> ";
  209. cin >> choice;
  210. } while (choice < 1 || choice > 2);
  211.  
  212. return choice;
  213. }
  214. int Putdata(short arr[], short i, short j, char symbol)
  215. {
  216. return 0;
  217. }
  218.  
  219. int WritePlayerName(short gamemode, string players[])
  220. {
  221. string player1, player2 = "Computer";
  222. if (gamemode == 1)
  223. {
  224. cout << "Player name: ";
  225. cin >> player1;
  226. players[0] = player1;
  227. players[1] = player2;
  228. }
  229. else if (gamemode == 2)
  230. {
  231. cout << "1.Player name: ";
  232. cin >> player1;
  233. players[0] = player1;
  234. cout << "2.Player name: ";
  235. cin >> player2;
  236. players[1] = player2;
  237. }
  238. else
  239. {
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. int GameTable(short arr[])
  245. {
  246. const short size = 9;
  247.  
  248. for (short i = 0; i < size; i++)
  249. {
  250. cout << arr[i] << ' ';
  251.  
  252. if ((i + 1) % 3 == 0)
  253. {
  254. cout << endl;
  255. }
  256.  
  257. }
  258. return 0;
  259. }
  260.  
  261.  
  262. int CheckTable(short arr[], short playersirasi)
  263. {
  264. const short size = 9;
  265.  
  266. if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
  267. {
  268. return 1;
  269. }
  270. else if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
  271. {
  272. return 1;
  273. }
  274. else if (arr[0] == playersirasi && arr[1] == playersirasi && arr[2] == playersirasi)
  275. {
  276. return 1;
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement