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 <stdio.h>
/* 優雅化原代碼失敗,需解決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<q1.tail && q2.head<q2.tail){
    //出牌循環
    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<q1.tail && q2.head<q2.tail ) //當隊列不為空的時候執行循環
  {
    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<q.tail;i++)
    printf("%d ",q.data[i]);

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM