基于链表的模拟21点游戏 C语言

题目

在这里插入图片描述

效果

在这里插入图片描述

代码

game.c:是游戏的逻辑

#include <stdlib.h> // for drand48
#include <stdio.h>
#include <string.h>
#include "card.h"
#define SHUFFLE_TIMES 7
const char suits[4]={'C','D','H','S'};
/* This main() is constructed just for testing purposes. See
 * play_game() below for actually connecting together the pieces */
// organize all the helper functions to play a complete game
cardT *creat_a(void){
    cardT *ret=NULL,*tmp;
    for(int i=0;i<4;i++){
        for(int j=1;j<=13;j++){
           tmp=makeCard(j, suits[i]);
           tmp->next=ret;
           ret=tmp;
        }
    }
    return ret;
}
void show_all(cardT* head){
    for(; head;head=head->next)
    {
        printf("%d%c ",head->rank,head->suit);
    }
}
int abss(cardT* x){
    if(x->rank<=10)return x->rank;
    else return 10;
}
void show(cardT* human_head,cardT*computer_head,int human_sum, int computer_sum){
    printf("human: ");
    show_all(human_head);
    printf("human sum=%d\ncomputer: ",human_sum);
    show_all(computer_head);
    printf("computer sum=%d\n",computer_sum);
}
void play_game(void) {
    cardT *deck=creat_a();
    for(int i=0;i<SHUFFLE_TIMES;i++){
        deck=shuffle(deck);
    }
    int human_sum=0,computer_sum=0;
    cardT * human_head,*human_now,*computer_head,*computer_now;

    human_head=deck;
    deck=deck->next->next;
    human_now=human_head->next;
    human_now->next=NULL;
    human_sum+=abss(human_head);
    human_sum+=abss(human_head->next);

    computer_head=deck;
    deck=deck->next;
    computer_now=computer_head;
    computer_now->next=NULL;
    computer_sum+=computer_head->rank;
    char input[16],fl=1;
    while(1){
        show(human_head,computer_head,human_sum,computer_sum);
        printf("human player,stand or hit?");
        scanf("%s",input);
        if(strcmp(input,"stand")==0 || strcmp(input,"s")==0 || strcmp(input,"S")==0){
            break;
        }else if(strcmp(input,"hit")==0 || strcmp(input,"h")==0 || strcmp(input,"H")==0){
            human_now->next=deck;
            deck=deck->next;
            human_now=human_now->next;
            human_now->next=NULL;
            human_sum+=abss(human_now);
            if(human_sum>21){
                fl=0;//human loss
                break;
            }
        }
    }
    while(fl==1 && computer_sum<=17){
        computer_now->next=deck;
        deck=deck->next;
        computer_now=computer_now->next;
        computer_now->next=NULL;
        computer_sum+=abss(computer_now);
    }
    show(human_head,computer_head,human_sum,computer_sum);
    if(fl==0){
        printf("Lost!");
    }else{
        if(computer_sum>21){
            printf("Win!");
        }else if(computer_sum>=human_sum){
            printf("Lost!");
        }else{
            printf("Win!");
        }
    }
    return;
}
int main(int argc, char *argv[]) {
    if(argc>1) {
        srand48(atol(argv[1]));
    }
    play_game();
    return 0;
 }

card.h 结构体card表示一张牌,

// note, the typedef name is "cardT", the struct name is "struct card"
typedef struct card {
    int rank;
    char suit;
    struct card *next; // Be sure you understand what data type "next" is
} cardT;

cardT *makeCard(int rank, char suit); // will need to allocate space!
cardT *shuffle(cardT *pile); // provided for you
int countPile(cardT *pile);  // provided for you
// Include the other required prototypes below

card.c:新建牌组,洗牌功能。

#include <stdio.h>
#include <stdlib.h>
#include "card.h"

/* counts the number of cards in the list headed by "deck" */
int countPile(cardT *pile) {
    int num=0;
    while(pile!=NULL){
        num++;
        pile=pile->next;
    }
    return num;
}

/* just shows the top card right now */
void showPile(cardT *pile) {
    printf("%d%c\n",pile->rank,pile->suit);
}

/* Emulates a "riffle shuffle" of "deck". */
cardT *shuffle(cardT *deck) {
    int count = countPile(deck);
    cardT *cut=deck;
    int i=0;
    while(i<count/2){
        cut=cut->next;
        i++;
    }
    /* cut is now the card 1/2 way through the deck */
    cardT *riffle=cut->next;
    cut->next = NULL;
    cardT *retdeck=NULL;
    while(deck || riffle) { /* just like a while loop */
        cardT *temp;
        if(deck && (!riffle || drand48()<0.5)) {
            /* next card comes from the top of 'deck' */
            temp=deck;
            deck=deck->next;
        } else if(riffle) {
            /* next card comes from the top of 'riffle' */
            temp=riffle;
            riffle=riffle->next;
        }
        /* put the card at the top of the "retdeck" */
        temp->next=retdeck;
        retdeck=temp;
    }
    return retdeck;
}
cardT *makeCard(int rank, char suit){
    cardT *ret =malloc(sizeof(struct card));
    ret->next=NULL;
    ret->rank=rank;
    ret->suit=suit;
    return ret;
}

makefile文件:用于编译游戏,test是用是链接到老师给定的.o文件。

CC=gcc
CARD:=cardAR.o
all:game

game:game.o card.o
	$(CC) -o game game.o card.o
test:game.o
	$(CC) -o test game.o $(CARD)
game.o:game.c
	$(CC) -c game.c
card.o:card.c
	$(CC) -c card.c
clean:
	rm -f main.o game.o card.o

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清欢_小铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值