Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gaoyu assignment 4 #59

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions dominion/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ testAll: dominion.o testSuite.c
interface.o: interface.h interface.c
gcc -c interface.c -g $(CFLAGS)

randomtestcard1: randomtestcard1.c dominion.o rngs.o
gcc -o randomtestcard1 -g randomtestcard1.c dominion.o rngs.o $(CFLAGS)

randomtestcard2: randomtestcard2.c dominion.o rngs.o
gcc -o randomtestcard2 -g randomtestcard2.c dominion.o rngs.o $(CFLAGS)

randomtestcard3: randomtestcard3.c dominion.o rngs.o
gcc -o randomtestcard3 -g randomtestcard3.c dominion.o rngs.o $(CFLAGS)

runtests: testDrawCard
./testDrawCard &> unittestresult.out
gcov dominion.c >> unittestresult.out
cat dominion.c.gcov >> unittestresult.out

randomtestresults: randomtestcard1 randomtestcard2 randomtestcard3
./randomtestcard1 >> randomtestresults.out
./randomtestcard2 >> randomtestresults.out
./randomtestcard3 >> randomtestresults.out
gcov dominion.c -b -f >> randomtestresults.out
cat dominion.c.gcov >> randomtestresults.out

player: player.c interface.o
gcc -o player player.c -g dominion.o rngs.o interface.o $(CFLAGS)
Expand Down
146 changes: 146 additions & 0 deletions dominion/randomtestcard1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*************************************************************
* Created by Yuning Gao 2019/7/28
*************************************************************/

#include "dominion.h"
#include "dominion_helpers.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
#include "rngs.h"

// Global Variables
int testsPassed = 0;
int testsFailed = 0;

/***************************************************
* asserttrue
* Parameter: take a true (non-0) or false (0) integer as input
* Output: PASSED if true, FAILED if false
* Returns: 1 for true and 0 for false
***************************************************/
int asserttrue(int testResult) {
if (testResult) {
printf("PASSED\n");
testsPassed++;
return 1;
}
else {
printf("FAILED\n");
testsFailed++;
return 0;
}
}


int main() {
int seed = rand();
int i, j, test;
int numPlayer = 2;
int player1 = 0;
int k[10] = { adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall };
int handCount;
int handEstate;
int supplyEstate;
int baronPos; // Baron
int choice1; // 0 = do not discard, 1 = discard estate
int discardBaron, discardEstate;
int numDiscard, numCoin, numSupply;
int numTest = 5000; // number of random tests
struct gameState G, preG;

// Run random tests
for (test = 0; test < numTest; test++) {
/******************************************
*Set up random hand and action choice
******************************************/
// hand size is radomly selected
handCount = (rand() % 5) + 1;
int hand[handCount];
handEstate = 0;
// check the card
baronPos = rand() % handCount;
printf("Setting Hand, %d card(s):", handCount);
for (i = 0; i < handCount; i++) {
if (i == baronPos) {
hand[baronPos] = baron;
}
else {
// Randomly select cards in hand (there are 27 different cards)
hand[i] = rand() % 27;
if (hand[i] == 1) { handEstate++; }
}
printf(" %d", hand[i]);
}
printf("\n");

// Randomly choose estate supply size (0-8)
supplyEstate = rand() % 9;

// Randomly choose action, 0: gain estate, 1: discard estate
choice1 = rand() % 2;
if (!choice1) { printf("Choosing gain estate\n"); }
else { printf("Choosing discard estate with %d estate(s) in hand\n", handEstate); }

/******************************************
*Run test
******************************************/

memset(&G, 0, sizeof(struct gameState)); // Clear game state
initializeGame(numPlayer, k, seed, &G); // Initialize new game
G.supplyCount[estate] = supplyEstate;
G.handCount[player1] = handCount; // Set number of cards in hand
// Copy hand with 1 Estate card
memcpy(G.hand[player1], hand, sizeof(int) * handCount);
// Save the previous game state before testing function
memcpy(&preG, &G, sizeof(struct gameState));

// Call the function being tested
playCard(baronPos, choice1, 0, 0, &G); // 1 for discard

// Check buys
printf("Checking that 1 buy was gained:\n");
asserttrue(G.numBuys == (preG.numBuys + 1));

// Check discard
printf("Checking that discard gained two cards:\n");
asserttrue(G.discardCount[player1] == preG.discardCount[player1] + 2);
discardBaron = 0;
discardEstate = 0;
for (j = 0; j < G.discardCount[player1]; j++) {
// Is there an estate in the discard?
if (G.discard[player1][j] == estate) { discardEstate++; }
else if (G.discard[player1][j] == baron) { discardBaron++; }
}
printf("Checking that discard gained a baron:\n");
asserttrue(discardBaron == 1);
printf("Checking that discard gained an estate:\n");
asserttrue(discardEstate == 1);

// Check hand
if (!choice1 || !handEstate) { numDiscard = 1; } // Baron discarded
else { numDiscard = 2; } // Estate and Baron discarded
printf("Check that hand lost %d card(s):\n", numDiscard);
asserttrue(G.handCount[player1] == preG.handCount[player1] - numDiscard);
if (!choice1 || !handEstate) { numCoin = 0; }
else { numCoin = 4; } // Estate discarded for coins
printf("Testing that player received %d coins:\n", numCoin);
asserttrue(G.coins == preG.coins + numCoin);

// Check Supply
if ((!choice1 || !handEstate) && supplyEstate) { numSupply = 1; }
else { numSupply = 0; }
printf("Testing that estate supply lost %d estate(s):\n", numSupply);
asserttrue(G.supplyCount[estate] == preG.supplyCount[estate] - numSupply);
printf("\n");
}

if (testsFailed) { printf("\nFAILED %d tests\n", testsFailed); }
else { printf("\nPASSED all %d tests\n", testsPassed); }

return 0;
}
132 changes: 132 additions & 0 deletions dominion/randomtestcard2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// Created by Yuning on 2019/07/28.
//
#include "dominion.h"
#include "dominion_helpers.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
//#include "rngs.h"

// Global Variables used by asserttrue function
int testsPassed = 0;
int testsFailed = 0;

/***************************************************
* asserttrue
* Parametter: a true (non-0) or false (0) integer input
* Outputs: PASSED if true, FAILED if false
* Returns: 1 for true and 0 for false
***************************************************/
int asserttrue(int testResult) {
if (testResult) {
printf("PASSED\n");
testsPassed++;
return 1;
}
else {
printf("FAILED\n");
testsFailed++;
return 0;
}
}


int main() {
int seed = rand();
int i, test;
int numPlayer = 2;
int player;
int player1 = 0;
int player2 = 1;
int k[10] = { minion, tribute, feast, gardens, mine,
remodel, smithy, village, baron, great_hall };
int tributePos; // Baron position in hand
int numHand, numDeck;
int card, numTreasure, numAction, numVictory;
int numTest = 500; // How many random tests to run
struct gameState G, preG;

// Run random tests
for (test = 0; test < numTest; test++) {
/***************************************************
*Set up random hand and action choice
***************************************************/
// Start game
memset(&G, 0, sizeof(struct gameState)); // Clear game state
initializeGame(numPlayer, k, seed, &G); // Initialize new game

// cards in player1 is randomly selected
numHand = (rand() % 5) + 1;
G.handCount[player1] = numHand;
tributePos = rand() % numHand;
G.hand[tributePos][i] = tribute;

for (player = 0; player < numPlayer; player++) {
// cards in player2 is randomly selected
numDeck = rand() % 5;
G.deckCount[player] = numDeck;
printf("Setting Player %d Deck, %d card(s):", (player + 1), numDeck);
for (i = 0; i < numDeck; i++) {
// Randomly select cards in hand (between 27 different cards)
G.deck[player2][i] = rand() % 27;
printf(" %d", G.deck[player][i]);
}
printf("\n");
numVictory = numTreasure = numAction = 0;
for (i = numDeck - 2; i < numDeck; i++) {
card = G.deck[player][i];
// If top card matches 2nd-to-top card it is ignored
if (i == (numDeck - 1) && card == G.deck[player][i - 1]) {
break;
}
if (card == estate || card == duchy || card == province ||
card == gardens || card == great_hall) {
numVictory++;
}
else if (copper <= card && card <= gold) {
numTreasure++;
}
else {
numAction++;
}
}

// Randomly select player 1 discard
G.discardCount[player] = rand() % 5;
printf("Setting Player %d Discard, %d card(s)\n", (player + 1),
G.discardCount[player1]);
}

/***************************************************
* Run test
***************************************************/
// Save the previous game state before testing function
memcpy(&preG, &G, sizeof(struct gameState));

// Call the function being tested
playCard(tributePos, 0, 0, 0, &G); // 1 for discard

printf("Checking that p1 gained %d actions:\n", numAction * 2);
// One action was lost playing card
asserttrue(G.numActions == preG.numActions - 1 + (numAction * 2));

printf("Checking that p1 gained %d coins:\n", numTreasure * 2);
asserttrue(G.coins == preG.coins + (numTreasure * 2));

printf("Checking that p1 drew %d cards:\n", numVictory * 2);
// Played tribute card should be discarded
asserttrue(G.handCount[player1] == preG.handCount[player1] + (numVictory * 2) - 1);

printf("Checking that p2 revealed 2 cards:\n");
asserttrue(G.deckCount[player2] == preG.deckCount[player2] - 2);
}

if (testsFailed) { printf("\nFAILED %d tests\n", testsFailed); }
else { printf("\nPASSED all %d tests\n", testsPassed); }

return 0;
}
Loading