Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <array>
- #include <queue>
- #include <iostream>
- enum Winner { kFirst, kSecond, kNone };
- struct GameResult {
- Winner winner;
- int turn;
- };
- GameResult SimulateWarGame(const std::array<int, 5>& first_deck,
- const std::array<int, 5>& second_deck) {
- int amount_of_turns = 0;
- std::queue<int> first;
- std::queue<int> second;
- for (int i = 0; i < 5; i++) {
- first.push(first_deck[i]);
- second.push(second_deck[i]);
- }
- while (amount_of_turns < 100000 && not first.empty() && not second.empty()) {
- int first_card = first.front();
- int second_card = second.front();
- first.pop();
- second.pop();
- if (first_card - second_card != 9 && first_card > second_card ||
- second_card - first_card == 9) {
- first.push(first_card);
- first.push(second_card);
- } else if (second_card > first_card || first_card - second_card == 9) {
- second.push(first_card);
- second.push(second_card);
- }
- ++amount_of_turns;
- }
- GameResult game_result;
- game_result.turn = amount_of_turns;
- if (first.empty()) {
- game_result.winner = kSecond;
- } else if (second.empty()) {
- game_result.winner = kFirst;
- } else {
- game_result.winner = kNone;
- }
- return game_result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement