关于啊哈算法——纸牌游戏疑惑

阅读了啊哈算法中纸牌游戏部分,于是打算用c++实现,自己实现的感觉没问题,运行结果不同,各位帮忙看下问题所在。

img

img

img

img

img

#include <queue>
#include <stack>
#include <iostream>
#include <array>

using namespace std;

typedef queue<int> queInt;
typedef stack<int> staInt;
void CardGame(queInt& opposedA,queInt& opposedB)
{
    staInt sta;
    array<int,10> arr = { 0 };

    while (!opposedA.empty() && !opposedB.empty())
    {
        // A play a hand
        int iTem = opposedA.front();
        if (arr[iTem])
        {
            int ic = opposedA.front();
            opposedA.pop();
            opposedA.push(ic);
            for (int i = 0; i < sta.size();i++)
            {
                if (sta.top() == iTem)
                {
                    opposedA.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    break;
                }
                else
                {
                    opposedA.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    i--;
                }
            }

        }
        else
        {
            sta.push(opposedA.front());
            opposedA.pop();
            arr[iTem] = 1;
        }

        // B play a hand
        iTem = opposedB.front();
        if (arr[iTem])
        {
            int ic = opposedB.front();
            opposedB.pop();
            opposedB.push(ic);
            for (int i = 0; i < sta.size(); i++)
            {
                if (sta.top() == iTem)
                {
                    opposedB.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    break;
                }
                else
                {
                    opposedB.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    i--;
                }
            }
        }
        else
        {
            sta.push(opposedB.front());
            opposedB.pop();
            arr[iTem] = 1;
        }
    }
}

int main() 
{
    array<int, 6> arr1 = {2,4,1,2,5,6};
    array<int, 6> arr2 = { 3,1,3,5,6,4};
    //array<int, 3> arr1 = {1,2,3};
    //array<int, 3> arr2 = { 1,2,3 };
    queInt opposedA, opposedB;

    for each (int var1 in arr1)
    {
        opposedA.push(var1);
    }

    for each (int var2 in arr2)
    {
        opposedB.push(var2);
    }

    CardGame(opposedA, opposedB);

    if (opposedA.empty())
    {
        cout << " B win,Congratulation!" << endl;
        while (!opposedB.empty())
        {
            cout << opposedB.front() << " ";
            opposedB.pop();
        }
    }
    else
    {
        cout << " A win,Congratulation!" << endl;
        while (!opposedA.empty())
        {
            cout << opposedA.front() << " ";
            opposedA.pop();
        }
    }

    return 0;
}