Why is my rand() function not working properly - c

So I am writing a gambling program for my c programming class. Essential its a menu for a dog track and you have to place a wager, select a dog, and the program chooses a winner and tells you if you won (how much) or lost. I use the ran() function but a specific dog that I set to win 10% of the time wins more than any other dog (including one that should win 40% of the time).
/*Programmer: John S. Bolen*/
/*Date: 3/1/19*/
/* User Will Choose to gamble, View Results, Or Quit program.
There are 9 Dogs to Chose from with a set %chance to win and
a set Payout Rate */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define SIZE 1000
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH nothingFlush()
#define SIZE 1000
void nothingFlush() {
char nothing;
while (scanf("%c", &nothing) == NULL);
}
typedef struct {
char name[100];
float payout;
float winOdds;
} DOG;
//Prototype Functions
void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
int countTwo, int countThree, int countFour, int countFive, int countSix,
int countSeven, int countEight, int countNine);
void displayMenu();
void getBet(float *bet);
char getChoice();
float getOdds();
void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
int *countSeven, int *countEight, int *countNine);
main() {
char userChoice;
DOG dog[SIZE];
int counter = 0;
int countOne = 0, countTwo = 0, countThree = 0, countFour = 0,
countFive = 0, countSix = 0, countSeven = 0, countEight = 0,
countNine = 0;
int winners[SIZE] = { 0 };
float bet = 0;
do {
userChoice = getChoice();
switch (userChoice) {
case 'G': // Gamble
getBet(&bet);
pickDog(dog, &counter, winners, &bet, &countOne, &countTwo, &countThree,
&countFour, &countFive, &countSix, &countSeven, &countEight, &countNine);
break;
case 'R': //Results
displayAllRaces(dog, winners,counter, countOne, countTwo, countThree, countFour,
countFive, countSix, countSeven, countEight, countNine);
break;
case 'L': //Leave
printf("\n\tThank you for visiting the Dog Track\n");
break;
default:
printf("\n\tERROR! -- Enter A Valid Selection...\n");
break;
}//End Switch
PAUSE;
} while (userChoice != 'L');
}
void displayAllRaces(DOG dog[], int winners[], int counter, int countOne,
int countTwo, int countThree, int countFour, int countFive, int countSix,
int countSeven, int countEight, int countNine) {
int i = 0;
CLS;
printf("\n\tRace Results: \n");
printf("\tDog\t\t\t\t\tNum of Wins\n");
if (counter > 0) {
printf("\t%s\t\t\t%i\n", dog[0].name, countOne);
printf("\t%s\t\t\t%i\n", dog[1].name, countTwo);
printf("\t%s\t\t%i\n", dog[2].name, countThree);
printf("\t%s\t\t%i\n", dog[3].name, countFour);
printf("\t%s\t\t%i\n", dog[4].name, countFive);
printf("\t%s\t\t%i\n", dog[5].name, countSix);
printf("\t%s\t\t%i\n", dog[6].name, countSeven);
printf("\t%s\t\t\t%i\n", dog[7].name, countEight);
printf("\t%s\t\t\t%i\n", dog[8].name, countNine);
}
else {
printf("\n\t\n");
}
}
void displayMenu(){
CLS;
printf("\n\t==================================================\n");
printf("\n\t== MAIN MENU ==\n");
printf("\n\t==================================================\n");
printf("\n\t[G]amble\n");
printf("\n\t[R]esults\n");
printf("\n\t[L]eave\n");
printf("\n\tEnter Your Selection: ");
}
void getBet(float *bet) {
float result = 0;
printf("\n\tEnter the amount you want to gamble: ");
printf("\n\t(Bet has to be between $1 and $1000)\n");
scanf_s("%f", &result); FLUSH;
if(result < 1 || result > 1000){
printf("\n\tERROR-- Please enter a valid amount\n");
printf("\n\tEnter the amount you want to gamble: ");
printf("\n\t(Bet has to be between $1 and $1000)\n");
scanf_s("%f", &result); FLUSH;
}
printf("\n\tThank you for placing your wager.\n\tGood luck!\n");
*bet = result;
PAUSE;
}
char getChoice() {
char result;
displayMenu();
scanf_s("%c", &result); FLUSH;
result = toupper(result);
return result;
}
float getOdds() {
float odds[9] = { 40, 10, 8, 6, 1, 4, 8, 10, 13 };
return odds[9];
}
void pickDog(DOG dog[], int *counter, int winners[], float *bet, int *countOne,
int *countTwo, int *countThree, int *countFour, int *countFive, int *countSix,
int *countSeven, int *countEight, int *countNine) {
int i = 0;
int choice;
float betMoney = 0;
int winner = 0;
CLS;
printf("\n\tPick a dog from the list:");
strcpy(dog[0].name, "\n\t1.Gandalf the Great");
strcpy(dog[1].name, "\n\t2.Fenrir Greyback");
strcpy(dog[2].name, "\n\t3.Marley the Magnificent");
strcpy(dog[3].name, "\n\t4.Clifford the Big Red Dog");
strcpy(dog[4].name, "\n\t5.Petey the Little Rascal");
strcpy(dog[5].name, "\n\t6.Courage the Cowardly Dog");
strcpy(dog[6].name, "\n\t7.Old Yeller Before the Bullet");
strcpy(dog[7].name, "\n\t8.Pongo the Dalmatian");
strcpy(dog[8].name, "\n\t9.Nymeria Stark\n");
for (i = 0; i < 9; i++) {
printf("%s", dog[i].name);
}
printf("\n");
scanf_s("%i", &choice); FLUSH;
do {
if (choice < 1 || choice > 9) {
printf("\n\tPlease, try again...\n");
scanf_s("%i", &choice);
}
} while (choice < 1 || choice > 9);
betMoney = *bet;
winner = 1 + rand() % (100 - 1 + 1);
if (winner <= 40) {
winner = 1;
(*countOne)++;
}
else if (winner <= 50) {
winner = 2;
(*countTwo)++;
}
else if (winner <= 48) {
winner = 3;
(*countThree)++;
}
else if (winner <= 64) {
winner = 4;
(*countFour)++;
}
else if (winner <= 65) {
winner = 5;
(*countFive)++;
}
else if (winner <= 69) {
winner = 6;
(*countSix)++;
}
else if (winner <= 77) {
winner = 7;
(*countSeven)++;
}
else if (winner <= 87) {
winner = 8;
(*countEight)++;
}
else if (winner <= 100) {
winner = 9;
(*countNine)++;
}
winners[*counter] = winner;
(*counter)++;
if (choice == winner) {
printf("\n\tYou picked a WINNER!\n");
if (winner == 1) {
(*bet) = betMoney * 2;
printf("You win $%.2f\n", *bet);
}
if (winner == 2) {
(*bet) = betMoney * 5;
printf("You win $%.2f\n", *bet);
}
if (winner == 3) {
(*bet) = betMoney * 10;
printf("You win $%.2f\n", *bet);
}
if (winner == 4) {
(*bet) = betMoney * 15;
printf("You win $%.2f\n", *bet);
}
if (winner == 5) {
(*bet) = betMoney * 50;
printf("You win $%.2f\n", *bet);
}
if (winner == 6) {
(*bet) = betMoney * 20;
printf("You win $%.2f\n", *bet);
}
if (winner == 7) {
(*bet) = betMoney * 10;
printf("You win $%.2f\n", *bet);
}
if (winner == 8) {
(*bet) = betMoney * 5;
printf("You win $%.2f\n", *bet);
}
if (winner == 9) {
(*bet) = betMoney * 3;
printf("You win $%.2f\n", *bet);
}
}
else {
printf("\n\tYou picked a LOSER!\n");
(*bet) = (*bet) - betMoney;
}
}
I have included the entire code for my program here (so you can test it if you wish), but only need help in fixing my rand() function. As you can see I want to randomly chose a number between 1-100. If the number is <= 40 then its dog one 40% chance to win, if the number is <= 50 (meaning its <= 50 but >40)then dog 2 wins with a 10% chance to win. I have run the program numerous times and dog 2 always seems to have the most wins after I run it x amount of times. Any help or suggestions would be greatly appreciated!

Related

Error displaying in "Guess my number" game

I am learning C programming and now I am trying to code this program called "Guess my number" whereby player 1 will pick a number in a range, and then player 2 will guess the number. If the number of guesses exceed 10 tries, player 1 will win. However, the code program breaks after 10 tries and does not display the "Player 1 wins". Anybody can help me in this? Thanks.
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
return 0;
if (count == maxguesses) {
printf("Player 1 wins");
}
}
You should put your return 0 statement after checking if count == maxguesses.
#include <stdio.h>
#include <stdbool.h>
#define boolean
int main()
{
int enternumber = -1;
int count = 0;
int maxguesses = 10;
int guessing;
int i;
bool currentguesses = false;
while (1) {
count += 1;
printf("Player 1, enter a number between 1 and 1000\n");
scanf("%d", &enternumber);
if (enternumber > 1000) {
printf("That number is out of range\n");
}
else {
printf("That number is in the range\n");
break;
}
}
printf("Player 2, you have %d tries remaining\n", maxguesses);
for (i = 0; i < maxguesses; i++) {
printf("Enter your guess\n");
scanf("%d", &guessing);
if (enternumber == guessing) {
printf("Player 2 wins.\n");
break;
}
else {
printf("Too %s.\n", enternumber < guessing ? "high" : "low");
}
}
if (count == maxguesses) {
printf("Player 1 wins");
}
return 0;
}

