Advertisement
Guest User

oldwar

a guest
Dec 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // wojna
  4. //
  5. // Created by Dawid Siarczyński on 23/11/2019.
  6. // Copyright © 2019 Dawid Siarczyński. All rights reserved.
  7.  
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <time.h>
  11. #include<string.h>
  12. #include <queue>
  13. #include <stdio.h>
  14. #define SIZE 52
  15. using namespace std;
  16.  
  17. struct Karta
  18. {
  19. char figura;
  20. char kolor;
  21. };
  22. //zmienne globalne
  23. Karta talia[SIZE];
  24. Karta player1[SIZE];
  25. Karta player2[SIZE];
  26. char const* const figura[13] =
  27. { "two of","three of","four of","five of","six of","seven of","eight of","nine of","ten of","jack of","queen of","King of","ace of" };
  28. char const* const kolor[4] = { "clubs","diamonds","hearts","spades" };
  29. int counter = 0;
  30. int decksize;
  31. int fsize;
  32. int add;
  33. int przebieg;
  34. queue <int> wplayer_1;
  35. queue <int> kplayer_1;
  36. queue <int> wplayer_2;
  37. queue <int> kplayer_2;
  38. queue <int> wwar_1;
  39. queue <int> wwar_2;
  40. queue <int> kwar_1;
  41. queue <int> kwar_2;
  42.  
  43.  
  44. void zlozTalie(Karta* newdeck, char const* const newFigura, char const* const newKolor)
  45. {
  46. for (int i = 0; i < decksize; i++)
  47. {
  48.  
  49. newdeck[i].figura = ((i % fsize) + add);
  50. newdeck[i].kolor = (i / fsize);
  51.  
  52. }
  53. }
  54. void tasuj(Karta* newdeck)
  55. {
  56. for (int i = 0; i < decksize; i++)
  57. {
  58. int j = rand() % decksize;
  59. Karta t = newdeck[i];
  60. newdeck[i] = newdeck[j];
  61. newdeck[j] = t;
  62. }
  63. }
  64.  
  65. void rozdaj(Karta* newdeck) {
  66. for (int i = 0; i < decksize; i++) {
  67. if (i % 2 == 0) {
  68. wplayer_1.push(newdeck[i].figura);
  69. kplayer_1.push(newdeck[i].kolor);
  70. }
  71. else {
  72. wplayer_2.push(newdeck[i].figura);
  73. kplayer_2.push(newdeck[i].kolor);
  74. }
  75. }
  76. };
  77. void licz() //do liczenia rangi
  78. {
  79. int i = 0;
  80. int* p1 = 0, * p2 = 0;
  81. while (i <= (SIZE / 2)) {
  82. if (wplayer_1.front() == 12)
  83. p1 += 25; // as=25
  84. if (wplayer_1.front() == 11)
  85. p1 += 20;
  86. if (wplayer_1.front() == 10)
  87. p1 += 15;
  88. if (wplayer_1.front() == 9)
  89. p1 += 10;
  90. wplayer_1.push(wplayer_1.front());
  91. wplayer_1.pop();
  92. if (wplayer_2.front() == 12)
  93. p2 += 25; // as=25
  94. if (wplayer_2.front() == 11)
  95. p2 += 20;
  96. if (wplayer_2.front() == 10)
  97. p2 += 15;
  98. if (wplayer_2.front() == 9)
  99. p2 += 10;
  100. wplayer_2.push(wplayer_1.front());
  101. wplayer_2.pop();
  102. i++;
  103. }
  104. cout << "player1;" << p1 << "range" << "player2;" << p2;
  105. }
  106.  
  107. void show() // pokazuje przebieg wojny
  108. {
  109. if (przebieg == 1) {
  110. cout << "PLAYER 1: " << figura[wplayer_1.front()] << " " << kolor[kplayer_1.front()] << " cards in hand: " << wplayer_1.size() - 1 << endl;
  111. cout << "PLAYER 2: " << figura[wplayer_2.front()] << " " << kolor[kplayer_2.front()] << " cards in hand: " << wplayer_2.size() - 1 << endl << endl;
  112. }
  113. }
  114. void win1() // wygrana gracza 1
  115. {
  116. wwar_1.push(wplayer_1.front());
  117. kwar_1.push(kplayer_1.front());
  118. wplayer_1.pop();
  119. kplayer_1.pop();
  120. while (!wwar_1.empty() && !kwar_1.empty()) {
  121. wplayer_1.push(wwar_1.front());
  122. kplayer_1.push(kwar_1.front());
  123. wwar_1.pop();
  124. kwar_1.pop();
  125. }
  126.  
  127. wwar_2.push(wplayer_2.front());
  128. kwar_2.push(kplayer_2.front());
  129. wplayer_2.pop();
  130. kplayer_2.pop();
  131. if (wplayer_2.empty()) {
  132. cout << "Player 1 won";
  133. }
  134.  
  135. while (!wwar_2.empty() && !kwar_2.empty()) {
  136. wplayer_1.push(wwar_2.front());
  137. kplayer_1.push(kwar_2.front());
  138. wwar_2.pop();
  139. kwar_2.pop();
  140. }
  141. counter++;
  142. }
  143.  
  144. void win2() // wygrana gracza 2
  145. {
  146. wwar_2.push(wplayer_2.front());
  147. kwar_2.push(kplayer_2.front());
  148. wplayer_2.pop();
  149. kplayer_2.pop();
  150.  
  151. while (!wwar_2.empty() && !kwar_2.empty()) {
  152. wplayer_2.push(wwar_2.front());
  153. kplayer_2.push(kwar_2.front());
  154. wwar_2.pop();
  155. kwar_2.pop();
  156. }
  157. wwar_1.push(wplayer_1.front());
  158. kwar_1.push(kplayer_1.front());
  159. wplayer_1.pop();
  160. kplayer_1.pop();
  161.  
  162. if (wplayer_1.empty())
  163. cout << "Player 2 won";
  164.  
  165. while (!wwar_1.empty() && !kwar_1.empty()) {
  166. wplayer_2.push(wwar_1.front());
  167. kplayer_2.push(kwar_1.front());
  168. wwar_1.pop();
  169. kwar_1.pop();
  170. }
  171. counter++;
  172. }
  173. void wojna() {
  174.  
  175. while (!wplayer_1.empty() && !wplayer_2.empty())
  176. {
  177. if (wplayer_1.front() > wplayer_2.front())
  178. {
  179. show();
  180. win1();
  181. }
  182. else if (wplayer_1.front() < wplayer_2.front())
  183. {
  184. show();
  185. win2();
  186. }
  187. else if (wplayer_1.front() == wplayer_2.front())
  188. {
  189. if (przebieg == 1)
  190. cout << " !!!!!!WAR!!!!!!" << endl;
  191. show();
  192. if (wplayer_1.size() <= 2) {
  193. cout << " Player 2 won";
  194. break;
  195. }
  196. else if (wplayer_2.size() <= 2) {
  197. cout << "Player 1 won";
  198. break;
  199. }
  200. else {
  201. wwar_2.push(wplayer_2.front());
  202. kwar_2.push(kplayer_2.front());
  203. wplayer_2.pop();
  204. kplayer_2.pop();
  205. wwar_1.push(wplayer_1.front());
  206. kwar_1.push(kplayer_1.front());
  207. wplayer_1.pop();
  208. kplayer_1.pop();
  209. show();
  210. }
  211. if (wplayer_1.empty())
  212. break;
  213. else if (wplayer_2.empty())
  214. break;
  215. else {
  216. wwar_2.push(wplayer_2.front());
  217. kwar_2.push(kplayer_2.front());
  218. wplayer_2.pop();
  219. kplayer_2.pop();
  220. wwar_1.push(wplayer_1.front());
  221. kwar_1.push(kplayer_1.front());
  222. wplayer_1.pop();
  223. kplayer_1.pop();
  224. }
  225. counter++;
  226. wojna();
  227. counter++;
  228. return;
  229. }
  230. }
  231.  
  232. cout << " moves counter: " << counter << endl;
  233. }
  234. void wojna_varB() //wojna tylko wariant B
  235. {
  236. while (!wplayer_1.empty() && !wplayer_2.empty())
  237. {
  238. if (wplayer_1.front() > wplayer_2.front())
  239. {
  240. show();
  241. win1();
  242.  
  243. }
  244. else if (wplayer_1.front() < wplayer_2.front())
  245. {
  246. show();
  247. win2();
  248.  
  249. }
  250. else if (wplayer_1.front() == wplayer_2.front())
  251. {
  252. if (przebieg == 1)
  253. cout << " !!!!!!WAR!!!!!!" << endl;
  254. show();
  255.  
  256. wwar_1.push(wplayer_1.front());
  257. kwar_1.push(kplayer_1.front());
  258. wplayer_1.pop();
  259. kplayer_1.pop();
  260. wwar_2.push(wplayer_2.front());
  261. kwar_2.push(kplayer_2.front());
  262. wplayer_2.pop();
  263. kplayer_2.pop();
  264. if (wplayer_1.empty() || wplayer_2.empty()) {
  265. if (wplayer_1.empty()) { // koncza sie 2 karty graczowi 1
  266. wwar_1.push(wplayer_2.front());
  267. kwar_1.push(kplayer_2.front());
  268. wplayer_2.pop();
  269. kplayer_2.pop();
  270. wplayer_1.push(wplayer_2.front());
  271. kplayer_1.push(kplayer_2.front());
  272. wplayer_2.pop();
  273. kplayer_2.pop(); // karta na stosie gracza nr 1
  274. show();
  275. }
  276. if (wplayer_2.empty()) { // koncza sie 2 karty graczowi 2
  277. wwar_2.push(wplayer_1.front());
  278. kwar_2.push(kplayer_1.front());
  279. wplayer_1.pop();
  280. kplayer_1.pop();
  281. wplayer_2.push(wplayer_1.front());
  282. kplayer_2.push(kplayer_1.front());
  283. wplayer_1.pop();
  284. kplayer_1.pop(); // karta na stosie gracza nr 2
  285. show();
  286. }
  287. }
  288. else {
  289. wwar_2.push(wplayer_2.front());
  290. kwar_2.push(kplayer_2.front());
  291. wplayer_2.pop();
  292. kplayer_2.pop();
  293. wwar_1.push(wplayer_1.front());
  294. kwar_1.push(kplayer_1.front());
  295. wplayer_1.pop();
  296. kplayer_1.pop();
  297. if (wplayer_1.empty()) { // konczy sie 1 karta graczowi 1
  298.  
  299. wplayer_1.push(wplayer_2.front());
  300. kplayer_1.push(kplayer_2.front());
  301. wplayer_2.pop();
  302. kplayer_2.pop(); // karta na stosie gracza nr 1
  303. show();
  304. }
  305. if (wplayer_2.empty()) { // konczy sie 1 karta graczowi 2
  306. wplayer_2.push(wplayer_1.front());
  307. kplayer_2.push(kplayer_1.front());
  308. wplayer_1.pop();
  309. kplayer_1.pop(); // karta na stosie gracza nr 2
  310. show();
  311. }
  312. }
  313.  
  314. counter++;
  315. if (wplayer_1.empty())
  316. break;
  317. else if (wplayer_2.empty())
  318. break;
  319. wojna_varB();
  320. counter++;
  321. return;
  322. }
  323. }
  324. if (!wplayer_1.empty())
  325. cout << " ";
  326. else
  327. cout << " ";
  328. cout << "moves counter: " << counter << endl;
  329. }
  330. void dequeue() // oproznianie kolejki
  331. {
  332. while (!wplayer_2.empty())
  333. {
  334. wplayer_2.pop();
  335. }
  336. while (!kplayer_2.empty())
  337. {
  338. kplayer_2.pop();
  339. }
  340. while (!wplayer_1.empty())
  341. {
  342. wplayer_1.pop();
  343. }
  344. while (!kplayer_1.empty())
  345. {
  346. kplayer_1.pop();
  347. }
  348. while (!wwar_1.empty() && !kwar_1.empty()) {
  349. wplayer_1.push(wwar_1.front());
  350. kplayer_1.push(kwar_1.front());
  351. wwar_1.pop();
  352. kwar_1.pop();
  353. }
  354. while (!wwar_2.empty() && !kwar_2.empty()) {
  355. wplayer_2.push(wwar_2.front());
  356. kplayer_2.push(kwar_2.front());
  357. wwar_2.pop();
  358. kwar_2.pop();
  359. }
  360. }
  361.  
  362. int main()
  363. {
  364. int g = 0;
  365. int h = 0;
  366. char wariant;
  367.  
  368. srand(time(0));
  369. cout << "Pick size of a deck (must be divisible by 4): ";
  370. cin >> decksize;
  371. fsize = (decksize / 4);
  372. add = (52 - decksize) / 4;
  373. cout << "Do you want to see course of the war?" << endl;
  374. cout << "If you want - press 1, If you don`t want - press 0: ";
  375. cin >> przebieg;
  376. cout << "Select variant: A or B: ";
  377. cin >> wariant;
  378. switch (wariant) {
  379.  
  380. case 'A':
  381.  
  382. for (int i = 0; i < 1; i++) {
  383.  
  384. cout << i;
  385.  
  386. counter = 0;
  387. zlozTalie(talia, *figura, *kolor);
  388. tasuj(talia);
  389. rozdaj(talia);
  390. wojna();
  391. dequeue();
  392. h += counter;
  393. if (i == 999) {
  394. cout << "Average of moves from 1000 games is: " << h / 1000;
  395. }
  396.  
  397. }
  398.  
  399. break;
  400. case 'B':
  401.  
  402. for (int i = 0; i < 1; i++) {
  403.  
  404. cout << i;
  405. counter = 0;
  406. zlozTalie(talia, *figura, *kolor);
  407. tasuj(talia);
  408. rozdaj(talia);
  409. wojna_varB();
  410. dequeue();
  411. g += counter;
  412. if (i == 999) {
  413. cout << "Average of moves from 1000 games is: " << g / 1000;
  414. }
  415. }
  416. break;
  417. default:
  418. cout << " you can only pick A or B";
  419.  
  420.  
  421. }
  422. return 0;
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement