Added Software Engineering II code

This commit is contained in:
2017-11-29 12:18:13 -08:00
parent c036c6e53f
commit 4566d98b5f
54 changed files with 9288 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
CFLAGS = -Wall -fpic -coverage -lm -std=c99 -ftest-coverage -fprofile-arcs
rngs.o: rngs.h rngs.c
gcc -c rngs.c -g $(CFLAGS)
dominion.o: dominion.h dominion.c rngs.o
gcc -c dominion.c -g $(CFLAGS)
playdom: dominion.o playdom.c
gcc -o playdom playdom.c -g dominion.o rngs.o $(CFLAGS)
testDrawCard: testDrawCard.c dominion.o rngs.o
gcc -o testDrawCard -g testDrawCard.c dominion.o rngs.o $(CFLAGS)
badTestDrawCard: badTestDrawCard.c dominion.o rngs.o
gcc -o badTestDrawCard -g badTestDrawCard.c dominion.o rngs.o $(CFLAGS)
testBuyCard: testDrawCard.c dominion.o rngs.o
gcc -o testDrawCard -g testDrawCard.c dominion.o rngs.o $(CFLAGS)
testAll: dominion.o testSuite.c
gcc -o testSuite testSuite.c -g dominion.o rngs.o $(CFLAGS)
interface.o: interface.h interface.c
gcc -c interface.c -g $(CFLAGS)
runtests: testDrawCard
./testDrawCard &> unittestresult.out
gcov dominion.c >> unittestresult.out
cat dominion.c.gcov >> unittestresult.out
player: player.c interface.o
gcc -o player player.c -g dominion.o rngs.o interface.o $(CFLAGS)
all: playdom player testDrawCard testBuyCard badTestDrawCard
unittestresults.out:
echo "Compiling and running all tests....." > unittestresults.out
gcc -o unittest1 unittest1.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Unittest1...." >> unittestresults.out
./unittest1 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> unittest1.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o unittest2 unittest2.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Unittest2...." >> unittestresults.out
./unittest2 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> unittest2.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o unittest3 unittest3.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Unittest3...." >> unittestresults.out
./unittest3 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> unittest3.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o unittest4 unittest4.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Unittest4...." >> unittestresults.out
./unittest4 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> unittest4.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o cardtest1 cardtest1.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Cardtest1...." >> unittestresults.out
./cardtest1 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> cardtest1.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o cardtest2 cardtest2.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Cardtest2...." >> unittestresults.out
./cardtest2 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> cardtest2.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o cardtest3 cardtest3.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Cardtest3...." >> unittestresults.out
./cardtest3 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> cardtest3.gcov
echo "######################################" >> unittestresults.out
echo "######################################" >> unittestresults.out
gcc -o cardtest4 cardtest4.c dominion.c rngs.c -g $(CFLAGS)
echo "Running Cardtest4...." >> unittestresults.out
./cardtest4 >> unittestresults.out
gcov dominion.c >> unittestresults.out
cat dominion.c.gcov >> unittestresults.out
cat dominion.c.gcov >> cardtest4.gcov
randomtests:
gcc -o randomtestadventurer randomtestadventurer.c dominion.c rngs.c -g $(CFLAGS)
./randomtestadventurer 29378 > randomtestadventurer.out
gcov dominion.c >> randomtestadventurer.out
cat dominion.c.gcov >> randomtestadventurer.out
gcc -o randomtestcard1 randomtestcard1.c dominion.c rngs.c -g $(CFLAGS)
./randomtestcard1 29378 > randomtestcard1.out
gcov dominion.c >> randomtestcard1.out
cat dominion.c.gcov >> randomtestcard1.out
gcc -o randomtestcard2 randomtestcard2.c dominion.c rngs.c -g $(CFLAGS)
./randomtestcard2 29378 > randomtestcard2.out
gcov dominion.c >> randomtestcard2.out
cat dominion.c.gcov >> randomtestcard2.out
buggytests:
gcc -o cardtest2 cardtest2.c dominion.c rngs.c -g $(CFLAGS)
clean:
rm -f *.o playdom.exe playdom test.exe test player player.exe testInit testInit.exe *.gcov *.gcda *.gcno *.so *.out
rm -f randomtestadventurer randomtestcard1 randomtestcard2

View File

@@ -0,0 +1,44 @@
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#define DEBUG 0
#define NOISY_TEST 1
int checkDrawCard(int p, struct gameState *post) {
int r;
r = drawCard (p, post);
}
int main () {
int i, n, r, p, deckCount, discardCount, handCount;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
printf ("Testing drawCard.\n");
printf ("RANDOM TESTS.\n");
SelectStream(2);
PutSeed(3);
for (n = 0; n < 2000; n++) {
for (i = 0; i < sizeof(struct gameState); i++) {
((char*)&G)[i] = floor(Random() * 256);
}
p = floor(Random() * 1000);
checkDrawCard(p, &G);
}
printf ("ALL TESTS OK\n");
exit(0);
}

View File

@@ -0,0 +1,70 @@
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#define DEBUG 0
#define NOISY_TEST 1
int checkDrawCard(int p, struct gameState *post) {
int r;
r = drawCard (p, post);
assert (r == 0);
}
int main () {
int i, n, r, p, deckCount, discardCount, handCount;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
printf ("Testing drawCard.\n");
printf ("RANDOM TESTS.\n");
SelectStream(2);
PutSeed(3);
for (n = 0; n < 2000; n++) {
for (i = 0; i < sizeof(struct gameState); i++) {
((char*)&G)[i] = floor(Random() * 256);
}
p = floor(Random() * 2);
G.deckCount[p] = floor(Random() * MAX_DECK);
G.discardCount[p] = floor(Random() * MAX_DECK);
G.handCount[p] = floor(Random() * MAX_HAND);
checkDrawCard(p, &G);
}
printf ("ALL TESTS OK\n");
exit(0);
printf ("SIMPLE FIXED TESTS.\n");
for (p = 0; p < 2; p++) {
for (deckCount = 0; deckCount < 5; deckCount++) {
for (discardCount = 0; discardCount < 5; discardCount++) {
for (handCount = 0; handCount < 5; handCount++) {
memset(&G, 23, sizeof(struct gameState));
r = initializeGame(2, k, 1, &G);
G.deckCount[p] = deckCount;
memset(G.deck[p], 0, sizeof(int) * deckCount);
G.discardCount[p] = discardCount;
memset(G.discard[p], 0, sizeof(int) * discardCount);
G.handCount[p] = handCount;
memset(G.hand[p], 0, sizeof(int) * handCount);
checkDrawCard(p, &G);
}
}
}
}
return 0;
}

View File

