C++小猫钓鱼游戏

2016-11-29  本文已影响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;

}

上一篇 下一篇

猜你喜欢

热点阅读