コード例 #1
0
ファイル: unittest3.c プロジェクト: cs362sp16/cs362sp16_fryta
//Testing endGame
int main() {
	struct gameState g, orig;
	
	int k[10] = {smithy,adventurer,gardens,embargo,cutpurse,mine,ambassador,
			   outpost,baron,tribute};
	int p[3]; //should be set in dominion.c
	int r = 0;
	int i;
	int count;
	//3 player game this time
	initializeGame(3, k, 1, &orig);
	//Trevor's suggestion, saving game state for testing purposes
	memcpy(&g, &orig, sizeof(struct gameState));	
	
	//buy an estate to gain victory points and "win"
	buyCard(estate, &g);
	//get to player 2's turn and end
	//Loop until end
	for(i = 0; i < 2; ++i){
		endTurn(&g);
	}
	getWinners(p, &g);
	myAssert(p[1] == 0 && p[2] == 0, "Player 1 and 2 should have lost but did not", __LINE__);
	myAssert(p[3] == -9999, "Player 4 should not exist but did", __LINE__);
	myAssert(p[0] == 1, "Player 0 did not win", __LINE__);
	
	
	
	

	if(r == 0)
		printf("Tests completed successfully!\n");

	return 0;
}
コード例 #2
0
}

int main(int argc, char** argv){
   struct gameState *G;
      int numplayers, money, handpos, rseed, rcard, choice1, choice2, choice3, coinflip, i, numtests,r;
      int *k;
      int players[4];
      numtests=1;
      for(i=0; i<numtests; i++){
	 G=newGame();
	    rseed=atoi(argv[1]);
	    k = malloc(sizeof(int)*10);
	    numplayers=rand()%3+2;
	    chooseKingdomCards(k);
	    initializeGame(numplayers, k, rseed, G);

	    printf("FOR SEED %d\n", rseed);
	    if(isGameOver(G)){
	       printf("game creation error\n");
	       printf("test %d failed\n",i);
	    }
	    else{
	       while(!isGameOver(G)){
                printf("player %d turn start\n", whoseTurn(G));
		  handpos=-1;
		     coinflip=rand()%10;
		  rcard=chooseHandCard(G, k, &handpos);
		     choice1=rand()%2;
		     choice2=0;
		     if(choice1==0){
			choice2=1;
		     }
		  choice3=rand()%2;
		     if(rcard!=-1 || rcard==curse){
			r=playCard(handpos, choice1,choice2,choice3,G);
			printf("player %d played ", whoseTurn(G));
			printCard(rcard, r);
		     }
		  money=countMoney(G);
		     printf("money: %d\n", money);
		     rcard=randomBuyCard(G, money, k);
		     if(coinflip!=9){			//small chance to not buy anything
			r=buyCard(rcard, G);
			printf("player %d bought ", whoseTurn(G));
			printCard(rcard, r);
		     }
		     printf("player %d hand\n", whoseTurn(G));
		     printhand(G);
		     printf("player %d score: %d\n", whoseTurn(G), scoreFor(whoseTurn(G),G));
		     printf("player %d turn ending\n", whoseTurn(G));
		     endTurn(G);
		     if(isGameOver(G)){
			printf("game over player %d won\n", getWinners(players, G));
		     }
	       }
	       free(k);
	       free(G);
	       printf("test %d passed\n", i);
	    }
      }
コード例 #3
0
int main (int argc, char** argv) {
  struct gameState G;
  int winners[MAX_PLAYERS] = {0};
  
  G.numPlayers = 3;
  G.whoseTurn = 0;
  
  for (int i=0; i<G.numPlayers; i++) {  
    G.handCount[i] = 0;
    G.discardCount[i] = 0;
    G.deckCount[i] = 0;
  }
  
  G.handCount[0] = 1;
  G.hand[0][0] = province;
  
  G.handCount[1] = 1;
  G.hand[1][0] = province;
  
  G.handCount[2] = 1;
  G.hand[2][0] = province;
  
  getWinners(winners, &G);
  
  assert(winners[0] == 0 && winners[1] == 1 && winners[2] == 1 && winners[3] == 0);
  printf("*** ALL TESTS PASSED ***\n");

  return 0;
}
コード例 #4
0
void print_state(struct gameState* p, int players)
{
    int i, j,score, winner[players];

    getWinners(winner, p);
    printf("Final gamestate:\n");
    for(i = 0; i < players; i++)
    {
        score = 0;
        printf("\nPlayer %d:\n", i);
        if(winner[i])
            printf("WINNER!\n");
        printf("    Cards:\n");
        for(j = 0; j < (p->deckCount)[i]; j++)
        {
            printf("%s, ", get_name((p->deck)[i][j]));
        }
        printf("\n    Score: %d\n", scoreFor(i, p));
    }
    

}
コード例 #5
0
ファイル: unittest2.c プロジェクト: orst-iqbald/cs362s15
int testGetWinners(int players[MAX_PLAYERS], struct gameState *after)
{
  int i;
  int j;
  int highScore;
  int currentPlayer;
  int playerIdx;
  int beforeOut;
  int afterOut;
  int failTests = 0;
  struct gameState before;
  memcpy(&before, after, sizeof(struct gameState));

  //this is getting score for each player
  for (i = 0; i < MAX_PLAYERS; i++)
  {
    if (i >= before.numPlayers)
    {
      players[i] = -9999;
    }
    else
    {
      players[i] = scoreFor(i, &before);
    }
  }

  //this is to find highScore
  j = 0;
  for (i = 0; i < MAX_PLAYERS; i++)
  {
    if (players[i] > players[j])
    {
      j = i;
    }
  }
  highScore = players[j];

  //this is adding 1 to players who had less turns
  currentPlayer = whoseTurn(&before);
  for (i = 0; i < MAX_PLAYERS; i++)
  {
    if ( players[i] == highScore && i > currentPlayer )
    {
      players[i]++;
    }
  }

  //this is to find new highScore
  j = 0;
  for (i = 0; i < MAX_PLAYERS; i++)
  {
    if ( players[i] > players[j] )
    {
      j = i;
    }
  }
  highScore = players[j];

  //this is to insert winners in array to 1 and losers to 0
  for (i = 0; i < MAX_PLAYERS; i++)
  {
    //player that has highScore
    if ( players[i] == highScore )
    {
      //then player gets a 1 for win
      players[i] = 1;
    }
    else
    {
      //else they get a 0 for lose
      players[i] = 0;
    }
  }

  //loop through all players and find winner
  for(playerIdx = 0; playerIdx < before.numPlayers; playerIdx++)
  {
    if (players[playerIdx] == 1)
    {
      if(TESTING) printf("Player %d\n", playerIdx);
      {
        beforeOut = playerIdx;
      }
    }
  }

  //loop through all players and reset
  for(playerIdx = 0; playerIdx < before.numPlayers; playerIdx++)
  {
    players[playerIdx] = 0;
  }

  //this is the call to getWinners function to find winners in the after struct
  getWinners(players, after);

  for(playerIdx = 0; playerIdx < before.numPlayers; playerIdx++)
  {
    if (players[playerIdx] == 1)
    {
      if(TESTING) printf("Player %d\n", playerIdx);
      {
        afterOut = playerIdx;
      }
    }
  }
  
  if (beforeOut != afterOut) 
  {
    printf("getWinners() FAIL: GET WINNERS\n");
    printf("getWinners() INFO: Not Calculating Winners Properly!\n");
    failTests = 1;
  }
  
  if (failTests == 0)
  {
    //return 0 if no errors
    return 0;
  }
  else
  {
    //return 1 if there were errors
    return failTests;
  }
}
コード例 #6
0
int main(int argc, char *argv[]) {
    
    // initialize variables
    int i = 0, j = 0, n = 0;
    int numPlayers = 0;
    int kingdomCards[10] = {8, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int card = 0;
    char *cardName = (char*)malloc(sizeof(char) * 20);
    int check = 0;
    int seed = 0;
    int result = 1;
    int winners[4];
    
    // do this so I can just put "state" in method calls, much cleaner
    struct gameState G;
    struct gameState *state = &G;
    
    // get seed
    if (argc != 2) {
        printf("NEED ONLY A SEED");
        return 1;
    }
    
    printf("***Initializing Game ***\n");
    
    // seed random
    seed = atoi(argv[1]);
    srand(seed);
    
    // get number of players (from 2 - 4)
    numPlayers = (rand() % 3) + 2;
    
    // get kingdom cards
    for (i = 0; i < 10; i++) {
        card = (rand() % 20) + 7;
        
        // check if card already in deck
        for (j = 0; j < i; j++) {
            if (card == kingdomCards[j]) {
                check = 1;
            } else {
                check = 0;
            }
            
            // if card is in deck @ j, decrement i to choose new card, break out of j loop
            if (check) {
                i--;
                break;
            } else {
                kingdomCards[i] = card;
                char *handCard = (char*)malloc(sizeof(char) * 20);
                cardNumToName(card, cardName);
                cardNumToName(kingdomCards[i], handCard);
                if (i == 0) { printf("Card: %s\nHand: %s\n", cardName, handCard); }
            }
        }
    }
    
    // initialize dominion.c
    result = initializeGame(numPlayers, kingdomCards, seed, state);
    if (result != 0) {
        printf("Game failed to initialize\n");
        return 2;
    }
    
    // print initialized values
    printf("Initialized game with values:\n");
    printf("Players: %i\n", numPlayers);
    printf("Cards: ");
    for (i = 0; i < 10; i++) {
        cardNumToName(kingdomCards[i], cardName);
        if (i == 0) {
            printf("%s", cardName);
        } else {
            printf(", %s", cardName);
        }
    }
    printf("\n");
    
    // start playing game
    printf("***Starting Game***\n");
    
    while (!isGameOver(state)) {
        int currentCard = 0;
        int actualCard = 0;
        int playersTurn = whoseTurn(state);
        printf("Player %i's turn\n", playersTurn + 1);
        
        //start actions
        printf("*Action Phase*\n");
        for (i = 0; i < numHandCards(state); i++) {
            // make sure card is a kingdom card
            if (handCard(i, state) >= adventurer && handCard(i, state) <= treasure_map && handCard(i, state) != feast && handCard(i, state) != tribute) {
                currentCard = i;
                actualCard = handCard(currentCard, state);
                cardNumToName(actualCard, cardName);
                printf("Card: %s\n", cardName);
                break;
            }
        }
        
        // perform actions
        printf("Actions left: %i\n", state->numActions);
        while (state->numActions > 0 && currentCard != 0) {
            cardNumToName(actualCard, cardName);
            
            // select random cards, better than static
            int one = rand() % state->handCount[playersTurn] + 1;
            int two = rand() % state->handCount[playersTurn] + 1;
            int three = rand() % state->handCount[playersTurn] + 1;
            result = playCard(currentCard, one, two, three, state);
            if (result == -1) {
                printf("Error: Could not play %s\n", cardName);
                discardCard(0, playersTurn, state, 0);
                break;
            } else {
                printf("Played: %s\n", cardName);
            }
            
            if (state->numActions == 0) {
                printf("Out of actions! Move to buy phase\n");
                break;
            }
            
            for (i = currentCard; i < numHandCards(state); i++) {
                if (handCard(i, state) >= adventurer && handCard(i, state) <= treasure_map && handCard(i, state) != feast && handCard(i, state) != tribute) {
                    currentCard = i;
                    actualCard = handCard(currentCard, state);
                    cardNumToName(actualCard, cardName);
                    printf("Other Card: %s\n", cardName);
                    break;
                } else {
                    currentCard = 0;
                }
            }
        }
        
        //start buy phase
        printf("*Buy Phase*\n");
        
        // figure out how many coins the user has
        int coins = 0;
        for (i = 0; i < numHandCards(state); i++) {
            if (handCard(i, state) == copper) {
                coins += 1;
            } else if (handCard(i, state) == silver) {
                coins += 2;
            } else if (handCard(i, state) == gold) {
                coins += 3;
            }
        }
        state->coins = coins;
        
        printf("Number of buys: %i\n", state->numBuys);
        
        // perform random buys
        int m = 0;
        while (state->numBuys > 0) {
            do {
                int available = 0;
                while (!available) {
                    // buy random card from kingdom cards
                    int randCard = rand() % 28;
                    // if rand card is a global kingdom card, check if available this round
                    if (randCard >= adventurer) {
                        for (i = 0; i < 10; i++) {
                            if (randCard == kingdomCards[i]) {
                                available = 1;
                                currentCard = randCard;
                                break;
                            }
                        }
                        // if not king card, try to buy
                    } else {
                        available = 1;
                        currentCard = randCard;
                    }
                }
                
                // Move on to buying specific items if taking too long
                m++;
                if (m == 20) {
                    currentCard = gold;
                } else if (m == 21) {
                    currentCard = silver;
                } else if (m > 21) {
                    currentCard = copper;
                }
                
            } while (buyCard(currentCard, state) != 0);
            
            cardNumToName(currentCard, cardName);
            printf("Bought %s\n", cardName);
        }
        
        printf("Out of buys! Next player's turn\n\n");
        endTurn(state);
        
        // stop game if each player has had 100 turns, no winners
        n++;
        if (n > 200) {
            break;
        }
    }
    
    getWinners(winners, state);
    for (i = 0; i < numPlayers; i++) {
        if (winners[i] == 1) {
            printf("Player %i wins!\nScore: %i\n", i + 1, scoreFor(i, state));
            break;
        }
    }
    
    printf("\n****Game Ended****\n");
    
    return 0;
}
コード例 #7
0
ファイル: unittest4.c プロジェクト: cr8zd/cs362w16
/**
 * Function:  unittest4
 * Inputs:  printVal, seed, results
 * Outputs: none
 * Description: tests getWinners() function in dominion.c
 */
void unitTest4(int printVal, int seed, struct results *result){

	int i, r=0;
	int players[MAX_PLAYERS] = {0};

	struct playerGroup *playGroup = newPlayerGroup();

	// setup the kingdom cards available
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse,
					 sea_hag, tribute, smithy};

	// setup the struct for the control and active Games
	struct gameState *controlGame = newGame();
	struct gameState *activeGame = newGame();

	playGroup->numPlayers = random_number(2, MAX_PLAYERS);
	initializeGame(playGroup->numPlayers, k, seed, controlGame);

	playGroup->playerValues[0]->cardCounts[0] = 23;
	playGroup->playerValues[1]->cardCounts[0] = 46;
	playGroup->playerValues[2]->cardCounts[0] = 75;
	playGroup->playerValues[3]->cardCounts[0] = 66;

	for(i = 0; i < playGroup->numPlayers; i++)
	{
		updateCards(i, playGroup->playerValues[i], controlGame);
	}

	//copy the values from the activeGame into the controlGame
	memcpy(activeGame, controlGame, sizeof(struct gameState));

	// calculate the scores for the players
	for(i = 0; i < MAX_PLAYERS; i++)
	{
		playGroup->playerValues[i]->score = calculate_total(i, controlGame);
	}

	// run the getWinners function
	r = getWinners(players, activeGame);
	assert(r == 0);

	int winningScore = playGroup->playerValues[0]->score;
	int curWinner = 0;
	playGroup->playerValues[0]->winner = 1;

	for(i = 0; i < MAX_PLAYERS; i++)
	{

		if(playGroup->playerValues[i]->score >= winningScore)
		{
			winningScore = playGroup->playerValues[i]->score;
			playGroup->playerValues[curWinner]->winner = 0;
			playGroup->playerValues[i]->winner = 1;
			curWinner = i;
		}

	}

	for(i=0; i < MAX_PLAYERS; i++)
	{
		int j;
		int score = scoreFor(i, activeGame);

		if(!(playGroup->playerValues[i]->winner == players[i]) || !(score == playGroup->playerValues[i]->score))
		{
			printf("TEST: %d, FAIL: Player: %d of %d players\nScore: %d, Expected: %d, Winner Val Returned: %d,"
					" Expected: %d\n",
					result->testNum, i, playGroup->numPlayers, score,
					playGroup->playerValues[i]->score, players[i], playGroup->playerValues[i]->winner);
			result->testsFailed++;
		}
		else
		{
			assert(playGroup->playerValues[i]->winner == players[i]);

			if(PRINTPASS){
				printf("TEST: %d, PASS: Player: %d of %d players\nScore: %d, Expected: %d, Winner Val Returned: %d,"
						" Expected: %d\n",
						result->testNum, i, playGroup->numPlayers, score,
						playGroup->playerValues[i]->score, players[i], playGroup->playerValues[i]->winner);
			}
		}

		if(!(scoreFor(i, activeGame) == playGroup->playerValues[i]->score))
		{
			printf("HAND: ");
			for(j = 0; j < activeGame->handCount[i]; j++)
			{
				printf("%d,", activeGame->hand[i][j]);
			}
			printf("\n");
			printf("DECK: ");
			for(j = 0; j < activeGame->deckCount[i]; j++)
			{
				printf("%d,", activeGame->deck[i][j]);
			}
			printf("\n");
			printf("DISCARD: ");
			for(j = 0; j < activeGame->discardCount[i]; j++)
			{
				printf("%d,", activeGame->discard[i][j]);
			}
			printf("\n");
			printf("CURSE: %d, ESTATES: %d, DUCHY: %d, PROVINCE: %d, GREAT_HALL:%d, GARDENS: %d, totalCards: %d\n",
					playGroup->playerValues[i]->cardCounts[CURSE_CT], playGroup->playerValues[i]->cardCounts[ESTATE_CT],
					playGroup->playerValues[i]->cardCounts[DUCHY_CT], playGroup->playerValues[i]->cardCounts[PROVINCE_CT],
					playGroup->playerValues[i]->cardCounts[GREATHALL_CT], playGroup->playerValues[i]->cardCounts[GARDENS_CT],
					playGroup->playerValues[i]->totalCards);
		}
	}


	result->testsPassed++;

	// clean up memory
	for(i = 0; i < MAX_PLAYERS; i++){
		free(playGroup->playerValues[i]);
	}
	free(playGroup);
	free(controlGame);
	free(activeGame);
}
コード例 #8
0
int main(int argc, char** argv) {

	srand(time(NULL));

	struct gameState G;
	struct gameState *state = &G;

	int k[23] = { adventurer, gardens, embargo, village, minion, mine, cutpurse,
		sea_hag, tribute, smithy, curse, estate, duchy, province, council_room, feast,
		remodel, baron, great_hall, steward, ambassador, outpost, salvager};

	int i, j, test, z;

	int money, smithyPos, adventurerPos, cutpursePos, gardensPos, embargoPos, villagePos;
	int minionPos, minePos, sea_hagPos, tributePos, cursePos, duchyPos, provincePos, council_roomPos;
	int feastPos, remodelPos, baronPos, great_hallPos, stewardPos, ambassadorPos;
	int outpostPos, salvagerPos, treasure_mapPos;

	int numSmithy, numAdventurer, numGardens, numEmbargo, numVillage, numMinion, numMine, numCutpurse;
	int numSea_Hag, numTribute, numCurse, numDuchy, numProvince, numCouncil_Room, numFeast;
	int numRemodel, numBaron, numGreat_Hall, numSteward, numAmbassador, numOutpost, numSalvager, numTreasure_Map;


	int players, seed;

	int counter;

	for (test = 0; test < MAX_TESTS; test++)
	{
		printf("\n***************************************\n");
		printf("************* START GAME **************\n");
		printf("***************************************\n\n");

		players = rand() % 3 + 2;

		seed = (rand() + 1);

		initializeGame(players, k, seed, state);

		numSmithy = 0;
		numAdventurer = 0;
		numGardens = 0;
		numEmbargo = 0;
		numVillage = 0;
		numMinion = 0;
		numMine = 0;
		numCutpurse = 0;
		numSea_Hag = 0;
		numTribute = 0;
		numCurse = 0;
		numDuchy = 0;
		numProvince = 0;
		numCouncil_Room = 0;
		numFeast = 0;
		numRemodel = 0;
		numBaron = 0;
		numGreat_Hall = 0;
		numSteward = 0;
		numAmbassador = 0;
		numOutpost = 0;
		numSalvager = 0;
		numTreasure_Map = 0;

		counter = 0;

		while (!isGameOver(state) == 1)
		{
			counter++;

			if (counter == MAX_PLAYS)
			{
				printf("\n***************************************");
				printf("\n************* TERMINATION *************");
				printf("\n************** MAX (250) **************");
				printf("\n***************************************\n");
				break;
			}


			money = 0;
			smithyPos = -1;
			adventurerPos = -1;
			cutpursePos = -1;
			gardensPos = -1;
			embargoPos = -1;
			villagePos = -1;
			minionPos = -1;
			minePos = -1;
			sea_hagPos = -1;
			tributePos = -1;
			cursePos = -1;
			duchyPos = -1;
			provincePos = -1;
			council_roomPos = -1;
			feastPos = -1;
			remodelPos = -1;
			baronPos = -1;
			great_hallPos = -1;
			stewardPos = -1;
			ambassadorPos = -1;
			outpostPos = -1;
			salvagerPos = -1;
			treasure_mapPos = -1;


			for (i = 0; i < numHandCards(state); i++)
			{
				if (handCard(i, state) == copper)
					money++;
				else if (handCard(i, state) == silver)
					money += 2;
				else if (handCard(i, state) == gold)
					money += 3;
				else if (handCard(i, state) == smithy)
					smithyPos = 1;
				else if (handCard(i, state) == adventurer)
					adventurerPos = i;
				else if (handCard(i, state) == cutpurse)
					cutpursePos = i;
				else if (handCard(i, state) == gardens)
					gardensPos = i;
				else if (handCard(i, state) == embargo)
					embargoPos = i;
				else if (handCard(i, state) == village)
					villagePos = i;
				else if (handCard(i, state) == minion)
					minionPos = i;
				else if (handCard(i, state) == mine)
					minePos = i;
				else if (handCard(i, state) == sea_hag)
					sea_hagPos = i;
				else if (handCard(i, state) == tribute)
					tributePos = i;
				else if (handCard(i, state) == curse)
					cursePos = i;
				else if (handCard(i, state) == duchy)
					duchyPos = i;
				else if (handCard(i, state) == province)
					provincePos = i;
				else if (handCard(i, state) == council_room)
					council_roomPos = i;
				else if (handCard(i, state) == feast)
					feastPos = i;
				else if (handCard(i, state) == remodel)
					remodelPos = i;
				else if (handCard(i, state) == baron)
					baronPos = i;
				else if (handCard(i, state) == great_hall)
					great_hallPos = i;
				else if (handCard(i, state) == steward)
					stewardPos = i;
				else if (handCard(i, state) == ambassador)
					ambassadorPos = i;
				else if (handCard(i, state) == outpost)
					outpostPos = i;
				else if (handCard(i, state) == salvager)
					salvagerPos = i;
				else if (handCard(i, state) == treasure_map)
					treasure_mapPos = i;
			}

			for (i = 0; i < players; i++)
			{
				if ((smithyPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: smithy played from position %d\n", i, smithyPos);
					playCard(smithyPos, -1, -1, -1, state);
					printf("    smithy played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((adventurerPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: adventurer played from position %d\n", i, adventurerPos);
					playCard(adventurerPos, -1, -1, -1, state);
					printf("    adventurer played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((cutpursePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: cutpurse played from position %d\n", i, cutpursePos);
					playCard(cutpursePos, -1, -1, -1, state);
					printf("    cutpurse played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((gardensPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: gardens played from position %d\n", i, gardensPos);
					playCard(gardensPos, -1, -1, -1, state);
					printf("    gardens played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((embargoPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: embargo played from position %d\n", i, embargoPos);
					playCard(embargoPos, -1, -1, -1, state);
					printf("    embargo played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((villagePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: village played from position %d\n", i, villagePos);
					playCard(villagePos, -1, -1, -1, state);
					printf("    village played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((minionPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: minion played from position %d\n", i, minionPos);
					playCard(minionPos, -1, -1, -1, state);
					printf("    minion played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((minePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: mine played from position %d\n", i, minePos);
					playCard(minePos, -1, -1, -1, state);
					printf("    mine played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((sea_hagPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: sea_hag played from position %d\n", i, sea_hagPos);
					playCard(sea_hagPos, -1, -1, -1, state);
					printf("    sea_hag played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				else if ((tributePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: tribute played from position %d\n", i, tributePos);
					playCard(tributePos, -1, -1, -1, state);
					printf("    tribute played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((cursePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: curse played from position %d\n", i, cursePos);
					playCard(cursePos, -1, -1, -1, state);
					printf("    curse played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((duchyPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: duchy played from position %d\n", i, duchyPos);
					playCard(duchyPos, -1, -1, -1, state);
					printf("    duchy played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((provincePos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: province played from position %d\n", i, provincePos);
					playCard(provincePos, -1, -1, -1, state);
					printf("    province played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((council_roomPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: council_room played from position %d\n", i, council_roomPos);
					playCard(council_roomPos, -1, -1, -1, state);
					printf("    council_room played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((feastPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: feast played from position %d\n", i, feastPos);
					playCard(feastPos, -1, -1, -1, state);
					printf("    feast played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((remodelPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: baron played from position %d\n", i, baronPos);
					playCard(baronPos, -1, -1, -1, state);
					printf("    baron played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((great_hallPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: great_hall played from position %d\n", i, great_hallPos);
					playCard(great_hallPos, -1, -1, -1, state);
					printf("    great_hall played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((stewardPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: steward played from position %d\n", i, stewardPos);
					playCard(stewardPos, -1, -1, -1, state);
					printf("    steward played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((ambassadorPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: ambassador played from position %d\n", i, ambassadorPos);
					playCard(ambassadorPos, -1, -1, -1, state);
					printf("    ambassador played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((outpostPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: outpost played from position %d\n", i, outpostPos);
					playCard(outpostPos, -1, -1, -1, state);
					printf("    outpost played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((salvagerPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: salvager played from position %d\n", i, salvagerPos);
					playCard(salvagerPos, -1, -1, -1, state);
					printf("    salvager played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((treasure_mapPos != -1) && ((rand() % 2) == 1))
				{
					printf("%d: treasure_map played from position %d\n", i, treasure_mapPos);
					playCard(treasure_mapPos, -1, -1, -1, state);
					printf("    treasure_map played.\n");
					money = 0;
					i = 0;
					while (i < numHandCards(state))
					{
						if (handCard(i, state) == copper)
						{
							playCard(i, -1, -1, -1, state);
							money++;
						}
						else if (handCard(i, state) == silver)
						{
							playCard(i, -1, -1, -1, state);
							money += 2;
						}
						else if (handCard(i, state) == gold)
						{
							playCard(i, -1, -1, -1, state);
							money += 3;
						}
						i++;
					}
				}

				if ((money >= 8) && ((rand() % 5) == 3))
				{
					printf("%d: bought province\n", i);
					buyCard(province, state);
				}

				if ((money >= 6) && ((rand() % 5) == 3))
				{
					int random;

					random = rand() % 24;

					int cost;

					cost = getCost(random);

					int update;

					update = updateCoins(i, state, 0);

					if (random == 0)
					{
						printf("%d: bought adventurer\n", i);
						buyCard(adventurer, state);
						numAdventurer++;
					}

					else if (random == 1)
					{
						printf("%d: bought smithy\n", i);
						buyCard(smithy, state);
						numSmithy++;
					}
					else if (random == 2)
					{
						printf("%d: bought gardens\n", i);
						buyCard(gardens, state);
						numGardens++;
					}
					else if (random == 3)
					{
						printf("%d: bought embargo\n", i);
						buyCard(embargo, state);
						numEmbargo++;
					}
					else if (random == 4)
					{
						printf("%d: bought village\n", i);
						buyCard(village, state);
						numVillage++;
					}
					else if (random == 5)
					{
						printf("%d: bought minion\n", i);
						buyCard(minion, state);
						numMinion++;
					}
					else if (random == 6)
					{
						printf("%d: bought mine\n", i);
						buyCard(mine, state);
						numMine++;
					}
					else if (random == 7)
					{
						printf("%d: bought cutpurse\n", i);
						buyCard(cutpurse, state);
						numCutpurse++;
					}
					else if (random == 8)
					{
						printf("%d: bought sea_hag\n", i);
						buyCard(sea_hag, state);
						numSea_Hag++;
					}
					else if (random == 9)
					{
						printf("%d: bought tribute\n", i);
						buyCard(tribute, state);
						numTribute++;
					}
					else if (random == 10)
					{
						printf("%d: bought gold\n", i);
						buyCard(gold, state);
					}
					else if (random == 11)
					{
						printf("%d: bought curse\n", i);
						buyCard(curse, state);
						numCurse++;
					}
					else if (random == 12)
					{
						printf("%d: bought duchy\n", i);
						buyCard(duchy, state);
						numDuchy++;
					}
					else if (random == 13)
					{
						printf("%d: bought province\n", i);
						buyCard(province, state);
						numProvince++;
					}
					else if (random == 14)
					{
						printf("%d: bought council_room\n", i);
						buyCard(council_room, state);
						numCouncil_Room++;
					}
					else if (random == 15)
					{
						printf("%d: bought feast\n", i);
						buyCard(feast, state);
						numFeast++;
					}
					else if (random == 16)
					{
						printf("%d: bought remodel\n", i);
						buyCard(remodel, state);
						numRemodel++;
					}
					else if (random == 17)
					{
						printf("%d: bought baron\n", i);
						buyCard(baron, state);
						numBaron++;
					}
					else if (random == 18)
					{
						printf("%d: bought great_hall\n", i);
						buyCard(great_hall, state);
						numGreat_Hall++;
					}
					else if (random == 19)
					{
						printf("%d: bought steward\n", i);
						buyCard(steward, state);
						numSteward++;
					}
					else if (random == 20)
					{
						printf("%d: bought ambassador\n", i);
						buyCard(ambassador, state);
						numAmbassador++;
					}
					else if (random == 21)
					{
						printf("%d: bought outpost\n", i);
						buyCard(outpost, state);
						numOutpost++;
					}
					else if (random == 22)
					{
						printf("%d: bought salvager\n", i);
						buyCard(salvager, state);
						numSalvager++;
					}
					else if (random == 23)
					{
						printf("%d: bought treasure_map\n", i);
						buyCard(treasure_map, state);
						numTreasure_Map++;
					}

				}

				if ((money >= 5) && ((rand() % 5) == 3))
				{
					printf("%d: bought duchy\n", i);
					buyCard(duchy, state);
				}

				if ((money >= 3) && ((rand() % 5) == 3))
				{
					printf("%d: bought silver\n", i);
					buyCard(silver, state);
				}

				if ((money >= 2) && ((rand() % 5) == 3))
				{
					printf("%d: bought estate\n", i);
					buyCard(estate, state);
				}

				printf("%d: end turn\n", i);
				endTurn(state);
			}
		}

		printf("\n***************************************\n");
		printf("************* END OF GAME *************\n");
		printf("***************************************\n\nSCORE:\n");

		int winner;
		int play[MAX_PLAYERS];
		getWinners(play, state);
		for (z = 0; z < MAX_PLAYERS; z++)
		{
			if (play[z] == 1)
				winner = (z + 1);
		}
		printf("Winner: Player %d\n", winner);

		for (j = 0; j < players; j++)
			printf("player %d: %d\n", (j + 1), scoreFor(j, state));
	}

	printf("\n***************************************\n");
	printf("*********** TESTS COMPLETE ************\n");
	printf("***************************************\n\n");

	return 0;
}
コード例 #9
0
int main(){
  srand(RAND_SEED);
  int i, j;
  int numPlayers = rand() % 3 + 2;

  for (j = 0; j < NUM_TESTS; j++){
    int k[10];
    for (i = 0; i < 10; i++)
      k[i] = 0;
    randKingCards(k);

    struct gameState G;

    initializeGame(numPlayers, k, RAND_SEED, &G);

    char *cardnames[treasure_map+1] = {"curse","estate","duchy","province","copper","silver","gold","adventurer","councilroom","feast","gardens","mine","remodel","smithy","village","baron","greathall","minion","steward","tribute","ambassador","cutpurse","embargo","outpost","salvager","seahag","treasuremap"};

    printf("Starting Dominion game...\nThere are %i players\nKingdom cards: ", numPlayers);
    for (i = 0; i < 10; i++)
      printf("%s ",cardnames[k[i]]);
    printf("\n");

    int error;
    while(!isGameOver(&G)){

      printf("\nPlayer %i's turn:\n",whoseTurn(&G)+1);
      printf("\tCards in hand: %i\n",G.handCount[whoseTurn(&G)]);

      // Play a card
      int handCount = G.handCount[whoseTurn(&G)];
      for (i = 0; i < handCount; i++){
        int cardtoplay = G.hand[whoseTurn(&G)][i];
        if(cardtoplay >= adventurer && cardtoplay <= treasure_map){

          printf("\tPlayer %i tries to play %s from hand position %i\n",whoseTurn(&G)+1,cardnames[cardtoplay],i);
          error = 0;
          switch (cardtoplay){
            case adventurer:
              error = playCard(i,0,0,0,&G);
            case gardens:
              error = playCard(i,0,0,0,&G);
            case smithy:
              error = playCard(i,0,0,0,&G);
            case village:
              error = playCard(i,0,0,0,&G);
            case great_hall:
              error = playCard(i,0,0,0,&G);
            case tribute:
              error = playCard(i,0,0,0,&G);
            case cutpurse:
              error = playCard(i,0,0,0,&G);
            case outpost:
              error = playCard(i,0,0,0,&G);
            case sea_hag:
              error = playCard(i,0,0,0,&G);
            case treasure_map:
              error = playCard(i,0,0,0,&G);
          }

          if (error == -1)
            printf("\tThat card cannot be played\n");
          else {
            printf("\t%s was played\n",cardnames[cardtoplay]);
            break;
          }
        }
      }

      // Buy a victory or money card
      if (rand() % 10 == 1){
        for (i = 6; i > 1; i--){
          error = buyCard(i, &G);
          printf("\tPlayer %i tries to buy %s\n",whoseTurn(&G)+1,cardnames[i]);

          if (error == -1)
            printf("\tThat card cannot be bought\n");
          else{
            printf("\t%s was purchased\n",cardnames[i]);
            break;
          }
        }
      }
      // Buy a kingdom card
      else {
        for (i = 0; i < 5; i++){
          int j = rand() % 10;
          error = buyCard(k[j], &G);
          printf("\tPlayer %i tries to buy %s\n",whoseTurn(&G)+1,cardnames[k[j]]);

          if (error == -1)
            printf("\tThat card cannot be bought\n");
          else{
            printf("\t%s was purchased\n",cardnames[k[j]]);
            break;
          }
        }
      }
      printf("\t----------Game State----------\n");
      printf("\tCoins:\t%i\n",G.coins);
      printf("\tnumBuys:\t%i\n",G.numBuys);
      printf("\thandCount:\t%i\n",G.handCount[whoseTurn(&G)]);
      printf("\tdeckCount:\t%i\n",G.deckCount[whoseTurn(&G)]);
      printf("\thandCount:\t%i\n",G.discardCount[whoseTurn(&G)]);
      printf("\tplayedCardCount:\t%i\n",G.playedCardCount);
      shuffle(whoseTurn(&G),&G);
      endTurn(&G);
    }

    // Print the players' scores
    for(i = 0; i < numPlayers; i++){
      printf("Player %i's score: %i\n",i+1,scoreFor(i, &G));
    }

    // Print the winners
    int winners[MAX_PLAYERS];
    getWinners(winners, &G);
    printf("Winner(s): ");
    for (i = 0; i < numPlayers; i++){
      if (winners[i] == 1)
        printf("%i ", i+1);
    }
  }

  return 0;

}
コード例 #10
0
int main (int argc, char** argv) {
	struct gameState G,g;
	struct gameState *p = &G;
	//srand (time(NULL));
	int k[10];
	int money = 0;
	int i,j=i=0;
	int turn = 0;
	int loop;
	int num = 0;
	int cardsrand =0;
	int Seed = atoi(argv[1]);
	int winner[MAX_PLAYERS];
	int randomBuy = 0;
	int buycard = 1;
	SelectStream(1);
	PutSeed(Seed);
	for(loop = 0; loop <Limit;loop++)
	{
		randCard(k);
		num = 2 + (Random()* (int)(MAX_PLAYERS - 2 + 1));
		printf("Number of players in this game: %d\n",num);
		initializeGame(num, k, Seed, p);
		money = 0;
		i=0;
		turn = 0;
		while (!isGameOver(p)) {
			money = 0;

			printf("	#######START#######\n");
			printHand(turn,p);
			for (i = 0; i < numHandCards(p); i++) {
				if (handCard(i, p) == copper)
					money++;
				else if (handCard(i, p) == silver)
					money += 2;
				else if (handCard(i, p) == gold)
					money += 3;
			}

			for(i = 0; i < numHandCards(p); i++)
			{
				if (handCard(i, p) == smithy)
				{
					printf("	%d: smithy played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}

				else if (handCard(i, p) == adventurer)
				{
					printf("	%d: adventurer played from position %d\n", turn,i);
					playCard(i, -1, -1, -1, p);break;
				}
				else if (handCard(i, p) == council_room)
				{
					printf("	%d: council_room played from position %d\n", turn,i);
					playCard(i, -1, -1, -1, p);break;
				}
				else if (handCard(i, p) == village)
				{
					printf("	%d: village played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == gardens)
				{
					printf("	%d: gardens played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == embargo)
				{
					printf("	%d: embargo played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == minion)
				{
					printf("	%d: minion played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == mine)
				{
					printf("	%d: mine played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == cutpurse)
				{
					printf("	%d: cutpurse played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == sea_hag)
				{
					printf("	%d: sea_hag played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == tribute)
				{
					printf("	%d: tribute played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == baron)
				{
					printf("	%d: baron played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == great_hall)
				{
					printf("	%d: great_hall played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == ambassador)
				{
					printf("	%d: ambassador played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
				else if (handCard(i, p) == ambassador)
				{
					printf("	%d: ambassador played from position %d\n", turn,i);
					playCard(i, 1, 1, 1, p);break;
				}
			}

			if(money >=8)
			{
				printf("	%d: bought province\n",turn);
				buyCard(province, p);
			}
			else if(money >=6)
			{
				randomBuy = Random() * 2;
				if(randomBuy==0)
				{
					printf("	%d: bought gold\n",turn);
					buyCard(gold, p);
				}
				else
				{
					printf("	%d: bought adventurer\n",turn);
					buyCard(adventurer, p);
				}
			}
			else if(money >=5)
			{
				randomBuy = Random() * 5;
				if(randomBuy==0)
				{
					printf("	%d: bought duchy\n",turn);
					buyCard(duchy, p);
				}
				else if(randomBuy==1)
				{
					printf("	%d: bought council_room\n",turn);
					buyCard(council_room, p);
				}
				else if(randomBuy==2)
				{
					printf("	%d: bought mine\n",turn);
					buyCard(mine, p);
				}
				else if(randomBuy==3)
				{
					printf("	%d: bought minion\n",turn);
					buyCard(minion, p);
				}
				else if(randomBuy==4)
				{
					printf("	%d: bought tribute\n",turn);
					buyCard(tribute, p);
				}
				else
				{
					printf("	%d: bought outpost\n",turn);
					buyCard(outpost, p);
				}
			}
			else if(money >=4)
			{
				randomBuy = Random() * 8;
				if(randomBuy==0)
				{
					printf("	%d: bought feast\n",turn);
					buyCard(feast, p);
				}
				else if(randomBuy==1)
				{
					printf("	%d: bought gardens\n",turn);
					buyCard(gardens, p);
				}
				else if(randomBuy==2)
				{
					printf("	%d: bought remodel\n",turn);
					buyCard(remodel, p);
				}
				else if(randomBuy==3)
				{
					printf("	%d: bought smithy\n",turn);
					buyCard(smithy, p);
				}
				else if(randomBuy==4)
				{
					printf("	%d: bought baron\n",turn);
					buyCard(baron, p);
				}
				else if(randomBuy==5)
				{
					printf("	%d: bought cutpurse\n",turn);
					buyCard(cutpurse, p);
				}
				else if(randomBuy==6)
				{
					printf("	%d: bought salvager\n",turn);
					buyCard(salvager, p);
				}
				else if(randomBuy==7)
				{
					printf("	%d: bought sea_hag\n",turn);
					buyCard(sea_hag, p);
				}
				else
				{
					printf("	%d: bought treasure_map\n",turn);
					buyCard(treasure_map, p);
				}
			}
			else if(money>=3)
			{
				randomBuy = Random() * 4;
				if(randomBuy==0)
				{
					printf("	%d: bought silver\n",turn);
					buyCard(silver, p);
				}
				else if(randomBuy==1)
				{
					printf("	%d: bought village\n",turn);
					buyCard(village, p);
				}
				else if(randomBuy==2)
				{
					printf("	%d: bought great_hall\n",turn);
					buyCard(great_hall, p);
				}
				else if(randomBuy==3)
				{
					printf("	%d: bought ambassador\n",turn);
					buyCard(ambassador, p);
				}


			}
			else if(money>=2)
			{
				randomBuy = Random() * 2;
				if(randomBuy==0)
				{
					printf("	%d: bought estate\n",turn);
					buyCard(estate, p);
				}
				else if(randomBuy==1)
				{
					printf("	%d: bought embargo\n",turn);
					buyCard(embargo, p);
				}
			}
			else
			{
				randomBuy = Random() * 2;
				if(randomBuy==0)
				{
					printf("	%d: bought curse\n",turn);
					buyCard(curse, p);
				}
				else if(randomBuy==1)
				{
					printf("	%d: bought copper\n",turn);
					buyCard(copper, p);
				}
			}
			printf("	randomBuy %d: \n",randomBuy);
			printf("	money %d: \n",money);

			printf("	player %d: end turn\n",turn);

			printf ("	Player %d: score:%d\n",turn, scoreFor(turn, p));
			printHand(turn,p);
			endTurn(p);
			printf("	#######END#######\n\n\n\n");
			turn = turn + 1;
			if(turn == (num))
			{
				turn = 0;
			}

		} // end of While

		getWinners(winner, p);
		printf ("	Finished game.\n");
		for(j = 0; j < num; j++)
		{
			printf ("	Player %d: %d, ",j, scoreFor(j, p));

		}
		printf("\n");
		for(j = 0; j < num; j++)
		{
			if(winner[j]==1)
				printf ("winner player %d\n",j);

		}


		memcpy ( &G, &g, sizeof(struct gameState) );
	}

	return 0;
}
コード例 #11
0
int main(int argc, char* argv[]){
	//deal with the commandline
	if(argc != 3)
	{
		printf("\nUSAGE: testdominion [seed][num_games]\n-1 IN EITHER FIELD USES DEFAULTS [TIME][50]\n");
		return 1;
	}
	
	int seed = atoi(argv[1]); 
	int num_games = atoi(argv[2]);
	
	
	if(seed == -1)
	{
		srand(time(0));
	}
	else
	{
		srand(seed);
	}
	
	if(num_games == -1)
	{
		num_games = 50;
	}
	//now move on to basic setup stuff	
	cardnames[0] = "curse";
	cardnames[1] = "estate";
	cardnames[2] = "duchy";
	cardnames[3] = "province";
	cardnames[4] = "copper";
	cardnames[5] = "silver";
	cardnames[6] = "gold";
	cardnames[7] = "adventurer";
	cardnames[8] = "council_room";
	cardnames[9] = "feast";
	cardnames[10] = "gardens";
	cardnames[11] = "mine";
	cardnames[12] = "remodel";
	cardnames[13] = "smithy";
	cardnames[14] = "village";
	cardnames[15] = "baron";
	cardnames[16] = "great_hall";
	cardnames[17] = "minion";
	cardnames[18] = "steward";
	cardnames[19] = "tribute";
	cardnames[20] = "ambassador";
	cardnames[21] = "cutpurse";
	cardnames[22] = "embargo";
	cardnames[23] = "outpost";
	cardnames[24] = "salvager";
	cardnames[25] = "sea_hag";
	cardnames[26] = "treasure_map";
	
	
	struct gameState* pre;
	struct gameState* post;
	
	int p;
	int f;
	int* pass = &p;
	int* fail = &f;
	
	//Try to allocate memory for the gamestates
	pre = malloc(sizeof(struct gameState));
	post = malloc(sizeof(struct gameState));
	if(!pre || !post){printf("\nFAILED TO MALLOC ONE OR BOTH GAMESTATES\n");return 1;}
	//if successful, carry on
	
	//Try to open Logfile
	FILE *fp;
	fp = fopen("gameResults.out", "w");
	if(!fp){printf("\nFAILED TO OPEN OUTPUT FILE gameResults.out FOR WRITE\n"); return 1;}
	//If successful, carry on
	printf("ALL FURTHER TESTING OUTPUT IS REDIRECTED TO gameResults.out\n");
	
	//set the file output version of fassert to point to the file we just setup.
	setfassert(fp);
	
	//Main Testing Loop
	for(int i = 0; i < num_games; i++)
	{
		int err = 0;
		int numPlayers = (rand() % 3) + 2; //2,3,4 players
		int *randomSupplyCards = randSupplyCards();
		
		fprintf(fp, "Trying to start game with %d players\n", numPlayers);
		fprintf(fp, "Using supply cards...\n");
		for(int j = 0; j < 10; j ++)
		{
			fprintf(fp, "%s\n",cardnames[randomSupplyCards[j]]);
		}
		fprintf(fp,"\n");
		err += fassert(initializeGame(numPlayers, randomSupplyCards, rand(), pre) == 0, pass, fail, "initializeGame returned good");
		free(randomSupplyCards);
		
		//now we make two copies of the gamestate for comparison
		memcpy(post,pre,sizeof(struct gameState));
		
		
		while(!isGameOver(pre))
		{
			err = TakeTurn(fp, pre->whoseTurn, pre,post, pass, fail);
			if(err)
			{
				fprintf(fp,"\nGame #%d Failed one or more tests. Ending run and moving to next Game\n",i);
				break;
			}
		}
		
		if(isGameOver(pre))
		{
			int winners[MAX_PLAYERS];
			getWinners(winners, pre);
			for(int j = 0; j < numPlayers; j++)
			{
				if(winners[j])
				{
					fprintf(fp, "\nplayer %d Won!",j);
				}
				else
				{
					fprintf(fp, "\nPlayer %d Lost",j);
				}
			}
		}
		
		fprintf(fp,"\n%d Errors on Game #%d\n",err,i);
		fprintf(fp,"=============================================================================================");
		
		
		
	}

}
コード例 #12
0
ファイル: testdominion.c プロジェクト: cs362sp15/projects
int main (int argc, char** argv) {
	int y = 0;
	int l = 0;
	int moneyError = 0; 
	srand (SEED_VALUE);
	struct gameState G;
	struct gameState *p = &G;
	struct posTrack o;
	struct posTrack *pt = &o;
	int z;
	int breakNum = 0;
	int * k = malloc(sizeof(int)*10);
	for(;y<NUM_GAMES;y++){
		p = newGame();
		k = getCard(); //get random kingdom cards; 
		//ger Random Number of Players
		int numPlayers = (rand() % 2) + 2;
		printf("-------------------------------------\n");
		printf ("Starting game with %d players.\n", numPlayers);
		printf("-------------------------------------\n");
		int countTurn = 0;
		initializeGame(numPlayers, k,5, p);
		int money = 0;
		int i=0;


		while (!isGameOver(p)) {
		if(countTurn >= 100){
			break;
			moneyError++;}
			
		printf("Start player %d's turn\n", p->whoseTurn);
		
		if(countTurn % numPlayers != p->whoseTurn){
			breakNum++;
			break;	
			}			//If a player gets skipped. end the game 
		structInitializer(pt);
		money = 0;
		money = setPos(pt,p); //gets card positions and money amount
		int hold = countPlayable(p); //counts action cards in hand
		int track = 0; //tracker that makes sure we don't check for another action card when all have been played
		int * playableCards = malloc(sizeof(int)*hold);
		int * cardPos = malloc(sizeof(int)*hold);
		makePlayable(p, playableCards);
		storePlayable(p,cardPos);
		if(hold != 0){ // if there is 1 or more action cards
			printf("Cards Played:");
			while(p->numActions != 0 && track < hold){ // while there are still actions left and not all the actions cards have been played
				playCard(playableCards[track], -1,-1,-1,p);
				if(printCard(cardPos[track]) != "Nothing")
					printf(" %s, ", printCard(playableCards[track]));
				track++;
			}
			printf("\n");
		}
		else
			printf("Nothing played.\n");
			money = 0;
			i=0;
			while(i<numHandCards(p)){
			  if (handCard(i, p) == copper){
				playCard(i, -1, -1, -1, p);
				money++;
			  }
			  else if (handCard(i, p) == silver){
				playCard(i, -1, -1, -1, p);
				money += 2;
			  }
			  else if (handCard(i, p) == gold){
				playCard(i, -1, -1, -1, p);
				money += 3;
			  }
			  i++;
			} 
		  printf("Total money: %d\n", money);
		  if (money >= 8) {
			buyCard(province, p);
			printf("%s bought\n",printCard(province));
		  }
		  else if (money >= 6) {
			z = rand() % 3;
			if(z == 0){
				buyCard(gold, p);
				printf("%s bought\n",printCard(gold));
				}
			else if(z == 1)
				buyCost5(p,k); //buys random kingdom card that is 5
		  }
		  else if ((money >= 4)) 
				buyCost4(p,k); //buys random card that costs 4
		  else if ((money >= 3)){
			z = rand() %2;
			if( z==0){
				buyCard(silver,p);
				printf("%s bought\n",printCard(silver));
				}
			else
				buyCost3(p,k); //buys random card that costs 3
		  
		  }
		  else if ((money >= 2)){
			z = rand() %2;
			if ( z == 0){
				buyCard(estate,p);
				printf("%s bought\n",printCard(estate));
				}
			else
				buyCost2(p,k);
			}
		  printf("%d: end turn\n", p->whoseTurn);
		  endTurn(p);
		for(l=0;l<numPlayers;l++)
			printf ("Player %d: %d\n", l, scoreFor(l, p));
		 // End of Game
		 countTurn++;
		}
		int * players;
		players = malloc(sizeof(int)*4);
		getWinners(players,p);
		printf ("Finished game.\n");
		for(l=0;l<numPlayers;l++)
			printf ("Score for Player %d: %d\n", l, scoreFor(l, p));
		for(l=0; l<4;l++){
			if(players[l] == 1)
				printf("Player %d wins!\n", l);
		}
		
	}
	printf("Of %d Games: Break was ran %d times.\n", NUM_GAMES, breakNum);
	printf("Number of money count loss: %d\n", moneyError);
	return 0;
}
コード例 #13
0
/*
 * Main
 */
int main (int argc, char** argv)
{
	int k[10];
	int i, j;
	struct gameState state;
	int hasEstate;
	int turnCount = 0;
	
	// Open the log file, gameResults.out
	gameResults = fopen("gameResults.out", "w");

	if(gameResults == NULL)
	{
		fprintf(stderr, "Error: could not open gameResults.out for writing.\n");
		exit(1);
	}
	
	signal(SIGSEGV, seg_handler);
	signal(SIGBUS, bus_handler);
	signal(SIGFPE, float_handler);

	// Initialize the seed
	if(argc == 2)
	{
		srand(atoi(argv[1]));
	}
	else
	{
		struct timeval tv;
		gettimeofday(&tv,NULL);
		srand((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
	}
	
	// Get 10 random kingdom cards
	i = 0;
	while(i < 10)
	{
		// Choose a card
		k[i] = rand() % (treasure_map - adventurer + 1) + adventurer;
		
		// Make sure the card wasn't chosen already
		int duplicate = FALSE;
		for(j = 0; j < i; j++)
		{
			if(k[j] == k[i])
			{
				duplicate = TRUE;
				break;
			}			
		}
		
		// Card is not a duplicate; we can continue to the next spot
		if(!duplicate)
		{
			i++;
		}
	}
	
	initializeGame(rand() % 3 + 2, k, rand(), &state);	
	
	for(i = 0; i < 10; i++)
	{
		fprintf(gameResults, "Kingdom card %d: %s\n", i + 1, getName(k[i]));
	}

	
	// Main game loop
	while(!isGameOver(&state))
	{


		fprintf(gameResults, "Player %d turn start. Hand contains: ", whoseTurn(&state) + 1);
		for(i = 0; i < numHandCards(&state); i++)
		{
			fprintf(gameResults, "%s", getName(handCard(i, &state)));
			if(i != numHandCards(&state) - 1)
			{
				fprintf(gameResults, ", ");
			}
		}
		
		fprintf(gameResults, "\n");
		
		// Step 1: Play as many action cards as possible, starting from the left
		// and going right		
		i = 0;
		while(i < numHandCards(&state) && state.numActions > 0)
		{
			if(isAction(handCard(i, &state)))
			{
				// Check if we need choices for this card
				switch(handCard(i, &state))
				{
					// These cards don't need any choices, so just play it
					case adventurer:
					case council_room:
					case smithy:
					case village:
					case great_hall:
					case tribute:
					case cutpurse:
					case outpost:
					case sea_hag:
					case treasure_map:
						fprintf(gameResults, "Player %d plays %s\n", whoseTurn(&state) + 1, getName(handCard(i, &state)));
						playCard(i, 0, 0, 0, &state);
						break;
					
					// These things need choices
					case feast:
						playCard(i, getMostExpensiveCard(&state, 5), 0, 0, &state);
						break;
					case mine:
						// Choose the first copper/silver
						for(j = 0; j < numHandCards(&state); j++)
						{
							if(handCard(j, &state) == copper)
							{
								playCard(i, copper, silver, 0, &state);
								break;
							}
							else if(handCard(j, &state) == silver) 
							{
								playCard(i, copper, silver, 0, &state);
								break;
							}
						}
						break;
					
					case remodel:
						// Remodel the first thing valid thing in hand
						if(i == 0 && numHandCards(&state) > 1)
						{
							playCard(i, 1, getMostExpensiveCard(&state, getCost(handCard(1, &state))) + 2, 0, &state);
						}
						else if(i != 0)
						{
							playCard(i, 0, getMostExpensiveCard(&state, getCost(handCard(0, &state))) + 2, 0, &state);
						}
						break;
					case baron:
						// play with an estate if we have one
						hasEstate = FALSE;
						for(j = 0; j < numHandCards(&state); j++)
						{
							if(handCard(j, &state) == estate)
							{
								hasEstate = TRUE;
								break;
							}
						}						
						playCard(i, hasEstate, 0, 0, &state);
						break;
					case minion:
						if(rand() % 2 == 0)
						{							
							playCard(i, 1, 0, 0, &state);
						}
						else
						{
							playCard(i, 0, 1, 0, &state);
						}
						break;
					case steward:
						playCard(i, rand() % 3 + 1, 0, 0, &state);
						break;
					case ambassador:
						// Reveal the first non-ambassador card
						if(i == 0 && numHandCards(&state) > 1)
						{
							playCard(i, 1, rand() % 3,  0, &state);
						}
						else if(i != 0)
						{
							playCard(i, 0, rand() % 3,  0, &state);
						}
						break;
					case embargo:
						playCard(i, rand() % (treasure_map - adventurer) + 1, 0, 0, &state);
						break;
					case salvager:
						// Reveal the first non-ambassador card
						if(i == 0 && numHandCards(&state) > 1)
						{
							playCard(i, 1, 0,  0, &state);
						}
						else if(i != 0)
						{
							playCard(i, 0, 0,  0, &state);
						}
						break;
					default:
						fprintf(gameResults, "%s WAS NOT AN ACTION!\n", getName(handCard(i, &state)));
						break;
				};
			}
			
			i++;
		}
		
		// Step 2: Buy the most expensive things we can
		int done = FALSE;
		while(!done && state.numBuys > 0)
		{
			int mostExpensiveCard = getMostExpensiveCard(&state, state.coins);
			if(mostExpensiveCard != copper && mostExpensiveCard != curse)
			{
				if(buyCard(mostExpensiveCard, &state) != -1)
				{
					fprintf(gameResults, "Player %d buys %s\n", whoseTurn(&state) + 1, getName(mostExpensiveCard));
				}
				else
				{
					fprintf(gameResults, "I thought I could afford that %s...\n", getName(mostExpensiveCard));
				}
			}
			else
			{
				done = TRUE;
			}
		}
		
		endTurn(&state);		

		turnCount++;
		if(turnCount == TURN_LIMIT)
		{
			fprintf(gameResults, "Terminating game after %d turns.", TURN_LIMIT);
			fclose(gameResults);
			exit(0);
		}
	}
	
	fprintf(gameResults, "Game over. Final scores:\n");
	for(i = 0; i < state.numPlayers; i++)
	{
		fprintf(gameResults, "Player %d: %d\n", i + 1, scoreFor(i, &state));
	}
	
	int winners[MAX_PLAYERS];
	getWinners(winners, &state);
	fprintf(gameResults, "The winners are:\n");
	for(i = 0; i < MAX_PLAYERS; i++)
	{
		if(winners[i] == 1)
		{
			fprintf(gameResults, "Player %d!\n", i + 1);
		}
	}

	fclose(gameResults);

	return 0;
}
コード例 #14
0
ファイル: unittest2.c プロジェクト: csDaniel/cs362sp16
int main() {
	int i;
    int seed = 1000;
    int numPlayer = 2;
    int p, r;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
	int x, y; // p1 and p2 scores respectively
	int players[MAX_PLAYERS];

	printf ("TESTING getWinners():\n");	

	//Player 1 higher score
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	G.whoseTurn = 0; // Set to player 1's turn
	// add province to player 1's deck
	G.hand[0][G.handCount[0]] = 4; //4 is code for province
	G.handCount[0]++;
	getWinners(players, &G);
	// Player 1 is winner
	if (players[0] == 1 && players[1] == 0)
		printf("PASSED: Player1 score = %d, expected = 1  Player2 score = %d, expected = 0.\n", players[0], players[1]);
	else
		printf("FAILED: Player1 score = %d, expected = 1  Player2 score = %d, expected = 0.\n", players[0], players[1]);


	//Player 2 higher score
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	G.whoseTurn = 0; // Set to player 1's turn
	// add province to player 2's deck
	G.hand[1][G.handCount[1]] = 4; //4 is code for province
	G.handCount[1]++;
	getWinners(players, &G);
	if (players[0] == 0 && players[1] == 1)
		printf("PASSED: Player1 score = %d, expected = 0  Player2 score = %d, expected = 1.\n", players[0], players[1]);
	else
		printf("FAILED: Player1 score = %d, expected = 0  Player2 score = %d, expected = 1.\n", players[0], players[1]);

	
	//Same score player 1's turn
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	G.whoseTurn = 0; // Set to player 1's turn
	getWinners(players, &G);
	if (players[0] == 0 && players[1] == 1)
		printf("PASSED: Player1 score = %d, expected = 0  Player2 score = %d, expected = 1.\n", players[0], players[1]);
	else
		printf("FAILED: Player1 score = %d, expected = 0  Player2 score = %d, expected = 1.\n", players[0], players[1]);	
	
	//Same score player 2's turn
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	G.whoseTurn = 1; // Set to player 1's turn
	getWinners(players, &G);
	if (players[0] == 1 && players[1] == 1)
		printf("PASSED: Player1 score = %d, expected = 1  Player2 score = %d, expected = 1.\n", players[0], players[1]);
	else
		printf("FAILED: Player1 score = %d, expected = 1  Player2 score = %d, expected = 1.\n", players[0], players[1]);
	
	return 0;
}
コード例 #15
0
int main(int argc, char* argv[]){
	int i,j,l,k, seed;
	
	srand(time(NULL));
	if(argv[1]!=NULL)
		seed = atoi(argv[1]);
	else
		seed = rand()%10 + 1;
	
	for(i=0; i<TEST_NUM; i++) {
		struct gameState g;

		int numPlayer, kingdomcards[10];

		SelectStream(1);
		PutSeed(seed);

		numPlayer = rand()%2+2;
		kingdomcards[0]=rand()%(treasure_map - 7 + 1) + 7;
	
		for(k=1; k<10; k++){
			do{
				kingdomcards[k]=rand()%(treasure_map - 7 + 1) + 7; 
			}
			while(checkKingdomcard(kingdomcards, k));
		}
		
		initializeGame(numPlayer, kingdomcards, seed, &g);
		
		printf("\n******Information of Game********\n");
	
		printf("The seed is %d\n", seed);
		printf("Number of Player: %d\n", g.numPlayers);
		printf("Kingdom Cards: \n");
		for(l=0; l<5; l++)
			printf("%d ", kingdomcards[l]);
		printf("\n");
		for(l=5; l<10; l++)
        printf("%d ", kingdomcards[l]);
		printf("\n");
	
		printf("*********Game Begins**********\n");	
		while(!isGameOver(&g)){
			int x, index;
			int handPos=0, gainCard, result=1;
			int handCount = numHandCards(&g);
			int choice1, choice2, choice3;
	
			if(checkNumAction(&g)>0){
			do{
				handPos = rand()%handCount;
				gainCard = g.hand[g.whoseTurn][handPos];
				if(gainCard >= adventurer && gainCard <= treasure_map) 
					result = 0;
				}while(result==1);
	
			randomChoice(&g, gainCard, &choice1, &choice2, &choice3);		
			playCard(handPos, choice1, choice2, choice3, &g);
			printf("Player %d: played %d\n", g.whoseTurn+1, gainCard);
			}

			else
				printf("Player %d: skip play phase\n", g.whoseTurn+1);
			int size=0, money=checkMoney(&g);
			int remain_supply[treasure_map+1];

	
			for(x=0; x<treasure_map+1; x++){
				if(money >= getCost(x) && supplyCount(x, &g)>0){
					remain_supply[size] = x;
					size++;
				}
			}

			if(size>0){
				index = rand()%size;
				gainCard = remain_supply[index];

				if(buyCard(gainCard, &g)==0)
					printf("Player %d: bought %d\n", g.whoseTurn+1, gainCard);
			}
			printf("Player %d: end turn\n", g.whoseTurn+1);
			endTurn(&g);
		}
		printf("\n********Game Over**********\n");

	for(j=0; j<g.numPlayers; j++)
		printf("Player %d: %d\n", j+1, scoreFor(j, &g));
	int players[g.numPlayers];
	getWinners(players, &g);
	
	for(j=0; j<g.numPlayers; j++)
		if(players[j]==1)
			printf("Player %d is the winner\n", j+1);
	}
	
	return 0;
}
コード例 #16
0
int main(int argc, char** argv) {
	int numPlayers;
	char s[BUFFSIZE];
	char s2[10];
	int win[MAX_PLAYERS];
	int player;
	int seed;
	int result;
	int i;
	
	success = 1;
	if ((rfs = fopen(GAME_RESULTS, "r")) == NULL) {
		printf("could not open %s\n", GAME_RESULTS);
		exit(1);
	}
	if ((wfd = creat(ALT_GAME_RESULTS, PERMISSIONS)) == -1) {
		printf("could not open %s\n", ALT_GAME_RESULTS);
		exit(1);
	}
	
	// setup
	fgets(s, BUFFSIZE, rfs);
	write(wfd, s, strlen(s));
	numPlayers = (int) strtol(s, (char**) NULL, 10);
	fgets(s, BUFFSIZE, rfs);
	write(wfd, s, strlen(s));
	seed = (int) strtol(s, (char**) NULL, 10);
	fgets(s, BUFFSIZE, rfs);
	write(wfd, s, strlen(s));
	k[0] = (int) strtol(strtok(s, ","), (char**) NULL, 10);
	for (i = 1; i < 10; i++) {
		k[i] = (int) strtol(strtok(NULL, ","), (char**) NULL, 10);
	}
	result = initializeGame(numPlayers, k, seed, &gs);
	if (result == -1) {
		printf("initializeGame returned -1\n");
		exit(1);
	}

	// play game
	player = 0;
	while(1) {
		if (gs.whoseTurn != player) {
			success = 0;
			printf("wrong turn, expected %d, found %d\n",
				player, gs.whoseTurn);
			player = gs.whoseTurn;
		}
		fgets(s, BUFFSIZE, rfs);
		if (s[0] == '*')
			break;
		sprintf(s, "> player %d:\n", player);
		write(wfd, s, strlen(s));
		act(s);
		write(wfd, "a\n", 2);
		buy(s);
		write(wfd, "b\n", 2);
		if (isGameOver(&gs))
			break;
		if (gs.outpostPlayed) {
			if (gs.whoseTurn != player) {
				success = 0;
				printf("wrong turn after outpost, expected %d, found %d\n",
					player, gs.whoseTurn);
				player = gs.whoseTurn;
			}
			fgets(s, BUFFSIZE, rfs);
			if (s[0] == '*')
				break;
			sprintf(s, "> outpost:\n");
			write(wfd, s, strlen(s));
			act(s);
			write(wfd, "a\n", 2);
			buy(s);
			write(wfd, "b\n", 2);
			if (isGameOver(&gs))
				break;
		}
		
		endTurn(&gs);
		player = (player + 1) % numPlayers;
	}

	// end game
	if (isGameOver(&gs)) {
		getWinners(win, &gs);
		sprintf(s, "* ");
	} else {
		getWinners(win, &gs);
		sprintf(s, "game is not over ");
	}
	for (i = 0; i < gs.numPlayers; i++) {
		if (win[i]) {
			sprintf(s2, "%d,", i);
			strcat(s, s2);
		}
	}
	s[strlen(s)-1] = '\n';
	write(wfd, s, strlen(s));
	fclose(rfs);
	close(wfd);
	
	if (success)
		printf("success\n");
	
	return 0;
}
コード例 #17
0
ファイル: unittest3.c プロジェクト: famorted/cs362f15
int main(){
	int a, b, c, d, i, j, k;
	struct gameState state;
	int players[MAX_PLAYERS];
	int passCount = 0;
	int failCount = 0;
	int testCount = 0;

	printf("Starting tests for \"getWinners\"...\n\n");

	memset(&state, 0, sizeof(struct gameState));

	c=d=0;
	//all players have same cards, winners should go to players who have more turns
	for(state.numPlayers=2; state.numPlayers<=MAX_PLAYERS; state.numPlayers++){
		if(state.numPlayers == 3){c=1;}
		if(state.numPlayers == 4){d=1;}
		getWinners(players, &state);
		if(testPlayersArray(players, 0, 1, c, d)){
			printf("PASS - getWinners returns correct players array with %d players and even score - array returned: %d %d %d %d\n", state.numPlayers, players[0], players[1], players[2], players[3]);
			passCount++;
		}
		else{
			printf("FAIL - getWinners returns incorrect players array with %d players and even score - array returned: %d %d %d %d\n", state.numPlayers, players[0], players[1], players[2], players[3]);
			failCount++;
		}
		testCount++;
	}

	c=d=0;
	//all have same cards, all players have same amount of turns, all players should be winners
	for(state.numPlayers=2; state.numPlayers<=MAX_PLAYERS; state.numPlayers++){
		//set whoseturn to last player
		state.whoseTurn=state.numPlayers-1;

		if(state.numPlayers == 3){c=1;}
		if(state.numPlayers == 4){d=1;}
		getWinners(players, &state);
		if(testPlayersArray(players, 1, 1, c, d)){
			printf("PASS - getWinners returns correct players array with %d players, even score and even amount of turns - Expected Result: players[ ]: 1 1 %d %d - Actual Result: players[ ]: %d %d %d %d\n", state.numPlayers, c, d, players[0], players[1], players[2], players[3]);
			passCount++;
		}
		else{
			printf("FAIL - getWinners does not return correct players array with %d players, even score and even amount of turns - Expected Result: players[ ]: 1 1 %d %d - Actual Result: players[ ]: %d %d %d %d\n", state.numPlayers, c, d, players[0], players[1], players[2], players[3]);
			failCount++;
		}
		testCount++;
	}

	//test if correct array is returned when each player wins
	for(i=0; i<MAX_PLAYERS; i++){
		//give player winning hand
		for(j=0; j<MAX_HAND; j++){
			state.hand[i][j] = province;
		}
		state.handCount[i] = MAX_HAND;
		state.numPlayers = MAX_PLAYERS;

		getWinners(players, &state);

		a=b=c=d=0;
		if(i == 0){a=1;}
		if(i == 1){b=1;}
		if(i == 2){c=1;}
		if(i == 3){d=1;}

		if(testPlayersArray(players, a, b, c, d)){
			printf("PASS - getWinners returns correct players array when player %d wins - Expected Result: players[ ]: %d %d %d %d - Actual Result: players[ ]: %d %d %d %d\n", i+1, a, b, c, d, players[0], players[1], players[2], players[3]);
			passCount++;
		}
		else{
			printf("FAIL - getWinners does not return correct players array when player %d wins - Expected Result: players[ ]: %d %d %d %d - Actual Result: players[ ]: %d %d %d %d\n", i+1, a, b, c, d, players[0], players[1], players[2], players[3]);
			failCount++;
		}
		testCount++;

		//reset state
		memset(&state, 0, sizeof(struct gameState));
	}

	//test if correct array is returned when 2 players tie
	for(i=0; i<MAX_PLAYERS-1; i++){
		for(j=i+1; j<MAX_PLAYERS; j++){
			//give both players winning hand
			for(k=0; k<MAX_HAND; k++){
				state.hand[i][k] = province;
				state.hand[j][k] = province;
			}
			state.handCount[i] = MAX_HAND;
			state.handCount[j] = MAX_HAND;
			state.numPlayers = MAX_PLAYERS;
			//give players even amount of turns
			state.whoseTurn = MAX_PLAYERS-1;

			getWinners(players, &state);

			a=b=c=d=0;
			if(i == 0 || j == 0){a=1;}
			if(i == 1 || j == 1){b=1;}
			if(i == 2 || j == 2){c=1;}
			if(i == 3 || j == 3){d=1;}

			if(testPlayersArray(players, a, b, c, d)){
				printf("PASS - getWinners returns correct players array when player %d and player %d tie - Expected Result: players[ ]: %d %d %d %d - Actual Result: players[ ]: %d %d %d %d\n", i+1, j+1, a, b, c, d, players[0], players[1], players[2], players[3]);
				passCount++;
			}
			else{
				printf("FAIL - getWinners does not return correct players array when player %d and player %d tie - Expected Result: players[ ]: %d %d %d %d - Actual Result: players[ ]: %d %d %d %d\n", i+1, j+1, a, b, c, d, players[0], players[1], players[2], players[3]);
				failCount++;
			}
			testCount++;

			//reset state
			memset(&state, 0, sizeof(struct gameState));
		}
	}

	//print summary
	printf("\nTesting complete for \"getWinners\"\n\n");
	printf("Summary:\n");
	printf("Tests run: %d\nTests passed: %d\nTests failed: %d\n\n", testCount, passCount, failCount);

	return 0;
}
コード例 #18
0
ファイル: testdominion.c プロジェクト: cs362sp15/projects
int main(int argc, char** argv)
{
  int testcounter = 0;
  int numTests = 20;
  //The random card values are between 0 and 26, so mod by 27, you should be
  //pretty jevo about it. As for initiating games, super easy to randomize
  //number of players playing, however, playing games is still intersting,
  //ask about in class on Tuesday (May 19th, 2015)
  struct gameState G;
  struct gameState *p = &G;
  while(testcounter != numTests){

    srand(time(NULL));
  
  int k[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  int r; //Store our random card values;
  int i, j;
  int numPlayers = rand() % 5; 
  int money = 0;

  //Make sure I have more than 2 players
  if (numPlayers < 2){
    while(1){
      numPlayers = rand() % 5;
      if (numPlayers < 2)
	continue;
      else
	break;
    }
  }
  int players[numPlayers];
  //intialize deck with cards ( have to be greater than or equal to 7)
  for (i = 0; i < 10; i++){
    r = rand() % 27;
    while (r < 7){
      r = rand() % 27;
    }
    k[i] = r;
  }
  
  //Make sure kingdom cards are random
  for (i = 0; i < 10; i++){
    r = rand() % 27;
    while (r < 7){
      r = rand() % 27;
    }
    if (k[i] == 0)
      k[i] = r;
    for (j = i+1; j < 10; j++){
      if (k[i] == k[j]){
	k[i] = r;
	i = -1;
	break;
      }     
    }
  }
  

 
  printf("***STARTING GAME***\n");
  initializeGame(numPlayers, k, atoi(argv[1])+ rand(), p);
  
  while (!isGameOver(p)){
    
    printf("\nCards in player %d's hand:  ", whoseTurn(p));
    for (i = 0; i < numHandCards(p); i++){
      if (i < 4){
	printCard(handCard(i, p));
	printf(", ");
      }
      else{
	printCard(handCard(i, p));
      }
    }
    printf("\n");

    //Not entirely sure what this phase does, 
    for (i = 0; i < numHandCards(p); i++){
      if (handCard(i, p) == copper)
	money++;
      else if (handCard(i, p) == silver)
	money += 2;
      else if (handCard(i, p) == gold)
	money += 3;
    }
    
    //How much money does the player have
    for (i = 0; i < numPlayers; i++){
      money = 0;
      j = 0;     
      while (j < numHandCards(p)){
        r = rand() % 2;

	if (handCard(j, p) == copper){
	  playCard(j, -1, -1, -1, p);
	  money++;
	}
	else if (handCard(j, p) == silver){
	  playCard(j, -1, -1, -1, p);
	  money += 2;
	}
	else if (handCard(j, p) == gold){
	  playCard(j, -1, -1, -1, p);
	  money += 3;
	}
	
	// 1/3 chance of being able to play another card besides money:

	if (r == 0){
	  if (handCard(j,p) == adventurer){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Adventurer***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == feast){
	    playCard(j, copper, -1, -1, p);
	    printf("Player %d: ***Played Feast***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == council_room){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Council Room***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == mine){
	    playCard(j, copper, gardens, -1, p);	    
	    printf("Player %d: ***Played Mine***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == smithy){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Smithy***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == remodel){
	    playCard(j, embargo, duchy, -1, p);	    
	    printf("Player %d: ***Played Remodel***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == village){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Village***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == baron){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Baron***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == great_hall){
	    playCard(j, -1, -1, -1, p);	    
	    printf("Player %d: ***Played Great Hall***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == minion){
	    playCard(j, adventurer, copper, -1, p);
	    printf("Player %d: ***Played Minion***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == steward){
	    playCard(j, 1, -1, -1, p);	    
	    printf("Player %d: ***Played Steward***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == tribute){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Tribute***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == ambassador){
	    playCard(j, 1, 1, -1, p);
	    printf("Player %d: ***Played Ambassador***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == cutpurse){
	    playCard(j, -1, -1, -1, p);
	    printf("Player %d: ***Played Cutpurse***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == embargo){
	    playCard(j, copper, -1, -1, p);
	    printf("Player %d: ***Played Embargo***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == outpost){
	    playCard(j, -1, -1, -1, p);	    
	    printf("Player %d: ***Played Outpost***\n", whoseTurn(p));
	  }	  
	  else if (handCard(j,p) == salvager){
	    playCard(j, copper, -1, -1, p);
	    printf("Player %d: ***Played Salvager***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == sea_hag){
	    playCard(j, -1, -1, -1, p);	    
	    printf("Player %d: ***Played Sea Hag***\n", whoseTurn(p));
	  }
	  else if (handCard(j,p) == treasure_map){
	    playCard(j, -1, -1, -1, p);	    
	    printf("Player %d: ***Played Treasure Map***\n", whoseTurn(p));
	  }
	}
	j++;
      }
      
      int check = 0, count = 0;
      //***Buying Phase***
      if (money >= 8){
	printf("Player %d: Bought Province\n\n", whoseTurn(p));
	buyCard(province, p);
	}
      else if (money >= 6){
	// adventurer
	for (count = 0; count < 10; count++){
	  if (k[count] == adventurer){
	    check = 1;
	  }
	}
	if (check == 1 && (rand() % 3 == 0)){ // 1/3 to buy adventurer
	  printf("Player %d: Bought Adventurer\n\n", whoseTurn(p));
	  buyCard(adventurer, p);
	}
	else {
	  printf("Player %d: Bought Gold\n\n", whoseTurn(p));
	  buyCard(gold, p);
	}
      }
      else if (money >= 5){
	//council_room, mine, minion, tribute, outpost
	for (count = 0; count < 10; count++){
	  if (k[count] == council_room || k[count] == mine ||
	      k[count] == minion || k[count] == tribute ||
	      k[count] == outpost){
	    
	    check += 1;
	  }
	}
	if (rand() % 5 == 0){
	  for (count = 0; count < 10; count++){
	    if(check > 0 && k[count] == council_room && rand()% 5 == 0){
	      printf("Player %d: Bought Council Room\n\n", whoseTurn(p));
	      buyCard(council_room, p);
	      break;
	    }
	    else if(check > 0 && k[count] == mine && rand()% 5 == 0){
	      printf("Player %d: Bought Mine\n\n", whoseTurn(p));
	      buyCard(mine, p);
	      break;
	    }
	    else if(check > 0 && k[count] == minion && rand()% 5 == 0){
	      printf("Player %d: Bought Minion\n\n", whoseTurn(p));
	      buyCard(minion, p);
	      break;
	    }
	    else if(check > 0 && k[count] == tribute && rand()% 5 == 0){
	      printf("Player %d: Bought Tribute\n\n", whoseTurn(p));
	      buyCard(tribute, p);
	      break;
	    }
	    else if(check > 0 && k[count] == outpost && rand()% 5 == 0){
	      printf("Player %d: Bought Outpost\n\n", whoseTurn(p));
	      buyCard(outpost, p);
	      break;
	    }
	  }
	}
	else {		      
	  printf("Player %d: Bought Duchy\n\n", whoseTurn(p));
	  buyCard(duchy, p);
	}
      }
      else if (money >= 4){
	//feast, gardens, remodel, smithy, baron, cutpurse, salvager, sea_hag
	// treasure_map
	for (count = 0; count < 10; count++){
	  if (k[count] == feast || k[count] == gardens || k[count] == remodel
	      || k[count] == smithy || k[count] == baron || 
	      k[count] == cutpurse || k[count] == salvager ||
	      k[count] == sea_hag || k[count] == treasure_map) {	   
	    
	      check += 1;
	  }
	}
	// 1/5 chance of buying a card in deck
	if (rand() % 5 == 0){
	  for (count = 0; count < 10; count++){
	    if(check > 0 && k[count] == feast && rand()% 5 == 0){
	      printf("Player %d: Bought Feast\n\n", whoseTurn(p));
	      buyCard(feast, p);
	      break;
	    }
	    else if(check > 0 && k[count] == gardens && rand()% 5 == 0){
	      printf("Player %d: Bought Gardens\n\n", whoseTurn(p));
	      buyCard(gardens, p);
	      break;
	    }
	    else if(check > 0 && k[count] == smithy && rand()% 5 == 0){
	      printf("Player %d: Bought Smithy\n\n", whoseTurn(p));
	      buyCard(smithy, p);
	      break;
	    }
	    else if(check > 0 && k[count] == baron && rand()% 5 == 0){
	      printf("Player %d: Bought Baron\n\n", whoseTurn(p));
	      buyCard(baron, p);
	      break;
	    }
	    else if(check > 0 && k[count] == cutpurse && rand()% 5 == 0){
	      printf("Player %d: Bought Cutpurse\n\n", whoseTurn(p));
	      buyCard(cutpurse, p);
	      break;
	    }
	    else if(check > 0 && k[count] == salvager && rand()% 5 == 0){
	      printf("Player %d: Bought Salvager\n\n", whoseTurn(p));
	      buyCard(salvager, p);
	      break;
	    }
	    else if(check > 0 && k[count] == sea_hag && rand()% 5 == 0){
	      printf("Player %d: Bought Sea Hag\n\n", whoseTurn(p));
	      buyCard(sea_hag, p);
	      break;
	    }
	    else if(check > 0 && k[count] == treasure_map && rand()% 5 == 0){
	      printf("Player %d: Bought Treasure Map\n\n", whoseTurn(p));
	      buyCard(treasure_map, p);
	      break;
	    }
	  }
	}
      }
      else if (money >= 3){
	//embargo, ambassador, steward, great_hall,village
	for (count = 0; count < 10; count++){
	  if (k[count] == embargo || k[count] == ambassador || 
	      k[count] == steward || k[count] == great_hall || 
	      k[count] == village){
	    
	    check += 1;
	  }	  
	}
	if (rand() % 5 == 0) {
	  for (count = 0; count < 10; count++){
	    if(check > 0 && k[count] == embargo && rand()% 5 == 0){
	      printf("Player %d: Bought Embargo\n\n", whoseTurn(p));
	      buyCard(embargo, p);
	      break;
	    }
	    else if(check > 0 && k[count] == ambassador && rand()% 5 == 0){
	      printf("Player %d: Bought Ambassador\n\n", whoseTurn(p));
	      buyCard(ambassador, p);
	      break;
	    }
	    else if(check > 0 && k[count] == steward && rand()% 5 == 0){
	      printf("Player %d: Bought Steward\n\n", whoseTurn(p));
	      buyCard(steward, p);
	      break;
	    }
	    else if(check > 0 && k[count] == great_hall && rand()% 5 == 0){
	      printf("Player %d: Bought Great Hall\n\n", whoseTurn(p));
	      buyCard(great_hall, p);
	      break;
	    }
	    else if(check > 0 && k[count] == village && rand()% 5 == 0){
	      printf("Player %d: Bought Village\n\n", whoseTurn(p));
	      buyCard(village, p);
	      break;
	    }
	  }
	}
	else {
	  printf("Player %d: Bought Silver\n\n", whoseTurn(p));
	  buyCard(silver, p);
	}
      }
      endTurn(p);
      
      for (i = 0; i < numPlayers; i++){
	printf("Player %d: %d\n", i, scoreFor(i, p));  
      }
    }
  }
    //***End of Game***
    printf("***GAME FINISHED***\n");
    
    for (i = 0; i < numPlayers; i++){
      printf("Player %d: %d\n", i, scoreFor(i, p));
    }
    getWinners(players, p);
    testcounter++;
  }    
    return 0;
}
コード例 #19
0
ファイル: GameState.cpp プロジェクト: nissafors/LawyerRace
void GameState::RenderResult() {
    std::list<Player*> winners = getWinners();
}
コード例 #20
0
ファイル: player.c プロジェクト: cs362sp16/cs362sp16_tolvstaa
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};

	gameState g;
	gameState * game = &g;

	memset(game,0,sizeof(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 = game->whoseTurn;
		
		//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 = game->whoseTurn;
				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 = game->whoseTurn;
				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 =	game->whoseTurn;
			printf("Player %d's turn\n", playerNum);
		} 
    	}
	
    	return EXIT_SUCCESS;

}
コード例 #21
0
int main(int argc, char* argv[]) {
	int test;
	int seed = atoi(argv[1]);
	
	//Create gameStates with random properties
	for (test = 0; test < NUMTESTS; test++) {
		
		int i, acard, init, command, numPlayers, playerNum, outcome, currentPlayer, turnNum;
		int kCards[10];
		struct gameState * g;
		
		char line[MAX_STRING_LENGTH];
		char cardName[MAX_STRING_LENGTH];
		int players[MAX_PLAYERS];
		
		int gameOver = FALSE;
		int arg0 = UNUSED;
		int arg1 = UNUSED;
		int arg2 = UNUSED;
		int arg3 = UNUSED;
		
		printf("------===================******* Game %d of %d ********==================--------\n", test + 1, NUMTESTS);
		
		//Pick random cards
		for (i = 0; i < 10; i++) {
			acard = floor(Random() * 20 + 7);
			if (ValInArr(acard, kCards, 10) || acard == feast)
				i--;
			else
				kCards[i] = acard;
		}
		kCards[6] = mine; //force choose mine
		printf("Cards chosen for game %d:\n", test + 1);
		for (i = 0; i < 10; i++) {
			strcpy(cardName,"");
			cardNumToName(kCards[i], cardName);
			printf("  (%d) - %s\n", kCards[i], cardName);
		}
		
		//Create game state
		g = (struct gameState*)malloc(sizeof(struct gameState));
		numPlayers = floor(Random() * (2 + 1) + 2);
		init = initializeGame(numPlayers, kCards, seed, g);
		assert(init > -1);
		
		
		currentPlayer = whoseTurn(g);
		PrintGameInfo(g, turnNum);
		PrintTurnInfo(g, turnNum);
		while(1) {
			
			outcome = FAILURE;
			strcpy(line,"");
			strcpy(cardName,"");
			
			gameOver = isGameOver(g); 		
			if(gameOver == TRUE)
				break; //Exit out of the game
			
			//Choose action for turn
			command = GetCommand();
			
			if (command == cmdPlay) {
				arg0 = floor(Random() * g->handCount[currentPlayer]);
				arg1 = floor(Random() * (treasure_map + 1));
				arg2 = floor(Random() * (treasure_map + 1));
				arg3 = floor(Random() * (1 + 1));
				strcpy(cardName,"");
			    cardNumToName(g->hand[currentPlayer][arg0], cardName);
				outcome = playCard(arg0, arg1, arg2, arg3, g);
				if (outcome == SUCCESS){
					printf("-Player %d plays %s...args: %d, %d, %d, %d\n\n", currentPlayer, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				} else {
					printf("-Player %d cannot play card %s...args: %d, %d, %d, %d\n\n", currentPlayer, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				}
			} else
			if (command == cmdBuy) {
				arg0 = floor(Random() * (treasure_map + 1));
				outcome = buyCard(arg0, g);
				strcpy(cardName,"");
			    cardNumToName(arg0, cardName);
				if(outcome == SUCCESS){
					printf("-Player %d buys card %d: %s...args: %d, %d, %d, %d\n\n", currentPlayer, arg0, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				} else {
					printf("-Player %d cannot buy card %d: %s...args: %d, %d, %d, %d\n\n", currentPlayer, arg0, cardName, arg0, arg1, arg2, arg3);
					PrintTurnInfo(g, turnNum);
				}
			} else
			if (command == cmdEnd) {
				printf("-Player %d ends turn %d...args: %d, %d, %d, %d\n\n", whoseTurn(g), turnNum, arg0, arg1, arg2, arg3);
				turnNum++;
				endTurn(g);
				currentPlayer = whoseTurn(g);
				PrintGameInfo(g, turnNum);
			}
		}
		
		printScores(g);
		getWinners(players, g);
		printf("After %d turns, the winner(s) are:\n", turnNum);
		for(playerNum = 0; playerNum < g->numPlayers; playerNum++)
			if(players[playerNum] == WINNER) printf("Player %d\n", playerNum);
		PrintGameInfo(g, turnNum);
		free(g);
		printf("Game %d of %d complete\n", test + 1, NUMTESTS);
	}
	
	printf("All games complete\n\n");
	//printf("%d of %d test were successful.\n", NUMTESTS * (NUMPLAYERS-1) - numFailed, NUMTESTS * (NUMPLAYERS-1));
	
	return 0;
}
コード例 #22
0
int main() {
	int w;
	int x;
	int b[10];
	int *k;
	int m;
	int n;
	int temp;
	int temp2;
	int pos;
	int i, j, d[4], dis[4], p[4], status, status2, players, player, handCount, expectedHandSize, deckCount, priorDeckCount, oldDiscardCount, seed, draw1, draw2;
	struct gameState *state;
	int money;
	int numViolated = 0;
	int winners[MAX_PLAYERS];

	srand(1317);
	printf("Running Random Dominion Test\n");
		
	for (i = 0; i < MAX_TESTS; i++) {
		fprintf(stderr, "STARTING A GAME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");	
		state = newGame();
		// Random number of players between 2 and 4
		players = rand() % 3 + 2;
		seed = rand();

		for (j = 0; j < 10; j++)
			b[j] = -1;
		// Random kingdom cards
		for (j = 0; j < 10; j++)
		{
			m = rand() % 20 + 7;
			temp = 0;
			for (n = 0; n < j; n++)
			{
				if (b[n] == m)
				{
					temp = 1;
				}
			}
			if (temp)
			{
				j -= 1;
				continue;
			}
			else
			{
				b[j] = m;
			}
		}
		k = kingdomCards(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9]);
		status = initializeGame(players, k, seed, state);	//initialize Gamestate 
	
		if (myassert(status != -1, "Failed to initialize game", &numViolated))
			fprintf(stderr, "Initialize failed\n");

		// Run the game/test
		while (!isGameOver(state))
		{
			fprintf(stderr, "And so a turn begins\n");
			player = whoseTurn(state);
			money = 0;
			pos = -1;
			for (j = 0; j < numHandCards(state); j++)
			{
				fprintf(stderr, "In money loop\n");
				if (handCard(j, state) == copper)
					money++;
				else if (handCard(j, state) == silver)
					money = money + 2;
				else if (handCard(j, state) == gold)
					money = money + 3;
				else
					pos = j;
			}	
			temp = handCard(pos, state);		
			if (DEBUG)
				fprintf(stderr, "Playing card of type %d\n", temp);

			w = 0;
			x = 0;
			if (temp == 9)
			{
				status = 0;
				while(!status)
				{
					if (supplyCount(w, state) > 0 && getCost(w) <= state->coins)
						status = 1;
					else
						w++;
				}
			}
			else if (temp == 15)
			{
				if (rand() % 2)
					w = 1;
			}

			else if (temp == 17)
			{
				status = rand() % 3;
				if (status == 0)
					w = 1;
				else if (status == 1)
					x = 1;
			}

			else if (temp == 24)
			{
				status = 0;
				while(!status)
				{
					if (w != pos)
						status = 1;
					else
						w++;
				}
			}



			status = playCard(pos, w, x, 0, state);
			//myassert(status != -1, "playCard failed", &numViolated);
			if (status != -1 && DEBUG)
				fprintf(stderr, "%d: Card of type %d played\n", player, temp);
			temp2 = 0;
			while (!temp2)
			{
				fprintf(stderr, "In loop prior to buy card\n");
				temp = rand() % 27;
				if (supplyCount(temp, state) >= 0 )
					temp2 = 1;

			}
			if (DEBUG)
				fprintf(stderr, "Buying card\n");
			status = buyCard(temp, state);
			//myassert(status != -1, "buyCard failed", &numViolated);
			if (DEBUG && status != -1)
				fprintf(stderr, "%d: bought card of type %d\n", player, temp);	
			if (DEBUG)
				fprintf(stderr, "Ending turn\n");
			status = endTurn(state);
			myassert( status != -1, "endTurn failed", &numViolated);


	
	
			/*	// Error info
				if (status2 && DEBUG)
				{
					printf("  Current player info:\n");
					printf("    old DeckCount: %d\n", priorDeckCount);
					printf("    new DeckCount: %d\n", state->deckCount[player]);
					printf("    old DiscardCount: %d\n", oldDiscardCount);
					printf("    new discardCount: %d\n", state->discardCount[player]);
					printf("    old handCount: %d\n", handCount);	
					printf("    new handCount: %d\n", state->handCount[player]);
				}
				
	*/
		
		  
		} 
		status = getWinners(winners, state);
		myassert(status != -1, "getWinners failed to run", &numViolated);

		for(j = 0; j < players; j++)
		{
			status = scoreFor(j, state);
			myassert(status != -1, "scoreFor failed", &numViolated);
		}

		free(k);		
 
	}
	printf("%d Tests Completed\n", MAX_TESTS);
	printf("Failed %d times\n", numViolated);
	return 0;
}
コード例 #23
0
int main(int argc, char *argv[]){

	//make sure correct number of arguments are provided
	if(argc > 2){
		printf("Too many arguments, only one expected.\n");
		return -1;
	}
	if(argc < 2){
		printf("Not enough arguments, need a random seed.\n");
		return -1;
	}

	//setup game
	int testSeed = argv[1];
	struct gameState game;
	srand(testSeed);
	//get 10 random kingdom cards
	int allKingCards[20] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall, minion, steward, tribute, ambassador, cutpurse, embargo, outpost, salvager, sea_hag, treasure_map};
	kingShuffle(allKingCards, 20);

	int k[10];

	for(int i = 0; i < 10; i++){
		k[i] = allKingCards[i];
	}

	kingdomCards(k[0], k[1], k[2], k[3], k[4], k[5], k[6], k[7], k[8], k[9]);

	//get random number of players between 2 and MAX_PLAYERS
	int players = (rand() % MAX_PLAYERS);
	if(players < 2){
			players = players + 2;
	}
	srand(testSeed);
	int gameSeed = rand();

	//initialize game
	initializeGame(players, k, gameSeed, &game);

	//loop until game is finished
	while(!isGameOver(&game)){
		int currentPlayer = whoseTurn(&game);
		printf("player: %d begins their turn\n",currentPlayer );
		//action phase
		int playResult = 0;
		while(game.numActions > 0){

			int numActionCards = 0;
			//make sure there is an action card in hand
			for(int i=0; i <numHandCards(&game); i++){
				if(game.hand[currentPlayer][i] >= adventurer && game.hand[currentPlayer][i] <= treasure_map){
					numActionCards++;
				}
			}

			//if no action cards in hand break loop
			if(numActionCards == 0){
				printf("no action cards in hand\n");
				break;
			}
			int handPos = rand() % game.handCount[currentPlayer];
			int cardPlay = game.hand[currentPlayer][handPos];
			//check if selected card is kingdom card
			if(cardPlay >= adventurer && cardPlay <= treasure_map){
				playResult = playCard(handPos,0,0,0,&game);
			}
			//check result of playCard
			if(playResult != 0){
				printf("Could not play card\n");
			}
			else
				printf("Card was successfully played\n");
			//1 in 10 chance to not use an action
			if(rand() % 10 == 0)
				break;
		}

		//buy phase
		while(game.numBuys > 0){
			//count the value of treasure in player's hand
			updateCoins(currentPlayer, &game, 0);
			int treasureAvailable = HandTreasure(&game, currentPlayer);

			//for buying cards, I seperated possible cards to buy into tiers based on cost
			//this is less about game strategy and more about trying to get good coverage of cards
			if(treasureAvailable >= 8){
				customBuy(province, &game);
				treasureAvailable = HandTreasure(&game, currentPlayer);
			}

			if(treasureAvailable >= 6){
				if(rand() % 1)
					customBuy(gold, &game);
				
				else
					customBuy(adventurer, &game);

				treasureAvailable = HandTreasure(&game, currentPlayer);
			}

			if(treasureAvailable >= 5){
				switch(rand() % 6){
					case 4:
						customBuy(minion, &game);
						break;
					case 3:
						customBuy(mine, &game);
						break;
					case 2:
						customBuy(council_room, &game);
						break;
					case 1:
						customBuy(duchy, &game);
						break;
					case 0:
						customBuy(outpost, &game);
						break;
					default:
						customBuy(tribute, &game);
						break;

				}
				treasureAvailable = HandTreasure(&game, currentPlayer);
			}

			if(treasureAvailable >= 4){
				switch(rand() % 9){
					case 7:
						customBuy(feast, &game);
						break;
					case 6:
						customBuy(gardens, &game);
						break;
					case 5:
						customBuy(salvager, &game);
						break;
					case 4:
						customBuy(treasure_map, &game);
						break;
					case 3:
						customBuy(sea_hag, &game);
						break;
					case 2:
						customBuy(cutpurse, &game);
					case 1:
						customBuy(remodel, &game);
						break;
					case 0:
						customBuy(smithy, &game);
						break;
					default:
						customBuy(baron, &game);
						break;
				}
				treasureAvailable = HandTreasure(&game, currentPlayer);
			}

			if(treasureAvailable >= 3){
				switch(rand() % 5){
					case 3:
						customBuy(steward, &game);
						break;
					case 2:
						customBuy(ambassador, &game);
						break;
					case 1:
						customBuy(great_hall, &game);
						break;
					case 0:
						customBuy(silver, &game);
						break;
					default:
						customBuy(village, &game);
						break;
				}
				treasureAvailable = HandTreasure(&game, currentPlayer);
			}

			if(treasureAvailable >= 2){
				if(rand() % 1)
					customBuy(estate, &game);
				
				else
					customBuy(embargo, &game);

				treasureAvailable = HandTreasure(&game, currentPlayer);
			}
			else
				customBuy(copper, &game);
				break;

		}//end buy loop

		//cleanup phase
		printf("player: %d ends their turn\n",currentPlayer );
		endTurn(&game);

	}//game loop

	printf("game is finished.\n");
	printf("Game results:\n");
	for(int i = 0; i < players; i++){
		printf("Player: %d Score: %d\n",i,scoreFor(i, &game));
	}

	int winners[5];
	getWinners(winners, &game);




	return 1;
}