Why does it say 'expected declaration specifiers before 'main''

first of all, I understand mostly everything in this program that I copied from this book.
second, i just wanted to see if it worked ,
the only problem is that it says 'expected declaration specifiers before 'main''
and i don't know what it means
ps this is a really long program (300+ lines)
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank[], int cardSuit[]);
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
int analyzeHand(int ranksinHand[], int suitsinHand[]);
main()
{
int bet;
int bank = 100;
int i;
int cardRank [5];
int cardSuit [5];
int finalRank[5];
int finalSuit[5];
int ranksinhand[13];
int suitsinhand[4];
int winnings;
time_t t;
char suit, rank, stillPlay;
printGreeting();
do{
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(cardsSuit[i]);
rank = getRank(cardRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
for (i=0; i < 4; i++)
{
suitsinHand[i] = 0;
}
for (i=0; i < 13; i++)
{
ranksinHand[i] = 0;
}
getFinalHand(cardRank, cardSuit, finalRank, finalSuit, ranksinHand,
suitsinHand);
printf("Your five final cards:\n\n");
for (i = 0; i < 5; i++)
{
suit = getSuit(finalSuit[i]);
rank = getRank(finalRank[i]);
printf("Card #%d: %c%c\n\n", i+1, rank, suit);
}
winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n\n", bet*winnings);
bank = bank - bet + (bet*winnings)
printf("\n\nYour bank is now %d.\n\n", bank);
printf("Want to play again? ");
scanf(" %c", &stillPlay);
}while (toupper(stillPlay) == 'Y');
return;
}
/*************************************************************************/
void printGreeting();
{
printf("**********************************************************\n\n");
printf("\n\n\tWelcome to the Absolute Beginner's Casino\n\n");
printf("\tHome of the Video Draw Poker");
printf("**********************************************************\n\n");
printf("Here are the rules\n");
printf("You start with 100 credits, and you make a bet from");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and then you choose which ");
printf("cards to keep");
printf("or discard\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair \t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight \t\t\t4 credits");
printf("Flush\t\t\t\t5 credits");
printf("Full House\t\t\t8 credits");
printf("Four of a Kind\t\t\t10 credits");
printf("Straight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}
void getFirstHand(int cardRank[], int cardSuit[]);
{
int i,j;
int carDup;
for(i=0; i < 5; i++)
{
carDup = 0;
do{
cardRank[i] = (rand() % 13);
cardSuit[i] = (rand() % 4);
for (j=0; j < i; j++)
{
if ((cardRank[i] == cardRank[j] &&
cardSuit[i] == cardSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1;);
}
}
char getSuit(int suit)
{
switch
{
case 0:
return('C');
case 1:
return('D');
case 2:
return('H');
case 3:
return('S');
}
}
char getRank(int rank)
{
switch (rank)
{
case 0:
return('A');
case 1:
return('2');
case 2:
return('3');
case 3:
return('4');
case 4:
return('5');
case 5:
return('6');
case 6:
return('7');
case 7;
return('8');
case 8:
return('9');
case 9:
return('T');
case 10:
return('J');
case 11:
return('Q');
case 12:
return('K');
}
}
int getBet()
{
int bet;
do
{
printf("How much do you want to bet?(Enter a number");
printf("from 1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet);
if (bet >= 1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game\n\n");
}
}while ((bet < 0) || (bet > 5));
}
int analyzeHand(int ranksinHand[], int suitsinHand[])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++;
if(num_consec == 5) {
straight = TRUE;
}
for (rank = 0; rank < 13; rank++){
if (ranksinHand[rank] == 4)
four == TRUE;
if (ranksinHand[rank] == 3)
three == TRUE;
if (ranksinHand[rank] == 2)
pairs++;
}
if (straight && flush){
printf("Straight Flush\n\n");
return(20);
}
else if (four){
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1){
printf("Full House\n\n");
return (8);
}
else if (flush){
printf("Flush\n\n");
return (5);
}
else if (straight){
printf("Straight\n\n");
return (4);
}
else if (three){
printf("Three of a Kind\n\n");
return (3);
}
else if (pairs == 2){
printf("Two Pairs\n\n");
return (2);
}
else if (pairs == 1){
printf("Pair\n\n");
return (1);
}
else{
printf("High Card\n\n");
return (0);
}
}
void getFinalHand
(int cardRank[], int cardSuit[], int finalRank[], int finalSuit[], int
ranksinHand[], int suitsinHand[])
{
int i, j, carDup;
char suit, rank, ans;
for (i=0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Do you want to keep card #%d: %c%c", i+1, rank, suit);
printf("\nPlease answer (Y/N):");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
finalRank[i] = cardRank[i];
finalSuit[i] = cardSuit[i];
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
continue;
}
else if (toupper(ans) == 'N')
{
carDup = 0;
do{
carDup = 0;
finalRank[i] = (rand() % 13);
finalSuit[i] = (rand() % 4);
for (j=0; j < 5; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
for (j=0; j < i; j++)
{
if((finalRank[i] == finalRank[j]) && (finalSuit[i] ==
finalSuit[j]))
{
carDup = 1;
}
}
}while (carDup == 1);
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
}
}
}
By convention main() must return an integer. You have to declare it like that:
int main()
{
// Your method
return 0;
}
int mainor
void main
would do the work. Here, int main is shown to be the C standard. Refer to
Return type of main() too.

blackjack program multiple issues (dealing, errors, hit)

I've only been using c for a couple of weeks, so there will very likely be obvious errors I've overlooked. I've looked at other threads, but I don't understand a lot of what I'm reading.
The program assumes an infinitely large deck.
KNOWN ISSUES:
clearBuffer isn't currently in use, I am trying different things.
dealerhand doesn't add values properly.
compare function doesn't exist yet.
Multiple non numeric inputs result in repeating of error message.
Attempting to "hit" crashes program.
Current code:
#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');
}
}
}
I tried to correct this but it have many issue see following source and try to correct your logical errors, this is best I can do for you:
#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');
}
}
}

Program Not Returning Results as Expected. Probably Misuse of "bool"?

I'm new to programming and I had to work on a program that would simulate 10,000 games of craps. I got it to calculate points for house and player just fine until I added in the function "diceRoll" where player rolls again and again until it matches the first roll or 7 (house wins). Now it gives decidedly not random results (such as the house winning 0 times out of 10,000). What did I do wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
bool diceRoll (int a)
{
srand( (unsigned)time(NULL));
int n = 0;
int b = 0;
while(n < 1) {
b = rand() % 12;
if(b == a || b == 6) n++;
}
if(b == 6) return false;
else return true;
}
int main (void)
{
srand( (unsigned)time(NULL));
int a, n, house, player, point;
house = 0;
player = 0;
point = 0;
for(n = 0; n < 10000; n++) {
a = rand() % 12;
if(a == 1 || a == 2 || a == 11) {
house++;
}
else if(a == 6 || a == 10) {
player++;
}
else {
if(diceRoll(a) == true) player++;
else house++;
}
}
printf("The house has %i points.\n", house);
printf("The player has %i points.\n", player);
return 0;
}
You've over-seeded, remove the call to srand() in diceRoll and you should be fine (this ignores bias due to modulo usage).
Only seed in main() (and not in a loop) and don't seed it in the diceRoll(a) function.
I ran it your way and got house = 2, player = 9998.
Removing the srand((unsigned)time(null)); in the diceroll(a) came back with:
The house has 5435 points
The player has 4565 points
I assume that is what you wanted
bool diceRoll (int a)
{
int n = 0;
int b = 0;
while(n < 1) {
b = rand() % 12;
if(b == a || b == 6) n++;
}
if(b == 6) return false;
else return true;
}
int main (void)
{
srand( (unsigned)time(NULL));
int a, n, house, player, point;
house = 0;
player = 0;
point = 0;
for(n = 0; n < 10000; n++) {
a = rand() % 12;
if(a == 1 || a == 2 || a == 11) {
house++;
}
else if(a == 6 || a == 10) {
player++;
}
else {
if(diceRoll(a) == true) player++;
else house++;
}
}
printf("The house has %i points.\n", house);
printf("The player has %i points.\n", player);
return 0;
}

Trying to get this display function to work

So I'm trying to get this code to draw a card, and then each time printcards in called it prints the previous card and the new card. Right now it it prints off one card and then another while I want it to constantly update the hand every time it's called.
example:
first call
prints: Ace of Spades
Second call:
Prints: Ace of Spades, 2 of Hearts
etc...
for sake of agrument assume the hand will never grow bigger than 10 cards.
Any help will be appreciated.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 52
enum faces{Ace = 0, Jack = 10, Queen, King};
char * facecheck(int d);
void shuffle( int deck[]);
void draw(int deck[SIZE]);
void printcards(int hand[], int numCards);
void players(int deck[]);
int question();
int i, //numCards = 1;
int top = 52;
int main()
{
int deck[SIZE], i, a;
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
srand( time( NULL ) ) ;
for(i = 0; i<SIZE; i++)
{
deck[i] = i;
};
shuffle(deck);
players(deck);
return 0;
}
char * facecheck(int d)
{
static char * face[] =
{
"Ace",
"Jack",
"Queen",
"King" };
if(d == Ace)
return face[0];
else
{
if(d == Jack)
return face[1];
else
{
if(d == Queen)
return face[2];
else
{
if(d == King)
return face[3];
}
}
}
}
void shuffle( int deck[])
{
int i, j, temp;
for(i = 0; i < SIZE; i++)
{
j = rand() % SIZE;
temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
printf("The deck has been shuffled \n");
}
void draw(int deck[SIZE])
{
int numCards = 1;
int i;
int hand[numCards];
int card;
for(i = 0; i < numCards && top > 0; i++)
{
card = deck[top-1];
hand[i] = card;
top--;
}
printcards(hand, numCards);
//numCards++;
}
void printcards(int hand[], int numCard)
{
char suits[4][9] =
{
"Hearts",
"Diamonds",
"Clubs",
"Spades"};
for(i = 0; i < numCard; i++)
{
int card = hand[i];
if(card%13 == 0 || card%13 == 10 || card%13 == 11 || card%13 == 12)
printf("%s ", facecheck(card%13) );
else
printf("%d ", card%13+1);
printf("of %s \n", suits[card/13]);
}
}
void players(int deck[])
{
int x;
int a;
int yourhand[10];
a = 1;
printf("Player 1 \n");
printf("Your Hand is: \n");
draw(deck);
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(x == 1)
{
draw(deck);
}
else
{
a--;
}
}
}
First, in general, your functions should perform one distinct, well-defined piece of work. In this case, you probably don't want your card drawing function to also be responsible for printing output. In addition, it's not clear what the players function logically represents. I'd recommend you refactor the code with these points in mind.
Second, you'll need to persist your hand state between card draws. You hinted at this with int yourhand[10]; in the players function, but you never used it.
I adjusted the draw function to return the card it pulled, and to update your total hand between draws:
Draw Function
int draw(int deck[SIZE])
{
int numCards = 1;
int i;
int hand[numCards];
int card;
for(i = 0; i < numCards && top > 0; i++)
{
card = deck[top-1];
hand[i] = card;
top--;
}
return card;
//numCards++;
}
Players Function
void players(int deck[])
{
int x;
int a;
int yourhand[10];
int handIndex = 0;
a = 1;
printf("Player 1 \n");
printf("Your Hand is: \n");
while(a == 1)
{
printf("What would you like to do: Press 1 to Draw. 2 to Stay. \n");
scanf("%d" , &x);
if(handIndex == 9)
{
break;
}
else if(x == 1)
{
yourhand[handIndex] = draw(deck);
}
else
{
a--;
}
printcards(yourhand, handIndex+1);
handIndex++;
}
}

Resources