题目
效果
代码
game.c:是游戏的逻辑
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "card.h"
#define SHUFFLE_TIMES 7
const char suits[4]={'C','D','H','S'};
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;
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表示一张牌,
typedef struct card {
int rank;
char suit;
struct card *next;
} cardT;
cardT *makeCard(int rank, char suit);
cardT *shuffle(cardT *pile);
int countPile(cardT *pile);
card.c:新建牌组,洗牌功能。
#include <stdio.h>
#include <stdlib.h>
#include "card.h"
int countPile(cardT *pile) {
int num=0;
while(pile!=NULL){
num++;
pile=pile->next;
}
return num;
}
void showPile(cardT *pile) {
printf("%d%c\n",pile->rank,pile->suit);
}
cardT *shuffle(cardT *deck) {
int count = countPile(deck);
cardT *cut=deck;
int i=0;
while(i<count/2){
cut=cut->next;
i++;
}
cardT *riffle=cut->next;
cut->next = NULL;
cardT *retdeck=NULL;
while(deck || riffle) {
cardT *temp;
if(deck && (!riffle || drand48()<0.5)) {
temp=deck;
deck=deck->next;
} else if(riffle) {
temp=riffle;
riffle=riffle->next;
}
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
|