gpt4 book ai didi

C Blackjack Lab,指针问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:31 25 4
gpt4 key购买 nike

很抱歉,这没有达到应有的详细程度,但我的时间实在是太紧迫了。我正在尝试编写一个程序来模拟 Blackjack 游戏,该程序使用代表输入文件(模拟牌组)中的纸牌的字符。我还没有完成,但正如我所说,我几乎没有时间把它打出来。我在指针的使用和涉及 main() 中的“if”语句的错误代码方面特别有问题。它说 ranOut(应该用于在文件用完纸牌时结束游戏,在文件中表示为 0)抛出异常:读取访问冲突,nullptr。如果这是微不足道的,我很抱歉,但我仍然是 C 的新手,指针是我存在的祸根。这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


FILE *fpOut;
FILE *fpIn;

#define TRUE 1
#define FALSE 0

int playHand(int *dealerTotal, int *playerTotal, int *ranOut); //Plays one hand

int getCard(int total); //Reads next card from input, assigns value to points

int main() {

if (!(fpOut = fopen("csis.txt", "w")))
{
printf("csis.txt could not be opened for output.");
exit(1);
}


if (!(fpIn = fopen("cards.txt", "r")))
{
printf("cards.txt could not be opened for input.");
exit(1);
}

int *dealerTotal = 0; //Initialize variables to count wins
int *playerTotal = 0;

int *ranOut = 0; //ends loop

while (1)
{
playHand(dealerTotal, playerTotal, ranOut);

if (*ranOut == TRUE)
break;
}

printf("Total Dealer Wins: %d", *dealerTotal);
printf("Total Player Wins: %d", *playerTotal);

fclose(fpIn);
fclose(fpOut);
getchar();

return 0;
}

int playHand(int *dealerTotal,int *playerTotal, int *ranOut)
{
int pCard[10] = { 0 };
int dCard[10] = { 0 };
int dWin = 0;
int pWin = 0;
int i = 0;
int dFin = 0;
int pFin = 0;

while (i < 10)
{
int sumP = pCard[0] + pCard[1] + pCard[2] + pCard[3] + pCard[4] +
pCard[5] + pCard[6] + pCard[7] + pCard[8] + pCard[9];
int sumD = dCard[0] + dCard[1] + dCard[2] + dCard[3] + dCard[4] +
dCard[5] + dCard[6] + dCard[7] + dCard[8] + dCard[9];

if (sumP <= 15)
{
pCard[i] = getCard(sumP);
}
else
pFin = TRUE;
if (sumD <= 16)
{
dCard[i] = getCard(sumD);
}
else
dFin = TRUE;

if (pCard[i] == 42 || dCard[i] == 42)
{
*ranOut = TRUE;
break;
}

sumP = pCard[0] + pCard[1] + pCard[2] + pCard[3] + pCard[4] + pCard[5] +
pCard[6] + pCard[7] + pCard[8] + pCard[9];
sumD = dCard[0] + dCard[1] + dCard[2] + dCard[3] + dCard[4] + dCard[5] +
dCard[6] + dCard[7] + dCard[8] + dCard[9];

if (sumP > 21 || sumD == 21)
{
dWin = TRUE;
pWin = FALSE;
}
else if (sumD > 21 || sumP == 21)
{
dWin = FALSE;
pWin = TRUE;
}
else if (dFin && pFin)
{
if ((21 - sumP) > (21 - sumD))
{
dWin = TRUE;
pWin = FALSE;
}
else
{
dWin = FALSE;
pWin = TRUE;
}
}



if (dWin)
{
*dealerTotal++;
break;
}
else if (pWin)
{
*playerTotal++;
break;
}

i++;
}

return 1;

}

int getCard(int total)
{
char x;
int y = 0;


if (!feof(fpIn))
{
x = fgetc(fpIn);
switch (x)
{
case '0':
y = 42;
break;

case '1':
y = 1;
break;
case '2':
y = 2;
break;
case '3':
y = 3;
break;
case '4':
y = 4;
break;
case '5':
y = 5;
break;
case '6':
y = 6;
break;
case '7':
y = 7;
break;
case '8':
y = 8;
break;
case '9':
y = 9;
break;
case 'T':
y = 10;
break;
case 'K':
y = 10;
break;
case 'Q':
y = 10;
break;
case 'J':
y = 10;
break;
case 'A':
if(total > 10)
y = 1;
else if (total >= 10)
y = 11;
break;
}
}
return y;
}

最佳答案

int *ranOut = 0; //ends loop

这声明了一个指向 0 的整数指针,因此它是一个 NULL 指针。

尝试将 ranOut 声明为整数

int ranOut = 0; //ends loop

然后将其地址传递给playHand()

playHand(dealerTotal, playerTotal, &ranOut);

当然,在 Main()

中将其作为整数访问
if (ranOut == TRUE)

关于C Blackjack Lab,指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com