我只用了几个星期的c,所以我很可能会忽略一些明显的错误。我看过其他主题,但我不太了解我正在阅读的内容。 该计划假设一个无限大的套牌。
已知问题:
当前代码:
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int deal()
{
unsigned long timenow;
timenow = time(NULL);
srand(timenow);
int deck[] ={2,3,4,5,6,7,8,9,10,10,10,10,11};
return deck[rand() % 13];
printf("%d\n", deal());
}
void clearBuffer(){
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int dealerhand()
{
int dealerhand[10];
int dealertotal = 0;
while (dealertotal < 17)
{
dealertotal = deal();
printf("%d\n dx", deal());
dealertotal = dealertotal + deal();
printf("%d\n D", dealertotal);
}
}
int playerhand()
{
int playerhand [10];
int i;
for(i=0; i<1; i++)
{
playerhand[i] = deal();
printf(" %d\n", playerhand[i]);
}
}
int hit()
{
int i;
int playerhand[10];
playerhand[i] = deal();
printf(" %d", playerhand[i]);
}
int main()
{
int hold = 1;
int num;
int num2;
int money = 500;
printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");
while (hold = 1)
{
scanf ("%d",&num);
if (num == 1)
{
printf("Great! Ante is $20, here's your hand!");
playerhand();
playerhand();
dealerhand();
money = money - 20;
printf("You have %d dollars. Hit? 1=y 2=n", money);
//clearBuffer();
scanf("%d", num2);
if (num2 = 1)
{
playerhand();
break;
}
if (num2 = 2)
{
// compare();
}
}
if (num == 2)
{
printf("Too bad, come back soon!");
break;
}
if (num != 1 || num != 2)
{
printf("Sorry what was that? Play a hand? 1=y 2=n.");
while((num = getchar()) =! EOF && num != '\n');
}
}
}
答案 0 :(得分:1)
我试图纠正这个但是它有很多问题,请参阅以下来源并尝试纠正您的逻辑错误,这是我能为您做的最好:
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int deal(void)
{
int deck[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11};
return deck[rand() % 13];
}
void clearBuffer(void)
{
char ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int dealerhand(void)
{
int dealerhand[10];
int dealertotal = 0;
while (dealertotal < 17) {
dealertotal = deal();
printf("%d\n dx", deal());
dealertotal = dealertotal + deal();
printf("%d\n D", dealertotal);
}
/* RETURN INT ... */
}
int playerhand(void)
{
int playerhand[10];
int i;
for (i = 0; i < 10; i++) {
playerhand[i] = deal();
printf(" %d\n", playerhand[i]);
}
/* RETURN INT ... */
}
int hit(void)
{
int i;
int playerhand[10];
/* I think you need for here ... */
for (i = 0; i < 10; i++) {
playerhand[i] = deal();
printf(" %d", playerhand[i]);
}
/* RETURN INT ... */
}
int main(int argc, char *argv[])
{
srand((unsigned)time(NULL));
int hold = 1;
int num;
int num2;
int money = 500;
printf("Welcome to Blackjack! You have $500. Play a hand? 1=y 2=n. ");
while (hold == 1) {
scanf("%d", &num);
if (num == 1) {
printf("Great! Ante is $20, here's your hand!");
playerhand();
playerhand();
dealerhand();
money = money - 20;
printf("You have %d dollars. Hit? 1=y 2=n", money);
scanf("%d", &num2);
if (num2 == 1) {
playerhand();
break;
}
if (num2 == 2);
/* compare(); */
}
if (num == 2) {
printf("Too bad, come back soon!");
break;
}
if (num != 1 || num != 2) {
printf("Sorry what was that? Play a hand? 1=y 2=n.");
while (((num = getchar()) != EOF) && num != '\n');
}
}
}