题目

效果

代码

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 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 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:gamegame: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

基于链表的模拟21点游戏 C语言相关推荐

  1. 操作系统大作业 基于Linux的模拟进程调度算法 运用c++语言编程 在VMware虚拟机里 centos 亲自写亲自测试 代码 说明书

    发布文章 博文管理我的博客退出 Trash Temp 操作系统大作业 基于Linux的模拟进程调度算法 运用c++语言编程 在VMware虚拟机里 centos 亲自写亲自测试 代码 说明书 @[TO ...

  2. 【java毕业设计】基于java+swing+GUI的连连看游戏设计与实现(毕业论文+程序源码)——连连看游戏

    基于java+swing+GUI的连连看游戏设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+swing+GUI的连连看游戏设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦 ...

  3. 转:高层游戏引擎——基于OGRE所实现的高层游戏引擎框架

    高层游戏引擎--基于OGRE所实现的高层游戏引擎框架 这是意念自己的毕业论文,在一个具体的实践之中,意念主要负责的是物件和GUI之外的其他游戏系统.意念才学疏陋,望众位前辈不吝赐教.由于代码质量不高. ...

  4. 高层游戏引擎——基于OGRE所实现的高层游戏引擎框架

    技术文档(Document) 来自:noslopforever的专栏 高层游戏引擎--基于OGRE所实现的高层游戏引擎框架 这是意念自己的毕业论文,在一个具体的实践之中,意念主要负责的是物件和GUI之 ...

  5. Python 实现简单的单机版 21 点游戏

    Python 实现简单的单机版 21 点游戏 1. 21 点游戏规则简介 2. 实现代码 3. 运行效果 4. 总结 1. 21 点游戏规则简介 21 点游戏规则: 点数计算:每张牌都有点数.2 到 ...

  6. 基于Springboot+Vue的MOBA类游戏攻略分享平台

    摘 要 随着信息技术和网络技术的飞速发展,人类已进入全新信息化时代,传统管理技术已无法高效,便捷地管理信息.为了迎合时代需求,优化管理效率,各种各样的管理系统应运而生,各行各业相继进入信息管理时代,M ...

  7. ML之FE:基于load_mock_customer数据集(模拟客户,单个DataFrame)利用featuretools工具实现自动特征生成/特征衍生

    ML之FE:基于load_mock_customer数据集(模拟客户,单个DataFrame)利用featuretools工具实现自动特征生成/特征衍生 推荐文章 ML之FE:基于load_mock_ ...

  8. 吃豆豆游戏的C语言程序码,C++实现基于控制台界面的吃豆子游戏

    本文实例讲述了C++实现基于控制台界面的吃豆子游戏.分享给大家供大家参考.具体分析如下: 程序运行界面如下所示: ESC键可退出游戏. main.cpp源文件如下: #include "li ...

  9. 基于android平台的24点游戏设计与实现需求分析,基于Android平台的24点游戏设计与实现需求分析_毕业设计论文.doc...

    基于Android平台的24点游戏设计与实现 摘要 随着移动设备的普及以及移动设备的硬件的提升,移动设备的功能越来越完善,移动设备的系统平台也日渐火热起来.目前国内最常见的移动开发平台有Symbian ...

最新文章

  1. opencv-python的格式转换 RGB与BGR互转
  2. smarty模板引擎_3-预保留变量
  3. python判断几个数最大最小_python 找出list中最大或者最小几个数的索引方法
  4. 不均衡数据的处理方法
  5. Vue 路由模块化配置
  6. 还在修改博士论文?这份《博士论文写作技巧》为你指南
  7. 谷歌紧急修复已遭在野利用的0day
  8. 至linuxNIC添加多个IP
  9. Oracle 宣布 Java EE 8 推迟到2017年年底发布
  10. 如何利用Camtasia给视频加上配音?
  11. 《Linux/UNIX OpenLDAP实战指南》——1.4 OpenLDAP目录条目概述
  12. Cadence Allegro 差分走线单根走的方法图文教程
  13. 奇妙的数字 小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。你能猜出这个数字是多少吗?
  14. 自在服务器虚拟化,新破晓诛仙3单机版16职业商业仿官青罗天界灵境神隐虚拟机一键端GM网单...
  15. 小学计算机余数在线,余数计算器-余数计算器
  16. 烽火超微信息科技 服务器,智算升级 烽火超微发布新一代V6服务器
  17. 涉密计算机涉密存储设备或者改作其他用途的,将未经安全技术处理的退出使用的涉密计算机涉密存储设备或者改作其他用途的依法给予处分...
  18. 向量化回测系列1——单只股票的回测
  19. base64 hash256 编码不一致问题
  20. 十年前的AlexNet,今天的NeurIPS 2022时间检验奖

热门文章

  1. 抛物型方程向前差分matlab,(整理)微分方程数值解(学生复习题).
  2. C#按Esc后退出对话框
  3. 怎么删除w7桌面计算机图标,w7桌面图标箭头怎么去掉?w7桌面图标箭头去掉方法...
  4. java扩展数组_Java数组扩展
  5. 今天小小的总结一下最近的小程序中的问题
  6. synchronized 关键字的底层原理
  7. [短评]企业裁员行为对整个社会的正面意义
  8. mybatis-plus超详细讲解
  9. VS2008创建win32动态链接库图文流程
  10. mysql导入txt linux_Linux中将txt导入到mysql的方法教程