在21点程序中处理ACE和查找segfault
这是我到目前为止所拥有的。。。 我还没有弄清楚如何用王牌来处理11/1的情况,当玩家选择命中/站立选项时,我会出错 救命啊 更新代码在21点程序中处理ACE和查找segfault,c,gcc,blackjack,C,Gcc,Blackjack,这是我到目前为止所拥有的。。。 我还没有弄清楚如何用王牌来处理11/1的情况,当玩家选择命中/站立选项时,我会出错 救命啊 更新代码 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #define DECKSIZE 52 #define VALUE 9 #define FACE 4 #define HANDSIZE 26 typedef
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define DECKSIZE 52
#define VALUE 9
#define FACE 4
#define HANDSIZE 26
typedef struct {
int value;
char* suit;
char* name;
}Card;
/*typedef struct {
int value;
char* suit;
char* name;
}dealerHand;
typedef struct {
int value;
char* suit;
char* name;
}playerHand;*/ //trying something different
Card cards[DECKSIZE];
/*dealerHand deal[HANDSIZE]; //trying something different
playerHand dealt[HANDSIZE];*/
char *faceName[]={"two","three", "four","five","six", "seven","eight","nine",
"ten", "jack","queen", "king","ace"};
char *suitName[]={"spades","diamonds","clubs","hearts"};
Card *deal[HANDSIZE];
Card *dealt[HANDSIZE];
void printDeck(){
int i;
for(i=0;i<DECKSIZE;i++){
printf("%s of %s value = %d\n ",cards[i].name,cards[i].suit,cards[i].value);
if((i+1)%13==0 && i!=0) printf("-------------------\n\n");
}
}
void shuffleDeck(){
srand(time(NULL));
int this;
int that;
Card temp;
int c;
for(c=0;c<10000;c++){ //c is the index for number of individual card shuffles should be set to c<10000 or more
this=rand()%DECKSIZE;
that=rand()%DECKSIZE;
temp=cards[this];
cards[this]=cards[that];
cards[that]=temp;
}
}
/*void hitStand(i,y){ // I dumped this because of a segfault i couldn't figure out.
int k;
printf(" Press 1 to HIT or press 2 to STAND:");
scanf("%d",k);
if(k=1){
dealt[y].suit=cards[i].suit;
dealt[y].name=cards[i].name;
dealt[y].value=cards[i].value;
y++;
i++;
}
}
*/
int main(){
int suitCount=0;
int faceCount=0;
int i;
int x;
int y;
int d;
int p;
int k;
for(i=0;i<DECKSIZE;i++){ //this for statement builds the deck
if(faceCount<9){
cards[i].value=faceCount+2;
}else{ //assigns face cards as value 10
cards[i].value=10;
}
cards[i].suit=suitName[suitCount];
cards[i].name=faceName[faceCount++];
if(faceCount==13){ //this if loop increments suit count once
cards[i].value=11; //all faces have been assigned, and also
suitCount++; //assigns the ace as 11
faceCount=0;
} //end building deck
}
/*printDeck(); //prints the deck in order
shuffleDeck(); //shuffles the deck
printDeck(); //prints the deck as shuffled
This was used in testing, commented out to keep the deck hidden!*/
shuffleDeck();
x=0;
y=0;
for(i=0;i<4;i++){ //this for loop deals the first 4 cards,
dealt[y]=&cards[i]; //first card to player, second to dealer, as per standard dealing practice.
i++;
y++;
deal[x]=&cards[i];
x++;
}
printf(" Dealer's hand is: %s of %s and XXXX of XXXX. (Second card is hidden!)\n",deal[0]->name,deal[0]->suit,deal[1]->name,deal[1]->suit);
printf(" Player's hand is: %s of %s and %s of %s.\n",dealt[0]->name,dealt[0]->suit,dealt[1]->name,dealt[1]->suit);
printf(" the current value of the index i=%d\n",i); //this line gave me the value of i for testing
d=deal[0]->value+deal[1]->value;
p=dealt[0]->value+dealt[1]->value;
if(d==21){
printf(" The Dealer has Blackjack! House win!\n");
}else{
if(d>21){
printf(" The dealer is Bust! You win!\n");
}else{
if(d>17){
printf(" Press 1 to HIT or 2 to STAND: ");
scanf("%d",&k);
if(k==1){
dealt[y]=&cards[i];
y++;
i++;
}
}else{
if(d<17){
printf(" Dealer Hits!");
deal[x]=&cards[i];
x++;
i++;
}
}
}
}
return 0;
}
#包括
#包括
#包括
#包括
#定义尺寸52
#定义值9
#定义面4
#定义掌上电脑26
类型定义结构{
int值;
char*套装;
字符*名称;
}卡片;
/*类型定义结构{
int值;
char*套装;
字符*名称;
}经销商;
类型定义结构{
int值;
char*套装;
字符*名称;
}playerHand;*///尝试不同的东西
卡片[卡片大小];
/*dealerHand交易[手推]//尝试不同的东西
playerHand处理[手推];*/
char*faceName[]={“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”,
“十”、“杰克”、“女王”、“国王”、“王牌”};
char*suitName[]={“黑桃”、“钻石”、“梅花”、“红心”};
卡*交易[手推];
发牌[手牌];
void printDeck(){
int i;
对于(i=0;i要修复segfault,请使用scanf(“%d”,&k);
而不是scanf(“%d”,k);
(注意我添加的符号。您需要它,因为scanf
的第二个参数是一个指针,指向它应该存储读入内容的位置。k
本身不是指针——添加&
会得到一个指向k
的指针
对于处理ACE,在什么条件下,ACE的值从11变为1?哪一行在您的代码中,您希望必须触摸才能启用它吗?我可以想出一种方法来实现它,其中包括一个变量,该变量跟踪计算一名球员的总得分时使用了多少个a,或者另一种方法是及时重新计算分数以使用它。请参见如何处理ace情况
作为关于代码的一般提示,基本上相同的结构有三次(Card、dealerHand、playerHand)。只需为卡定义一次结构就足够了,例如,将指向卡的指针存储为指针。一般来说:启用警告的编译往往很有帮助(默认情况下,gcc帮不了你多少忙!)
比较:
$ gcc -o blackjack blackjack.c
$
与:
这就立刻发现了scanf()
问题(还有另一个!)。好吧……听起来很简单……但我在这方面很在行,我甚至不知道从哪里开始做……例如,删除dealerHand
和playerHand
结构,并像Card*deal[HANDSIZE]那样声明它们的数组;
然后分配卡片,如deal[0]=&cards[51];
。然后您可以从手上引用卡片的数据,如deal[0]->西服
。一方面,这样你每次发一张卡时就不用复制三条数据。呃……转换所有内容需要几分钟时间……我在那里时会编辑帖子……好的,我已经更新了……它似乎已经编译并运行了……所有卡片的合计仍然让我心神不宁……我不敢相信我错过了这一点segfault。谢谢。我正在考虑ace问题,比如d>21和(不知道如何编码)交易[(任何卡值)].value==11然后将值更改为1。但是,我不知道如何编码…可能应该在第134行左右…@Bill,我认为你的思路是正确的。记下到目前为止你给每个玩家发了多少张a怎么样。需要时,从玩家的分数中减去10,然后减少玩家的a数如果专业人士认为这是必要的,那么学习者根本没有理由认为他们不需要它。我还想添加-std=c99-pedantic
或-ansi-pedantic
-1,因为没有启用警告进行编译-我理解这可能会有伤害,但我宁愿这样做你对我很生气,以后永远不要忘记使用编译器警告。我改进了你的标题。语言、编译器以及它是家庭作业这一事实应该用标记表示;标题是用来描述问题或问题的。我如何(不使用邪恶的GOTO)在if语句中获得EOF(在p>21或d>21的情况下)?
$ gcc -Wall -o blackjack blackjack.c
blackjack.c: In function 'main':
blackjack.c:124: warning: too many arguments for format
blackjack.c:139: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
$