@@ -0,0 +1,74 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
unsigned int arbitrary_hand_count_max = MAX_HAND - 1;
for(int i = 0 ; i < 4 ; i++){
initializeGame(4, k, 65432, &G);
G.deckCount[i] = MAX_DECK;
G.whoseTurn = i;
for(int j = 0 ; j < arbitrary_hand_count_max ; j++){
int hand_count = G.handCount[i];
int num_actions = G.numActions;
playGreat_Hall(&G, j);
testing_assert(G.handCount[i] == hand_count);
testing_assert(G.numActions == num_actions + 1);
for(int j = 0 ; j < G.handCount[i] ; j++){
testing_assert((G.hand[i][j] >= curse) && (G.hand[i][j] <= treasure_map));
}
}
}
// printf("Hand: %d\n", G.handCount[0]);
// drawCard(0, &G);
// printf("Hand: %d\n", G.handCount[0]);
// G.numActions++;
// discardCard(0, 0, &G, 0);
// printf("Hand: %d\n\n", G.handCount[0]);
//
// printf("Hand: %d\n", G.handCount[0]);
// playGreat_Hall(&G, 0);
// printf("Hand: %d\n\n", G.handCount[0]);
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,85 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression, int should_print);
long sucesses = 0;
long failures = 0;
int main(int argc, char** argv) {
struct gameState G, G_copy;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
unsigned int arbitrary_hand_count_max = MAX_HAND - 3;
for(int l = 0 ; l < 4 ; l++){
for(int i = 1 ; i < arbitrary_hand_count_max ; i++){
initializeGame(4, k, 65432, &G);
G.handCount[0] = arbitrary_hand_count_max;
G.whoseTurn = l;
G_copy = G;
drawCard(0, &G_copy);
drawCard(0, &G_copy);
drawCard(0, &G_copy);
discardCard(i, l, &G_copy, 0);
playSmithy(&G, i);
testing_assert(G.handCount[l] == G_copy.handCount[l], 0);
for(int j = 0 ; j < arbitrary_hand_count_max ; j++){
testing_assert((G.hand[l][j] >= curse) && (G.hand[l][j] <= treasure_map), 0);
testing_assert((G_copy.hand[l][j] >= curse) && (G_copy.hand[l][j] <= treasure_map), 0);
}
}
}
printf("Run complete!\n");
printf("SUCCESSES: %ld\n", sucesses);
if(failures > 0){
printf("Some tests failed!!!\n");
printf("FAILURES: %ld\n", failures);
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression, int should_print) {
if (expression) {
if(should_print){
printf("TEST SUCCEEDED!\n");
}
sucesses++;
return 1;
} else {
if(should_print){
printf("TEST FAILED!\n");
}
failures++;
return 0;
}
}

View File

@@ -0,0 +1,77 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
unsigned int arbitrary_hand_count_max = MAX_HAND - 2;
for(int l = 0 ; l < 4 ; l++){
for(int i = 1 ; i < arbitrary_hand_count_max ; i++){
initializeGame(4, k, 65432, &G);
G.handCount[l] = arbitrary_hand_count_max;
G.whoseTurn = l;
playAdventurer(&G);
testing_assert(G.handCount[l] == arbitrary_hand_count_max + 2);
testing_assert((G.hand[l][G.handCount[l] - 1] >= copper) && (G.hand[l][G.handCount[l] - 1] <= gold));
testing_assert((G.hand[l][G.handCount[l] - 2] >= copper) && (G.hand[l][G.handCount[l] - 2] <= gold));
initializeGame(4, k, 65432, &G);
G.handCount[l] = arbitrary_hand_count_max;
G.whoseTurn = l;
G.deckCount[l] = 0;
for(int m = 0 ; m < MAX_DECK ; m += 3){
G.discard[l][m] = copper;
G.discard[l][m + 1] = silver;
G.discard[l][m + 2] = gold;
G.discardCount[l] += 3;
}
playAdventurer(&G);
testing_assert(G.handCount[l] == arbitrary_hand_count_max + 2);
testing_assert((G.hand[l][G.handCount[l] - 1] >= copper) && (G.hand[l][G.handCount[l] - 1] <= gold));
testing_assert((G.hand[l][G.handCount[l] - 2] >= copper) && (G.hand[l][G.handCount[l] - 2] <= gold));
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,57 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
unsigned int arbitrary_hand_count_max = MAX_HAND - 3;
for(int l = 0 ; l < 4 ; l++){
for(int i = 1 ; i < arbitrary_hand_count_max ; i++){
initializeGame(4, k, 65432, &G);
G.handCount[0] = arbitrary_hand_count_max;
G.whoseTurn = l;
int outpost_before = G.outpostPlayed;
int handcount_before = G.handCount[l];
playOutpost(&G, i);
testing_assert(G.outpostPlayed == outpost_before + 1);
testing_assert(G.handCount[l] == handcount_before - 1);
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,142 @@
#ifndef _DOMINION_H
#define _DOMINION_H
// Code from various sources, baseline from Kristen Bartosz
#define MAX_HAND 500
#define MAX_DECK 500
#define MAX_PLAYERS 4
#define DEBUG 0
//Testing commit
/* http://dominion.diehrstraits.com has card texts */
/* http://dominion.isotropic.org has other stuff */
/* hand# means index of a card in current active player's hand */
enum CARD
{curse = 0,
estate,
duchy,
province,
copper,
silver,
gold,
adventurer,
/* If no/only 1 treasure found, stop when full deck seen */
council_room,
feast, /* choice1 is supply # of card gained) */
gardens,
mine, /* choice1 is hand# of money to trash, choice2 is supply# of
money to put in hand */
remodel, /* choice1 is hand# of card to remodel, choice2 is supply# */
smithy,
village,
baron, /* choice1: boolean for discard of estate */
/* Discard is always of first (lowest index) estate */
great_hall,
minion, /* choice1: 1 = +2 coin, 2 = redraw */
steward, /* choice1: 1 = +2 card, 2 = +2 coin, 3 = trash 2 (choice2,3) */
tribute,
ambassador, /* choice1 = hand#, choice2 = number to return to supply */
cutpurse,
embargo, /* choice1 = supply# */
outpost,
salvager, /* choice1 = hand# to trash */
sea_hag,
treasure_map
};
struct gameState {
int numPlayers; //number of players
int supplyCount[treasure_map+1]; //this is the amount of a specific type of card given a specific number.
int embargoTokens[treasure_map+1];
int outpostPlayed;
int outpostTurn;
int whoseTurn;
int phase;
int numActions; /* Starts at 1 each turn */
int coins; /* Use as you see fit! */
int numBuys; /* Starts at 1 each turn */
int hand[MAX_PLAYERS][MAX_HAND];
int handCount[MAX_PLAYERS];
int deck[MAX_PLAYERS][MAX_DECK];
int deckCount[MAX_PLAYERS];
int discard[MAX_PLAYERS][MAX_DECK];
int discardCount[MAX_PLAYERS];
int playedCards[MAX_DECK];
int playedCardCount;
};
/* All functions return -1 on failure, and DO NOT CHANGE GAME STATE;
unless specified for other return, return 0 on success */
struct gameState* newGame();
int* kingdomCards(int k1, int k2, int k3, int k4, int k5, int k6, int k7,
int k8, int k9, int k10);
int initializeGame(int numPlayers, int kingdomCards[10], int randomSeed,
struct gameState *state);
/* Responsible for initializing all supplies, and shuffling deck and
drawing starting hands for all players. Check that 10 cards selected
are in fact (different) kingdom cards, and that numPlayers is valid.
Cards not in game should initialize supply position to -1 */
int shuffle(int player, struct gameState *state);
/* Assumes all cards are now in deck array (or hand/played): discard is
empty */
int playCard(int handPos, int choice1, int choice2, int choice3,
struct gameState *state);
/* Play card with index handPos from current player's hand */
int buyCard(int supplyPos, struct gameState *state);
/* Buy card with supply index supplyPos */
int numHandCards(struct gameState *state);
/* How many cards current player has in hand */
int handCard(int handNum, struct gameState *state);
/* enum value of indexed card in player's hand */
int supplyCount(int card, struct gameState *state);
/* How many of given card are left in supply */
int fullDeckCount(int player, int card, struct gameState *state);
/* Here deck = hand + discard + deck */
int whoseTurn(struct gameState *state);
int endTurn(struct gameState *state);
/* Must do phase C and advance to next player; do not advance whose turn
if game is over */
int isGameOver(struct gameState *state);
int scoreFor(int player, struct gameState *state);
/* Negative here does not mean invalid; scores may be negative,
-9999 means invalid input */
int getWinners(int players[MAX_PLAYERS], struct gameState *state);
/* Set array position of each player who won (remember ties!) to
1, others to 0 */
int playAdventurer(struct gameState *state);
int playSmithy(struct gameState *state, int handPos);
int playVillage(struct gameState *state, int handPos);
int playFeast(struct gameState *state, int choice1);
int playCouncil_Room(struct gameState *state, int handPos);
int playGreat_Hall(struct gameState *state, int handPos);
int playOutpost(struct gameState *state, int handPos);
#endif

View File

@@ -0,0 +1,15 @@
#ifndef _DOMINION_HELPERS_H
#define _DOMINION_HELPERS_H
#include "dominion.h"
int drawCard(int player, struct gameState *state);
int updateCoins(int player, struct gameState *state, int bonus);
int discardCard(int handPos, int currentPlayer, struct gameState *state,
int trashFlag);
int gainCard(int supplyPos, struct gameState *state, int toFlag, int player);
int getCost(int cardNumber);
int cardEffect(int card, int choice1, int choice2, int choice3,
struct gameState *state, int handPos, int *bonus);
#endif

View File

@@ -0,0 +1,363 @@
/* Interactive Dominion Interface
Sam Heinith CS362
1/26/2010
*/
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include "rngs.h"
#include "interface.h"
#include "dominion.h"
void cardNumToName(int card, char *name){
switch(card){
case curse: strcpy(name,"Curse");
break;
case estate: strcpy(name,"Estate");
break;
case duchy: strcpy(name,"Duchy");
break;
case province: strcpy(name,"Province");
break;
case copper: strcpy(name,"Copper");
break;
case silver: strcpy(name,"Silver");
break;
case gold: strcpy(name,"Gold");
break;
case adventurer: strcpy(name,"Adventurer");
break;
case council_room: strcpy(name,"Council Room");
break;
case feast: strcpy(name,"Feast");
break;
case gardens: strcpy(name,"Gardens");
break;
case mine: strcpy(name,"Mine");
break;
case remodel: strcpy(name,"Remodel");
break;
case smithy: strcpy(name,"Smithy");
break;
case village: strcpy(name,"Village");
break;
case baron: strcpy(name,"Baron");
break;
case great_hall: strcpy(name,"Great Hall");
break;
case minion: strcpy(name,"Minion");
break;
case steward: strcpy(name,"Steward");
break;
case tribute: strcpy(name,"Tribute");
break;
case ambassador: strcpy(name,"Ambassador");
break;
case cutpurse: strcpy(name,"Cutpurse");
break;
case embargo: strcpy(name,"Embargo");
break;
case outpost: strcpy(name,"Outpost");
break;
case salvager: strcpy(name,"Salvager");
break;
case sea_hag: strcpy(name,"Sea Hag");
break;
case treasure_map: strcpy(name,"Treasure Map");
break;
default: strcpy(name,"?");
}
}
int getCardCost(int card) {
int cost;
switch(card) {
case curse: cost = CURSE_COST;
break;
case estate: cost = ESTATE_COST;
break;
case duchy: cost = DUCHY_COST;
break;
case province: cost = PROVINCE_COST;
break;
case copper: cost = COPPER_COST;
break;
case silver: cost = SILVER_COST;
break;
case gold: cost = GOLD_COST;
break;
case adventurer: cost = ADVENTURER_COST;
break;
case council_room: cost = COUNCIL_ROOM_COST;
break;
case feast: cost = FEAST_COST;
break;
case gardens: cost = GARDEN_COST;
break;
case mine: cost = MINE_COST;
break;
case remodel: cost = REMODEL_COST;
break;
case smithy: cost = SMITHY_COST;
break;
case village: cost = VILLAGE_COST;
break;
case baron: cost = BARON_COST;
break;
case great_hall: cost = GREAT_HALL_COST;
break;
case minion: cost = MINION_COST;
break;
case steward: cost = STEWARD_COST;
break;
case tribute: cost = TRIBUTE_COST;
break;
case ambassador: cost = AMBASSADOR_COST;
break;
case cutpurse: cost = CUTPURSE_COST;
break;
case embargo: cost = EMBARGO_COST;
break;
case outpost: cost = OUTPOST_COST;
break;
case salvager: cost = SALVAGER_COST;
break;
case sea_hag: cost = SEA_HAG_COST;
break;
case treasure_map: cost = TREASURE_MAP_COST;
break;
default: cost = ONETHOUSAND;
}
return cost;
}
void printHand(int player, struct gameState *game) {
int handCount = game->handCount[player];
int handIndex;
printf("Player %d's hand:\n", player);
if(handCount > 0) printf("# Card\n");
for(handIndex = 0; handIndex < handCount; handIndex++) {
int card = game->hand[player][handIndex];
char name[MAX_STRING_LENGTH];
cardNumToName(card, name);
printf("%-2d %-13s\n", handIndex, name);
}
printf("\n");
}
void printDeck(int player, struct gameState *game) {
int deckCount = game->deckCount[player];
int deckIndex;
printf("Player %d's deck: \n", player);
if(deckCount > 0) printf("# Card\n");
for(deckIndex = 0; deckIndex < deckCount; deckIndex++) {
int card = game->deck[player][deckIndex];
char name[MAX_STRING_LENGTH];
cardNumToName(card, name);
printf("%-2d %-13s\n", deckIndex, name);
}
printf("\n");
}
void printPlayed(int player, struct gameState *game) {
int playedCount = game->playedCardCount;
int playedIndex;
printf("Player %d's played cards: \n", player);
if(playedCount > 0) printf("# Card\n");
for(playedIndex = 0; playedIndex < playedCount; playedIndex++) {
int card = game->playedCards[playedIndex];
char name[MAX_STRING_LENGTH];
cardNumToName(card, name);
printf("%-2d %-13s \n", playedIndex, name);
}
printf("\n");
}
void printDiscard(int player, struct gameState *game) {
int discardCount = game->discardCount[player];
int discardIndex;
printf("Player %d's discard: \n", player);
if(discardCount > 0) printf("# Card\n");
for(discardIndex = 0; discardIndex < discardCount; discardIndex++) {
int card = game->discard[player][discardIndex];
char name[MAX_STRING_LENGTH];
cardNumToName(card, name);
printf("%-2d %-13s \n", discardIndex, name);
}
printf("\n");
}
void printSupply(struct gameState *game) {
int cardNum, cardCost, cardCount;
char name[MAX_STRING_LENGTH];
printf("# Card Cost Copies\n");
for(cardNum = 0; cardNum < NUM_TOTAL_K_CARDS; cardNum++){
cardCount = game->supplyCount[cardNum];
if(cardCount == -1) continue;
cardNumToName(cardNum, name);
cardCost = getCardCost(cardNum);
printf("%-2d %-13s %-5d %-5d", cardNum, name, cardCost, cardCount);
printf("\n");
}
printf("\n");
}
void printState(struct gameState *game) {
int numActions = game->numActions;
int numCoins = game->coins;
int numBuys = game->numBuys;
int currentPlayer = game->whoseTurn;
int phase = game->phase;
char phaseName[MAX_STRING_LENGTH];
phaseNumToName(phase,phaseName);
printf("Player %d:\n%s phase\n%d actions\n%d coins\n%d buys\n\n", currentPlayer, phaseName, numActions, numCoins, numBuys);
}
void printScores(struct gameState *game) {
int playerNum, score[MAX_PLAYERS];
int numPlayers = game->numPlayers;
for(playerNum = 0; playerNum < numPlayers; playerNum++) {
score[playerNum] = scoreFor(playerNum,game);
printf("Player %d has a score of %d\n", playerNum, score[playerNum]);
}
}
void printHelp(void) {
printf("Commands are: \n\
add [Supply Card Number] - add any card to your hand (teh hacks)\n\
buy [Supply Card Number] - buy a card at supply position\n\
end - end your turn\n\
init [Number of Players] [Number of Bots] - initialize the game\n\
num - print number of cards in your hand\n\
play [Hand Index] [Choice] [Choice] [Choice] - play a card from your hand\n\
resign - end the game showing the current scores\n\
show - show your current hand\n\
stat - show your turn's status\n\
supp - show the supply\n\
whos - whos turn\n\
exit - exit the interface");
printf("\n\n");
}
void phaseNumToName(int phase, char *name) {
switch(phase){
case ACTION_PHASE: strcpy(name,"Action");
break;
case BUY_PHASE: strcpy(name,"Buy");
break;
case CLEANUP_PHASE: strcpy(name,"Cleanup");
break;
}
}
int addCardToHand(int player, int card, struct gameState *game) {
if(card >= adventurer && card < NUM_TOTAL_K_CARDS){
int handTop = game->handCount[player];
game->hand[player][handTop] = card;
game->handCount[player]++;
return SUCCESS;
} else {
return FAILURE;
}
}
void selectKingdomCards(int randomSeed, int kingCards[NUM_K_CARDS]) {
int i, used, card, numSelected = 0;
SelectStream(1);
PutSeed((long)randomSeed);
while(numSelected < NUM_K_CARDS) {
used = FALSE;
card = floor(Random() * NUM_TOTAL_K_CARDS);
if(card < adventurer) continue;
for(i = 0; i < numSelected; i++) {
if(kingCards[i] == card) {
used = TRUE;
break;
}
}
if(used == TRUE) continue;
kingCards[numSelected] = card;
numSelected++;
}
}
int countHandCoins(int player, struct gameState *game) {
int card, index, coinage = 0;
for(index = 0; index < game->handCount[player]; index++) {
card = game->hand[player][index];
switch(card) {
case copper: coinage += COPPER_VALUE;
break;
case silver: coinage += SILVER_VALUE;
break;
case gold: coinage += GOLD_VALUE;
break;
}
}
return coinage;
}
void executeBotTurn(int player, int *turnNum, struct gameState *game) {
int coins = countHandCoins(player, game);
printf("*****************Executing Bot Player %d Turn Number %d*****************\n", player, *turnNum);
printSupply(game);
//sleep(1); //Thinking...
if(coins >= PROVINCE_COST && supplyCount(province,game) > 0) {
buyCard(province,game);
printf("Player %d buys card Province\n\n", player);
}
else if(supplyCount(province,game) == 0 && coins >= DUCHY_COST ) {
buyCard(duchy,game);
printf("Player %d buys card Duchy\n\n", player);
}
else if(coins >= GOLD_COST && supplyCount(gold,game) > 0) {
buyCard(gold,game);
printf("Player %d buys card Gold\n\n", player);
}
else if(coins >= SILVER_COST && supplyCount(silver,game) > 0) {
buyCard(silver,game);
printf("Player %d buys card Silver\n\n", player);
}
if(player == (game->numPlayers -1)) (*turnNum)++;
endTurn(game);
if(! isGameOver(game)) {
int currentPlayer = whoseTurn(game);
printf("Player %d's turn number %d\n\n", currentPlayer, (*turnNum));
}
}

View File

@@ -0,0 +1,128 @@
/* Interactive Dominion Interface
Sam Heinith CS362
1/26/2010
*/
#ifndef _INTERFACE_H
#define _INTERFACE_H
#include "dominion.h"
//Last card enum (Treasure map) card number plus one for the 0th card.
#define NUM_TOTAL_K_CARDS (treasure_map + 1)
#define NUM_K_CARDS 10
#define NUM_V_CARDS_2 8
#define NUM_V_CARDS_3or4 12
#define NUM_C_CARDS_2 10
#define NUM_C_CARDS_3 20
#define NUM_C_CARDS_4 30
#define NUM_COPPER 60
#define NUM_SILVER 40
#define NUM_GOLD 30
#define UNUSED -1
#define START_COPPER 7
#define START_ESTATE 3
#define HANDSIZE 5
#define COMPARE(string1, string2) strncmp(string1, string2, 4)
#define MAX_STRING_LENGTH 32
#define TRUE 1
#define FALSE 0
#define SUCCESS 0
#define FAILURE -1
#define MATCH 0
#define WINNER 1
#define NOT_WINNER 0
//The Game Phases
#define ACTION_PHASE 0
#define BUY_PHASE 1
#define CLEANUP_PHASE 2
#define COPPER_VALUE 1
#define SILVER_VALUE 2
#define GOLD_VALUE 3
//From Dominion List Spoiler
#define COPPER_COST 0
#define SILVER_COST 3
#define GOLD_COST 6
#define ESTATE_COST 2
#define DUCHY_COST 5
#define PROVINCE_COST 8
#define CURSE_COST 0
#define ADVENTURER_COST 6
#define COUNCIL_ROOM_COST 5
#define FEAST_COST 4
#define GARDEN_COST 4
#define MINE_COST 5
#define MONEYLENDER_COST 4
#define REMODEL_COST 4
#define SMITHY_COST 4
#define VILLAGE_COST 3
#define WOODCUTTER_COST 3
#define BARON_COST 4
#define GREAT_HALL_COST 3
#define MINION_COST 5
#define SHANTY_TOWN_COST 3
#define STEWARD_COST 3
#define TRIBUTE_COST 5
#define WISHING_WELL_COST 3
#define AMBASSADOR_COST 3
#define CUTPURSE_COST 4
#define EMBARGO_COST 2
#define OUTPOST_COST 5
#define SALVAGER_COST 4
#define SEA_HAG_COST 4
#define TREASURE_MAP_COST 4
#define ONETHOUSAND 1000
int addCardToHand(int player, int card, struct gameState *game);
int countHandCoins(int player, struct gameState *game);
void executeBotTurn(int player, int *turnNum, struct gameState *game);
void phaseNumToName(int phase, char *name);
void cardNumToName(int card, char *name);
int getCardCost(int card);
void printHelp(void);
void printHand(int player, struct gameState *game);
void printDeck(int player, struct gameState *game);
void printDiscard(int player, struct gameState *game);
void printPlayed(int player, struct gameState *game);
void printState(struct gameState *game);
void printSupply(struct gameState *game);
void printGameState(struct gameState *game);
void printScores(struct gameState *game);
void selectKingdomCards(int randomSeed, int kingdomCards[NUM_K_CARDS]);
#endif

View File

@@ -0,0 +1,134 @@
#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>
int main (int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
printf ("Starting game.\n");
initializeGame(2, k, atoi(argv[1]), &G);
int money = 0;
int smithyPos = -1;
int adventurerPos = -1;
int i=0;
int numSmithies = 0;
int numAdventurers = 0;
while (!isGameOver(&G)) {
money = 0;
smithyPos = -1;
adventurerPos = -1;
for (i = 0; i < numHandCards(&G); i++) {
if (handCard(i, &G) == copper)
money++;
else if (handCard(i, &G) == silver)
money += 2;
else if (handCard(i, &G) == gold)
money += 3;
else if (handCard(i, &G) == smithy)
smithyPos = i;
else if (handCard(i, &G) == adventurer)
adventurerPos = i;
}
if (whoseTurn(&G) == 0) {
if (smithyPos != -1) {
printf("0: smithy played from position %d\n", smithyPos);
playCard(smithyPos, -1, -1, -1, &G);
printf("smithy played.\n");
money = 0;
i=0;
while(i<numHandCards(&G)){
if (handCard(i, &G) == copper){
playCard(i, -1, -1, -1, &G);
money++;
}
else if (handCard(i, &G) == silver){
playCard(i, -1, -1, -1, &G);
money += 2;
}
else if (handCard(i, &G) == gold){
playCard(i, -1, -1, -1, &G);
money += 3;
}
i++;
}
}
if (money >= 8) {
printf("0: bought province\n");
buyCard(province, &G);
}
else if (money >= 6) {
printf("0: bought gold\n");
buyCard(gold, &G);
}
else if ((money >= 4) && (numSmithies < 2)) {
printf("0: bought smithy\n");
buyCard(smithy, &G);
numSmithies++;
}
else if (money >= 3) {
printf("0: bought silver\n");
buyCard(silver, &G);
}
printf("0: end turn\n");
endTurn(&G);
}
else {
if (adventurerPos != -1) {
printf("1: adventurer played from position %d\n", adventurerPos);
playCard(adventurerPos, -1, -1, -1, &G);
money = 0;
i=0;
while(i<numHandCards(&G)){
if (handCard(i, &G) == copper){
playCard(i, -1, -1, -1, &G);
money++;
}
else if (handCard(i, &G) == silver){
playCard(i, -1, -1, -1, &G);
money += 2;
}
else if (handCard(i, &G) == gold){
playCard(i, -1, -1, -1, &G);
money += 3;
}
i++;
}
}
if (money >= 8) {
printf("1: bought province\n");
buyCard(province, &G);
}
else if ((money >= 6) && (numAdventurers < 2)) {
printf("1: bought adventurer\n");
buyCard(adventurer, &G);
numAdventurers++;
}else if (money >= 6){
printf("1: bought gold\n");
buyCard(gold, &G);
}
else if (money >= 3){
printf("1: bought silver\n");
buyCard(silver, &G);
}
printf("1: endTurn\n");
endTurn(&G);
}
} // end of While
printf ("Finished game.\n");
printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, &G), scoreFor(1, &G));
return 0;
}

View File

@@ -0,0 +1,216 @@
/* Interactive Dominion Interface
Version 7
Sam Heinith CS362
Questions/Comments:
heiniths@onid.orst.edu
1/26/2010
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dominion.h"
#include "interface.h"
#include "rngs.h"
int main2(int argc, char *argv[]) {
//Default cards, as defined in playDom
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};
struct gameState g;
initializeGame(2,k,1,&g);
printf ("SUCCESSFUL INIT\n");
getchar();
}
int main(int argc, char* argv[]) {
char *add = "add";
char *buyC = "buy";
char *endT = "end";
char *exit = "exit";
char *help = "help";
char *init = "init";
char *numH = "num";
char *play = "play";
char *resign = "resi";
char *show = "show";
char *stat = "stat";
char *supply = "supp";
char *whos = "whos";
char command[MAX_STRING_LENGTH];
char line[MAX_STRING_LENGTH];
char cardName[MAX_STRING_LENGTH];
//Array to hold bot presence
int isBot[MAX_PLAYERS] = { 0, 0, 0, 0};
int players[MAX_PLAYERS];
int playerNum;
int outcome;
int currentPlayer;
int gameOver = FALSE;
int gameStarted = FALSE;
int turnNum = 0;
int randomSeed = atoi(argv[1]);
//Default cards, as defined in playDom
int kCards[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};
struct gameState g;
struct gameState * game = &g;
memset(game,0,sizeof(struct gameState));
if(argc != 2){
printf("Usage: player [integer random number seed]\n");
return EXIT_SUCCESS;
}
if(randomSeed <= 0){
printf("Usage: player [integer random number seed]\n");
return EXIT_SUCCESS;
}
initializeGame(2,kCards,randomSeed,game);
printf("Please enter a command or \"help\" for commands\n");
while(TRUE) {
int arg0 = UNUSED;
int arg1 = UNUSED;
int arg2 = UNUSED;
int arg3 = UNUSED;
outcome = FAILURE;
strcpy(line,"");
strcpy(command,"");
strcpy(cardName,"");
currentPlayer = whoseTurn(game);
//If you are getting a seg fault comment this if block out
gameOver = isGameOver(game);
if(gameStarted == TRUE && gameOver == TRUE){
printScores(game);
getWinners(players, game);
printf("After %d turns, the winner(s) are:\n", turnNum);
for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
if(players[playerNum] == WINNER) printf("Player %d\n", playerNum);
}
for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
printHand(playerNum, game);
printPlayed(playerNum, game);
printDiscard(playerNum, game);
printDeck(playerNum, game);
}
break; //Exit out of the game/while loop
}
if(isBot[currentPlayer] == TRUE) {
executeBotTurn(currentPlayer, &turnNum, game);
continue;
}
printf("$ ");
fgets(line, MAX_STRING_LENGTH, stdin);
sscanf(line, "%s %d %d %d %d", command, &arg0, &arg1, &arg2, &arg3);
if(COMPARE(command, add) == 0) {
outcome = addCardToHand(currentPlayer, arg0, game);
cardNumToName(arg0, cardName);
printf("Player %d adds %s to their hand\n\n", currentPlayer, cardName);
} else
if(COMPARE(command, buyC) == 0) {
outcome = buyCard(arg0, game);
cardNumToName(arg0, cardName);
if(outcome == SUCCESS){
printf("Player %d buys card %d, %s\n\n", currentPlayer, arg0, cardName);
} else {
printf("Player %d cannot buy card %d, %s\n\n", currentPlayer, arg0, cardName);
}
} else
if(COMPARE(command, endT) == 0) {
if(gameStarted == TRUE) {
if(currentPlayer == (game->numPlayers -1)) turnNum++;
endTurn(game);
currentPlayer = whoseTurn(game);
printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
}
} else
if(COMPARE(command, exit) == 0) {
break;
} else
if(COMPARE(command, help) == 0) {
printHelp();
} else
if(COMPARE(command, init) == 0) {
int numHuman = arg0 - arg1;
for(playerNum = numHuman; playerNum < arg0; playerNum++) {
isBot[playerNum] = TRUE;
}
// selectKingdomCards(randomSeed, kCards); //Comment this out to use the default card set defined in playDom.
outcome = initializeGame(arg0, kCards, randomSeed, game);
printf("\n");
if(outcome == SUCCESS){
gameStarted = TRUE;
currentPlayer = whoseTurn(game);
printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
}
} else
if(COMPARE(command, numH) == 0) {
int numCards = numHandCards(game);
printf("There are %d cards in your hand.\n", numCards);
} else
if(COMPARE(command, play) == 0) {
int card = handCard(arg0,game);
outcome = playCard(arg0, arg1, arg2, arg3, game);
cardNumToName(card, cardName);
if(outcome == SUCCESS){
printf("Player %d plays %s\n\n", currentPlayer, cardName);
} else {
printf("Player %d cannot play card %d\n\n", currentPlayer, arg0);
}
} else
if(COMPARE(command, resign) == 0) {
endTurn(game);
printScores(game);
break;
} else
if(COMPARE(command, show) == 0) {
if(gameStarted == FALSE) continue;
printHand(currentPlayer, game);
printPlayed(currentPlayer, game);
//printDiscard(currentPlayer, game);
//printDeck(currentPlayer, game);
} else
if(COMPARE(command, stat) == 0) {
if(gameStarted == FALSE) continue;
printState(game);
} else
if(COMPARE(command, supply) == 0) {
printSupply(game);
} else
if(COMPARE(command, whos) == 0) {
int playerNum = whoseTurn(game);
printf("Player %d's turn\n", playerNum);
}
}
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression, int should_print);
long sucesses = 0;
long failures = 0;
int main(int argc, char** argv) {
// Get seed from command line arg and setup randomization or exit
if(argc < 2){
printf("Please enter a seed value for randomization.\n");
printf("usage: %s [seed]\n", argv[0]);
return EXIT_FAILURE;
}
long seed = atoi(argv[1]);
SelectStream(1);
PutSeed(seed);
// Game variables
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
// Initialize the game
initializeGame(4, k, seed, &G);
// Limiting variables
unsigned int arbritray_num_tests = 100000;
unsigned int arbitrary_hand_count_max = MAX_HAND - 2;
for(int z = 0 ; z < arbritray_num_tests ; z++){
int turn = floor(Random() *MAX_PLAYERS);
int starting_hand = floor(Random() * arbitrary_hand_count_max);
G.deckCount[turn] = MAX_DECK;
G.discardCount[turn] = 0;
G.handCount[turn] = starting_hand;
G.whoseTurn = turn;
playAdventurer(&G);
testing_assert(G.handCount[turn] == starting_hand + 2, 0);
testing_assert((G.hand[turn][G.handCount[turn] - 1] >= copper) && (G.hand[turn][G.handCount[turn] - 1] <= gold), 0);
testing_assert((G.hand[turn][G.handCount[turn] - 2] >= copper) && (G.hand[turn][G.handCount[turn] - 2] <= gold), 0);
G.handCount[turn] = starting_hand;
G.deckCount[turn] = 0;
G.discardCount[turn] = 0;
for(int m = 0 ; m < MAX_DECK ; m++){
int card = copper + floor(Random() * 2);
G.discard[turn][m] = card;
G.discardCount[turn]++;
}
playAdventurer(&G);
testing_assert(G.handCount[turn] == starting_hand + 2, 0);
testing_assert((G.hand[turn][G.handCount[turn] - 1] >= copper) && (G.hand[turn][G.handCount[turn] - 1] <= gold), 0);
testing_assert((G.hand[turn][G.handCount[turn] - 2] >= copper) && (G.hand[turn][G.handCount[turn] - 2] <= gold), 0);
}
printf("Run complete!\n");
printf("SUCCESSES: %ld\n", sucesses);
if(failures > 0){
printf("Some tests failed!!!\n");
printf("FAILURES: %ld\n", failures);
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression, int should_print) {
if (expression) {
if(should_print){
printf("TEST SUCCEEDED!\n");
}
sucesses++;
return 1;
} else {
if(should_print){
printf("TEST FAILED!\n");
}
failures++;
return 0;
}
}

View File

@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression, int should_print);
long sucesses = 0;
long failures = 0;
int main(int argc, char** argv) {
// Get seed from command line arg and setup randomization or exit
if(argc < 2){
printf("Please enter a seed value for randomization.\n");
printf("usage: %s [seed]\n", argv[0]);
return EXIT_FAILURE;
}
long seed = atoi(argv[1]);
SelectStream(1);
PutSeed(seed);
// Game variables
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
// Initialize the game
initializeGame(4, k, seed, &G);
// Limiting variables
unsigned int arbritray_num_tests = 1000000;
unsigned int arbitrary_hand_count_max = MAX_HAND - 2;
for(int z = 0 ; z < arbritray_num_tests ; z++){
int turn = floor(Random() * MAX_PLAYERS);
int starting_hand = floor(Random() * arbitrary_hand_count_max);
G.handCount[turn] = starting_hand;
G.whoseTurn = turn;
int outpost_before = G.outpostPlayed;
int handcount_before = G.handCount[turn];
playOutpost(&G, turn);
testing_assert(G.outpostPlayed == outpost_before + 1, 0);
testing_assert(G.handCount[turn] == handcount_before - 1, 0);
}
printf("Run complete!\n");
printf("SUCCESSES: %ld\n", sucesses);
if(failures > 0){
printf("Some tests failed!!!\n");
printf("FAILURES: %ld\n", failures);
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression, int should_print) {
if (expression) {
if(should_print){
printf("TEST SUCCEEDED!\n");
}
sucesses++;
return 1;
} else {
if(should_print){
printf("TEST FAILED!\n");
}
failures++;
return 0;
}
}

View File

@@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression, int should_print);
long sucesses = 0;
long failures = 0;
int main(int argc, char** argv) {
// Get seed from command line arg and setup randomization or exit
if(argc < 2){
printf("Please enter a seed value for randomization.\n");
printf("usage: %s [seed]\n", argv[0]);
return EXIT_FAILURE;
}
long seed = atoi(argv[1]);
SelectStream(1);
PutSeed(seed);
// Game variables
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
// Initialize the game
initializeGame(4, k, seed, &G);
// Limiting variables
unsigned int arbritray_num_tests = 1000000;
unsigned int arbitrary_hand_count_max = MAX_HAND - 2;
for(int z = 0 ; z < arbritray_num_tests ; z++){
int turn = floor(Random() * MAX_PLAYERS);
int hand_pos = floor(Random() * arbitrary_hand_count_max);
G.deckCount[turn] = MAX_DECK;
G.whoseTurn = turn;
int hand_count = G.handCount[turn];
int num_actions = G.numActions;
playGreat_Hall(&G, hand_pos);
testing_assert(G.handCount[turn] == hand_count, 0);
testing_assert(G.numActions == num_actions + 1, 0);
for(int k = 0 ; k < G.handCount[turn] ; k++){
testing_assert((G.hand[turn][k] >= curse) && (G.hand[turn][k] <= treasure_map), 0);
}
}
printf("Run complete!\n");
printf("SUCCESSES: %ld\n", sucesses);
if(failures > 0){
printf("Some tests failed!!!\n");
printf("FAILURES: %ld\n", failures);
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression, int should_print) {
if (expression) {
if(should_print){
printf("TEST SUCCEEDED!\n");
}
sucesses++;
return 1;
} else {
if(should_print){
printf("TEST FAILED!\n");
}
failures++;
return 0;
}
}

View File

@@ -0,0 +1,179 @@
/* -------------------------------------------------------------------------
* This is an ANSI C library for multi-stream random number generation.
* The use of this library is recommended as a replacement for the ANSI C
* rand() and srand() functions, particularly in simulation applications
* where the statistical 'goodness' of the random number generator is
* important. The library supplies 256 streams of random numbers; use
* SelectStream(s) to switch between streams indexed s = 0,1,...,255.
*
* The streams must be initialized. The recommended way to do this is by
* using the function PlantSeeds(x) with the value of x used to initialize
* the default stream and all other streams initialized automatically with
* values dependent on the value of x. The following convention is used
* to initialize the default stream:
* if x > 0 then x is the state
* if x < 0 then the state is obtained from the system clock
* if x = 0 then the state is to be supplied interactively.
*
* The generator used in this library is a so-called 'Lehmer random number
* generator' which returns a pseudo-random number uniformly distributed
* 0.0 and 1.0. The period is (m - 1) where m = 2,147,483,647 and the
* smallest and largest possible values are (1 / m) and 1 - (1 / m)
* respectively. For more details see:
*
* "Random Number Generators: Good Ones Are Hard To Find"
* Steve Park and Keith Miller
* Communications of the ACM, October 1988
*
* Name : rngs.c (Random Number Generation - Multiple Streams)
* Authors : Steve Park & Dave Geyer
* Language : ANSI C
* Latest Revision : 09-22-98
* -------------------------------------------------------------------------
*/
#include <stdio.h>
#include <time.h>
#include "rngs.h"
#define MODULUS 2147483647 /* DON'T CHANGE THIS VALUE */
#define MULTIPLIER 48271 /* DON'T CHANGE THIS VALUE */
#define CHECK 399268537 /* DON'T CHANGE THIS VALUE */
#define STREAMS 256 /* # of streams, DON'T CHANGE THIS VALUE */
#define A256 22925 /* jump multiplier, DON'T CHANGE THIS VALUE */
#define DEFAULT 123456789 /* initial seed, use 0 < DEFAULT < MODULUS */
static long seed[STREAMS] = {DEFAULT}; /* current state of each stream */
static int stream = 0; /* stream index, 0 is the default */
static int initialized = 0; /* test for stream initialization */
double Random(void)
/* ----------------------------------------------------------------
* Random returns a pseudo-random real number uniformly distributed
* between 0.0 and 1.0.
* ----------------------------------------------------------------
*/
{
const long Q = MODULUS / MULTIPLIER;
const long R = MODULUS % MULTIPLIER;
long t;
t = MULTIPLIER * (seed[stream] % Q) - R * (seed[stream] / Q);
if (t > 0)
seed[stream] = t;
else
seed[stream] = t + MODULUS;
return ((double) seed[stream] / MODULUS);
}
void PlantSeeds(long x)
/* ---------------------------------------------------------------------
* Use this function to set the state of all the random number generator
* streams by "planting" a sequence of states (seeds), one per stream,
* with all states dictated by the state of the default stream.
* The sequence of planted states is separated one from the next by
* 8,367,782 calls to Random().
* ---------------------------------------------------------------------
*/
{
const long Q = MODULUS / A256;
const long R = MODULUS % A256;
int j;
int s;
initialized = 1;
s = stream; /* remember the current stream */
SelectStream(0); /* change to stream 0 */
PutSeed(x); /* set seed[0] */
stream = s; /* reset the current stream */
for (j = 1; j < STREAMS; j++) {
x = A256 * (seed[j - 1] % Q) - R * (seed[j - 1] / Q);
if (x > 0)
seed[j] = x;
else
seed[j] = x + MODULUS;
}
}
void PutSeed(long x)
/* ---------------------------------------------------------------
* Use this function to set the state of the current random number
* generator stream according to the following conventions:
* if x > 0 then x is the state (unless too large)
* if x < 0 then the state is obtained from the system clock
* if x = 0 then the state is to be supplied interactively
* ---------------------------------------------------------------
*/
{
char ok = 0;
if (x > 0)
x = x % MODULUS; /* correct if x is too large */
if (x < 0)
x = ((unsigned long) time((time_t *) NULL)) % MODULUS;
if (x == 0)
while (!ok) {
printf("\nEnter a positive integer seed (9 digits or less) >> ");
scanf("%ld", &x);
ok = (0 < x) && (x < MODULUS);
if (!ok)
printf("\nInput out of range ... try again\n");
}
seed[stream] = x;
}
void GetSeed(long *x)
/* ---------------------------------------------------------------
* Use this function to get the state of the current random number
* generator stream.
* ---------------------------------------------------------------
*/
{
*x = seed[stream];
}
void SelectStream(int index)
/* ------------------------------------------------------------------
* Use this function to set the current random number generator
* stream -- that stream from which the next random number will come.
* ------------------------------------------------------------------
*/
{
stream = ((unsigned int) index) % STREAMS;
if ((initialized == 0) && (stream != 0)) /* protect against */
PlantSeeds(DEFAULT); /* un-initialized streams */
}
void TestRandom(void)
/* ------------------------------------------------------------------
* Use this (optional) function to test for a correct implementation.
* ------------------------------------------------------------------
*/
{
long i;
long x;
/*double u;*/ /* used to be uncommented */
char ok = 0;
SelectStream(0); /* select the default stream */
PutSeed(1); /* and set the state to 1 */
for(i = 0; i < 10000; i++)
Random(); /* used to have u = Random() */
GetSeed(&x); /* get the new state value */
ok = (x == CHECK); /* and check for correctness */
SelectStream(1); /* select stream 1 */
PlantSeeds(1); /* set the state of all streams */
GetSeed(&x); /* get the state of stream 1 */
ok = ok && (x == A256); /* x should be the jump multiplier */
if (ok)
printf("\n The implementation of rngs.c is correct.\n\n");
else
printf("\n\a ERROR -- the implementation of rngs.c is not correct.\n\n");
}

View File

@@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------
* Name : rngs.h (header file for the library file rngs.c)
* Author : Steve Park & Dave Geyer
* Language : ANSI C
* Latest Revision : 09-22-98
* -----------------------------------------------------------------------
*/
#if !defined( _RNGS_ )
#define _RNGS_
double Random(void);
void PlantSeeds(long x);
void GetSeed(long *x);
void PutSeed(long x);
void SelectStream(int index);
void TestRandom(void);
#endif

View File

@@ -0,0 +1,27 @@
#include "rngs.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if (argc < 3) {
printf ("Not enough inputs: seed target\n");
}
SelectStream(1);
PutSeed((long)atoi(argv[1]));
int done = 0;
int c = 1000000000;
while (!done) {
c = floor(Random() * 1000000000);
// if (c % 100000 == 0) {
// printf ("c = %d\n", c);
// }
if (c == atoi(argv[2])) {
printf ("Found the bug!\n");
done = 1;
}
}
}

View File

@@ -0,0 +1,30 @@
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#define DEBUG 0
#define NOISY_TEST 1
int main () {
int r;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
r = initializeGame(4, k, 1, &G);
printf ("initializeGame(4, k, 1, &G) = %d\n", r);
assert(r == 0);
r = supplyCount(adventurer, &G);
printf ("supplyCount(adventurer, &G) = %d\n", r);
assert(r == 10);
return 0;
}

View File

@@ -0,0 +1,94 @@
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#define DEBUG 0
#define NOISY_TEST 1
int checkDrawCard(int p, struct gameState *post) {
struct gameState pre;
memcpy (&pre, post, sizeof(struct gameState));
int r;
// printf ("drawCard PRE: p %d HC %d DeC %d DiC %d\n",
// p, pre.handCount[p], pre.deckCount[p], pre.discardCount[p]);
r = drawCard (p, post);
//printf ("drawCard POST: p %d HC %d DeC %d DiC %d\n",
// p, post->handCount[p], post->deckCount[p], post->discardCount[p]);
if (pre.deckCount[p] > 0) {
pre.handCount[p]++;
pre.hand[p][pre.handCount[p]-1] = pre.deck[p][pre.deckCount[p]-1];
pre.deckCount[p]--;
} else if (pre.discardCount[p] > 0) {
memcpy(pre.deck[p], post->deck[p], sizeof(int) * pre.discardCount[p]);
memcpy(pre.discard[p], post->discard[p], sizeof(int)*pre.discardCount[p]);
pre.hand[p][post->handCount[p]-1] = post->hand[p][post->handCount[p]-1];
pre.handCount[p]++;
pre.deckCount[p] = pre.discardCount[p]-1;
pre.discardCount[p] = 0;
}
assert (r == 0);
assert(memcmp(&pre, post, sizeof(struct gameState)) == 0);
}
int main () {
int i, n, r, p, deckCount, discardCount, handCount;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
printf ("Testing buyCard.\n");
printf ("RANDOM TESTS.\n");
SelectStream(2);
PutSeed(3);
for (n = 0; n < 2000; n++) {
for (i = 0; i < sizeof(struct gameState); i++) {
((char*)&G)[i] = floor(Random() * 256);
}
p = floor(Random() * 2);
G.deckCount[p] = floor(Random() * MAX_DECK);
G.discardCount[p] = floor(Random() * MAX_DECK);
G.handCount[p] = floor(Random() * MAX_HAND);
checkDrawCard(p, &G);
}
printf ("ALL TESTS OK\n");
exit(0);
printf ("SIMPLE FIXED TESTS.\n");
for (p = 0; p < 2; p++) {
for (deckCount = 0; deckCount < 5; deckCount++) {
for (discardCount = 0; discardCount < 5; discardCount++) {
for (handCount = 0; handCount < 5; handCount++) {
memset(&G, 23, sizeof(struct gameState));
r = initializeGame(2, k, 1, &G);
G.deckCount[p] = deckCount;
memset(G.deck[p], 0, sizeof(int) * deckCount);
G.discardCount[p] = discardCount;
memset(G.discard[p], 0, sizeof(int) * discardCount);
G.handCount[p] = handCount;
memset(G.hand[p], 0, sizeof(int) * handCount);
checkDrawCard(p, &G);
}
}
}
}
return 0;
}

View File

@@ -0,0 +1,94 @@
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "rngs.h"
#define DEBUG 0
#define NOISY_TEST 1
int checkDrawCard(int p, struct gameState *post) {
struct gameState pre;
memcpy (&pre, post, sizeof(struct gameState));
int r;
// printf ("drawCard PRE: p %d HC %d DeC %d DiC %d\n",
// p, pre.handCount[p], pre.deckCount[p], pre.discardCount[p]);
r = drawCard (p, post);
//printf ("drawCard POST: p %d HC %d DeC %d DiC %d\n",
// p, post->handCount[p], post->deckCount[p], post->discardCount[p]);
if (pre.deckCount[p] > 0) {
pre.handCount[p]++;
pre.hand[p][pre.handCount[p]-1] = pre.deck[p][pre.deckCount[p]-1];
pre.deckCount[p]--;
} else if (pre.discardCount[p] > 0) {
memcpy(pre.deck[p], post->deck[p], sizeof(int) * pre.discardCount[p]);
memcpy(pre.discard[p], post->discard[p], sizeof(int)*pre.discardCount[p]);
pre.hand[p][post->handCount[p]-1] = post->hand[p][post->handCount[p]-1];
pre.handCount[p]++;
pre.deckCount[p] = pre.discardCount[p]-1;
pre.discardCount[p] = 0;
}
assert (r == 0);
assert(memcmp(&pre, post, sizeof(struct gameState)) == 0);
}
int main () {
int i, n, r, p, deckCount, discardCount, handCount;
int k[10] = {adventurer, council_room, feast, gardens, mine,
remodel, smithy, village, baron, great_hall};
struct gameState G;
printf ("Testing drawCard.\n");
printf ("RANDOM TESTS.\n");
SelectStream(2);
PutSeed(3);
for (n = 0; n < 2000; n++) {
for (i = 0; i < sizeof(struct gameState); i++) {
((char*)&G)[i] = floor(Random() * 256);
}
p = floor(Random() * 2);
G.deckCount[p] = floor(Random() * MAX_DECK);
G.discardCount[p] = floor(Random() * MAX_DECK);
G.handCount[p] = floor(Random() * MAX_HAND);
checkDrawCard(p, &G);
}
printf ("ALL TESTS OK\n");
exit(0);
printf ("SIMPLE FIXED TESTS.\n");
for (p = 0; p < 2; p++) {
for (deckCount = 0; deckCount < 5; deckCount++) {
for (discardCount = 0; discardCount < 5; discardCount++) {
for (handCount = 0; handCount < 5; handCount++) {
memset(&G, 23, sizeof(struct gameState));
r = initializeGame(2, k, 1, &G);
G.deckCount[p] = deckCount;
memset(G.deck[p], 0, sizeof(int) * deckCount);
G.discardCount[p] = discardCount;
memset(G.discard[p], 0, sizeof(int) * discardCount);
G.handCount[p] = handCount;
memset(G.hand[p], 0, sizeof(int) * handCount);
checkDrawCard(p, &G);
}
}
}
}
return 0;
}

View File

@@ -0,0 +1,56 @@
#include "dominion.h"
#include <stdio.h>
#include <stdlib.h>
#include "rngs.h"
int main (int argc, char** argv) {
struct gameState G;
int i;
int start = -1;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
memset(&G, 'z', sizeof(struct gameState));
initializeGame(4, k, atoi(argv[1]), &G);
printf ("Rough guide to locations in structure:\n");
printf ("0: numPlayers\n");
printf ("%ld: supplyCount[0]\n", ((long)&(G.supplyCount[0]))-((long)&G));
printf ("%ld: embargoTokens[0]\n", ((long)&(G.embargoTokens[0]))-((long)&G));
printf ("%ld: hand[0][0]\n", ((long)&(G.hand[0][0]))-(long)(&G));
printf ("%ld: deck[0][0]\n", ((long)&(G.deck[0][0]))-(long)(&G));
printf ("%ld: discard[0][0]\n", ((long)&(G.discard[0][0]))-(long)(&G));
printf ("%ld: playerCards[0]\n", ((long)&(G.playedCards[0]))-(long)(&G));
for (i = 0; i < sizeof(struct gameState); i++) {
if (((char*)&G)[i] == 'z') {
if (start == -1) {
start = i;
}
} else{
if (start != -1) {
if (start == (i-1)) {
printf ("Byte %d uninitialized.\n", start);
} else {
printf ("Bytes %d-%d uninitialized.\n", start, i-1);
}
start = -1;
}
}
}
if (start != -1) {
if (start == (i-1)) {
printf ("Byte %d uninitialized.\n", start);
} else {
printf ("Bytes %d-%d uninitialized.\n", start, i-1);
}
}
return 0;
}

View File

@@ -0,0 +1,27 @@
#include "dominion.h"
#include <stdio.h>
#include <assert.h>
int compare(const int* a, const int* b);
int main () {
struct gameState G;
struct gameState G2;
// Initialize G.
memcpy (&G2, &G, sizeof(struct gameState));
int ret = shuffle(0,&G);
if (G.deckCount[0] > 0) {
assert (ret != -1);
qsort ((void*)(G.deck[0]), G.deckCount[0], sizeof(int), compare);
qsort ((void*)(G2.deck[0]), G2.deckCount[0], sizeof(int), compare);
} else
assert (ret == -1);
assert(memcmp(&G, &G2, sizeof(struct gameState)) == 0);
}

View File

@@ -0,0 +1,69 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
initializeGame(2, k, 65432, &G);
//If province cards are empty or are least three supply piles are, game end
//else game is not over. 1 is game end, 0 is game continues
unsigned int victory_cards_max = 12;
unsigned int num_supply_piles = treasure_map+1;
unsigned int max_count_for_any_supply = 60;
int i;
for (i = 0; i < victory_cards_max; i++) {
int num_zeroed = 0;
for (int k = 0; k < num_supply_piles; k++) {
G.supplyCount[k] = max_count_for_any_supply;
}
for (; num_zeroed < num_supply_piles; num_zeroed++) {
for (int j = 0; j < num_zeroed; j++) {
G.supplyCount[j] = 0;
}
G.supplyCount[province] = i;
if ((G.supplyCount[province] == 0) || (num_zeroed >= 3)) {
testing_assert(isGameOver(&G) == 1);
} else {
testing_assert(isGameOver(&G) == 0);
}
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,88 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
unsigned int min_players = 2;
unsigned int max_players = MAX_PLAYERS;
unsigned int max_hand = MAX_HAND;
unsigned int arbitrary_bonus_max = 1000;
unsigned int copper_worth = 1;
unsigned int silver_worth = 2;
unsigned int gold_worth = 3;
for(int i = min_players ; i <= max_players ; i++){
initializeGame(i, k, 65432, &G);
for(int j = 0 ; j < i ; j++){
G.handCount[j] = max_hand;
for(int k = 0 ; k < max_hand ; k++){
G.hand[j][k] = copper;
}
updateCoins(j, &G, 0);
testing_assert(G.coins == (max_hand*copper_worth));
updateCoins(j, &G, arbitrary_bonus_max);
testing_assert(G.coins == ((max_hand*copper_worth) + arbitrary_bonus_max));
for(int k = 0 ; k < max_hand ; k++){
G.hand[j][k] = silver;
}
updateCoins(j, &G, 0);
testing_assert(G.coins == (max_hand*silver_worth));
updateCoins(j, &G, arbitrary_bonus_max);
testing_assert(G.coins == ((max_hand*silver_worth) + arbitrary_bonus_max));
for(int k = 0 ; k < max_hand ; k++){
G.hand[j][k] = gold;
}
updateCoins(j, &G, 0);
testing_assert(G.coins == (max_hand*gold_worth));
updateCoins(j, &G, arbitrary_bonus_max);
testing_assert(G.coins == ((max_hand*gold_worth) + arbitrary_bonus_max));
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,47 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
for(int i = 2 ; i <= MAX_PLAYERS ; i++){
initializeGame(i, k, 65432, &G);
for(int j = 0 ; j < i ; j++){
G.whoseTurn = j;
testing_assert(whoseTurn(&G) == j);
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}

View File

@@ -0,0 +1,55 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: unittest1.c
* Author: corwinperren
*
* Created on February 1, 2017, 9:02 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
int testing_assert(int expression);
int main(int argc, char** argv) {
struct gameState G;
int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
sea_hag, tribute, smithy};
initializeGame(4, k, 65432, &G);
unsigned int num_card_types = 27;
for(int i = 0 ; i < MAX_PLAYERS ; i++){
G.whoseTurn = i;
G.handCount[i] = MAX_HAND;
for(int j = 0 ; j < MAX_HAND ; j++){
for(int k = 0 ; k < num_card_types ; k++){
G.hand[i][j] = k;
testing_assert(handCard(j, &G) == k);
}
}
}
return (EXIT_SUCCESS);
}
int testing_assert(int expression) {
if (expression) {
printf("TEST SUCCEEDED!\n");
return 1;
} else {
printf("TEST FAILED!\n");
return 0;
}
}