400字范文,内容丰富有趣,生活中的好帮手!
400字范文 > 基于链表的模拟21点游戏 C语言

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

时间:2021-08-28 03:09:40

相关推荐

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

题目

效果

代码

game.c:是游戏的逻辑

#include <stdlib.h> // for drand48#include <stdio.h>#include <string.h>#include "card.h"#define SHUFFLE_TIMES 7const 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 gamecardT *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 lossbreak;}}}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 youint 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=gccCARD:=cardAR.oall:gamegame:game.o card.o$(CC) -o game game.o card.otest:game.o$(CC) -o test game.o $(CARD)game.o:game.c$(CC) -c ard.o:card.c$(CC) -c lean:rm -f main.o game.o card.o

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。