【发布时间】:2016-04-11 00:51:50
【问题描述】:
我正在用 C 语言开发二十一点游戏。我有三个功能,一个用来装牌,一个用来洗牌,一个用来发牌。我的问题是我不知道如何给我的卡片一个整数值,我需要它来看看谁赢了。对于如何解决这个问题,我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
/*fill deck with 52 cards*/
void fillDeck(Card * const Deck, const char *suit[], const char *deck[]){
int s;
for (s = 0; s < 52; s++){
Deck[s].suits = deck[s % 13];
Deck[s].decks = suit[s / 13];
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
/*shuffle cards*/
void shuffle(Card * const Deck){
int i, j;
Card temp;
for (i = 0; i < 52; i++){
j = rand() % 52;
temp = Deck[i];
Deck[i] = Deck[j];
Deck[j] = temp;
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "func.h"
/*deal cards*/
void deal(const Card * const Deck, int size, int size_1, int size_2){
int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];
printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);
printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
printf("\n%s's card:\n", name2);
for (i = 2; i < size_1; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
printf("\nDealer card:\n");
for (i = 4; i < size_2; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "func.h"
int main(void){
Card allCards[52];
const char *suits[] = { "spades", "hearts", "diamonds", "clubs" };
char *decks[] = { "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king" };
int *values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
srand(time(NULL));
fillDeck(allCards, suits, decks, values);
shuffle(allCards);
deal(allCards, 2, 4, 6);
getchar();
return 0;
}
/*func.h*/
struct card{
const char *suits;
const char *decks;
};
typedef struct card Card;
void fillDeck(Card * const Deck, char *suit[], char *deck[]);
void shuffle(Card * const Deck);
void deal(const Card * const Deck, int size, int size_1, int size_2);
#endif
【问题讨论】:
-
到底是什么问题?不是很清楚。
-
"I don't know how to give my cards an integer value"的代码过多。你在哪里尝试这样做? -
给牌编号
0..51,然后花色可以是card / 13,等级可以是card % 13。西装编号为0..3,等级编号为0..12。 -
我无法从他的问题中看出,但重要的是要注意 ENUMS 可以很容易地做到这一点,因为您可以分配 ENUMS 在幕后的“隐藏”值。
-
使用
enums 定义名次和花色,使用字符串数组将索引值0...转换为名次和花色。
标签: c blackjack const-char