No.2.3 纸牌-小猫钓鱼
1.规则:将一副扑克牌平均分成两份,每人拿一份。小哼先拿出手中的第一张扑克牌放在桌上,然后小哈也拿出手中的第一张扑克牌,并放在小哼刚打出的扑克牌的上面,就像这样两人交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一人手中的牌全部出完时,游戏结束,对手获胜。【假设牌面只有1~9】
2.分析:
每个人都有两种操作:出牌和赢牌 <=> 队列的出栈与入栈
struct queue {int data[100]; int head; int tail;};
桌面规则:有相同的牌,则两张牌及其之间的牌面全部出栈,并给赢牌的一方入栈,此处用栈
struct stack {int data[10]; int top;}
3.解题思路:
struct queue{
int data[100];
int head;
int tail;
};
struct stack{
int data[100];
int top;
};
struct stack s;
struct queue push_card(){ //发牌
struct queue q;
int n,i;
printf("开始发牌:");
scanf("请输入牌数%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&q.data[q.tail]);
q.tail++;
}
return q;
}
void play_card(queue q){
int top, i, book[10];
for (i=1;i<=10;i++){
book[i]=0;
}
//出牌
if (book[q.head]==0){
s.top++;
s.data[s.top]=book[q.head];
q.head++;
book[q.head]=1;
}
if (book[q.head]==1){
book[s.data[s.top]]=0;
q.head++;
q.data[q.tail]=book[q.head];
q.tail++;
while(s.data[s.top]!=book[q.head]){
q.data[q.tail]=s.data[s.top];
q.tail++;
s.top--;
}
}
}
void who_win(){
struct queue q;
int i;
if (q.head == q.tail){
printf("小哈赢");
for (i=q.head;i<=q.tail-1;i++)
printf("%d ",q.data[i]);
if (s.top>0){
printf("%d ",s.data[s.top]);
s.top--;
}
else
printf("table is clear");
}
}
int main(){
struct queue q1, q2;
struct stack s;
int i,n,t,flag;
int top;
//初始化指针
q1.head=1;q1.tail=1;
q2.head=1;q2.tail=1;
s.top=0;
flag = 0;
//发牌
//谁赢谁输?
while(q1.head //出牌循环
}
// who win
}
4.代码
#include
/* 优雅化原代码失败,需解决queue参数传递、返回问题先
struct queue{
int data[100];
int head;
int tail;
};
struct stack{
int data[100];
int top;
};
struct stack s;
int flag = 0;
//发牌
struct queue push_card(queue *q){
int n,i;
printf("开始发牌:");
scanf("请输入牌数%d",&n);
for (i=1;i<=n;i++){
scanf("%d",q->data[q->tail]);
q->tail++;
}
return *q;
}
//出牌
void play_card(queue *q){
int t;
int top, i, book[10];
for (i=1;i<=10;i++){
book[i]=0;
}
t = (*q).data[(*q).head];
if (book[t]==0){
s.top++;
s.data[s.top]=t;
q->head++;
book[t]=1;
}
else
{
q->head++;
q->data[q->tail]=t;
q->tail++;
while(s.data[s.top]!=t){
book[s.data[s.top]]=0;
q->data[q->tail]=s.data[s.top];
q->tail++;
s.top--;
}
}
}
//谁输谁赢?
int who_win(queue *q){
int i;
if (q->head == q->tail){
flag = 1;
for (i=q->head;i<=q->tail-1;i++)
printf("%d ",q->data[i]);
if (s.top>0){
printf("%d ",s.data[s.top]);
s.top--;
}
else
printf("table is clear");
}
return flag;
}
int main(){
struct queue q1, q2;
int i,n,t;
int top;
//初始化指针
q1.head=1;q1.tail=1;
q2.head=1;q2.tail=1;
s.top=0;
flag=0;
//发牌
q1 = push_card(&q1);
q2 = push_card(&q2);
//出牌
while(q1.head
play_card(&q1);
play_card(&q2);
}
// who win
flag = who_win(&q1);
if (flag == 1)
printf("q1 win");
flag = who_win(&q2);
if (flag == 1)
printf("q2 win");
return 0;
}
*/
struct queue
{
int data[1000];
int head;
int tail;
};
struct stack
{
int data[10];
int top;
};
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;
//发牌
for(i=1;i<=6;i++)
{scanf("%d",&q1.data[q1.tail]);
q1.tail++;}
for(i=1;i<=6;i++)
{scanf("%d",&q2.data[q2.tail]);
q2.tail++;}
while(q1.head
t=q1.data[q1.head];//小哼出一张牌
//判断小哼当前打出的牌是否能赢牌
if(book[t]==0) //表明桌上没有牌面为t的牌
{
//小哼此轮没有赢牌
q1.head++; //小哼已经打出一张牌,所以要把打出的牌出队
s.top++;
s.data[s.top]=t; //再把打出的牌放到桌上,即入栈
book[t]=1; //标记桌上现在已经有牌面为t的牌
}
else
{
//小哼此轮可以赢牌
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--; //栈中少了一张牌,所以栈顶要减1
}
}
t=q2.data[q2.head]; //小哈出一张牌
//判断小哈当前打出的牌是否能赢牌
if(book[t]==0) //表明桌上没有牌面为t的牌
{
//小哈此轮没有赢牌
q2.head++; //小哈已经打出一张牌,所以要把打出的牌出队
s.top++;
s.data[s.top]=t; //再把打出的牌放到桌上,即入栈
book[t]=1; //标记桌上现在已经有牌面为t的牌
}
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--;
}
}
}
if(q2.head==q2.tail)
{
printf("小哼win\n");
printf("小哼当前手中的牌是");
for(i=q1.head;i<=q1.tail-1;i++)
printf(" %d",q1.data[i]);
if(s.top>0) //如果桌上有牌则依次输出桌上的牌
{
printf("\n桌上的牌是");
for(i=1;i<=s.top;i++)
printf(" %d",s.data[i]);
}
else
printf("\n桌上已经没有牌了");
}
else
{
printf("小哈win\n");
printf("小哈当前手中的牌是");
for(i=q2.head;i<=q2.tail-1;i++)
printf(" %d",q2.data[i]);
if(s.top>0) //如果桌上有牌则依次输出桌上的牌
{
printf("\n桌上的牌是");
for(i=1;i<=s.top;i++)
printf(" %d",s.data[i]);
}
else
printf("\n桌上已经没有牌了");
}
getchar();getchar();
return 0;
}
5.优雅化代码,调试出一部分,暂做记录,主要是传址与传值得区别,后续代码不调了,太懒!
struct queue {
int data[100];
int head;
int tail;
};
int m,i;
struct queue push_card(queue *q){
printf("Input number :");
scanf("%d",&m);
for (i=1;i<=m;i++){
scanf("%d",&(q->data[q->tail]));
q->tail++;
}
return *q;
}
void main(){
struct queue q;
q.head = 1;
q.tail = 1;
q = push_card(&q);
for (i=q.head;i
}