C++小猫钓鱼游戏

#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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌...
    青葱烈马阅读 1,550评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • include<iostream> using namespace std; //单链表 typedef stru...
    jmychou阅读 455评论 0 0
  • hello
    王朵莱阅读 163评论 0 1
  • 看你几日的字里行间总是平静庄重,无喜无悲,吾心便觉杂陈五味,既欣慰,又心忧。为何欣慰?为何心忧?前者系因你遇事不惧...
    韩尚小阅读 305评论 0 1