Advertisement
Guest User

Untitled

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