Help me fix the C++code

The Drunkard Game In the drunkard game, a deck of cards is distributed equally to two players. Next, they reveal one top card each, and the one whose card is older takes both of the revealed cards, which are placed under the bottom of his deck. The one who remains without cards loses.

For simplicity, we will assume that all cards are different in face value, and that the lowest card wins the highest card ("six beats ace").

The player who takes the cards, first, he puts the card of the first player under the bottom of his deck, then the card of the second player (that is, the card of the second player is at the bottom of the deck).

Write a program that simulates a drunkard game and determines who wins. The game involves 10 cards with values from 0 to 9, the larger card defeats the smaller one, the card with a value of 0 defeats the card 9.

Input data

The program receives two lines as input: the first line contains 5 numbers separated by spaces, - the numbers of the first player's cards, the second - similarly to the 5 cards of the second player. The cards are listed from top to bottom, meaning each row starts with the card that will be opened first.

Output data

The program must determine who wins in this hand, and print the word first or second, and then print the number of moves made before winning. If the game does not end within 106 moves, the program should output the word botva.

Examples Input Conclusion 1 3 5 7 9 2 4 6 8 0

Second 5

#include<iostream>
#include<stdio.h>
#include<deque>
 
using namespace std;
 
int main(){
    freopen("open","r",stdin);
    int a[6],b[6];
    deque<int> d,d1;
    for(int i=1;i<=5;i++){
        cin>>a[i]>>b[i];
        d.push_back(a[i]);
        d1.push_back(b[i]);
    }
    int n=0;
    while(!d.empty() || !d1.empty()){
        int x=d.front();
        int y=d1.front();
        d.pop_front();
        d1.pop_front();
        if(n>1000000){
            cout<<"botva";
            return 0;
        }
        else{
        if((x==0 && y==9))   {
            d.push_back(x);
            d.push_back(y);
        }
        else
        if(x==9 && y==0){
            d1.push_back(y);
            d1.push_back(x);
        }
        else{
            
            if(x>y){
                d.push_back(x);
                d.push_back(y);
            }
            if(x<y){
                d1.push_back(y);
                d1.push_back(x);
            }
            if(x==y){
                d.push_back(x);
                d1.push_back(y);
            }     
           }
            n++;  }
    }
    if(d.empty()){
        cout<<"second "<<n;
    }
    else
        cout<<"first "<<n;
    return 0;
}
Author: Grundy, 2020-06-22

1 answers

As I said, 30 lines is enough for the eyes...

#include <queue>
#include <iostream>
using namespace std;

int main()
{
    int N = 10;
    queue<int> a, b;
    for(int i = 0, x; i < N/2; ++i) cin >> x, a.push(x);
    for(int i = 0, x; i < N/2; ++i) cin >> x, b.push(x);
    int moves = 0;
    for(; !a.empty() && !b.empty() && moves <= 200000; ++moves)
    {
        int ac = a.front(), bc = b.front();
        a.pop(); b.pop();
        queue<int> * win = (ac == 0 && bc == N-1) ? &a : 
                           (bc == 0 && ac == N-1) ? &b : 
                           (ac > bc) ? &a : &b;
        win->push(ac); win->push(bc);
    }
    if (a.empty()) cout << "second " << moves << endl;
    else if (b.empty()) cout << "first " << moves << endl;
    else cout << "botva\n";
}
 3
Author: Harry, 2020-06-22 10:45:01