【问题标题】:Stack of cards and the game of War一堆纸牌和战争游戏
【发布时间】:2023-04-04 01:50:01
【问题描述】:

所以我正在制作一款名为战争的纸牌游戏,规则如下:

  1. 玩家 1 和玩家 2 分别排列称为 q1 和 q2 的牌。两个都 队列不为空。玩家也有空的战斗栈s1 和 s2。
  2. 每位玩家从队列前面取出一张牌并将其放入 在他们的堆栈顶部
  3. 如果牌面值相等,就会发生战争。
  4. 每位玩家将 3 张额外的牌从他们的队列中移到他们的堆叠中 并再次检查他们的顶牌的面值。这可能会发生 几次。再次转到 3。
  5. 如果卡牌的值不同,则战斗结束并获得 可以确定。

我必须编写的函数名为 start_battle.c,我想出的逻辑如下,queue_frontqueue_emptystack_top 函数已经编写好了。所以我的伪代码如下:

function start_battle(q1, q2, s1, s2, logfile)

warc=0
move front card of q1 to the top of s1
move front card of q2 to the top of s2
while (top cards of s1 and s2 have equal value
       and both q1 and q2 are not empty)           
  for i=1 to 3
    if q1 is not empty
      move front card of q1 to the top of s1
    endif
  done
  for i=1 to 3
    if q2 is not empty
      move front card of q2 to the top of s2
    endif
  done
done
return warc

我的代码如下:

#include "libcardlist.h"
int start_battle(queue *q1, queue *q2, stack *s1, stack *s2, FILE *logfile){    
    int warc =0;
    int i;
    stack_push(s1, queue_front(q1));
    stack_push(s2, queue_front(q2));
    card c1 = stack_top(s1);
    card c2 = stack_top(s2);
    while (c1.face==c2.face&&  queue_empty(q1)==0 && queue_empty(q2)==0) {        
        warc++;        
        for (i=0; i<3; i++) {
            if(queue_empty(q1)==0){
                stack_push(s1, queue_front(q1));
            }
        }
        for (i=0; i<3; i++) {
            if (queue_empty(q2)==0){
              stack_push(s2, queue_front(q2));
            }    
        }       
   }
   return warc;
}

哦和

typedef struct {
  int face;         /* 2-14 */
  char suit;            /* C H S D */
} card;

当我测试它时,我的代码卡在了 while 循环中,有人可以帮我解决它吗?

【问题讨论】:

  • queue_front() 真的会从队列中移除卡片吗?
  • queue_front() 只是获取队列最前面的数据。
  • 那你需要把它拿掉,否则你的卡在两堆里。
  • 它们是 2 张不同的牌,有 2 叠,s1 和 s2。
  • 另外,您的while 条件似乎无效,因为queue_empty(q1)==0 表示q1 is not empty,这始终是正确的,并且c1c2 在循环内永远不会改变。

标签: c stack


【解决方案1】:

我在您的算法的适当位置添加了queue_pop()。将卡片从队列复制到堆栈(通过推送)后,您需要将其从堆中移除。实际上,这应该是游戏主要玩法的一部分,而不是介绍(它是一样的,但你最终会弄明白的),你需要移动 both 堆栈宣布特定回合的获胜者后,将卡片放到相应的队列中。

无论如何,把你的流行音乐放在那里。

int start_battle(queue *q1, queue *q2, stack *s1, stack *s2, FILE *logfile){    
    int warc =0;
    int i;
    stack_push(s1, queue_front(q1));
    queue_pop(q1); // no longer in this queue.
    stack_push(s2, queue_front(q2));
    queue_pop(q2); // no longer in this queue.
    card c1 = stack_top(s1);
    card c2 = stack_top(s2);
    while (c1.face==c2.face && queue_empty(q1)==0 && queue_empty(q2)==0) {        
        warc++;        
        for (i=0; i<3; i++) {
            if(queue_empty(q1)==0){
                stack_push(s1, queue_front(q1));
                queue_pop(q1);
            }
        }
        for (i=0; i<3; i++) {
            if (queue_empty(q2)==0){
                stack_push(s2, queue_front(q2));
                queue_pop(q2);
            }    
        }       
   }

   return warc;
}

【讨论】:

  • 嘿,谢谢,但我们只允许使用这些功能:dropbox.com/s/ghbfdqb9l7erz5e/libcardlist.c
  • @Jenny 链接您的输出,如果需要,发布更多代码(如主运行循环)。实际玩游戏的代码与您的启动循环非常相似(事实上,这个循环实际上是实际游戏的部分)。顺便说一句,最初的牌组也应该被打印出来。
猜你喜欢
  • 2021-03-05
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2014-09-20
相关资源
最近更新 更多