Advertisement
mfgnik

Untitled

Jun 28th, 2020
1,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <array>
  4. #include <queue>
  5. #include <iostream>
  6.  
  7. enum Winner { kFirst, kSecond, kNone };
  8.  
  9. struct GameResult {
  10.     Winner winner;
  11.     int turn;
  12. };
  13.  
  14. GameResult SimulateWarGame(const std::array<int, 5>& first_deck,
  15.                            const std::array<int, 5>& second_deck) {
  16.     int amount_of_turns = 0;
  17.     std::queue<int> first;
  18.     std::queue<int> second;
  19.     for (int i = 0; i < 5; i++) {
  20.         first.push(first_deck[i]);
  21.         second.push(second_deck[i]);
  22.     }
  23.     while (amount_of_turns < 100000 && not first.empty() && not second.empty()) {
  24.         int first_card = first.front();
  25.         int second_card = second.front();
  26.         first.pop();
  27.         second.pop();
  28.         if (first_card - second_card != 9 && first_card > second_card ||
  29.             second_card - first_card == 9) {
  30.             first.push(first_card);
  31.             first.push(second_card);
  32.         } else if (second_card > first_card || first_card - second_card == 9) {
  33.             second.push(first_card);
  34.             second.push(second_card);
  35.         }
  36.         ++amount_of_turns;
  37.     }
  38.     GameResult game_result;
  39.     game_result.turn = amount_of_turns;
  40.     if (first.empty()) {
  41.         game_result.winner = kSecond;
  42.     } else if (second.empty()) {
  43.         game_result.winner = kFirst;
  44.     } else {
  45.         game_result.winner = kNone;
  46.     }
  47.     return game_result;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement