美文网首页
C++小猫钓鱼游戏

C++小猫钓鱼游戏

作者: 魏天述 | 来源:发表于2016-11-29 21:32 被阅读0次

#include#includeusing namespace std;

struct queue

{

int data[1000];

int head;

int tail;

}; //新建结构体

struct stack

{

int data[10];

int top;

};//栈的结构体

//牌面只有1~9的小猫钓鱼游戏

int main() {

struct queue q1, q2;

struct stack s;

int book[10];

int i, t;

//初始化队列

q1.head = 1;

q1.tail = 1;

q2.head = 1;

q2.tail = 1;

//初始化栈

s.top = 0;

//初始化牌桌上的数组

for (i = 1; i <= 9; i++) {

book[i] = 0;

}

//摸牌六张

//Player1

for (i = 0; i < 6; i++) {

cin >> q1.data[q1.tail];

q1.tail++;

}

//Player2

for (i = 0; i < 6; i++) {

cin >> q2.data[q2.tail];

q2.tail++;

}

while (q1.head < q1.tail&&q2.head < q2.tail) {

t = q1.data[q1.head];//Player1出牌

if (book[t] == 0) {//桌上没有为t的牌

//player1没有赢到牌

q1.head++;//Player1出队一张牌

s.top++;//栈+1

s.data[s.top] = t;//刚刚打出的t牌入栈

book[t] = 1;//标记t牌在桌上是存在的

}

else {

//桌上有t牌,Player1可以赢牌

q1.head++;

q1.data[q1.tail] = t;

q1.tail++;

while (s.data[s.top] != t) {

book[s.data[s.top]] = 0;

q1.data[q1.tail] = s.data[s.top];

q1.tail++;

s.top--;

}

//回收桌上的牌

book[s.data[s.top]] = 0;

q1.data[q1.tail] = s.data[s.top];

q1.tail++;

s.top--;

}

if (q1.head == q1.tail) {//当Player1手上没有牌的时候

break;//退出游戏

}

t = q2.data[q2.head];//Player2出牌

if (book[t] == 0) {

q2.head++;

s.top++;

s.data[s.top] = t;

book[t] = 1;

}

else {

q2.head++;

q2.data[q2.tail] = t;

q2.tail++;

while (s.data[s.top] != t) {

book[s.data[s.top]] = 0;//取消标记

q2.data[q2.tail] = s.data[s.top];

q2.tail++;

s.top--;

}

//回收桌上的t牌

book[s.data[s.top]] = 0;

q2.data[q2.tail] = s.data[s.top];

q2.tail++;

s.top--;

}

}

if (q2.head == q2.tail) {

cout << "Player1 WIN !" << endl;

cout << "Player1's cards are:";

for (i = q1.head; i < q1.tail - 1; i++) {

cout << " " << q1.data[i] << endl;

}

if (s.top > 0) {

cout << "桌上剩余牌:";

for (i = 0; i < s.top; i++) {

cout << " " << s.data[i] << endl;

}

}

else {

cout << "There is no card in the desk" << endl;

}

}

else {

cout << "Player2 WIN !" << endl;

cout << "Player2's cards are:";

for (i = q2.head; i < q2.tail - 1; i++) {

cout << " " << q2.data[i] << endl;

}

if (s.top > 0) {

cout << "桌上剩余牌:";

for (i = 0; i < s.top; i++) {

cout << " " << s.data[i] << endl;

}

}

else {

cout << "There is no card in the desk" << endl;

}

}

Sleep(5000);

return 0;

}

相关文章

  • C++小猫钓鱼游戏

    #include#includeusing namespace std; struct queue { int d...

  • 重拾算法Day07-小猫钓鱼

    小猫钓鱼游戏

  • 纸牌游戏-小猫钓鱼

    规则: 星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一...

  • 小猫钓鱼的游戏算法

    目前是模拟是两个人对战的。使用栈和队列模拟。 #define kCardSize 52 typedef stru...

  • 小猫钓鱼🎣

    老猫和小猫一块儿在河边钓鱼。 一只蜻蜓飞来了。小猫看见了,放下钓鱼竿,就去捉蜻蜓。蜻蜓飞走了,小猫没捉着,空着手回...

  • 小猫钓鱼

    年纪一大把了,但我始终是那只钓鱼的小猫。无论年纪怎么增长,我永远无法学会像一只老猫那样钓鱼。 今天是星期天,没有应...

  • 小猫钓鱼

    僵尸打开了你的脑袋,失望地摇了摇头,走开了!旁边的屎壳郎眼睛亮了 我看你像书包( ꒪ д꒪ ⊂彡))Д´) 小猫钓...

  • 小猫钓鱼

    秋秋 2018-3-21

  • 小猫钓鱼

    亲子阅读第321天(3月5日) 今天晚上给崽崽讲了一个名叫《小猫钓鱼》的故事。故事讲的是一天,猫妈妈带着小...

  • 小猫钓鱼

    老师,今天我给你讲个故事,故事的名字叫小猫钓鱼。有一天嗯猫妈妈要带着小猫去河边钓鱼,猫妈妈嗯5:00就起来了,小猫...

网友评论

      本文标题:C++小猫钓鱼游戏

      本文链接:https://www.haomeiwen.com/subject/ppjzpttx.html