int playDominion(struct gameState *state, int k[10]) {
	int i;
	int cardPos, curCard, returnCard, toBuyCard, curPlayer, money;
	int numPlayers = state->numPlayers;

	while (!isGameOver(state)) {
    money = 0;
    curPlayer = whoseTurn(state);
    cardPos = -1;

		printf("\nPlayer %d's turn.\n", curPlayer);

		//Check money 
		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) > 6) {
				cardPos = i;
			}
		}

		if (cardPos != -1) {
			curCard = handCard(cardPos, state);
			printf("Player %d plays %d\n", curPlayer, curCard);
			returnCard = playCard(cardPos, 1, 1, 1, state);
			if (returnCard == -1) {
				printf("Playing card %d failed\n", curCard);
			} else {
				printf ("Playing card %d succeded\n", curCard);
			}
		}

		toBuyCard = k[rand() % 10];
		printf("Player %d has %d coins\n", curPlayer, money);

		if (money >= 8) {
			printf("Player %d is trying to buy a province\n", curPlayer);
			if(buyCard(province, state) == -1) {
				printf("Failed to buy a province");
			}
		} else if (money >= getCost(toBuyCard)) {
			printf("Player %d is trying to buy card %s\n", curPlayer, getName(toBuyCard));
			if (buyCard(toBuyCard, state) == -1) {
				printf("Buying %s failed\n", getName(toBuyCard));
			}
		} else if (money >= 6) {
			printf("Player %d is trying to buy gold\n", curPlayer);
			if (buyCard(gold, state) == -1) {
				printf("Failed to buy gold\n");
			}
		} else if (money >= 3) {
			printf("Player %d is trying to buy silver\n", curPlayer);
			if (buyCard(silver, state) == -1) {
				printf("Failed to buy silver\n");
			}
		}
  printf("Player %d has %d cards in hand\n", curPlayer, numHandCards(state));
  printf("Player %d ends turn\n", curPlayer);
  endTurn(state);
	}

  for (i = 0; i < numPlayers; i++) {
    printf("Player %d's score: %d\n", i, scoreFor(i, state));
  }

	return 0;

}
Example #2
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;
}
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
{
	int i;
	int j;
	int k;
	int index;
	int currentPlayer = whoseTurn(state);
	int nextPlayer = currentPlayer + 1;

	int tributeRevealedCards[2] = {-1, -1};
	int drawntreasure=0;
	if (nextPlayer > (state->numPlayers - 1)){
		nextPlayer = 0;
	}
  
	
	//uses switch to select card and perform actions
	switch( card ) 
		{
		case adventurer:
			return adventurerCard (drawntreasure, state, currentPlayer);
		case council_room:
			return councilRoomCard (currentPlayer, state, handPos);
		case feast:
			return feastCard (currentPlayer, state, choice1);			
		case gardens:
			return -1;			
		case mine:
			return mineCard (currentPlayer, state, choice1, choice2, handPos);
		case remodel:
			return remodelCard (currentPlayer, state, choice1, choice2, handPos);
		case smithy:
			//+3 Cards
			for (i = 0; i < 3; i++)
				{
					drawCard(currentPlayer, state);
				}
			
			//discard card from hand
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case village:
			//+1 Card
			drawCard(currentPlayer, state);
			
			//+2 Actions
			state->numActions = state->numActions + 2;
			
			//discard played card from hand
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case baron:
			state->numBuys++;//Increase buys by 1!
			if (choice1 > 0){//Boolean true or going to discard an estate
				int p = 0;//Iterator for hand!
				int card_not_discarded = 1;//Flag for discard set!
				while(card_not_discarded){
					if (state->hand[currentPlayer][p] == estate){//Found an estate card!
						state->coins += 4;//Add 4 coins to the amount of coins
						state->discard[currentPlayer][state->discardCount[currentPlayer]] = state->hand[currentPlayer][p];
						state->discardCount[currentPlayer]++;
						for (;p < state->handCount[currentPlayer]; p++){
							state->hand[currentPlayer][p] = state->hand[currentPlayer][p+1];
						}
						state->hand[currentPlayer][state->handCount[currentPlayer]] = -1;
						state->handCount[currentPlayer]--;
						card_not_discarded = 0;//Exit the loop
					}
					else if (p > state->handCount[currentPlayer]){
						if(DEBUG) {
							printf("No estate cards in your hand, invalid choice\n");
							printf("Must gain an estate if there are any\n");
						}
						if (supplyCount(estate, state) > 0){
							gainCard(estate, state, 0, currentPlayer);
							state->supplyCount[estate]--;//Decrement estates
							if (supplyCount(estate, state) == 0){
								isGameOver(state);
							}
						}
						card_not_discarded = 0;//Exit the loop
					}
			    
					else{
						p++;//Next card
					}
				}
			}
			    
			else{
				if (supplyCount(estate, state) > 0){
					gainCard(estate, state, 0, currentPlayer);//Gain an estate
					state->supplyCount[estate]--;//Decrement Estates
					if (supplyCount(estate, state) == 0){
						isGameOver(state);
					}
				}
			}
	    
      
			return 0;
		
		case great_hall:
			//+1 Card
			drawCard(currentPlayer, state);
			
			//+1 Actions
			state->numActions++;
			
			//discard card from hand
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case minion:
			//+1 action
			state->numActions++;
			
			//discard card from hand
			discardCard(handPos, currentPlayer, state, 0);
			
			if (choice1)		//+2 coins
				{
					state->coins = state->coins + 2;
				}
			
			else if (choice2)		//discard hand, redraw 4, other players with 5+ cards discard hand and draw 4
				{
					//discard hand
					while(numHandCards(state) > 0)
						{
							discardCard(handPos, currentPlayer, state, 0);
						}
				
					//draw 4
					for (i = 0; i < 4; i++)
						{
							drawCard(currentPlayer, state);
						}
				
					//other players discard hand and redraw if hand size > 4
					for (i = 0; i < state->numPlayers; i++)
						{
							if (i != currentPlayer)
								{
									if ( state->handCount[i] > 4 )
										{
											//discard hand
											while( state->handCount[i] > 0 )
												{
													discardCard(handPos, i, state, 0);
												}
							
											//draw 4
											for (j = 0; j < 4; j++)
												{
													drawCard(i, state);
												}
										}
								}
						}
				
				}
			return 0;
		
		case steward:
			if (choice1 == 1)
				{
					//+2 cards
					drawCard(currentPlayer, state);
					drawCard(currentPlayer, state);
				}
			else if (choice1 == 2)
				{
					//+2 coins
					state->coins = state->coins + 2;
				}
			else
				{
					//trash 2 cards in hand
					discardCard(choice2, currentPlayer, state, 1);
					discardCard(choice3, currentPlayer, state, 1);
				}
			
			//discard card from hand
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case tribute:
			if ((state->discardCount[nextPlayer] + state->deckCount[nextPlayer]) <= 1){
				if (state->deckCount[nextPlayer] > 0){
					tributeRevealedCards[0] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
					state->deckCount[nextPlayer]--;
				}
				else if (state->discardCount[nextPlayer] > 0){
					tributeRevealedCards[0] = state->discard[nextPlayer][state->discardCount[nextPlayer]-1];
					state->discardCount[nextPlayer]--;
				}
				else{
					//No Card to Reveal
					if (DEBUG){
						printf("No cards to reveal\n");
					}
				}
			}
	    
			else{
				if (state->deckCount[nextPlayer] == 0){
					for (i = 0; i < state->discardCount[nextPlayer]; i++){
						state->deck[nextPlayer][i] = state->discard[nextPlayer][i];//Move to deck
						state->deckCount[nextPlayer]++;
						state->discard[nextPlayer][i] = -1;
						state->discardCount[nextPlayer]--;
					}
			    
					shuffle(nextPlayer,state);//Shuffle the deck
				} 
				tributeRevealedCards[0] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
				state->deck[nextPlayer][state->deckCount[nextPlayer]--] = -1;
				state->deckCount[nextPlayer]--;
				tributeRevealedCards[1] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
				state->deck[nextPlayer][state->deckCount[nextPlayer]--] = -1;
				state->deckCount[nextPlayer]--;
			}    
		       
			if (tributeRevealedCards[0] == tributeRevealedCards[1]){//If we have a duplicate card, just drop one 
				state->playedCards[state->playedCardCount] = tributeRevealedCards[1];
				state->playedCardCount++;
				tributeRevealedCards[1] = -1;
			}

			for (i = 0; i <= 2; i ++){
				if (tributeRevealedCards[i] == copper || tributeRevealedCards[i] == silver || tributeRevealedCards[i] == gold){//Treasure cards
					state->coins += 2;
				}
		    
				else if (tributeRevealedCards[i] == estate || tributeRevealedCards[i] == duchy || tributeRevealedCards[i] == province || tributeRevealedCards[i] == gardens || tributeRevealedCards[i] == great_hall){//Victory Card Found
					drawCard(currentPlayer, state);
					drawCard(currentPlayer, state);
				}
				else{//Action Card
					state->numActions = state->numActions + 2;
				}
			}
	    
			return 0;
		
		case ambassador:
			j = 0;		//used to check if player has enough cards to discard

			if (choice2 > 2 || choice2 < 0)
				{
					return -1;				
				}

			if (choice1 == handPos)
				{
					return -1;
				}

			for (i = 0; i < state->handCount[currentPlayer]; i++)
				{
					if (i != handPos && i == state->hand[currentPlayer][choice1] && i != choice1)
						{
							j++;
						}
				}
			if (j < choice2)
				{
					return -1;				
				}

			if (DEBUG) 
				printf("Player %d reveals card number: %d\n", currentPlayer, state->hand[currentPlayer][choice1]);

			//increase supply count for choosen card by amount being discarded
			state->supplyCount[state->hand[currentPlayer][choice1]] += choice2;
			
			//each other player gains a copy of revealed card
			for (i = 0; i < state->numPlayers; i++)
				{
					if (i != currentPlayer)
						{
							gainCard(state->hand[currentPlayer][choice1], state, 0, i);
						}
				}

			//discard played card from hand
			discardCard(handPos, currentPlayer, state, 0);			

			//trash copies of cards returned to supply
			for (j = 0; j < choice2; j++)
				{
					for (i = 0; i < state->handCount[currentPlayer]; i++)
						{
							if (state->hand[currentPlayer][i] == state->hand[currentPlayer][choice1])
								{
									discardCard(i, currentPlayer, state, 1);
									break;
								}
						}
				}			

			return 0;
		
		case cutpurse:

			updateCoins(currentPlayer, state, 2);
			for (i = 0; i < state->numPlayers; i++)
				{
					if (i != currentPlayer)
						{
							for (j = 0; j < state->handCount[i]; j++)
								{
									if (state->hand[i][j] == copper)
										{
											discardCard(j, i, state, 0);
											break;
										}
									if (j == state->handCount[i])
										{
											for (k = 0; k < state->handCount[i]; k++)
												{
													if (DEBUG)
														printf("Player %d reveals card number %d\n", i, state->hand[i][k]);
												}	
											break;
										}		
								}
					
						}
				
				}				

			//discard played card from hand
			discardCard(handPos, currentPlayer, state, 0);			

			return 0;

		
		case embargo: 
			//+2 Coins
			state->coins = state->coins + 2;
			
			//see if selected pile is in play
			if ( state->supplyCount[choice1] == -1 )
				{
					return -1;
				}
			
			//add embargo token to selected supply pile
			state->embargoTokens[choice1]++;
			
			//trash card
			discardCard(handPos, currentPlayer, state, 1);		
			return 0;
		
		case outpost:
			//set outpost flag
			state->outpostPlayed++;
			
			//discard card
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case salvager:
			//+1 buy
			state->numBuys++;
			
			if (choice1)
				{
					//gain coins equal to trashed card
					state->coins = state->coins + getCost( handCard(choice1, state) );
					//trash card
					discardCard(choice1, currentPlayer, state, 1);	
				}
			
			//discard card
			discardCard(handPos, currentPlayer, state, 0);
			return 0;
		
		case sea_hag:
			for (i = 0; i < state->numPlayers; i++){
				if (i != currentPlayer){
					state->discard[i][state->discardCount[i]] = state->deck[i][state->deckCount[i]-1];
					state->discardCount[i]++;
					state->deck[i][state->deckCount[i]-1] = curse;//Top card now a curse
				}
			}
			return 0;
		
		case treasure_map:
			//search hand for another treasure_map
			index = -1;
			for (i = 0; i < state->handCount[currentPlayer]; i++)
				{
					if (state->hand[currentPlayer][i] == treasure_map && i != handPos)
						{
							index = i;
							break;
						}
				}
			if (index > -1)
				{
					//trash both treasure cards
					discardCard(handPos, currentPlayer, state, 1);
					discardCard(index, currentPlayer, state, 1);

					//gain 4 Gold cards
					for (i = 0; i < 4; i++)
						{
							gainCard(gold, state, 1, currentPlayer);
						}
				
					//return success
					return 1;
				}
			
			//no second treasure_map found in hand
			return -1;
		}
	
	return -1;
}
Example #4
0
int main() {
  int i;
  struct gameState* state;
  int returnValue;

  // clear and initialize game state manually
  state = newGame();
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    state->supplyCount[i] = 8;
  }

  // test that game does NOT end when stacks are full
  returnValue = isGameOver(state);
  assert(returnValue == 0);
  printf("\nGame does not end when stacks are full - PASSED\n");
  #if (VERBOSE == 1)
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    printf("Supply for card %d: %d\n", i, state->supplyCount[i]);
  }
  #endif

  // test that game does NOT end when one supply pile is empty
  state->supplyCount[curse] = 0;
  returnValue = isGameOver(state);
  assert(returnValue == 0);
  printf("\nGame does not end when 1 supply pile empty - PASSED\n");
  #if (VERBOSE == 1)
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    printf("Supply for card %d: %d\n", i, state->supplyCount[i]);
  }
  #endif

  // test game does NOT end when two supply piles are empty
  state->supplyCount[estate] = 0;
  returnValue = isGameOver(state);
  assert(returnValue == 0);
  printf("\nGame does not end when 2 supply piles empty - PASSED\n");
  #if (VERBOSE == 1)
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    printf("Supply for card %d: %d\n", i, state->supplyCount[i]);
  }
  #endif

  // test that game ends when three supply piles are empty
  state->supplyCount[duchy] = 0;
  returnValue = isGameOver(state);
  assert(returnValue == 1);
  printf("\nGame ends when 3 supply piles empty - PASSED\n");
  #if (VERBOSE == 1)
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    printf("Supply for card %d: %d\n", i, state->supplyCount[i]);
  }
  #endif

  // test that game ends when no provinces left
  // reset all ingdom card supply piles
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    state->supplyCount[i] = 8;
  }
  state->supplyCount[province] = 0;
  returnValue = isGameOver(state);
  assert(returnValue == 1);
  printf("\nGame ends when provinces are empty - PASSED\n");
  #if (VERBOSE == 1)
  for(i = 0; i < sizeof(state->supplyCount)/sizeof(int); i++) {
    printf("Supply for card %d: %d\n", i, state->supplyCount[i]);
  }
  #endif

  printf("\nAll tests passed.\n\n");
  return 0;
}
int main (int argc, char** argv)
{
    
    printf("argument2 is %d\n", argv[1]);
    if(argc == 2)
    {
        srand(atoi(argv[1]));
    }
    else
    {
        //srand(time(NULL));
        srand(600);
    }
    struct gameState G; 
    struct gameState *Game = &G; 
    struct gameState state;
    int randomKingdomCard;
    int randomNumPlayers; 
    int KingdomCards[10];
    int KingdomCardsPos = 0;
    int RandomSeed;
    //Initializes random seed
    //srand (time(NULL));
    
    //int KingdomCards [10] = {adventurer, council_room, mine, minion, tribute, outpost, salvager, sea_hag, remodel, smithy};
    
    
    //Generates random number of players (2-4)
    randomNumPlayers = rand() % 3 + 2;
    printf("The random number of players is: %d\n", randomNumPlayers);

    //Gets the 10 random kingdom cards that must be unique 
    while(KingdomCardsPos != 10)
    {
        //Note: kingdom card values are between 7 and 26
        randomKingdomCard = rand() % 20 + 7;
        //Checks if the kingdom is not already in the array 
        if(isvalueinarray(randomKingdomCard, KingdomCards, KingdomCardsPos) == false)
        {
            KingdomCards[KingdomCardsPos] = randomKingdomCard;
            //printf("The kingdom card number is: %d\n", KingdomCards[KingdomCardsPos]);
            KingdomCardsPos = KingdomCardsPos + 1; 
        }
    }
    
    
    
    
    //Displays kingdom cards chosen 
     ChosenKingdomCards(KingdomCards);
    //Get random seed
    RandomSeed = rand();
    //Initialize game 
    initializeGame(randomNumPlayers, KingdomCards, RandomSeed, Game);
   
//Keep playing Dominion until the game is over 
    int TotTurns = 1;
    while(!isGameOver(Game))
    {
        printf("This is turn %d\n", TotTurns);
        //Displays the current player's hand
        printf("Start hand\n");
        PlayersHand(Game);
       
        //Gets the number of actions 
        int numberActions = 0;
        numberActions = Game->numActions;
        printf("The number of actions is: %d\n", numberActions);
        printf("The number of action cards is: %d\n", NumActionCards(Game));
        
//Action Phase     
        //Activate as many action cards as possible 
        while(Game->numActions > 0)
        {
                //PlayersHand(Game);
                //ChosenKingdomCards(KingdomCards);
                //printf("\n");
                //Exit loop no more action cards left 
                if(NumActionCards(Game) == 0)
                {
                        break;
                }
                //There are action cards in the player's hand 
                else
                {
                        
                        int randomChoice = GetMostExpensiveAction(Game, KingdomCards);
                        printf("The random choice is: %d\n", randomChoice);

                        int cardValue = handCard(randomChoice, Game);
                        printf("the cards name is %s\n", GetCardName(cardValue));

                        //Checks to see if the random card is a kingdom card that was chosen and if it's not gardens 
                        if(isvalueinarray(cardValue, KingdomCards, 10) == true && cardValue != 10)
                        {
                                
                                //printf("the name is %s\n", GetCardName(cardValue));
                               //These cards don't need choices adventurer, council_room, smithy, village, great_hall, tribute, cutpurse, outpost, sea_hag, and treasure_map
                               if(cardValue == 7 || cardValue == 8 || cardValue == 13 || cardValue == 14 || cardValue == 16 || cardValue == 19 || cardValue == 21 || cardValue == 23 || cardValue == 25 || cardValue == 26)
                               {
                                       //printf("number of actions is %d\n", NumActionCards(Game));
                                       int returnValue = playCard(randomChoice, 0 , 0, 0, Game);
                                       //printf("played %s return value is %d\n", GetCardName(cardValue),returnValue);
                                       
                                       //Success was able to play the card 
                                       if(returnValue == 0)
                                       {
                                                printf("Activated %s\n", GetCardName(cardValue)); 
                                                PlayersHand(Game);
  
                                       }
                                       
                                       //Exit the loop if you failed to play treasure_map 
                                       if(returnValue == -1 )
                                       {
                                               
                                               break; 
                                       }
                                       
                               }
                               //These cards require choices exit the loop 
                               else
                               {
                                       break;
                               }
                        }
                        
                }
        }
   
        //Displays had after action has been made 
        printf("Hand after action phase\n");
        PlayersHand(Game);
//Buy phase
        printf("The total money is: %d\n", moneyInHand(Game));
        printf("The total buys is: %d\n", Game->numBuys);
        //Buy a random card while you have money and buys available 
        while(Game->numBuys > 0 && moneyInHand(Game) > 0)
        {
                 int randomChoice = rand () % 17 + 0;
                 int CardValue = 0;
                 //Don't buy anything if you have less than 3 of money
                 if(moneyInHand(Game) < 3)
                 {
                         break; 
                 }
                 //The random card is one of the 10 kingdom cards chosen 
                 if(randomChoice < 10)
                 {
                         CardValue = KingdomCards[randomChoice];
                 }
                 //The random card is curse
                 if(randomChoice == 10)
                 {
                         CardValue = 0;
                 }
                 //The random card is estate 
                 if(randomChoice == 11)
                 {
                         CardValue = 1;
                 }
                 //The random card is duchy 
                 if(randomChoice == 12)
                 {
                        CardValue = 2; 
                 }
                 //The random card is province 
                 if(randomChoice == 13)
                 {
                     CardValue = 3; 
                 }
                 //The random card is copper 
                 if(randomChoice == 14)
                 {
                     CardValue = 4;
                 }
                 //The random card is silver 
                 if(randomChoice == 15)
                 {
                         CardValue = 5;
                 }
                 //The random card is gold
                 if(randomChoice == 16)
                 {
                         CardValue = 6;
                 }
                 
                 //Attempt to buy a card that is greater or equal
                 /*if(moneyInHand(Game) >= MostExpensiveKingdomCard(KingdomCards)-1 && getCost(CardValue) <= moneyInHand(Game))
                 {
                         int returnValue = 0;
                         returnValue = buyCard(CardValue, Game);
                         //You were able to buy the card 
                         if(returnValue == 0)
                         {
                                printf("Player bought %s and it cost %d\n", GetCardName(CardValue), getCost(CardValue));
                                break;
                         }
                         //You could not buy the card 
                         else
                         {
                                printf("The player could not buy %s\n", GetCardName(CardValue));
                         }
                 }*/
                 /*if(moneyInHand(Game) < MostExpensiveKingdomCard(KingdomCards)-1)
                 {
                         int returnValue = 0;
                         returnValue = buyCard(CardValue, Game);
                         //You were able to buy the card 
                         if(returnValue == 0)
                         {
                                printf("Player bought %s and it cost %d\n", GetCardName(CardValue), getCost(CardValue));
                                break;
                         }
                         //You could not buy the card 
                         else
                         {
                                printf("The player could not buy %s\n", GetCardName(CardValue));
                         }
                 }
                 
                 
                 */
                 //if(CardValue != 0 && CardValue != 1 && CardValue)
                 if(CardValue != 0 && CardValue != 1)
                 {
                         int returnValue = 0;
                         returnValue = buyCard(CardValue, Game);
                         //You were able to buy the card 
                         if(returnValue == 0)
                         {
                                printf("Player bought %s and it cost %d\n", GetCardName(CardValue), getCost(CardValue));
                                break;
                         }
                         //You could not buy the card 
                         else
                         {
                                printf("The player could not buy %s\n", GetCardName(CardValue));
                         }
                 }
        }
//End turn  
        printf("end turn\n\n");
        endTurn(Game);
        TotTurns = TotTurns + 1;
    }
//Displays the games results 
    printf ("The game is over!!!!!\n");
    ChosenKingdomCards(KingdomCards);
    printf("The total turns made in the game was %d\n", TotTurns-1);
    printf("This is the supply left of each kingdom card:\n");
    //Displays supply left of each card used in the game 
    for(int i = 0; i < 10; i++)
    {
            int TotCardsLeft =0;
            TotCardsLeft = supplyCount(KingdomCards[i], Game);
            printf("%s: %d\n", GetCardName(KingdomCards[i]),TotCardsLeft);
    }
    printf("%s: %d\n", GetCardName(0),supplyCount(0, Game));
    printf("%s: %d\n", GetCardName(1),supplyCount(1, Game));
    printf("%s: %d\n", GetCardName(2),supplyCount(2, Game));
    printf("%s: %d\n", GetCardName(3),supplyCount(3, Game));
    printf("%s: %d\n", GetCardName(4),supplyCount(4, Game));
    printf("%s: %d\n", GetCardName(5),supplyCount(5, Game));
    printf("%s: %d\n", GetCardName(6),supplyCount(6, Game));
    
    
    int HighestScore = -1;
    int PlayerWhoWon = 0; 
    for(int i = 0; i < randomNumPlayers; i++)
    {
        printf("Player %d score is: %d\n", i, scoreFor(i, Game));
        if(scoreFor(i, Game) > HighestScore)
        {
                HighestScore = scoreFor(i, Game);
                PlayerWhoWon = i;
        }
    }
    printf("The player who won is %d with a score of %d\n", PlayerWhoWon, HighestScore);
    printf("Updated version503\n");
    return 0;
}
Example #6
0
int main(int argc, char** argv) {
  struct gameState G;
  struct gameState *s = &G;
  int numPlayers;
  int seed;
  int k[10];
  int i;
  int j;
  int temp, check;
  int who;
  int r;

  if(argc != 2) {
    printf("Must give a random seed");
    return 0;
  } else {
    seed = atoi(argv[1]);
    printf("Seed is %d\n", seed);
  }

  srand(seed);
  numPlayers = rand() % 3 + 2;
  
  for(i = 0; i < 10;) {
    temp = rand() % (treasure_map - adventurer) + adventurer;
    check = 0;
    for(j = 0; j < i; j++) {
      if(temp == k[j]) {
	check = 1;
      }
    }
    if(check == 0) {
      printf("%d is in kingdom cards.\n", temp);
      k[i] = temp;
      i++;
    }
  }

  printf("Starting game with %d players.\n", numPlayers);

  initializeGame(numPlayers, k, seed, s);

  while(!isGameOver(s)) {
    who = s->whoseTurn;
    printf("Player %d turn:\n", who+1);

    //Action phase
    for(i = 0; i < numHandCards(s); i++) {
	if(handCard(i, s) <= gold) {
          continue;
	}
        if(s->numActions == 0) {
	  break;
	}
        if(handCard(i, s) == feast) {
          temp = 0;
	  if(k[temp] == adventurer) {
	    temp = 1;
	  }
	  playCard(i, temp, -1, -1, s);
	  printf("Played feast gaining %d\n", k[temp]);
	}
	else if(handCard(i, s) == mine) {
	  temp = -1;
          for(j = 0; j < numHandCards(s); j++) {
            if(handCard(j, s) == silver) { 
	      playCard(i, j, gold, -1, s);
	      printf("Played mine and upgraded to gold\n");
	      temp = 1;
	      break;
	    }
	  }
	  if(temp == 1) { break; }
          for(j = 0; j < numHandCards(s); j++) {
            if(handCard(j, s) == copper) {
	      playCard(i, j, silver, -1, s);
	      printf("Played mine and upgraded to silver\n");
	      break;
	    }
	  }
	}
	else if(handCard(i, s) == baron) {
          temp = 0;
	  for(j = 0; j < numHandCards(s); j++) {
	    if(handCard(j, s) == estate) {
              temp = 1;
	    } 
	  }
	  playCard(i, temp, -1, -1, s);
	  printf("Played baron\n");
	}
	else if(handCard(i, s) == minion) {
	  if(rand() % 2 == 0) {
	    playCard(i, 1, -1, -1, s);
	    printf("Played minion with choice 1\n");
	  } else {
	    playCard(i, 2, -1, -1, s);
	    printf("played minion with choice 2\n");
	  }
	}
	else if(handCard(i, s) == steward) {
          if(rand() % 2 == 0) {
	    playCard(i, 1, -1, -1, s);
	  } else {
	    playCard(i, 2, -1, -1, s);
	  }
	  printf("Played steward\n");
	}
	else if(handCard(i, s) == ambassador) {
	}
	else if(handCard(i, s) == embargo) {
          temp = rand() % 10;
	  playCard(i, temp, -1, -1, s);
	  printf("Played embargo on %d\n", temp);
	}
	else if(handCard(i, s) == salvager) {
	  temp = 0;
	  if(i == 0) {
	    temp = 1;
	  }
	  playCard(i, temp, -1, -1, s);
	  printf("Played salvager on %d\n", temp);
	}
	else {
	  printf("Played %d\n", i); 
	  playCard(i, -1, -1, -1, s);
	}
    }

    //Buy phase
    printf("money: %d\n", s->coins);
    if(s->coins >= 8) {
      buyCard(province, s);
      printf("Bought province\n");
    } else if(s->coins >= 6 && rand() % 2 == 0) {
      buyCard(gold, s);
      printf("Bought gold\n");
    } else if(s->coins >= 3 && rand() % 2 == 0) {
      buyCard(silver, s);
      printf("bought silver\n");
    }
    
    //give it the old college try
    
    for(i = 0; i < 15; i++) {
      temp = rand() % 10;
      r = buyCard(k[temp], s);
      if(r == 0) {
        printf("bought %d\n", k[temp]);
      }
    }
    //temp = rand() % 10;
    //buyCard(k[temp], s);
    //printf("Attempted to buy %d\n", temp);

    endTurn(s);
  }

  printf("FINAL SCORE:\n");
  for(i = 0; i < numPlayers; i++) {
    printf("Player %d: %d\n", i + 1, scoreFor(i, s));
  }
  return 0;
}
Example #7
0
int main (){

    int r;
    int failures = 0;   //Number of checks failed
    int comparison;

    int k[10] = {adventurer, council_room, feast, gardens, mine,
	       remodel, smithy, village, baron, great_hall};

    //Initialize game
    struct gameState post;
    r = initializeGame(2, k, 1, &post);

    printf("Testing function isGameOver\n");

    //Test case 1: Provinces empty
    //isGameOver should return 1; game state should not be changed
    printf("Test Case 1: Provinces are empty, no kingdom cards are empty\n");
    post.supplyCount[province] = 0;
    struct gameState pre;
    memcpy(&pre, &post, sizeof(struct gameState));
    r = isGameOver(&post);
    printf("Testing isGameOver return value. Expecting 1. ");
    if(r == 1){
        printf("Returned 1 ... PASS\n");
    }
    else{
        printf("Returned %d ... FAIL\n", r);
        failures++;
    }
    comparison = memcmp(&pre, &post, sizeof(struct gameState));
    printf("Testing that game state is unchanged ... ");
    if(comparison == 0){
        printf("PASS\n");
    }
    else{
        printf("FAIL\n");
        failures++;
    }

    //Test case 2: Provinces not empty. One kingdom card empty
    printf("\nTest Case 2: Provinces not empty. One kingdom card empty\n");
    r = initializeGame(2, k, 1, &post);
    post.supplyCount[council_room] = 0;
    post.supplyCount[province] = 10; //Make sure province not empty
    memcpy(&pre, &post, sizeof(struct gameState));
    r = isGameOver(&post);

    //Test return value
    printf("Testing isGameOver return value. Expecting 0. ");
    if(r == 0){
        printf("Returned 0 ... PASS\n");
    }
    else{
        printf("Returned %d ... FAIL\n", r);
        failures++;
    }

    //Test game state
    comparison = memcmp(&pre, &post, sizeof(struct gameState));
    printf("Testing that game state is unchanged ... ");
    if(comparison == 0){
        printf("PASS\n");
    }
    else{
        printf("FAIL\n");
        failures++;
    }

    //Test case 3: Provinces not empty. Two kingdom cards empty
    printf("\nTest Case 3: Provinces not empty. Two kingdom cards empty\n");
    r = initializeGame(2, k, 1, &post);
    post.supplyCount[council_room] = 0;
    post.supplyCount[baron] = 0;
    post.supplyCount[province] = 10; //Make sure province not empty
    memcpy(&pre, &post, sizeof(struct gameState));
    r = isGameOver(&post);

    //Test return value
    printf("Testing isGameOver return value. Expecting 0. ");
    if(r == 0){
        printf("Returned 0 ... PASS\n");
    }
    else{
        printf("Returned %d ... FAIL\n", r);
        failures++;
    }

    //Test game state
    comparison = memcmp(&pre, &post, sizeof(struct gameState));
    printf("Testing that game state is unchanged ... ");
    if(comparison == 0){
        printf("PASS\n");
    }
    else{
        printf("FAIL\n");
        failures++;
    }

    //Test case 4: Provinces not empty. Three kingdom cards empty
    printf("\nTest Case 4: Provinces not empty. Three kingdom cards empty\n");
    r = initializeGame(2, k, 1, &post);
    post.supplyCount[council_room] = 0;
    post.supplyCount[baron] = 0;
    post.supplyCount[remodel] = 0;
    post.supplyCount[province] = 10; //Make sure province not empty
    memcpy(&pre, &post, sizeof(struct gameState));
    r = isGameOver(&post);

    //Test return value
    printf("Testing isGameOver return value. Expecting 1. ");
    if(r == 1){
        printf("Returned 1 ... PASS\n");
    }
    else{
        printf("Returned %d ... FAIL\n", r);
        failures++;
    }

    //Test game state
    comparison = memcmp(&pre, &post, sizeof(struct gameState));
    printf("Testing that game state is unchanged ... ");
    if(comparison == 0){
        printf("PASS\n");
    }
    else{
        printf("FAIL\n");
        failures++;
    }

   if(failures == 0){
        printf("All tests passed\n");
   }
   else{
        printf("%d failures occurred\n", failures);
   }

   //Test case 5: Provinces not empty. Three kingdom cards almost empty
    printf("\nTest Case 5: Provinces not empty. Three kingdom cards have 1 remaining\n");
    r = initializeGame(2, k, 1, &post);
    post.supplyCount[council_room] = 1;
    post.supplyCount[baron] = 1;
    post.supplyCount[remodel] = 1;
    post.supplyCount[province] = 10; //Make sure province not empty
    memcpy(&pre, &post, sizeof(struct gameState));
    r = isGameOver(&post);

    //Test return value
    printf("Testing isGameOver return value. Expecting 0. ");
    if(r == 0){
        printf("Returned 0 ... PASS\n");
    }
    else{
        printf("Returned %d ... FAIL\n", r);
        failures++;
    }

    //Test game state
    comparison = memcmp(&pre, &post, sizeof(struct gameState));
    printf("Testing that game state is unchanged ... ");
    if(comparison == 0){
        printf("PASS\n");
    }
    else{
        printf("FAIL\n");
        failures++;
    }

   if(failures == 0){
        printf("All tests passed\n");
   }
   else{
        printf("%d failures occurred\n", failures);
   }

    return 0;
}
Example #8
0
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;
}
Example #9
0
int playHand(struct gameState *game){

  if(isGameOver(game)) {
    return 0;
  }

  if(hasNegative(game)) {
    printf("Negative card count\n\r");
    return -1;
  }


  int i, count, retVal,player = whoseTurn(game);

  printf("Current Player: %d\n\r",player+1);

  game->phase=0;

  count = 0;
  while(game->handCount[player]<hand_size){
    if(hasNegative(game)) {
      printf("Error encountered: Negative card count\n\r");
      return -1;
    }
    retVal = drawCard(player,game);
    if (retVal != 0) {
      if (game->deckCount[player]+game->discardCount[player] == 0)
	break;
      else {
	printf("Error encountered in previous draw card!\n\r");
	return -1;
      }
    }
    if(count++ > MAX_HAND) {
      printf("Error encountered in previous draw card!\n\r");
      printf("Too many draw cards!\n\r");
      return -1;
    }
  }
  printf("Cards in hand: ");
  count = 0;
  for(i=0;i<game->handCount[player]; i++) {
    printCard(game->hand[player][i]);
    printf("\t");
  }

  printf("\n\r");

  count = 0;
  while(game->numActions > 0) {
    if(hasNegative(game)) {
      printf("Error encountered: Negative card count\n\r");
      return -1;
    }
    retVal = randAction(game);
    if (retVal != 0){
      printf("Error encountered in previous action!\n\r");
      return -1;
    }
    if(count++ > MAX_HAND) {
      printf("Error encountered in previous action!\n\r");
      printf("Too many actions tried!\n\r");
      return -1;
    }
  }
  count = 0;
  while(game->numBuys > 0){
    if(hasNegative(game)) {
      printf("Error encountered: Negative card count\n\r");
      return -1;
    }
    retVal = randBuy(game);
    if (retVal != 0){
      printf("Error encountered in previous buy!\n\r");
      printSupply(game);
      return -1;
    }
    if(count++ > MAX_HAND) {
      game->numBuys=0;
      /*
      printf("Error encountered in previous buy!\n\r");
      printf("Too many buys!\n\r");
      printSupply(game);
      return -1;
      */
    }
  }
  
  if(!isGameOver(game)){
    printf("End of turn\n\r");
    printf("-----------------------------------------------------------\n\r");
    endTurn(game);
  }
  if(hasNegative(game)) {
    printf("Error encountered: Negative card count\n\r");
    return -1;
  }
  
  return 0;
}
Example #10
0
void Game::startGame(bool isStartingGame)
{
	gfx.pal.clear();
	
	if(isStartingGame)
	{
		if(settings.regenerateLevel
		|| settings.randomLevel != oldRandomLevel
		|| settings.levelFile != oldLevelFile)
		{
			generateLevel();
		}
	
		
		initGame();
		
		
		
		for(std::size_t i = 0; i < viewports.size(); ++i)
		{
			viewports[i]->x = 0;
			viewports[i]->y = 0;
		}

		selectWeapons();
		
		sfx.play(22, 22);

		cycles = 0;
		
		for(int w = 0; w < 40; ++w)
		{
			weapons[w].computedLoadingTime = (settings.loadingTime * weapons[w].loadingTime) / 100;
			if(weapons[w].computedLoadingTime == 0)
				weapons[w].computedLoadingTime = 1;
		}
	}
	
	int fadeAmount = isStartingGame ? 180 : 0;
	bool shutDown = false;
	
	do
	{
		++cycles;
		
		if(!H[HBonusDisable]
		&& settings.maxBonuses > 0
		&& rand(C[BonusDropChance]) == 0)
		{
			createBonus();
		}
			
		for(std::size_t i = 0; i < worms.size(); ++i)
		{
			worms[i]->process();
		}
		
		for(std::size_t i = 0; i < worms.size(); ++i)
		{
			worms[i]->ninjarope.process(*worms[i]);
		}
		
		switch(game.settings.gameMode)
		{
		case Settings::GMGameOfTag:
		{
			bool someInvisible = false;
			for(std::size_t i = 0; i < worms.size(); ++i)
			{
				if(!worms[i]->visible)
				{
					someInvisible = true;
					break;
				}
			}
			
			if(!someInvisible
			&& lastKilled
			&& (cycles % 70) == 0
			&& lastKilled->timer < settings.timeToLose)
			{
				++lastKilled->timer;
			}
		}
		break;
		}
		
		processViewports();
		drawViewports();
				
		for(BonusList::iterator i = bonuses.begin(); i != bonuses.end(); ++i)
		{
			i->process();
		}
		
		if((cycles & 1) == 0)
		{
			for(std::size_t i = 0; i < viewports.size(); ++i)
			{
				Viewport& v = *viewports[i];
				
				bool down = false;
				
				if(v.worm->killedTimer > 16)
					down = true;
					
				if(down)
				{
					if(v.bannerY < 2)
						++v.bannerY;
				}
				else
				{
					if(v.bannerY > -8)
						--v.bannerY;
				}
			}
		}
		
		for(SObjectList::iterator i = game.sobjects.begin(); i != game.sobjects.end(); ++i)
		{
			i->process();
		}
		
		// TODO: Check processing order of bonuses, wobjects etc.
		
		for(WObjectList::iterator i = wobjects.begin(); i != wobjects.end(); ++i)
		{
			i->process();
		}
		
		for(NObjectList::iterator i = nobjects.begin(); i != nobjects.end(); ++i)
		{
			i->process();
		}
		
		for(BObjectList::iterator i = bobjects.begin(); i != bobjects.end(); ++i)
		{
			i->process();
		}
		
		if((cycles & 3) == 0)
		{
			for(int w = 0; w < 4; ++w)
			{
				gfx.origpal.rotate(gfx.colourAnim[w].from, gfx.colourAnim[w].to);
			}
		}
		
		gfx.pal = gfx.origpal;
		
		if(fadeAmount <= 32)
			gfx.pal.fade(fadeAmount);

		if(gfx.screenFlash > 0)
		{
			gfx.pal.lightUp(gfx.screenFlash);
		}
		
		gfx.flip();
		gfx.process();
		
		if(gfx.screenFlash > 0)
			--gfx.screenFlash;
		
		if(isGameOver())
		{
			gfx.firstMenuItem = 1;
			shutDown = true;
		}
		
		for(std::size_t i = 0; i < viewports.size(); ++i)
		{
			if(viewports[i]->shake > 0)
				viewports[i]->shake -= 4000; // TODO: Read 4000 from exe?
		}

		if(gfx.testSDLKeyOnce(SDLK_ESCAPE)
		&& !shutDown)
		{
			gfx.firstMenuItem = 0;
			fadeAmount = 31;
			shutDown = true;
		}

		if(shutDown)
		{
			fadeAmount -= 1;
		}
		else if(!isStartingGame)
		{
			if(fadeAmount < 33)
			{
				fadeAmount += 1;
				if(fadeAmount >= 33)
					fadeAmount = 180;
			}
		}
	}
	while(fadeAmount > 0);
	
	gfx.clearKeys();
}
int main() {
	
	int i, test1=0, scoreTrack=10;
	int currentPlayer, money;
	struct gameState G;
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, sea_hag, great_hall, tribute, outpost};
	int r = initializeGame(2, k, 2, &G);
	assert (r ==0);
	
	///// ----------------------- game -----------------------
	while (!isGameOver(&G)) 
	{
		money=0;
		for (i = 0; i < numHandCards(&G); i++) 
		{
			/*
			if (handCard(i, &G) == outpost)
				{
				test1=numHandCards(&G);
				playCard(i, -1, -1, -1, &G);
				//assert(test1==numHandCards(&G)+2);
				}*/
			if (handCard(i, &G) == copper)
				{
				playCard(i, -1, -1, -1, &G);
				money++;
				}
			else if (handCard(i, &G) == silver)
				{
				playCard(i, -1, -1, -1, &G);
				money += 2;
				}
			else if (handCard(i, &G) == gold)
				{
				playCard(i, -1, -1, -1, &G);
				money += 3;
				}
		}	
		
		if ((supplyCount(gold, &G)==0) && (supplyCount(silver, &G)==0))
		{
			if (money >= 8) 
			{
				buyCard(province, &G);
				scoreTrack=scoreTrack+6;
			}
		}
		else if (money >= 6) 
		{
			buyCard(gold, &G);
		}
		/*
		else if (money >= 5)
		{ 
			buyCard(outpost, &G);
		}*/
		else if (money >= 3) 
		{ 
			buyCard(silver, &G);
		}
		endTurn(&G);
	}

	
}
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,"=============================================================================================");
		
		
		
	}

}
Example #13
0
int main(){
    int runStatus;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

	//Initialize game
	runStatus = initializeGame(2, k, 1, &G);
	//Check if initialize successful
	assert(runStatus == 0);
	//Check if game is over at start
	assert(isGameOver(&G) == 0);

	//Check province end game
	//Set province pile to empty
	//Check if game is over
	G.supplyCount[province] = 0;
	assert(isGameOver(&G) == 1);

	//Reset game state
	runStatus = initializeGame(2, k, 1, &G);
	//Check if reset initialize successful
	assert(runStatus == 0);

	//One supply pile empty with empty province pile
	G.supplyCount[silver] = 0;
	G.supplyCount[province] = 0;
	assert(isGameOver(&G) == 1);

	//Reset game state
	runStatus = initializeGame(2, k, 1, &G);
	//Check if reset initialize successful
	assert(runStatus == 0);

	//Two supply pile empty with empty province pile
	G.supplyCount[gold] = 0;
	G.supplyCount[adventurer] = 0;
	G.supplyCount[province] = 0;
	assert(isGameOver(&G) == 1);

	//Reset game state
	runStatus = initializeGame(2, k, 1, &G);
	//Check if reset initialize successful
	assert(runStatus == 0);

	//Three supply pile empty with empty province pile
	//Check if 4 empty supply pile will still trigger end game
	G.supplyCount[council_room] = 0;
	G.supplyCount[feast] = 0;
	G.supplyCount[gardens] = 0;
	G.supplyCount[province] = 0;
	assert(isGameOver(&G) == 1);

	//Check if 3 empty supply file triggers end game
	//Reset game state
	runStatus = initializeGame(2, k, 1, &G);
	//Check if reset initialize successful
	assert(runStatus == 0);

	//Set curse supply pile to empty
	//Check if game is over
	G.supplyCount[curse] = 0;
	assert(isGameOver(&G) == 0);

	//Set estate supply pile to empty
	//Check if game is over
	G.supplyCount[estate] = 0;
	assert(isGameOver(&G) == 0);

	//Set duchy supply pile to empty
	//Check if game is over
	G.supplyCount[duchy] = 0;
	assert(isGameOver(&G) == 1);
	
	printf("All tests passed for isGameOver function, unittest4.c\r\n\r\n");
	printf("--------------------\r\n\r\n");
	return 0;
}
int main(int argc, char**argv){

   struct gameState Game;
   struct gameState *Ga = &Game;
   int players, ranCard, i;
   int money, cardPos, curPlayer, useCard, returnCard, buyCa;
   int k[10];
   int pool[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};

   srand(atoi(argv[1]));
   players = rand()% 3 + 2;
   ranCard = rand() % 20;

   for(i=0;i<10;i++){
      k[i] = pool[(ranCard+i)%20];
   }

   printf("Starting Game\n");
   initializeGame(players,k,atoi(argv[1]),Ga);
   printf("Number of player %d\n",players);

   while(!isGameOver(Ga)){
      money = 0;
      cardPos = -1;
      curPlayer = whoseTurn(Ga);
      for(i=0;i<numHandCards(Ga);i++){
	 if (handCard(i,Ga) == copper)
	    money++;
	 else if(handCard(i,Ga) == silver)
	    money += 2;
	 else if(handCard(i,Ga) == gold)
	    money += 3;
	 else if(handCard(i,Ga) > 6)
	    cardPos = i;
      }
      if(cardPos != -1){
     	useCard = handCard(cardPos,Ga);
      	printf("Player %d: is playing card %d\n",curPlayer,useCard);
      	returnCard = playCard(cardPos,1,1,1,Ga);
      	if(returnCard== -1)
		 printf("Playing card Failed\n",useCard);
      	else
		 printf("Playing card %d Succeded\n",useCard);
      }
      buyCa = k[rand()%10];
      printf("Player %d has %d coins\n",curPlayer,money);

      if (money >= 8){
	 printf("Player %d is trying to buy province\n",curPlayer);
	 if(buyCard(province,Ga)==-1){
	    printf("Buying Province Failed\n");}
      }
      else if (money >= getCost(buyCa)){
	 printf("Player %d is trying to by card %d \n"layer,buyCa);
	 if(buyCard(buyCa,Ga)==-1){
	    printf("Buying %d Failed\n", buyCa);}
      }
      else if (money >= 6){
	 printf("Player %d is trying to buy gold\n",curPlayer);
	 if(buyCard(gold,Ga)==-1){
	    printf("Buying Gold Faile\n");}
      }
      else if (money >= 3){
	 printf("Player %d is trying to buy silver\n",curPlayer);
	 if (buyCard(silver,Ga)==-1){
	    printf("Buying Silver Failed\n");}
      }
      printf("Player %d has %d Cards in hand\n",curPlayer,numHandCards(Ga));
      printf("Player %d ends turn\n",curPlayer);
      endTurn(Ga);


   };
   
   for(i=0;i<players;i++){
      printf("Player %d Score: %d\n",i,scoreFor(i,Ga));
   }


   return 0;
}
int main3(int argc, char* argv[]) {
	char *add  = "add";
	char *buyC = "buy";
	char *endT = "end";
	char *exit = "exit";
	char *help = "help";
	char *init = "init";
	char *numH = "num";
	char *play = "play";
	char *resign  = "resi";
	char *show = "show";
	char *stat = "stat";
	char *supply = "supp";
	char *whos = "whos";
		
	char command[MAX_STRING_LENGTH];
	char line[MAX_STRING_LENGTH];
	char cardName[MAX_STRING_LENGTH];

	//Array to hold bot presence 
	int isBot[MAX_PLAYERS] = { 0, 0, 0, 0};

	int players[MAX_PLAYERS];
	int playerNum;
	int outcome;
	int currentPlayer;
	int gameOver = FALSE;
	int gameStarted = FALSE;
	int turnNum = 0;

	int randomSeed = atoi(argv[1]);

	//Default cards, as defined in playDom
	int kCards[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

	struct gameState g;
	struct gameState * game = &g;

	memset(game,0,sizeof(struct gameState));
		
	if(argc != 2){
		printf("Usage: player [integer random number seed]\n");
		return EXIT_SUCCESS;
	}

	if(randomSeed <= 0){
		printf("Usage: player [integer random number seed]\n");
		return EXIT_SUCCESS;
	}	
	
	initializeGame(2,kCards,randomSeed,game);

	printf("Please enter a command or \"help\" for commands\n");
	

	while(TRUE) {
		int arg0 = UNUSED;
		int arg1 = UNUSED;
		int arg2 = UNUSED;
		int arg3 = UNUSED;

		outcome = FAILURE;
		strcpy(line,"");
		strcpy(command,"");
		strcpy(cardName,"");
		
		currentPlayer = whoseTurn(game);
		
		//If you are getting a seg fault comment this if block out
		gameOver = isGameOver(game); 		
		if(gameStarted == TRUE && gameOver == TRUE){
			printScores(game);
			getWinners(players, game);
			printf("After %d turns, the winner(s) are:\n", turnNum);
			for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
				if(players[playerNum] == WINNER) printf("Player %d\n", playerNum);
			}
		for(playerNum = 0; playerNum < game->numPlayers; playerNum++){
				printHand(playerNum, game);
				printPlayed(playerNum, game);
				printDiscard(playerNum, game);
				printDeck(playerNum, game);
			}
			
			break; //Exit out of the game/while loop
		}         
		

		if(isBot[currentPlayer] == TRUE) {
				executeBotTurn(currentPlayer, &turnNum, game);
				continue;
		}
		
		printf("$ ");
		fgets(line, MAX_STRING_LENGTH, stdin);
		sscanf(line, "%s %d %d %d %d", command, &arg0, &arg1, &arg2, &arg3);


		if(COMPARE(command, add) == 0) {
			outcome = addCardToHand(currentPlayer, arg0, game);
			cardNumToName(arg0, cardName);
			printf("Player %d adds %s to their hand\n\n", currentPlayer, cardName);
		} else
		if(COMPARE(command, buyC) == 0) {
			outcome = buyCard(arg0, game);
			cardNumToName(arg0, cardName);
			if(outcome == SUCCESS){
				printf("Player %d buys card %d, %s\n\n", currentPlayer, arg0, cardName);
			} else {
				printf("Player %d cannot buy card %d, %s\n\n", currentPlayer, arg0, cardName);
			}
		} else
		if(COMPARE(command, endT) == 0) {
			if(gameStarted == TRUE) {
				if(currentPlayer == (game->numPlayers -1)) turnNum++;
				endTurn(game);
				currentPlayer = whoseTurn(game);
				printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
			}

		} else			
		if(COMPARE(command, exit) == 0) {
			break;
		} else
		if(COMPARE(command, help) == 0) {
			printHelp();
		} else
		if(COMPARE(command, init) == 0) {
			int numHuman = arg0 - arg1;
			for(playerNum = numHuman; playerNum < arg0; playerNum++) {
				isBot[playerNum] = TRUE;
			}			
	//		selectKingdomCards(randomSeed, kCards);  //Comment this out to use the default card set defined in playDom.
			outcome = initializeGame(arg0, kCards, randomSeed, game);
			printf("\n");
			if(outcome == SUCCESS){
				gameStarted = TRUE;
				currentPlayer = whoseTurn(game);
				printf("Player %d's turn number %d\n\n", currentPlayer, turnNum);
			}

		} else
		if(COMPARE(command, numH) == 0) {
			int numCards = numHandCards(game);
			printf("There are %d cards in your hand.\n", numCards);
		} else
		if(COMPARE(command, play) == 0) {
			int card = handCard(arg0,game);
			outcome = playCard(arg0, arg1, arg2, arg3, game);
			cardNumToName(card, cardName);
			if(outcome == SUCCESS){
				printf("Player %d plays %s\n\n", currentPlayer, cardName);
			} else {
				printf("Player %d cannot play card %d\n\n", currentPlayer, arg0);
			}

		} else
		if(COMPARE(command, resign) == 0) {
			endTurn(game);
			printScores(game);
			break;
		} else
		if(COMPARE(command, show) == 0) {
			if(gameStarted == FALSE) continue;
			printHand(currentPlayer, game);
			printPlayed(currentPlayer, game);
			//printDiscard(currentPlayer, game);
			//printDeck(currentPlayer, game);
		} else
		if(COMPARE(command, stat) == 0) {
			if(gameStarted == FALSE) continue;
			printState(game);
		} else
		if(COMPARE(command, supply) == 0) {
			printSupply(game);
		} else
		if(COMPARE(command, whos) == 0) {
			int playerNum =	whoseTurn(game);
			printf("Player %d's turn\n", playerNum);
		} 
    	}
	
    	return EXIT_SUCCESS;

}
int main() {
	struct gameState G;
	FILE *out;
	int nplayers = 0, ntests=0,i=0,j=0,k[10],redo = 1,seed, currentp=0, actions =0, *played, turn, ncoins=0, buyA = -1;
	out = fopen("gameResults.out","w");
	srand(time(NULL));
	
	nplayers = rand() % 3 + 2;
	// iterate through multiple tests
	while(ntests < MAX_TESTS) {
		turn = 1;
		seed = rand();
		printf("random seed %d ", seed);
		printf("in tests\n");
		fprintf(out, "Initializing game: %d\n", ntests);
		//pick cards
		for(i=0;i < 10; i++){
			printf("stuck picking cards\n");
			//kingdom cards unique no repeats
			do {
			redo = 0;
			k[i] = rand() % 20 + 7;
			printf("%d ", k[i]);
				for(j=0; j < i; j++) {
					if(k[i] == k[j]){
						redo = 1;
					} 
				}
			} while(redo);
		}
		fprintf(out, "Kingdom cards: \n");
		for(i=0; i < 10; i++) {
			fprintf(out,"%s, ", numName(k[i]));
		}
		fprintf(out,"\n");
		initializeGame(nplayers, k, seed, &G);
		printf("past init games\n");
		//Print player hands
		/*fprintf(out, "Player hands: \n");
		for(i=0; i < nplayers; i++) {
			fprintf(out,"P%d: {\n",i);
			for(j=0; j < numHandCards(&G); j++) {
				//fprintf(out, "\tC%d: %s\n", j, numName(handCard(j, &G)));
				//debug
				fprintf(out, "\tC%d: %d\n", j, handCard(j, &G));
			}
			fprintf(out, "}\n");
		}
		*/
		
		while(!isGameOver(&G)) {
			//printf("stuck in game\n");
			currentp = whoseTurn(&G);
			//Print turn player cards and info
			fprintf(out, "-- P%d turn %d --\n Current hand: {", currentp,turn);
			for(i=0; i < numHandCards(&G); i++) {
				fprintf(out, "C%d: %s ", i, numName(handCard(i, &G)));
			}
			fprintf(out, "}\n <-- Action Phase -->\n");
			//start action phase
			actions = 0;
			ncoins = coins(&G);
			G.coins = ncoins;
			for(i=0;i < G.numActions;i++) {
				fprintf(out, "#A: %d\n", G.numActions);
				played = playing(k, &G);
				if(played == 0) {
					fprintf(out, "No playable cards\n");
					break;
				}
				fprintf(out, "Playing: %s\n Choices: %d, %d, %d\n", numName(played[0]), played[1], played[2], played[3]);
				
				if(playCard(played[0], played[1], played[2], played[3], &G) == -1)
					fprintf(out, "\tFailure to play %s\n", numName(played[0]));
			}
			
			//start buy phase
			fprintf(out, "<-- Buy Phase --> \n");
			for(i=0; i < G.numBuys; i++) {
				buyA = canBuy(k, &G);
				fprintf(out, "Buying a %s\n", numName(buyA));
				if(buyCard(buyA, &G) == -1)
					fprintf(out, "\tFailed to buy\n");
				G.coins -= getCost(buyA);
			}
			
			//end of turn
			//fprintf(out, "-- End P%d turn --\n", currentp);
			endTurn(&G);
			free(played);
			turn++;
		}
		
	
	ntests++;
	turn = 1;
	}
	fclose(out);
	return 0;
}
Example #17
0
// is game over tests  
int main(int argc, char** argv) 
{
    struct gameState game; 
    int i; 
    int retValue; 
    int errorPresent = 0; 
    int valueChanged = 0; 
    
    // should return that game is not over 
    // fill all supply counts with 20 cards 
    for (i = 0; i < 25; i++)
    {
        game.supplyCount[i] = 20; 
    }
    
    retValue = isGameOver(&game);
    
    if(retValue == 1)
    {
        printf("\nError in isGameOver, game over returned when all cards are greater than 0\n");
        errorPresent = 1; 
    } 
    
    // check that all card values are still set to 20 and have not been changed 
    for(i = 0; i < 25; i++)
    {
        if(game.supplyCount[i] != 20)
        {
            valueChanged = 1; 
        }
    }
    
    if(valueChanged == 1)
    {
        printf("\nError in isGameOver, value of supplyCount for cards is being changed when being returned from the function.\n");
        errorPresent = 1; 
    }
    
    // set province = 0 so game over should return true 
    game.supplyCount[3] = 0; 
    
    retValue = isGameOver(&game);
    
    if(retValue == 0)
    {
        printf("\nError in isGameOver, function returned false when game state contains 0 province cards\n");
        errorPresent = 1; 
    }
    
    // reset province to have a value and then run the test again with each card being 0
    game.supplyCount[3] = 10; 
    
    for(i = 0; i < 24; i++)
    {
        // province card should end game so remove that from the test of the other cards 
        if(i != 3)
        {
            game.supplyCount[i] = 0; 
            
            retValue = isGameOver(&game);
        
            if(retValue == 1)
            {
                printf("\nError in isGameOver, function returned true when game state supply count for card %d contains 0 cards\n", i);
                errorPresent = 1; 
            }
            
            game.supplyCount[i] = 20;    
        }
    }
    
    // set two cards to 0 and check to see if proper return value is provided
    game.supplyCount[0] = 0; 
    game.supplyCount[1] = 0;
    
    retValue = isGameOver(&game); 
    
    if(retValue == 1) 
    {
        printf("\nError in isGameOver, function returned true when game state supply count for 2 cards contains 0 \n");        
        errorPresent = 1; 
    }
    
    // set third card to 0 and check to see if false is returned 
    game.supplyCount[2] = 0; 
    retValue = isGameOver(&game); 
    
    if(retValue == 0)
    {
        printf("\nError in isGameOver, function returned false when game state supply count for 3 cards contains 0 \n");        
        errorPresent = 1; 
    }
    
    if(errorPresent == 0)
    {
        printf("\nNo errors found in isGameOver unit tests\n"); 
    }
    
    return 0; 
}
int main (int argc, char** argv) {
  struct gameState G;
  struct gameState *p = &G;

  srand(atoi(argv[1]));
  
  int a[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};
  
  int j[10], k[10];
  
  int i, ii;
  for(i=0; i<10; i++){
	  int loop = 1;
	  while(loop){
		  j[i] = rand()%20;
		  for(ii=0; ii<i; ii++){
			  if(j[ii] == j[i]){
				  loop++;
				  break;
			  }
		  }
		  loop--;
	  }
  }
  
  for(i=0; i<10; i++){
	k[i] = a[j[i]];
  }
  
  for(i=0; i<10; i++){
	  printf("%s ", card_name(k[i]));
	  //printf("%d %s\n", k[i], card_name(k[i]));
  }
  printf("\n");
  
  int players = rand()%3+2;
  printf("Players: %d\n\n", players);

  printf ("Starting game.\n");
  
  initializeGame(players, k, atoi(argv[1]), p);
  
  i = 0;

  while (!isGameOver(p)) {
	
	int active = whoseTurn(p);
	
	printf("Player %d\n", active);
	
	i = 0;
	while(p->numActions > 0){
		int playing = rand()%p->handCount[active]+1;
		if(i > 100){
			playing = p->handCount[active];
		}
		if(playing == p->handCount[active]){	//Chance to stop playing cards early
			printf("Stop playing cards early\n");
			break;
		}
		else{
			if(playCard(playing, rand()%20, rand()%20, rand()%20, p) != -1){
				printf("Playing %s\n", card_name(handCard(playing,p)));
			}
		}
		i++;
	}
	
	while(p->numBuys > 0){
		int action = rand()%18;
		
		if(i > 100){
			action = 17;;
		}
		
		if(action <= 6){			//Attempting to get a utility card, i.e. silver or province
			if(buyCard(action, p) != -1){
				printf("Buying %s\n", card_name(action));
			}
		}
		else if(action == 17){		//Chance to end turn early
			printf("Stop buying cards early\n");
			break;
		}
		else{						//Attempt to buy one of the randomly selected cards
			if(buyCard(k[action-7], p) != -1){
				printf("Buying %s\n", card_name(k[action-7]));
			}
		}
		i++;
	}
	
	printf("END_TURN\n\n");
	
	printf ("Player %d: %d\n", active, scoreFor(active, p));
	printf("Deck Size: %d\nDiscard Size: %d\n\n", p->deckCount[active], p->discardCount[active]);
	
	endTurn(p);
	
      
	    
  } // end of While

  printf ("Finished game.\n");
  printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, p), scoreFor(1, p));

  return 0;
}
int main() {

    srand(12345);

    struct gameState G;
    struct gameState *p = &G;
    int numPlayers, i, j, r, ran, card, pass;
    int seed = 2;
    int turn = 0;
    int cont = 1;
    int k[10];
    FILE *fp;
    fp = fopen("gameResults.out", "w");

    printf("Initializing Dominion Game\n");

    pass = -1;
    while(pass == -1)
    {
        for(i = 0; i < 10; i++)
        {
            ran = rand() % 20 + 7;
            k[i] = ran;
        }

        numPlayers = rand() % 3 + 2;

        pass = initializeGame(numPlayers, k, seed, p);
    }

    printf("Hold onto your butt\n\n");

    printf("Number of Players: %d\n\n", numPlayers);

    while(!isGameOver(p))
    {

        fprintf(fp, "GAMESTATE\n");
        //GameState Currently
        fprintf(fp, "NumPlayers: %d\n\n", p->numPlayers);
        for(i = 0; i < numPlayers; i++)
        {
            fprintf(fp, "Player: %d\n", i);
            fprintf(fp, "HandCount: %d\n", p->handCount[i]);
            fprintf(fp, "Hand: ");
            for(j = 0; j < p->handCount[i]; j++)
            {
                fprintf(fp, "%d ", p->hand[i][j]);
            }
            fprintf(fp, "\nDeckCount: %d\nDiscardCount %d\n", p->deckCount[i], p->discardCount[i]);
            fprintf(fp, "Score: %d\n", scoreFor(i, p));
        }

        //Begin Turn
        if(whoseTurn(p) == 0)
        {
            if(turn == 1000)
            {
                return 0;
            }
            turn++;
            printf("\nROUND %d\n", turn);
            fprintf(fp, "\nROUND %d\n", turn);
        }

        printf("PLAYER %d TURN\n", p->whoseTurn);
        fprintf(fp, "PLAYER %d TURN\n", p->whoseTurn);
        updateCoins(whoseTurn(p), p, 0);

        //action phase
        cont = 1;
        while(p->numActions != 0)
        {
            cont = 0;
            for(j = 0; j < p->handCount[i]; j++)
            {
                if((p->hand[i][j] != estate) && (p->hand[i][j] && duchy) && (p->hand[i][j] != province) && (p->hand[i][j] != copper) && (p->hand[i][j] != silver) && (p->hand[i][j] != gold))
                {
                    cont = 1;
                }
            }
            if(cont == 0)
                break;

            ran = rand() % p->handCount[i];

            while((p->hand[i][ran] == estate) || (p->hand[i][ran] == duchy) || (p->hand[i][ran] == province))
            {
                ran = rand() % p->handCount[i];
            }

            r = playCard(ran, 1, 1, 1, p);

            if(r == -1)
            {
                printf("Player %d attempted to play %d, but failed\n", whoseTurn(p), p->hand[whoseTurn(p)][ran]);
                fprintf(fp, "Player %d attempted to play %d, but failed\n", whoseTurn(p), p->hand[whoseTurn(p)][ran]);
                p->numActions = 0;

            }
            else
            {
                printf("Player %d played %d\n", whoseTurn(p), p->hand[whoseTurn(p)][ran]);
                fprintf(fp, "Player %d played %d\n", whoseTurn(p), p->hand[whoseTurn(p)][ran]);
            }
        }

        //buy phase
        while(p->numBuys != 0 )
        {
            printf("Number of Coins: %d\n", p->coins);
            fprintf(fp, "Number of Coins: %d\n", p->coins);
            if(p->coins <= 1)
            {
                r = buyCard(copper, p);
                card = copper;
            }
            else if(p->coins == 2 || p->coins == 4)
            {
                r = buyCard(estate, p);
                card = estate;
            }
            else if(p->coins == 5 || p->coins == 7)
            {
                r = buyCard(duchy, p);
                card = duchy;
            }
            else if(p->coins == 8)
            {
                r = buyCard(province, p);
                card = province;
            }
            else if(p->coins == 3)
            {
                r = buyCard(silver, p);
                card = silver;
            }
            else if(p->coins == 6)
            {
                r = buyCard(gold, p);
                card = gold;
            }
            else if((p->coins > 8))
            {
                ran = rand() % 10;
                if(ran == 0)
                {
                    r = buyCard(estate, p);
                    card = estate;
                }
                else if(ran == 1)
                {
                    r = buyCard(silver, p);
                    card = silver;
                }
                else if(ran == 2)
                {
                    r = buyCard(silver, p);
                    card = silver;
                }
                else if(ran == 3)
                {
                    r = buyCard(smithy, p);
                    card = silver;
                }
                else if(ran == 4)
                {
                    r = buyCard(village, p);
                    card = village;
                }
            }
            else if(p->coins == 7)
            {
                r = -1;
                while(r == -1)
                {
                    ran = rand() % 10;
                    r = buyCard(k[ran], p);

                    card = k[ran];
                }
            }


            if(r == -1)
            {
                printf("Player %d attempted to buy %d, but failed\n", whoseTurn(p), card);
                fprintf(fp, "Player %d attempted to buy %d, but failed\n", whoseTurn(p), card);
                printf("Player Ended Turn\n");
                fprintf(fp, "Player Ended Turn\n");
                p->numBuys = 0;
            }
            else
            {
                printf("Player %d purchased %d\n", whoseTurn(p), card);
                fprintf(fp, "Player %d purchased %d\n", whoseTurn(p), card);
            }
        }
        printf("Player %d Score: %d\n", whoseTurn(p), scoreFor(whoseTurn(p), p));
        fprintf(fp, "Player %d Score: %d\n", whoseTurn(p), scoreFor(whoseTurn(p), p));

        printf("End Turn\n\n");
        fprintf(fp, "End Turn\n\n");

        endTurn(p);

    }

    printf("GAME OVER\n\n");
    fprintf(fp, "GAME OVER\n\n");

    printf("FINAL SCORES\n");
    fprintf(fp, "FINAL SCORES\n");
    for(i = 0; i < p->numPlayers; i++)
    {
        printf("Player %d: %d\n", i, scoreFor(i, p));
        fprintf(fp, "Player %d: %d\n", i, scoreFor(i, p));
    }

    return 0;
}
Example #20
0
int main(){
	int i, j, k, r, index;
	int failCount = 0;
	int totalCount = 0;
	int numPlayers = 2;
	int seed = 5555;
	int kingCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
	struct gameState G;

	FILE* outfile;
	FILE* reportfile;

	outfile = fopen("unittest4.result", "w");
	reportfile = fopen("unittestresult.out", "a");

	fprintf(reportfile, "Testing isGameOver, with all combinations of number of provinces left (0-12), and number of other piles at zer0 (0-17).\n");

	for(i = 0; i < 13; i++)/*Number of Provinces*/{
		for(j = 0; j < 18; j++)/*Number of Piles at zero*/{
			memset(&G, 23, sizeof(struct gameState));
			r = initializeGame(numPlayers, kingCards, seed, &G);

			G.supplyCount[3] = i;

			for(k = 0; k < j; k++){
				index = floor(Random() * treasure_map);
				if(index == 3) k--;
				else if(G.supplyCount[index] == 0) k--;
				else G.supplyCount[index] = 0;
			}

			r = isGameOver(&G);
			totalCount++;

			if(i <= 0){
				if(r == 1) fprintf(outfile, "SUCCESS: Correctly found that Province pile is empty. (%d, %d)\n", i, j);
				else{
					fprintf(outfile, "FAILURE: Did not find that Province pile is empty. (%d, %d)\n", i, j);
					failCount++;
				}
			}
			else if(r == 1){
				fprintf(outfile, "FAILURE: Incorrectly found that Province pile is empty. (%d, %d)\n", i, j);
				failCount++;
			}
			else fprintf(outfile, "SUCCESS: Did not incorrectly find the Province pile is empty. (%d, %d)\n", i, j);

			if(j >= 3 && r > 1){
				if(r == 2) fprintf(outfile, "SUCCESS: Correctly found that 3 or more piles are empty. (%d, %d)\n", i, j);
				else{
					fprintf(outfile, "FAILURE: Did not find that 3 or more piles were empty. (%d, %d)\n", i, j);
					failCount++;
				}
			}
			else if(r == 2){
				fprintf(outfile, "FAILURE: Incorrectly found that 3 or more piles were empty. (%d, %d)\n", i, j);
				failCount++;
			}
			else fprintf(outfile, "SUCCESS: Did not incorrectly find that 3 or more piles were empty. (%d, %d)\n", i, j);

		}
	}

	fprintf(reportfile, "NUMBER OF COMBINATIONS TESTED: %d\n", totalCount);
	fprintf(reportfile, "NUMBER OF FAILED TESTS (2 possible per unique set of inputs): %d\n", failCount);

	fclose(outfile);
	fclose(reportfile);

	return 0;
}
Example #21
0
int main() {

    int numPlayers = 2;
    int PlayerID = 0;
    int seed = 2;
    int i;
    int errorFlag = 0;
	struct gameState G, testG;

    SelectStream(6);
    PutSeed(7);

    int k[10] = {adventurer, embargo, village, minion, mine, cutpurse,
    sea_hag, tribute, smithy, council_room};

    printf ("TEST 1: Testing isGameOver() - Province Check\n");

    //Initializes game
    initializeGame(numPlayers, k, seed, &G);

    //Copies gamestate into a test gamestate
    memcpy(&testG, &G, sizeof(struct gameState));

    int provinceNum;

    //This loop randomly assigns the number of provinces to supplyCount
    for (i = 0; i < 1000; i++) {
        provinceNum = floor(Random() * 3);
        testG.supplyCount[province] = provinceNum;

//If the supply count count = 0, isGameOver should indicate the game should end
        if (testG.supplyCount[province] == 0) {
            if (isGameOver(&testG) != 1) {
                printf("TEST 1 HAS FAILED\n\n");
                errorFlag = 1;
            }
        }

//If the supply count is greater than 0, isGameOver should indicate the game should continue
        else {
            if (isGameOver(&testG) != 0) {
                printf("TEST 1 HAS FAILED\n\n");
                errorFlag = 1;
            }
        }
    }

    printf("TEST 2: Testing isGameOver() - Supply Piles Empty\n");

    int supplyEmpty;
    int supplyIndex;
    int j, pp;

    memset(&G, 23, sizeof(struct gameState));
    memset(&testG, 23, sizeof(struct gameState));
    initializeGame(numPlayers, k, seed, &G);
    memcpy(&testG, &G, sizeof(struct gameState));

    supplyEmpty = floor(Random() * 25);

    for (i = 0; i < 10000; i++) {
        int m = 0;
        supplyEmpty = floor(Random() * 25);
        for (j = 0; j < supplyEmpty; j++) {
            supplyIndex = floor(Random() * 25);
            testG.supplyCount[supplyIndex] = 0;
        }

        for (pp = 0; pp < 25; pp++) {
            if (testG.supplyCount[pp] == 0) {
                m++;
            }

        }
        if (m >= 3) {
            if (isGameOver(&testG) != 1) {
                printf("TEST 2 HAS FAILED\n\n");
                errorFlag = 1;
            }
        }
        else if (testG.supplyCount[province] == 0 && isGameOver(&testG) != 0) {
            printf("TEST 4 HAS FAILED\n\n");
            errorFlag = 1;
        }
    }

    if (errorFlag == 0) {
        printf("ALL TESTS PASSED\n\n");
    }


    return 0;

}
Example #22
0
int main (int argc, char** argv) {
  struct gameState G;
  struct gameState *p = &G;

  int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
	       sea_hag, tribute, smithy};

  printf ("Starting game.\n");
  
  initializeGame(2, k, atoi(argv[1]), p);
  
  int money = 0;
  int smithyPos = -1;
  int adventurerPos = -1;
  int i=0;

  int numSmithies = 0;
  int numAdventurers = 0;

  while (!isGameOver(p)) 
  {
    money = 0;
    smithyPos = -1;
    adventurerPos = -1;
    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;
      else if (handCard(i, p) == smithy)
       smithyPos = i;
       else if (handCard(i, p) == adventurer)
    adventurerPos = i;
    }

    if (whoseTurn(p) == 0) 
    {
      if (smithyPos != -1) 
      {
        printf("0: smithy played from position %d\n", smithyPos); 
	      playCard(smithyPos, -1, -1, -1, p); 
	      printf("smithy 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++;
	      }
      }

      if (money >= 8) {
        printf("0: bought province\n"); 
        buyCard(province, p);
      }
      else if (money >= 6) {
        printf("0: bought gold\n"); 
        buyCard(gold, p);
      }
      else if ((money >= 4) && (numSmithies < 2)) {
        printf("0: bought smithy\n"); 
        buyCard(smithy, p);
        numSmithies++;
      }
      else if (money >= 3) {
        printf("0: bought silver\n"); 
        buyCard(silver, p);
      }

      printf("0: end turn\n");
      endTurn(p);
    }

    else 
    {
      if (adventurerPos != -1) 
      {
        printf("1: adventurer played from position %d\n", adventurerPos);
	      playCard(adventurerPos, -1, -1, -1, p); 
	      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++;
	      }
      }

      if (money >= 8) 
      {
         printf("1: bought province\n");
         buyCard(province, p);
     }
     
     else if ((money >= 6) && (numAdventurers < 2)) 
     {
         printf("1: bought adventurer\n");
	       buyCard(adventurer, p);
	       numAdventurers++;
      }

      else if (money >= 6)
      {
         printf("1: bought gold\n");
	        buyCard(gold, p);
      }
    
     else if (money >= 3)
     {
          printf("1: bought silver\n");
	        buyCard(silver, p);
     }
    
     printf("1: endTurn\n");
      
     endTurn(p);      
    }

      printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, p), scoreFor(1, p));
	    
  } // end of While

  printf ("Finished game.\n");
  printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, p), scoreFor(1, p));

  return 0;
}
Example #23
0
void PopStar::onReduce()
{
	if (!gameLayer)
	{
		return;
	}

	int num = selectStars.size();
	int score = getPopStarDataMgr().getScore() + getPopStarDataMgr().getScoreByReduceNum(num);
	setScore(score);
	
	StarListIter iter;
	for (iter = selectStars.begin(); iter != selectStars.end(); ++iter)
	{
		gameLayer->removeChild(iter->star);
		stars[iter->row][iter->col] = NULL;
	}
	selectStars.clear();


	// 竖直调整
	for (int row = 0; row < ROW_NUM; ++row)
	{
		for (int col = 0; col < COL_NUM; ++col)
		{
			int tempRow = row;
			Star* star = stars[row][col];
			if (star)
			{
				continue;
			}
			else
			{
				while (!star && (tempRow < ROW_NUM))
				{
					star = stars[tempRow][col];
					if (star)
					{
						break;
					}
					++tempRow;
				}

				if (star)
				{
					stars[row][col] = stars[tempRow][col];
					stars[tempRow][col] = NULL;
				}
			}
		}
	}

	while (isNeedHoriAdjust())
	{
		int endCol = getCheckEndCol();
		for (int col = 0; col <= endCol; ++col)
		{
			if (!stars[0][col])
			{
				for (int i = col; i < endCol; ++i)
				{
					for (int row = 0; row < ROW_NUM; ++row)
					{
						stars[row][i] = stars[row][i+1];
					}
				}

				for (int row = 0; row < ROW_NUM; ++row)
				{
					stars[row][endCol] = NULL;
				}
				
				break;
			}
		}
	}


	// 刷新位置
	for (int row = 0; row < ROW_NUM; ++row)
	{
		for (int col = 0; col < COL_NUM; ++col)
		{
			Star* star = stars[row][col];
			if (star)
			{
				star->setDestPos(ccp(STAR_WIDTH*col + STAR_WIDTH/2, STAR_HEIGHT*row + STAR_HEIGHT/2));
			}
		}
	}

	if (isLevelEnd())
	{
		int num = getLeftStarNum();
		int score = getPopStarDataMgr().getScore() + getPopStarDataMgr().getScoreByLeftNum(num);
		setScore(score);

		int historyScore = getPopStarDataMgr().getHistoryScore();
		if (score > historyScore)
		{
			setHistoryScore(score);
		}

		int level = getPopStarDataMgr().getLevel();
		int historyLevelScore = getPopStarDataMgr().getHistoryLevelScoreByLevel(level);
		if (score > historyLevelScore)
		{
			setHistoryLevelScore(score);
		}
		

		if (isGameOver()) // 游戏结束
		{
			changeState( new GameOverState(this) );
		}
		else // 下一等级
		{
			changeState( new GamePassLevelState(this) );
		}
	}
}
Example #24
0
// Tests the smithy card in dominion.c
int main (int argc, char** argv) {
    printf("TESTING smithy card\n");

    srand(time(NULL));

    int numplayers = 2;
    struct gameState G;
    int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

    printf("RANDOM TESTS\n");

    int numtests = 100;
    for(int i = 0; i < numtests; i++){
        initializeGame(numplayers, k, rand(), &G);

          int money = 0;
          int smithyPos = -1;
          int adventurerPos = -1;
          int i=0;

          int numSmithies = 0;
          int numAdventurers = 0;

          while (!isGameOver(&G)) {
            money = 0;
            smithyPos = -1;
            adventurerPos = -1;
            for (i = 0; i < numHandCards(&G); i++) {
              if (handCard(i, &G) == copper)
            money++;
              else if (handCard(i, &G) == silver)
            money += 2;
              else if (handCard(i, &G) == gold)
            money += 3;
              else if (handCard(i, &G) == smithy)
            smithyPos = i;
              else if (handCard(i, &G) == adventurer)
            adventurerPos = i;
            }

            if (whoseTurn(&G) == 0) {
              if (smithyPos != -1) {
                struct gameState orig;
                memcpy(&orig, &G, sizeof(struct gameState));

                playCard(smithyPos, -1, -1, -1, &G); 
                
               // assert(G.handCount[0] == orig.handCount[0] + 2);
                money = 0;
                i=0;
            while(i<numHandCards(&G)){
              if (handCard(i, &G) == copper){
                playCard(i, -1, -1, -1, &G);
                money++;
              }
              else if (handCard(i, &G) == silver){
                playCard(i, -1, -1, -1, &G);
                money += 2;
              }
              else if (handCard(i, &G) == gold){
                playCard(i, -1, -1, -1, &G);
                money += 3;
              }
              i++;
            }
              }

              if (money >= 8) {
                buyCard(province, &G);
              }
              else if (money >= 6) {
                buyCard(gold, &G);
              }
              else if ((money >= 4) && (numSmithies < 2)) {
                buyCard(smithy, &G);
                numSmithies++;
              }
              else if (money >= 3) {
                buyCard(silver, &G);
              }

              endTurn(&G);
            }
            else {
              if (adventurerPos != -1) {
            playCard(adventurerPos, -1, -1, -1, &G); 
            money = 0;
            i=0;
            while(i<numHandCards(&G)){
              if (handCard(i, &G) == copper){
                playCard(i, -1, -1, -1, &G);
                money++;         
              }
              else if (handCard(i, &G) == silver){
                playCard(i, -1, -1, -1, &G);
                money += 2;
              }
              else if (handCard(i, &G) == gold){
                playCard(i, -1, -1, -1, &G);
                money += 3;
              }
              i++;
            }
              }

              if (money >= 8) {
                buyCard(province, &G);
              }
              else if ((money >= 6) && (numAdventurers < 2)) {
            buyCard(adventurer, &G);
            numAdventurers++;
              }else if (money >= 6){
                buyCard(gold, &G);
                }
              else if (money >= 3){
                buyCard(silver, &G);
              }
              
              endTurn(&G);      
            }
          } // end of While
    }

    printf("ALL TESTS OK\n");
}
int main (int argc, char** argv) 
{
 //List of the name of the card 
  const char* cardNames[] = {
           "curse", "estate", "duchy", "province", "copper", "silver",
           "gold", "adventurer", "council_room", "feast", "gardens",
           "mine", "remodel", "smithy", "village", "baron", "great_hall",
           "minion", "steward", "tribute", "ambassador", "cutpurse", 
           "embargo", "outpost", "salvager", "sea_hag", "treasure_map"};

  struct gameState state;
  struct gameState *p = &state;

  int numberOfPlayer = 0;
  int seed;
  int k[10];
  printf ("Start The Test\n");

  // Set random number of players between 2 and 4
  numberOfPlayer = rand() % 3 + 2;

  if (argc == 2)
  {
    seed = atoi(argv[1]);
    srand(atoi(argv[1]));
  }
  else 
  {
    srand(time(NULL));
    seed = rand();
  } 

  int money = 0;
  int actionPos = -1;
  int i=0;
  int kingdomCard[treasure_map] = {0};
  int card;
  int turn = 0;

  printf("List of Kingdom Cards\n");

  // Assign the kingdom cards on the deck 
  for (i = 0; i < 10; i++)
  {
    card = rand() % (treasure_map - adventurer + 1) + adventurer;
    if (kingdomCard[card])
	{
		i--;
        continue;
    }
    kingdomCard[card] = 1;
    k[i] = card;
    printf("%s, ", cardNames[card]);
  }
  printf("\n");

  // Initialize the game 
  initializeGame(numberOfPlayer, k, seed, p);
  
  for (i = 0; i < numberOfPlayer; i++)
  {
	  printf("Cuurent score for player %d : %d\n", i+1, scoreFor(i, p));
  }

  // Logic for the game 
  while (!isGameOver(p))
  {
	  printf("Turn#: %d\n", ++turn);
      money = 0;
      actionPos = -1;
      int *a;
      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;		  
		  }
		  else if (handCard(i,p) <= treasure_map && handCard(i,p) >= adventurer)
		  {
			  actionPos = i;
		  }
      }

    // Play an action card 
    if (actionPos != -1) 
	{
    	a = comLogic(kingdomCard,p,p->hand[whoseTurn(p)][actionPos],actionPos);
		printf("Player %d attempts to play %s from hand with choice1 = %d, choice2 = %d, choice3 = %d\n", 
						  p->whoseTurn+1, cardNames[p->hand[whoseTurn(p)][actionPos]], a[0], a[1], a[2]);
      
      if(playCard(actionPos, a[0], a[1], a[2], p) == -1)
	  {
		  printf("Play failed or the choice can't be handle\n");
      }
      else
	  {
		  printf("Play succeeded\n");
      }

      money = 0;
      i=0;
	  
      // Count the amount of money that the players have
      while(i<numHandCards(p))
	  {
		  if (handCard(i, p) == copper)
		  {
			money++;
          }
          else if (handCard(i, p) == silver)
		  {
			money += 2;
          }
          else if (handCard(i, p) == gold)
		  {
			money += 3;
          }
		  i++;
      }
    }

    // If player have the money, buy province 
    if (money >= 8)
	{
		printf("player %d : bought province\n", p->whoseTurn+1);
		buyCard(province, p);
    }
    // Player can buy treasure or action card 
    else if(rand() % 2)
	{                                 
      // Buy the most expensive treasure card they can afford
      if (money >= 6)
	  {
        printf("player %d : bought gold\n", p->whoseTurn+1); 
        buyCard(gold, p);
      }
      else if (money >= 3)
	  {
        printf("player %d : bought silver\n", p->whoseTurn+1); 
        buyCard(silver, p);
      }
      else
	  {
        printf("player %d: bought copper\n", p->whoseTurn+1); 
        buyCard(copper, p);
      }
    }
    else
	{
      for (i = 0; i < 10; i++)
	  {
        card = k[rand() % 10];
        if (buyCard(card,p) != -1)
		{
          i = -1;
          break;
        }
      }
      
      if (i == -1)
	  {
        printf("Player %d bought %s\n", p->whoseTurn+1, cardNames[card]);
      }
      else
	  {
        printf("Player %d did not have enough money to buy a card...\n", p->whoseTurn+1);
      }
    }

    printf("player%d: end turn\n", p->whoseTurn+1);
    endTurn(p);

    // Scores for each players
    for (i = 0; i < numberOfPlayer; i++)
	{
      printf("Score for player %d : %d\n", i+1, scoreFor(i, p));
    }
  } 

  printf ("Game end \n");
  // Final game score 
  for (i = 0; i < numberOfPlayer; i++)
  {
	  printf("Final score for player %d : %d\n", i+1, scoreFor(i, p));
  }
  
  printf ("Test successfull \n");
  return 0;
}
Example #26
0
int testIsGameOver() {
    int seed = 1000;
    int numPlayer = 2;
    int r;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, sea_hag, treasure_map};
    struct gameState G;

    printf ("TESTING isGameOver():\n");
    
    //Test that game is not over immediately after initialization
    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when game is initialized\n");
    else
        printf("isGameOver(): FAIL when game is initialized\n");
    
    //Test when province cards run out
    G.supplyCount[province] = 0;
    
    if (isGameOver(&G))
        printf("isGameOver(): PASS when province pile is empty\n");
    else
        printf("isGameOver(): FAIL when province pile is empty\n");
    
    //Test when there is only one province card
    G.supplyCount[province]++;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when province pile is set to 1\n");
    else
        printf("isGameOver(): FAIL when province pile is set to 1\n");
    
    //Reset and test when gardens cards run out
    memset(&G, 23, sizeof(struct gameState));   // clear the game state
    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
    G.supplyCount[gardens] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when gardens pile is empty\n");
    else
        printf("isGameOver(): FAIL when gardens pile is empty\n");
    
    //Test when gardens and copper cards run out
    G.supplyCount[copper] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when gardens and copper piles are empty\n");
    else
        printf("isGameOver(): FAIL when gardens and copper piles are empty\n");
    
    //Test when gardens, copper, and treasure_map cards run out
    G.supplyCount[treasure_map] = 0;
    
    if (isGameOver(&G))
        printf("isGameOver(): PASS when gardens, copper, and treasure_map piles are empty\n");
    else
        printf("isGameOver(): FAIL when gardens, copper, and treasure_map piles are empty\n");
    
    //Reset and test when sea_hag cards run out
    memset(&G, 23, sizeof(struct gameState));   // clear the game state
    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
    G.supplyCount[sea_hag] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when sea_hag pile is empty\n");
    else
        printf("isGameOver(): FAIL when sea_hag pile is empty\n");
    
    //Test when sea_hag and silver cards run out
    G.supplyCount[silver] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when sea_hag and silver piles are empty\n");
    else
        printf("isGameOver(): FAIL when sea_hag and silver piles are empty\n");
    
    //Test when sea_hag, silver, and gold cards run out
    G.supplyCount[gold] = 0;
    
    if (isGameOver(&G))
        printf("isGameOver(): PASS when sea_hag, silver, and gold piles are empty\n");
    else
        printf("isGameOver(): FAIL when sea_hag, silver, and gold piles are empty\n");
    
    //Reset and test when curse cards run out
    memset(&G, 23, sizeof(struct gameState));   // clear the game state
    r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
    G.supplyCount[curse] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when curse pile is empty\n");
    else
        printf("isGameOver(): FAIL when curse pile is empty\n");
    
    //Test when curse and estate cards run out
    G.supplyCount[estate] = 0;
    
    if (!isGameOver(&G))
        printf("isGameOver(): PASS when curse and estate piles are empty\n");
    else
        printf("isGameOver(): FAIL when curse and estate piles are empty\n");
    
    //Test when curse, estate, and duchy cards run out
    G.supplyCount[duchy] = 0;
    
    if (isGameOver(&G))
        printf("isGameOver(): PASS when curse, estate, and duchy piles are empty\n");
    else
        printf("isGameOver(): FAIL when curse, estate, and duchy piles are empty\n");

    return 0;
}
int main (int argc, char** argv) {
    int i;
    int j;
    int valid;
    int numPlayers;
    int k[10];
    int result;
    int seed;
    char cardName[100];
    struct gameState *state = newGame();
    int currentPlayer;



    printf ("------------------ Starting game --------------------\n");
    //set the seed
    srand(1);
    seed = rand();
    //2-4 players
    numPlayers = rand() % 3 + 2;

    printf("Number of Players: %d\n", numPlayers);

    for (i = 0; i < 10; i++) {
        valid = 0;
        while (k[i] == feast || k[i] == tribute || k[i] == council_room || valid == 0) {
            k[i] = rand() % (treasure_map - adventurer + 1) + adventurer;
            valid = 1;
            for(j = 0; j < i; j++) {
                if(k[i] == k[j]) {
                    valid = 0;
                }
            }
        }
    }

    printf("Kingdom Cards:\n");
    for(i = 0; i < 10; i++) {
        printf("Test.\n");
        cardNumToName(k[i], cardName);
        printf("Test..\n");
        printf("%s\n", cardName);
    }

    if(initializeGame(numPlayers, k, seed, state) != 0) {
        printf("Error when calling initializeGame\n");
        exit(1);
    }

    int money = 0;
    int actionCard = 0;
    int currentCard = 0;

    while (!isGameOver(state)) {

        currentPlayer = state->whoseTurn;
        printf("Player %d's Turn:\n", currentPlayer);

        printf("----------- Action phase -----------\n");

        actionCard = 0;
        for(i = 0; i < numHandCards(state); i++) {
            if((handCard(i, state) < treasure_map) && (handCard(i, state) >= adventurer) && (handCard(i, state) != feast) && (handCard(i, state) != tribute)) {
                actionCard = i;
                break;
            }
        }

        while(state->numActions > 0 && actionCard != 0) {
            currentCard = handCard(actionCard, state);
            cardNumToName(currentCard, cardName);

            if(playCard(actionCard, -1, -1, -1, state) == -1) {
                discardCard(actionCard, currentPlayer, state, 0);
            }
            else {
                printf("Played Card: %s, %d\n", cardName, currentCard );
            }

            if(state->numActions <= 0) {
                printf("All actions used, move to buy phase\n");
                break;
            }
            for(i = actionCard; i < numHandCards(state); i++) {
                if(handCard(i, state) < treasure_map && handCard(i, state) >= adventurer && handCard(i, state) != feast && handCard(i, state) != tribute) {
                    actionCard = i;
                    break;
                }
                else {
                    actionCard = 0;
                }
            }
        }

        printf("----------- Buy phase -----------\n");
        money = countHandCoins(state->whoseTurn, state);
        int buy_card;

        while(state->numBuys > 0 && money > 0) {
            do {
                if(money >= 8) {
                    buy_card = province;
                }
                else {
                    buy_card = rand() % treasure_map;
                    if(buy_card == 4) {
                        if(rand() % 4 != 0) {
                            buy_card++;
                        }
                    }
                }

            } while(buyCard(buy_card, state) != 0);

            cardNumToName(buy_card, cardName);
            printf("Buying card: %s\n", cardName);
        }
        printf("End Turn\n");
        endTurn(state);

    }

    printScores(state);

    return 0;
}
Example #28
0
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;
}
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus)
{
  int i;
  int j;
  int k;
  int x;
  int index;
  int currentPlayer = whoseTurn(state);
  int nextPlayer = currentPlayer + 1;

  int tributeRevealedCards[2] = {-1, -1};
  int temphand[MAX_HAND];// moved above the if statement
  int drawntreasure=0;
  int cardDrawn;
  int z = 0;// this is the counter for the temp hand
  if (nextPlayer > (state->numPlayers - 1)){
    nextPlayer = 0;
  }
  
	
  //uses switch to select card and perform actions
  switch( card ) 
    {
    case adventurer:
      while(drawntreasure<2){
	if (state->deckCount[currentPlayer] <1){//if the deck is empty we need to shuffle discard and add to deck
	  shuffle(currentPlayer, state);
	}
	drawCard(currentPlayer, state);
	cardDrawn = state->hand[currentPlayer][state->handCount[currentPlayer]-1];//top card of hand is most recently drawn card.
	if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
	  drawntreasure++;
	else{
	  temphand[z]=cardDrawn;
	  state->handCount[currentPlayer]--; //this should just remove the top card (the most recently drawn one).
	  z++;
	}
      }
      while(z-1>=0){
	state->discard[currentPlayer][state->discardCount[currentPlayer]++]=temphand[z-1]; // discard all cards in play that have been drawn
	z=z-1;
      }
      return 0;
			
    case council_room:
      //+4 Cards
      for (i = 0; i < 4; i++)
	{
	  drawCard(currentPlayer, state);
	}
			
      //+1 Buy
      state->numBuys++;
			
      //Each other player draws a card
      for (i = 0; i < state->numPlayers; i++)
	{
	  if ( i != currentPlayer )
	    {
	      drawCard(i, state);
	    }
	}
			
      //put played card in played card pile
      discardCard(handPos, currentPlayer, state, 0);
			
      return 0;
			
    case feast:
      //gain card with cost up to 5
      //Backup hand
      for (i = 0; i <= state->handCount[currentPlayer]; i++){
	temphand[i] = state->hand[currentPlayer][i];//Backup card
	state->hand[currentPlayer][i] = -1;//Set to nothing
      }
      //Backup hand

      //Update Coins for Buy
      updateCoins(currentPlayer, state, 5);
      x = 1;//Condition to loop on
      while( x == 1) {//Buy one card
	if (supplyCount(choice1, state) <= 0){
	  if (DEBUG)
	    printf("None of that card left, sorry!\n");

	  if (DEBUG){
	    printf("Cards Left: %d\n", supplyCount(choice1, state));
	  }
	}
	else if (state->coins < getCost(choice1)){
	  printf("That card is too expensive!\n");

	  if (DEBUG){
	    printf("Coins: %d < %d\n", state->coins, getCost(choice1));
	  }
	}
	else{

	  if (DEBUG){
	    printf("Deck Count: %d\n", state->handCount[currentPlayer] + state->deckCount[currentPlayer] + state->discardCount[currentPlayer]);
	  }

	  gainCard(choice1, state, 0, currentPlayer);//Gain the card
	  x = 0;//No more buying cards

	  if (DEBUG){
	    printf("Deck Count: %d\n", state->handCount[currentPlayer] + state->deckCount[currentPlayer] + state->discardCount[currentPlayer]);
	  }

	}
      }     

      //Reset Hand
      for (i = 0; i <= state->handCount[currentPlayer]; i++){
	state->hand[currentPlayer][i] = temphand[i];
	temphand[i] = -1;
      }
      //Reset Hand
      			
      return 0;
			
    case gardens:
      return -1;
			
    case mine:
      j = state->hand[currentPlayer][choice1];  //store card we will trash

      if (state->hand[currentPlayer][choice1] < copper || state->hand[currentPlayer][choice1] > gold)
	{
	  return -1;
	}
		
      if (choice2 > treasure_map || choice2 < curse)
	{
	  return -1;
	}

      if ( (getCost(state->hand[currentPlayer][choice1]) + 3) > getCost(choice2) )
	{
	  return -1;
	}

      gainCard(choice2, state, 2, currentPlayer);

      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);

      //discard trashed card
      for (i = 0; i < state->handCount[currentPlayer]; i++)
	{
	  if (state->hand[currentPlayer][i] == j)
	    {
	      discardCard(i, currentPlayer, state, 0);			
	      break;
	    }
	}
			
      return 0;
			
    case remodel:
      j = state->hand[currentPlayer][choice1];  //store card we will trash

      if ( (getCost(state->hand[currentPlayer][choice1]) + 2) > getCost(choice2) )
	{
	  return -1;
	}

      gainCard(choice2, state, 0, currentPlayer);

      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);

      //discard trashed card
      for (i = 0; i < state->handCount[currentPlayer]; i++)
	{
	  if (state->hand[currentPlayer][i] == j)
	    {
	      discardCard(i, currentPlayer, state, 0);			
	      break;
	    }
	}


      return 0;
		
    case smithy:
      //+3 Cards
      for (i = 0; i < 3; i++)
	{
	  drawCard(currentPlayer, state);
	}
			
      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case village:
      //+1 Card
      drawCard(currentPlayer, state);
			
      //+2 Actions
      state->numActions = state->numActions + 2;
			
      //discard played card from hand
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case baron:
      state->numBuys++;//Increase buys by 1!
      if (choice1 > 0){//Boolean true or going to discard an estate
	int p = 0;//Iterator for hand!
	int card_not_discarded = 1;//Flag for discard set!
	while(card_not_discarded){
	  if (state->hand[currentPlayer][p] == estate){//Found an estate card!
	    state->coins += 4;//Add 4 coins to the amount of coins
	    state->discard[currentPlayer][state->discardCount[currentPlayer]] = state->hand[currentPlayer][p];
	    state->discardCount[currentPlayer]++;
	    for (;p < state->handCount[currentPlayer]; p++){
	      state->hand[currentPlayer][p] = state->hand[currentPlayer][p+1];
	    }
	    state->hand[currentPlayer][state->handCount[currentPlayer]] = -1;
	    state->handCount[currentPlayer]--;
	    card_not_discarded = 0;//Exit the loop
	  }
	  else if (p > state->handCount[currentPlayer]){
	    if(DEBUG) {
	      printf("No estate cards in your hand, invalid choice\n");
	      printf("Must gain an estate if there are any\n");
	    }
	    if (supplyCount(estate, state) > 0){
	      gainCard(estate, state, 0, currentPlayer);
	      state->supplyCount[estate]--;//Decrement estates
	      if (supplyCount(estate, state) == 0){
		isGameOver(state);
	      }
	    }
	    card_not_discarded = 0;//Exit the loop
	  }
			    
	  else{
	    p++;//Next card
	  }
	}
      }
			    
      else{
	if (supplyCount(estate, state) > 0){
	  gainCard(estate, state, 0, currentPlayer);//Gain an estate
	  state->supplyCount[estate]--;//Decrement Estates
	  if (supplyCount(estate, state) == 0){
	    isGameOver(state);
	  }
	}
      }
	    
      
      return 0;
		
    case great_hall:
      //+1 Card
      drawCard(currentPlayer, state);
			
      //+1 Actions
      state->numActions++;
			
      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case minion:
      //+1 action
      state->numActions++;
			
      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);
			
      if (choice1)		//+2 coins
	{
	  state->coins = state->coins + 2;
	}
			
      else if (choice2)		//discard hand, redraw 4, other players with 5+ cards discard hand and draw 4
	{
	  //discard hand
	  while(numHandCards(state) > 0)
	    {
	      discardCard(handPos, currentPlayer, state, 0);
	    }
				
	  //draw 4
	  for (i = 0; i < 4; i++)
	    {
	      drawCard(currentPlayer, state);
	    }
				
	  //other players discard hand and redraw if hand size > 4
	  for (i = 0; i < state->numPlayers; i++)
	    {
	      if (i != currentPlayer)
		{
		  if ( state->handCount[i] > 4 )
		    {
		      //discard hand
		      while( state->handCount[i] > 0 )
			{
			  discardCard(handPos, i, state, 0);
			}
							
		      //draw 4
		      for (j = 0; j < 4; j++)
			{
			  drawCard(i, state);
			}
		    }
		}
	    }
				
	}
      return 0;
		
    case steward:
      if (choice1 == 1)
	{
	  //+2 cards
	  drawCard(currentPlayer, state);
	  drawCard(currentPlayer, state);
	}
      else if (choice1 == 2)
	{
	  //+2 coins
	  state->coins = state->coins + 2;
	}
      else
	{
	  //trash 2 cards in hand
	  discardCard(choice2, currentPlayer, state, 1);
	  discardCard(choice3, currentPlayer, state, 1);
	}
			
      //discard card from hand
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case tribute:
      if ((state->discardCount[nextPlayer] + state->deckCount[nextPlayer]) <= 1){
	if (state->deckCount[nextPlayer] > 0){
	  tributeRevealedCards[0] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
	  state->deckCount[nextPlayer]--;
	}
	else if (state->discardCount[nextPlayer] > 0){
	  tributeRevealedCards[0] = state->discard[nextPlayer][state->discardCount[nextPlayer]-1];
	  state->discardCount[nextPlayer]--;
	}
	else{
	  //No Card to Reveal
	  if (DEBUG){
	    printf("No cards to reveal\n");
	  }
	}
      }
	    
      else{
	if (state->deckCount[nextPlayer] == 0){
	  for (i = 0; i < state->discardCount[nextPlayer]; i++){
	    state->deck[nextPlayer][i] = state->discard[nextPlayer][i];//Move to deck
	    state->deckCount[nextPlayer]++;
	    state->discard[nextPlayer][i] = -1;
	    state->discardCount[nextPlayer]--;
	  }
			    
	  shuffle(nextPlayer,state);//Shuffle the deck
	} 
	tributeRevealedCards[0] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
	state->deck[nextPlayer][state->deckCount[nextPlayer]--] = -1;
	state->deckCount[nextPlayer]--;
	tributeRevealedCards[1] = state->deck[nextPlayer][state->deckCount[nextPlayer]-1];
	state->deck[nextPlayer][state->deckCount[nextPlayer]--] = -1;
	state->deckCount[nextPlayer]--;
      }    
		       
      if (tributeRevealedCards[0] == tributeRevealedCards[1]){//If we have a duplicate card, just drop one 
	state->playedCards[state->playedCardCount] = tributeRevealedCards[1];
	state->playedCardCount++;
	tributeRevealedCards[1] = -1;
      }

      for (i = 0; i <= 2; i ++){
	if (tributeRevealedCards[i] == copper || tributeRevealedCards[i] == silver || tributeRevealedCards[i] == gold){//Treasure cards
	  state->coins += 2;
	}
		    
	else if (tributeRevealedCards[i] == estate || tributeRevealedCards[i] == duchy || tributeRevealedCards[i] == province || tributeRevealedCards[i] == gardens || tributeRevealedCards[i] == great_hall){//Victory Card Found
	  drawCard(currentPlayer, state);
	  drawCard(currentPlayer, state);
	}
	else{//Action Card
	  state->numActions = state->numActions + 2;
	}
      }
	    
      return 0;
		
    case ambassador:
      j = 0;		//used to check if player has enough cards to discard

      if (choice2 > 2 || choice2 < 0)
	{
	  return -1;				
	}

      if (choice1 == handPos)
	{
	  return -1;
	}

      for (i = 0; i < state->handCount[currentPlayer]; i++)
	{
	  if (i != handPos && i == state->hand[currentPlayer][choice1] && i != choice1)
	    {
	      j++;
	    }
	}
      if (j < choice2)
	{
	  return -1;				
	}

      if (DEBUG) 
	printf("Player %d reveals card number: %d\n", currentPlayer, state->hand[currentPlayer][choice1]);

      //increase supply count for choosen card by amount being discarded
      state->supplyCount[state->hand[currentPlayer][choice1]] += choice2;
			
      //each other player gains a copy of revealed card
      for (i = 0; i < state->numPlayers; i++)
	{
	  if (i != currentPlayer)
	    {
	      gainCard(state->hand[currentPlayer][choice1], state, 0, i);
	    }
	}

      //discard played card from hand
      discardCard(handPos, currentPlayer, state, 0);			

      //trash copies of cards returned to supply
      for (j = 0; j < choice2; j++)
	{
	  for (i = 0; i < state->handCount[currentPlayer]; i++)
	    {
	      if (state->hand[currentPlayer][i] == state->hand[currentPlayer][choice1])
		{
		  discardCard(i, currentPlayer, state, 1);
		  break;
		}
	    }
	}			

      return 0;
		
    case cutpurse:

      updateCoins(currentPlayer, state, 2);
      for (i = 0; i < state->numPlayers; i++)
	{
	  if (i != currentPlayer)
	    {
	      for (j = 0; j < state->handCount[i]; j++)
		{
		  if (state->hand[i][j] == copper)
		    {
		      discardCard(j, i, state, 0);
		      break;
		    }
		  if (j == state->handCount[i])
		    {
		      for (k = 0; k < state->handCount[i]; k++)
			{
			  if (DEBUG)
			    printf("Player %d reveals card number %d\n", i, state->hand[i][k]);
			}	
		      break;
		    }		
		}
					
	    }
				
	}				

      //discard played card from hand
      discardCard(handPos, currentPlayer, state, 0);			

      return 0;

		
    case embargo: 
      //+2 Coins
      state->coins = state->coins + 2;
			
      //see if selected pile is in play
      if ( state->supplyCount[choice1] == -1 )
	{
	  return -1;
	}
			
      //add embargo token to selected supply pile
      state->embargoTokens[choice1]++;
			
      //trash card
      discardCard(handPos, currentPlayer, state, 1);		
      return 0;
		
    case outpost:
      //set outpost flag
      state->outpostPlayed++;
			
      //discard card
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case salvager:
      //+1 buy
      state->numBuys++;
			
      if (choice1)
	{
	  //gain coins equal to trashed card
	  state->coins = state->coins + getCost( handCard(choice1, state) );
	  //trash card
	  discardCard(choice1, currentPlayer, state, 1);	
	}
			
      //discard card
      discardCard(handPos, currentPlayer, state, 0);
      return 0;
		
    case sea_hag:
      for (i = 0; i < state->numPlayers; i++){
	if (i != currentPlayer){
	  state->discard[i][state->discardCount[i]] = state->deck[i][state->deckCount[i]--];			    state->deckCount[i]--;
	  state->discardCount[i]++;
	  state->deck[i][state->deckCount[i]--] = curse;//Top card now a curse
	}
      }
      return 0;
		
    case treasure_map:
      //search hand for another treasure_map
      index = -1;
      for (i = 0; i < state->handCount[currentPlayer]; i++)
	{
	  if (state->hand[currentPlayer][i] == treasure_map && i != handPos)
	    {
	      index = i;
	      break;
	    }
	}
      if (index > -1)
	{
	  //trash both treasure cards
	  discardCard(handPos, currentPlayer, state, 1);
	  discardCard(index, currentPlayer, state, 1);

	  //gain 4 Gold cards
	  for (i = 0; i < 4; i++)
	    {
	      gainCard(gold, state, 1, currentPlayer);
	    }
				
	  //return success
	  return 1;
	}
			
      //no second treasure_map found in hand
      return -1;
    }
	
  return -1;
}
int main() {
	int k[10] = {0,0,0,0,0,0,0,0,0,0};
	time_t t;
	srand((unsigned) time(&t));	
	int i, j, players, seed;
	struct gameState state;
        int card = 0;
	FILE *fp;
	char name[100];
	//Set number of players between two and four	
	players = rand() % 3 + 2;
	state.numPlayers = players;	
	  
	  //set random seed 
	seed = rand();		
	
	 //intialize kingdom cards and supply
	int same = 0;
	for(i = 0; i < 10; i++){
		card = rand() % 20 + adventurer;
		for(j = 0; j < 10; j++){
			if(card == k[j]){
				same = 1;
				break;
			}
			else{
			 same = 0;	
			}	
		}
		if(same){
			i--;	
		} 	
		else{
			k[i] = card;
		}
	}
		
	initializeGame(players, k, seed,&state);
	fp = fopen("gameResults.out", "w");
	fprintf(fp,"After Initalization\n");
	printf("After Initalization\n");
	fclose(fp);


	int hc = 0;
	struct trackers track; 

	int cardToBuy = 0;
	int choice1, choice2, choice3;
	int coinBonus;	
	while(!isGameOver(&state)){
		coinBonus = 0;	
		fp = fopen("gameResults.out", "a");
		printf("--------TURN %d--------\n",track.turns);	
		fprintf(fp, "--------TURN %d--------\n",track.turns);
//		print_Supply(&state);	
		fclose(fp);

		track.hasAction = 0;
		track.ableToPlay = -1;
		track.cardPlace = 0;
		card = 0;
	
		i = state.whoseTurn;
		track.hasAction = 0;
		printPlayerInfo(i,&state);
		printPlayerInfoGR(i,&state);
		printGameInfo(i,&state);
		
		//Determine if they have an action
		for(j = 0; j < state.handCount[i]; j++){
			card = state.hand[i][j];
			if(card >= adventurer && card <= treasure_map){
				track.hasAction = 1;
				track.cardPlace = j;
				cardNumToName(card,name);
//				printf("%s is >= %d and <= %d\n",name, adventurer, treasure_map);
				break;
			}
	//		printf("Looking at card %d\n",card);
		}
		
		if(track.hasAction){
			printf("Player has at least one action card to start\n");
			fp = fopen("gameResults.out", "a");
			fprintf(fp,"Player has at least one action card to start\n");
			fclose(fp);
		}	
		//play Actions
		while (state.numActions > 0 && track.hasAction){
			choice1 = rand() % state.handCount[i];
			choice2 = rand() % 20 + adventurer;
			choice3 = rand() % 20 + adventurer;
					

			printf("Card to Play %s\n",name);
			fp = fopen("gameResults.out", "a");
			fprintf(fp,"Card to play: %s\n",name);
			fclose(fp);

			//protection against the feast infinite loop
	//		if(card == feast){
	//			printf("You are trying to play a feast. There is an infinite loop in most dominion implementations\n");
	//			discardCard(track.cardPlace,i, &state,0); 
	//		}
			//try to play the card
	//		else{	
				track.ableToPlay = playCard(track.cardPlace,choice1,choice2,choice3,&state);
	//		}
			//the card will return a -1 if their is an error
			//trying to play the card  
			if(track.ableToPlay == -1){
				printf("You cannot play that card\n");
				fp = fopen("gameResults.out", "a");
				fprintf(fp,"You cannot play that card: %d\n",state.numActions);
				fclose(fp);
				discardCard(track.cardPlace,i, &state,0); 
			}

			printf("Actions after playing card %d\n",state.numActions);	
			fp = fopen("gameResults.out", "a");
			fprintf(fp,"Game after playing card: %d\n",state.numActions);
			fclose(fp);

			printf("Hand after: ");
			for(hc = 0; hc < state.handCount[i]; hc++){
				printf("%d, ",state.hand[i][hc]);	
			}
			printf("\n");
			//don't keep looking for actions if their used up	
			if (state.numActions <= 0){
				break;
			}
			//otherwise, try to look for another action to play
			card = lookForAction(i,&state, &track);
			cardNumToName(card,name);	
			if(track.hasAction){
				printf("Player has at least one action card\n");
			}	
		}

		coinBonus = state.coins;
		fp = fopen("gameResults.out", "a");
		fprintf(fp,"Player %d has %d coin now\n",i+1,state.coins);
		fclose(fp);
		tryBuyCard(&state);
		fp = fopen("gameResults.out", "a");
		fprintf(fp,"Player %d has %d coin now after buy\n",i+1,state.coins);
		fclose(fp);
		//Entering the Buy Phase
		//pick a card to try to buy
	
		//Game info after the play 
		printPlayerInfoGR(i,&state);
		printGameInfo(i,&state);
		fp = fopen("gameResults.out", "a");
		for(i = 0; i < state.numPlayers; i ++){
			fprintf(fp,"score for player %d: %d\n",i+1, scoreFor(i, &state));
			track.estateScore = track.duchyScore = track.provinceScore = 0;
			for(j = 0; j < state.handCount[i]; j++){
				card  = state.hand[i][j];
				if(card == estate){
					track.estateScore ++;
				}
				else if(card == duchy){
					track.duchyScore ++;
				}
				else if(card == province){
					track.provinceScore ++;
				}	
			} 	
			for(j = 0; j < state.deckCount[i]; j++){
				card  = state.deck[i][j];
				if(card == estate){
					track.estateScore ++;
				}
				else if(card == duchy){
					track.duchyScore ++;
				}
				else if(card == province){
					track.provinceScore ++;
				}
			}	
			for(j = 0; j < state.discardCount[i]; j++){
				card  = state.discard[i][j];
				if(card == estate){
					track.estateScore ++;
				}
				else if(card == duchy){
					track.duchyScore ++;
				}
				else if(card == province){
					track.provinceScore ++;
				}	
			}
			fprintf(fp,"estate %d, duchy %d, province %d \n",track.estateScore, track.duchyScore, track.provinceScore);
		}
		fclose(fp);		
	  


		//turn is over
		endTurn(&state);
		track.turns ++;
	}
	//when the Game is Over, print out the scores		
	for(i = 0; i < state.numPlayers; i ++){
		printf("score for player %d: %d\n",i+1, scoreFor(i, &state));	
	}		
	  
	printf("Tests Complete\n");
	return 0;
}