Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//// help correcting my code C++, it doesn\'t compile for (Card Game) Crazy Eigh

ID: 3728039 • Letter: #

Question

//// help correcting my code C++, it doesn't compile for (Card Game) Crazy Eights.

/// No Shuffling and ask players name.

///----------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

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);

}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <time.h>

#define SIZE 52

#define HAND 7

void shuffleDeck(int deck [], int n);

void deal(int deck [], int playerHand [], int compHand []);

void RevealCard(int deck[], int position, float card[]);

int main(void)

{

char proceed;

int deck [SIZE], playerHand[SIZE], compHand [SIZE], discardDeck[SIZE];

int firstPlayer, i, position, cPlayerHand, cDiscardDeck, cDiscardDeck2;

float card [2];

//seed random number generator

srand(time(NULL));

printf("***************Welcome to CRaZy 8iGhTs!**************** ");

printf("You and the computer are both going to be delt seven ");

printf("cards. Then the top card in the remaining deck will be ");

printf("displayed and becomes the in-play card. Depending on who ");

printf("delt, determines who lays first. The card layed must ");

printf("match thein-play card in face value or suite. If this ");

printf("cannot be done, then cards must be drawn until it is ");

printf("possible to lay. Once a card is layed it becomes the new ");

printf("in-play card. The first player to exhaust their hand ");

printf("wins! If the deck is exhausted before there is a winner, ");

printf("the discard deck is reshuffled and put back into play. ");

printf("***************Have fun and good luck!***************** ");

printf("Do you wish to play? (Y or N) ");

scanf("%c", &proceed);

proceed = toupper(proceed);

while (proceed != 'Y' && proceed != 'N')

{

printf("Enter Y or N: ");

fflush(stdin);

scanf("%c", &proceed);

proceed = toupper(proceed);

}

if (proceed == 'N')

exit(0);

else

{

printf("Lets play! ");

for (i = 0; i < SIZE; i++)

deck[i]= i + 1;

shuffleDeck(deck, SIZE);

deal(deck, playerHand, compHand);

//randomly decide if computer or player goes first

//firstPlayer = (rand() % 2);

firstPlayer = 1;

if (firstPlayer == 0)

{

printf("The computer deals first. ");

printf("Shuffling the deck... ");

//TESTING: DISPLAY SHUFFLED DECK

printf("Shuffled deck ... ");

for(i = 0; i < SIZE; i++)

printf("Card # %d: %d ",i+1,deck[i]);

printf("You get 7 cards, and they are: ");

printf("%d %d %d %d %d %d %d ", playerHand [0],

playerHand [1], playerHand [2], playerHand [3],

playerHand [4], playerHand [5], playerHand [6]);

printf("The Computer gets 7 cards. ");

/*printf("%d, %d, %d, %d, %d, %d, %d ", compHand [0],

compHand [1], compHand [2], compHand [3],

compHand [4], compHand [5], compHand [6]);*/

}

else

{

printf("You deal first. ");

printf("Shuffling the deck... ");

//TESTING: DISPLAY SHUFFLED DECK

printf("Shuffled deck ... ");

for(i = 0; i < SIZE; i++)

printf("Card # %d: %d ",i+1,deck[i]);

printf("The Computer gets 7 cards. ");

/* printf("You get 7 cards, and they are: ");

printf("%d %d %d %d %d %d %d ", playerHand [0],

playerHand [1], playerHand [2], playerHand [3],

playerHand [4], playerHand [5], playerHand [6]); */

//display player's hand

for(cPlayerHand = 1; cPlayerHand < HAND + 1; cPlayerHand++)

{

RevealCard(playerHand, cPlayerHand, card);

}

printf(" %d ", deck[15]);

printf("The current card to match is the ");

cDiscardDeck = 1;

discardDeck [cDiscardDeck] = deck[15];

cDiscardDeck2 = 0;

RevealCard(discardDeck, cDiscardDeck2, card);

printf(" %d", discardDeck[0]);

}

}

return 0;

}

void shuffleDeck(int deck[], int n)

{

int temp, dummy, d;

for (d = 0; d < n; d++)

{

temp = (rand() % n);

dummy = deck[d];

deck [d] = deck [temp];

deck[temp] = dummy;

}

}

void deal(int deck [], int playerHand [], int compHand [])

{

int pHandPosition = 0;

int cHandPosition = 0;

int deckPosition;

int i;

for (i = 0; i < 13;)

{

deckPosition = deck[i];

playerHand[pHandPosition] = deckPosition;

pHandPosition++;

i++;

deckPosition = deck[i];

compHand[cHandPosition] = deckPosition;

cHandPosition++;

i++;

}

}

void RevealCard(int deck[], int position, float card[])

{

card[0]= deck[position - 1] % 13;

card[1]= deck[position - 1] / 13.0;

if(card[0]>0 && card[0]<10)

printf("%.0f of ", card[0] + 1);

else if(card[0] == 10)

printf("Jack of ");

else if(card[0] == 11)

printf("Queen of ");

else if(card[0] == 12)

printf("King of ");

else if(card[0] == 0)

printf("Ace of ");

if(card[1] <= 1.0)

printf("Diamonds ");

else if(card[1] <= 2.0)

printf("Hearts ");

else if(card[1] <= 3.0)

printf("Clubs ");

else

printf("Spades ");

}