小猫钓鱼纸牌游戏

小猫钓鱼纸牌游戏

问题描述及要求
A和B两个同学玩简单的纸牌游戏,每人手里有n张牌,两人轮流出牌并依次排列在桌面上,每次出掉手里的第1张牌,出牌后如果发现桌面上有跟刚才打出的牌的数字相同的牌,则把从相同的那张牌开始的全部牌按次序放在自己手里的牌的末尾。当一个人手中的牌先出完时,游戏结束,对方获胜。
如n为5,A手里的牌依次为2 3 5 6 1,B手里的牌依次为1 5 4 2 9;
A出2;
B出1;
A出3;
B出5;
A出5,发现前面有一张5,则把两个5都拿掉,这时他手里有6 1 5 5;
桌子上的牌依次为2 1 3;
B出4;
A出6;
B出2,发现前面有一张2,则把从2开始的牌全部拿掉,这时他手里有9 2 1 3 4 6 2;
桌子上没有牌了;
A出1;
B出9;
A出5;
B出2;
依次类推,直到某人先出完牌为止,则对方是胜者。
编写程序,利用栈和队列,判断谁是胜者。

思路:
首先构建一个栈和队列的类函数。然后运用这两个数据结构模拟小猫钓鱼游戏的过程,这也是解决该题目的关键步骤。运用栈模拟牌堆,运用队列模拟A和B的手牌,一开始读入俩人手牌。游戏开始:以下A为所带表队列,B为B所代表队列,C为牌堆所代表的栈,再用一个book数组标记C中已出现的牌。判断A是否为空,若空则B获胜,之后A出队一张牌t,如果book中已标记该牌已存于C则该牌入队A,从C中出栈一张牌入队A循环直到出栈的牌等于t,将这最后一个牌入队于A。再判断B是否为空,若空则A获胜,之后B出队一张牌t,如果book中已标记该牌已存于C则该牌入队B,从C中出栈一张牌入队B循环直到出栈的牌等于t,将这最后一个牌入队于B。循环直到某人获胜。

Code

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e3 + 5;
int book[Max];

class cir_queue
{
public:
	void enqueue(int x)
	{
		if ((rear + 1) % Max == front)
		{
			cout << "溢出" << endl;return;
		}
		rear = (rear + 1) % Max;
		data[rear] = x;
	}
	int dequeue()
	{
		if (rear == front) 
		{
			cout << "下溢1" << endl;return 0;
		}
		front = (front + 1) % Max;
		return data[front];
	}
	int get_front()
	{
		if (rear == front) { cout << "下溢2" << endl;return 0; }
		return data[(front + 1) % Max];
	}
	bool is_empty()
	{
		if (front == rear)return true;
		else return false;
	}
private:
	int data[Max] = {0,0}, front = 0, rear = 0;
};

class stack
{
public:
	void push(int a)
	{
		if (length >= Max)cout << "溢出" << endl;
		data[++length] = a;
	}
	int gettop()
	{
		return data[length];
	}
	int pop()
	{
		if (length == 0)
		{
			cout << "下溢" << endl;
		}
		return data[length--];
	}
	bool is_empty()
	{
		if (length == 0)return true;
		else return false;
	}
private:
	int data[Max], length = 0;
};

void project()
{
	cir_queue q1, q2;
	stack sta;
	cout << "请输入每人手里的数字个数" << endl;
	int n;cin >> n;
	cout << "请输入每人手里的牌" << endl;
	for (int i = 1;i <= n;i++)
	{
		int t;cin >> t;
		q1.enqueue(t);
	}
	for (int i = 1;i <= n;i++)
	{
		int t;cin >> t;
		q2.enqueue(t);
	}
	int f;
	while (1)
	{
		if (q1.is_empty()) { cout<<"player_B win"<<endl; break; }
		else
		{
			int t = q1.get_front();
			if (book[t])
			{
				q1.dequeue();
				q1.enqueue(t);
				int q = sta.pop();
				while (q != t)
				{
					book[q] = 0;
					q1.enqueue(q);
					q = sta.pop();
				}
				q1.enqueue(q);
				book[t] = 0;
			}
			else
			{
				sta.push(q1.dequeue());
				book[t] = 1;
				if (q1.is_empty()) {
					cout << "player_B win" << endl;break;
				}
			}
		}
		if (q2.is_empty()) { cout<<"player_A win"; break; }
		else
		{
			int t = q2.get_front();
			if (book[t])
			{
				int q = sta.pop();
				q2.dequeue();
				q2.enqueue(t);
				while (q != t)
				{
					book[q] = 0;
					q2.enqueue(q);
					q = sta.pop();
				}
				q2.enqueue(q);
				book[t] = 0;
			}
			else
			{
				sta.push(q2.dequeue());
				book[t] = 1;
				if (q2.is_empty()) {
					cout << "player_A win" << endl;break;
				}
			}
		}

	}
}

int main()
{
	project();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值