Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- // wojna
- //
- // Created by Dawid Siarczyński on 23/11/2019.
- // Copyright © 2019 Dawid Siarczyński. All rights reserved.
- #include <cstdlib>
- #include <iostream>
- #include <time.h>
- #include<string.h>
- #include <queue>
- #include <stdio.h>
- #define SIZE 52
- using namespace std;
- struct Karta
- {
- char figura;
- char kolor;
- };
- //zmienne globalne
- Karta talia[52];
- char const* const figura[13] =
- { "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" };
- char const* const kolor[4] = { "clubs","diamonds","hearts","spades" };
- int counter = 0;
- int decksize;
- int fsize;
- int add;
- int przebieg;
- queue <int> wplayer_1;
- queue <int> kplayer_1;
- queue <int> wplayer_2;
- queue <int> kplayer_2;
- queue <int> wwar_1;
- queue <int> wwar_2;
- queue <int> kwar_1;
- queue <int> kwar_2;
- void zlozTalie(Karta* newdeck, char const* const newFigura, char const* const newKolor)
- {
- for (int i = 0; i < decksize; i++)
- {
- newdeck[i].figura = ((i % fsize) + add);
- newdeck[i].kolor = (i / fsize);
- }
- }
- void tasuj(Karta* newdeck)
- {
- for (int i = 0; i < decksize; i++)
- {
- int j = rand() % decksize;
- Karta t = newdeck[i];
- newdeck[i] = newdeck[j];
- newdeck[j] = t;
- }
- }
- void rozdaj(Karta* newdeck) {
- for (int i = 0; i < decksize; i++) {
- if (i % 2 == 0) {
- wplayer_1.push(newdeck[i].figura);
- kplayer_1.push(newdeck[i].kolor);
- }
- else {
- wplayer_2.push(newdeck[i].figura);
- kplayer_2.push(newdeck[i].kolor);
- }
- }
- };
- void licz(Karta talia[SIZE], Karta player1[SIZE], Karta player2[SIZE]) //do liczenia rangi
- {
- int i = 0;
- int *p1 = 0, *p2 = 0;
- while (i <= (SIZE / 2)) {
- if (wplayer_1.front() == 12)
- p1 += 25; // as=25
- if (wplayer_1.front() == 11)
- p1 += 20;
- if (wplayer_1.front() == 10)
- p1 += 15;
- if (wplayer_1.front() == 9)
- p1 += 10;
- wplayer_1.push(wplayer_1.front());
- wplayer_1.pop();
- if (wplayer_2.front() == 12)
- p2 += 25; // as=25
- if (wplayer_2.front() == 11)
- p2 += 20;
- if (wplayer_2.front() == 10)
- p2 += 15;
- if (wplayer_2.front() == 9)
- p2 += 10;
- wplayer_2.push(wplayer_1.front());
- wplayer_2.pop();
- i++;
- }
- cout << "player1;" << p1 << "range" << "player2;" << p2;
- }
- void show() // pokazuje przebieg wojny
- {
- if (przebieg == 1) {
- cout <<"PLAYER 1: "<< figura[wplayer_1.front()] << " " << kolor[kplayer_1.front()] << " cards in hand: " << wplayer_1.size()-1 << endl;
- cout << "PLAYER 2: "<< figura[wplayer_2.front()] << " " << kolor[kplayer_2.front()] << " cards in hand: " << wplayer_2.size()-1 << endl<< endl;
- }
- }
- void win1() // wygrana gracza 1
- {
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- while (!wwar_1.empty() && !kwar_1.empty()) {
- wplayer_1.push(wwar_1.front());
- kplayer_1.push(kwar_1.front());
- wwar_1.pop();
- kwar_1.pop();
- }
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- if (wplayer_2.empty()) {
- cout << "Player 1 won";}
- while (!wwar_2.empty() && !kwar_2.empty()) {
- wplayer_1.push(wwar_2.front());
- kplayer_1.push(kwar_2.front());
- wwar_2.pop();
- kwar_2.pop();
- }
- counter++;
- }
- void win2() // wygrana gracza 2
- {
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- while (!wwar_2.empty() && !kwar_2.empty()) {
- wplayer_2.push(wwar_2.front());
- kplayer_2.push(kwar_2.front());
- wwar_2.pop();
- kwar_2.pop();
- }
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- if (wplayer_1.empty())
- cout << "Player 2 won";
- while (!wwar_1.empty() && !kwar_1.empty()) {
- wplayer_2.push(wwar_1.front());
- kplayer_2.push(kwar_1.front());
- wwar_1.pop();
- kwar_1.pop();
- }
- counter++;
- }
- void wojna() {
- while (!wplayer_1.empty() && !wplayer_2.empty())
- {
- if (wplayer_1.front() > wplayer_2.front())
- {
- show();
- win1();
- }
- else if (wplayer_1.front() < wplayer_2.front())
- {
- show();
- win2();
- }
- else if (wplayer_1.front() == wplayer_2.front())
- {
- if(przebieg==1)
- cout<< " !!!!!!WAR!!!!!!"<<endl;
- show();
- if (wplayer_1.size() <= 2){
- cout<<" Player 2 won";
- break;}
- else if (wplayer_2.size() <= 2){
- cout<< "Player 1 won";
- break;}
- else {
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- show();
- }
- if (wplayer_1.empty())
- break;
- else if (wplayer_2.empty())
- break;
- else {
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- }
- counter++;
- wojna();
- counter++;
- return;
- }
- }
- cout << " moves counter: " << counter << endl;
- }
- void wojna_varB() //wojna tylko wariant B
- {
- while (!wplayer_1.empty() && !wplayer_2.empty())
- {
- if (wplayer_1.front() > wplayer_2.front())
- {
- show();
- win1();
- }
- else if (wplayer_1.front() < wplayer_2.front())
- {
- show();
- win2();
- }
- else if (wplayer_1.front() == wplayer_2.front())
- {
- if(przebieg==1)
- cout<< " !!!!!!WAR!!!!!!"<<endl;
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- show();
- if (wplayer_1.empty() || wplayer_2.empty()) {
- if (wplayer_1.empty()) { // koncza sie 2 karty graczowi 1
- wwar_1.push(wplayer_2.front());
- kwar_1.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- wplayer_1.push(wplayer_2.front());
- kplayer_1.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop(); // karta na stosie gracza nr 1
- show();
- }
- if (wplayer_2.empty()) { // koncza sie 2 karty graczowi 2
- wwar_2.push(wplayer_1.front());
- kwar_2.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- wplayer_2.push(wplayer_1.front());
- kplayer_2.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop(); // karta na stosie gracza nr 2
- show();
- }
- }
- else {
- wwar_2.push(wplayer_2.front());
- kwar_2.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop();
- wwar_1.push(wplayer_1.front());
- kwar_1.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop();
- if (wplayer_1.empty()) { // konczy sie 1 karta graczowi 1
- wplayer_1.push(wplayer_2.front());
- kplayer_1.push(kplayer_2.front());
- wplayer_2.pop();
- kplayer_2.pop(); // karta na stosie gracza nr 1
- show();
- }
- if (wplayer_2.empty()) { // konczy sie 1 karta graczowi 2
- wplayer_2.push(wplayer_1.front());
- kplayer_2.push(kplayer_1.front());
- wplayer_1.pop();
- kplayer_1.pop(); // karta na stosie gracza nr 2
- show();
- }
- }
- counter++;
- if (wplayer_1.empty())
- break;
- else if (wplayer_2.empty())
- break;
- wojna_varB();
- counter++;
- return;
- }
- }
- if (!wplayer_1.empty())
- cout << " ";
- else
- cout << " ";
- cout << "moves counter: " << counter << endl;
- }
- void dequeue() // oproznianie kolejki
- {
- while (!wplayer_2.empty())
- {
- wplayer_2.pop();
- }
- while (!kplayer_2.empty())
- {
- kplayer_2.pop();
- }
- while (!wplayer_1.empty())
- {
- wplayer_1.pop();
- }
- while (!kplayer_1.empty())
- {
- kplayer_1.pop();
- }
- while (!wwar_1.empty() && !kwar_1.empty()) {
- wplayer_1.push(wwar_1.front());
- kplayer_1.push(kwar_1.front());
- wwar_1.pop();
- kwar_1.pop();
- }
- while (!wwar_2.empty() && !kwar_2.empty()) {
- wplayer_2.push(wwar_2.front());
- kplayer_2.push(kwar_2.front());
- wwar_2.pop();
- kwar_2.pop();
- }
- }
- int main()
- {
- int g = 0;
- int h = 0;
- char wariant;
- srand(time(0));
- cout << "Pick size of a deck (must be divisible by 4): ";
- cin >> decksize;
- fsize = (decksize / 4);
- add = (52 - decksize) / 4;
- cout << "Do you want to see course of the war?" << endl;
- cout << "If you want - press 1, If you don`t want - press 0: ";
- cin >> przebieg;
- cout << "Select variant: A or B: ";
- cin >> wariant;
- switch (wariant) {
- case 'A':
- for (int i = 0; i < 1000; i++) {
- cout << i;
- counter = 0;
- zlozTalie(talia, *figura, *kolor);
- tasuj(talia);
- rozdaj(talia);
- wojna();
- dequeue();
- h += counter;
- if (i == 999) {
- cout << "Average of moves from 1000 games is: " << h / 1000;
- }
- }
- break;
- case 'B':
- for (int i = 0; i < 1; i++) {
- cout << i;
- counter = 0;
- zlozTalie(talia, *figura, *kolor);
- tasuj(talia);
- rozdaj(talia);
- wojna_varB();
- dequeue();
- g += counter;
- if (i == 999) {
- cout << "Average of moves from 1000 games is: " << g / 1000;
- }
- }
- break;
- default:
- cout<< " you can only pick A or B";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement