I need some help. We are working on code that will simulate the card game Domini
ID: 3876587 • Letter: I
Question
I need some help. We are working on code that will simulate the card game Dominion. I have provided the relevant code I believe. Please let me know if you need clarification.
Pick 5 cards implemented in dominion.c. Choose 3 cards of your choice and smithy and adventurer cards are mandatory. Refactor the code so that these cards are implemented in their own functions, rather than as part of the switch statement in cardEffect. You should call the functions for these cards in the appropriate place in cardEffect. The code for the 5 cards is below along with the cardEffect function.
Introduce some bug(s) in 4 cards out of these 5 cards, preferably “subtle” ones that might easily escape a decent test suite. By bugs I mean something that does not behave correctly – it may crash, or it may cause incorrect Dominion behavior. Introducing bugs in smithy and adventurer is mandatory. ALL CODE SHOULD BE COMPILED and RUN. Again, the code for the cards is below.
Document your changes of the five cards in the, discussing the process of extracting the functions. In addtion, write information of your bugs.
CODE FOR THE CARDS AND cardEFFECT FUNCTION
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
{
int i;
int j;
int k;
int x;
int index;
int currentPlayer = whoseTurn(state);
int nextPlayer = currentPlayer + 1;
int tributeRevealedCards[2] = {-1, -1};
int temphand[MAX_HAND];// moved above the if statement
int drawntreasure=0;
int cardDrawn;
int z = 0;// this is the counter for the temp hand
if (nextPlayer > (state->numPlayers - 1)){
nextPlayer = 0;
}
case smithy:
//+3 Cards
for (i = 0; i < 3; i++)
{
drawCard(currentPlayer, state);
}
//discard card from hand
discardCard(handPos, currentPlayer, state, 0);
return 0;
case adventurer:
while(drawntreasure<2){
if (state->deckCount[currentPlayer] <1){//if the deck is empty we need to shuffle discard and add to deck
shuffle(currentPlayer, state);
}
drawCard(currentPlayer, state);
cardDrawn = state->hand[currentPlayer][state->handCount[currentPlayer]-1];//top card of hand is most recently drawn card.
if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
drawntreasure++;
else{
temphand[z]=cardDrawn;
state->handCount[currentPlayer]--; //this should just remove the top card (the most recently drawn one).
z++;
}
}
while(z-1>=0){
state->discard[currentPlayer][state->discardCount[currentPlayer]++]=temphand[z-1]; // discard all cards in play that have been drawn
z=z-1;
}
return 0;
case council_room:
//+4 Cards
for (i = 0; i < 4; i++)
{
drawCard(currentPlayer, state);
}
//+1 Buy
state->numBuys++;
//Each other player draws a card
for (i = 0; i < state->numPlayers; i++)
{
if ( i != currentPlayer )
{
drawCard(i, state);
}
}
//put played card in played card pile
discardCard(handPos, currentPlayer, state, 0);
return 0;
case outpost:
//set outpost flag
state->outpostPlayed++;
//discard card
discardCard(handPos, currentPlayer, state, 0);
return 0;
case gardens:
return -1;
Explanation / Answer
We must use switch condition in the above program
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
{
//uses switch to select card and perform actions
switch(card)
{ //uses switch to select card and perform actions
case smithy:
//+3 Cards
for (i = 0; i < 3; i++)
{
drawCard(currentPlayer, state);
}
//discard card from hand
discardCard(handPos, currentPlayer, state, 0);
return 0;
case adventurer:
while(drawntreasure<2){
if (state->deckCount[currentPlayer] <1){
//if the deck is empty we need to shuffle discard and add to deck
shuffle(currentPlayer, state);
}
drawCard(currentPlayer, state);
cardDrawn = state->hand[currentPlayer][state->handCount[currentPlayer]-1];//top card of hand is most recently drawn card.
if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
drawntreasure++;
else{
temphand[z]=cardDrawn;
state->handCount[currentPlayer]--; //this should just remove the top card (the most recently drawn one)
z++;
}
}
while(z-1>=0){
state->discard[currentPlayer][state->discardCount[currentPlayer]++]=temphand[z-1]; // discard all cards in play that have been drawn
z=z-1;
}
return 0;
case council_room:
//+4 Cards
for (i = 0; i < 4; i++)
{
drawCard(currentPlayer, state);
}
//+1 Buy
state->numBuys++;
//Each other player draws a card
for (i = 0; i < state->numPlayers; i++)
{
if ( i != currentPlayer )
{
drawCard(i, state);
}
}
//put played card in played card pile
discardCard(handPos, currentPlayer, state, 0);
return 0;
case outpost:
//set outpost flag
state->outpostPlayed++;
//discard card
discardCard(handPos, currentPlayer, state, 0);
return 0;
case gardens:
return -1;
}// end of switch case
While choosing the card,if we choose smithy card then it switch case will check for the condition whether smithy card is there are not it will execute the smithy card condition.
similarly for adventurer and cards switch case is executed same.
{ struct gameState* g = malloc(sizeof(struct gameState)); return g; }