Question
//// help correcting my code C++, it doesn't compile for (Card Game) Crazy Eights. /// No Shuffling and ask players name. ///---------------------------------------------------------------------------------------------------------------------- #include #include #include
//// help correcting my code C++, it doesn't compile for (Card Game) Crazy Eights.
/// No Shuffling and ask players name.
///----------------------------------------------------------------------------------------------------------------------
#include
void shuffle (int[], int size);
void PrintCard (int card);
void PrintValue (int card);
void PrintSuit (int card);
int IsValidPlay (int cardPlayed, int inPlayCard);
int SameValue (int card1, int card2);
int SameSuit (int card1, int card2);
void GetNewCard (int card);
int main(void)
{
char proceed;
std::string player;
std::cout << "Please enter player's name: ";
getline(std::cin, player);
char choice, decide, end, middle;
int player_hand[52], i, play, in_play;
int comp_hand[52];
int comp_cards = 0;
int deck[52];
int const deckSize = 52;
int comp_hand_size = 7;
int player_hand_size = 7;
int draw_card = 0;
int player_wins = 0;
int comp_wins = 0;
int playerturn = 0;
FILE *inFile;
FILE *inFile2;
inFile = fopen_s ("project2.txt", "r");
fscanf_s (inFile, "%d %d", &player_wins, &comp_wins);
fclose(inFile);
printf("You and the computer will be dealt 7 cards each. ");
printf("The next card in the deck becomes the in-play card. ");
printf("You can play either a card of the same number or suit as the in-play card. ");
printf("You may however play an 8 at any time and choose the suit. ");
printf("The first player to use all the cards in his hand wins the round. ");
printf(" ");
while (choice != 'P' && choice != 'p' && choice != 'e' && choice != 'E' &&choice != 'L' && choice != 'l') {
printf("Enter P to play, E to exit, or L to load saved game: ");
scanf(" %c", &choice);
}
if (choice == 'E' || choice == 'e') {
printf("Thanks for playing! ");
}
else if (choice == 'p' || choice == 'P') {
printf("Begin Game. ");
}
else if (choice == 'l' || choice == 'L') {
inFile2 = fopen("middle.txt", "r");
fscanf(inFile2, "%d %d %d %d", &player_hand_size, &comp_hand_size, &in_play, &deck);
fclose(inFile2);
}
int card;
shuffle(deck, deckSize);
printf("The in-play card is: ");
PrintCard(deck[14]);
in_play = 14;
printf("Your hand: ");
PrintCard(deck[0]);
PrintCard(deck[2]);
PrintCard(deck[4]);
PrintCard(deck[6]);
PrintCard(deck[8]);
PrintCard(deck[10]);
PrintCard(deck[12]);
player_hand[0] = deck[0];
player_hand[1] = deck[2];
player_hand[2] = deck[4];
player_hand[3] = deck[6];
player_hand[4] = deck[8];
player_hand[5] = deck[10];
player_hand[6] = deck[12];
comp_hand[0] = deck[1];
comp_hand[1] = deck[3];
comp_hand[2] = deck[5];
comp_hand[3] = deck[7];
comp_hand[5] = deck[9];
comp_hand[7] = deck[11];
comp_hand[9] = deck[13];
printf(" ");
for (i = 0; i
comp_cards++;
}
draw_card = deck[15];
printf("The computer has %d cards. ", comp_cards);
while (middle != 'S'&&middle != 's'&&middle != 'P'&&middle != 'p') {
printf("Would you like to save and exit (S) or play (P)?: ");
scanf(" %c", &middle);
}
if (middle == 'S' || middle == 's') {
inFile2 = fopen("middle.txt", "w");
fprintf(inFile2, "%d %d %d %d", player_hand_size, comp_hand_size, in_play, deck);
fclose(inFile2);
}
else if (middle == 'p' || middle == 'P') {
}
while (player_hand_size != 0 || comp_hand_size != 0) {
if (playerturn == 0) {
printf("Which card would you like to play? (Enter 0 to draw card): ");
scanf("%d", &play);
int a = 15;
int b = 7;
if (play == 0) {
do {
printf("You drew: ");
PrintCard(deck[a]);
player_hand[b] = deck[a];
++a;
++b;
player_hand_size++;
printf("Which card would you like to play?: ");
scanf("%d", &play);
} while (IsValidPlay(player_hand[play - 1], deck[in_play]) != 1);
}
PrintCard(player_hand[play - 1]);
if (IsValidPlay(player_hand[play - 1], deck[in_play]) != 1) {
while (IsValidPlay(player_hand[play - 1], deck[in_play]) != 1) {
printf("You can't play that card. ");
printf("Which card would you like to play?: ");
scanf("%d", &play);
}
}
if (IsValidPlay(player_hand[play - 1], deck[in_play]) == 1) {
in_play = play - 1;
printf("The in-play card is now ");
PrintCard(player_hand[play - 1]);
}
printf("Your hand now contains: ");
for (i = 0; i
if (i != (play - 1)) {
PrintCard(player_hand[i]);
}
}
printf(" ");
playerturn = 1;
}
else {
printf("--Computer turn-- ");
int b = comp_hand_size;
int a = in_play;
for (i = 0; i
if (IsValidPlay(comp_hand[i], deck[in_play]) != 1) {
do {
comp_hand[b] = deck[a];
++a;
++b;
comp_hand_size++;
} while (IsValidPlay(comp_hand[play - 1], deck[in_play]) != 1);
}
}
if (IsValidPlay(comp_hand[i], deck[in_play]) == 1) {
for (i = 0; i
if (i == in_play) {
PrintCard(comp_hand[in_play]);
comp_hand_size--;
}
}
}
printf("The computer now has %d cards", comp_hand_size);
playerturn = 1;
}
}
if (player_hand_size == 0) {
printf("You won!");
player_wins++;
}
else if (comp_hand == 0) {
printf("The computer won.");
comp_wins++;
}
printf(" ");
printf("You have %d wins. ", player_wins);
printf("The computer has %d wins. ", comp_wins);
inFile2 = fopen("project2.txt", "w");
fprintf(inFile2, "%d %d", player_wins, comp_wins);
fclose(inFile2);
return 0;
}
int shuffle(int x[], int size) {
int i;
int index1;
int index2;
srand(time(NULL));
for (i = 0; i
x[i] = i + 1;
}
for (i = 0; i
index1 = rand() % size;
index2 = rand() % size;
int temporary = x[index1];
x[index1] = x[index2];
x[index2] = temporary;
}
return i;
}
void PrintCard(int card) {
PrintValue(card);
printf(" of ");
PrintSuit(card);
}
void PrintValue(int card) {
char const *values[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace" };
int const cardsPerSuit = 13;
printf("%s", values[(card - 1) % cardsPerSuit]);
}
void PrintSuit(int card) {
char const *suits[] = { "Diamonds ", "Hearts ", "Clubs ", "Spades " };
int const cardsPerSuit = 13;
printf("%s", suits[(card - 1) / cardsPerSuit]);
}
int IsValidPlay(int cardPlayed, int inPlayCard) {
if (SameValue(cardPlayed, inPlayCard) == 1 || SameSuit(cardPlayed, inPlayCard) == 1) {
return 1;
return 0;
}
}
int SameValue(int card1, int card2) {
int const cardsPerSuit = 13;
if ((card1 - 1) % cardsPerSuit == (card2 - 1) % cardsPerSuit)
return 1;
return 0;
}
int SameSuit(int card1, int card2) {
int const cardsPerSuit = 13;
if ((card1 - 1) / cardsPerSuit == (card2 - 1) / cardsPerSuit)
return 1;
return 0;
}
void GetNewCard(int card) {
PrintCard(++card);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started