Exemplo n.º 1
0
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, smithy};
	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) == great_hall)
				{
				test1=numHandCards(&G);
				playCard(i, -1, -1, -1, &G);
				//assert(test1==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;
				}
		}	
		
		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 >= 3) 
		{ 
			buyCard(great_hall, &G);
		}*/
		else if (money >= 3) 
		{ 
			buyCard(silver, &G);
		}
		endTurn(&G);
	}

	
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
int testEndTurn() {
	char* UNITTEST = "endTurn()";
    int i;
    int seed = 1000;
    int numPlayers = 3;
    int thisPlayer = 0;
	struct gameState G, testG, realG;
	int k[10] = {adventurer, remodel, village, ambassador, mine, cutpurse,
			sea_hag, tribute, smithy, council_room};
	int prevHandCount;
	int prevDiscardCount;

	// Initialize the game state and player cards
	initializeGame(numPlayers, k, seed, &G);
	
	printf("\n\n----------------- Testing of Function: %s ----------------\n", UNITTEST);

	/****************************************************
	Test 1: First player has 5 cards in hand when turn ends
	****************************************************/
	printf("\nTEST 1: First player has 5 cards in hand when turn ends \n");
	
	// Store previous hand count and previous discard count
	prevHandCount = G.handCount[thisPlayer];
	printf("prevHandCount of previous player = %d\n", prevHandCount);
	prevDiscardCount = G.discardCount[thisPlayer];
	printf("prevDiscard of previous player = %d\n", prevDiscardCount);
	
	// End the first player's turn
	endTurn(&G);
	thisPlayer = (thisPlayer + 1)%3;
	
	// At the end of the turn, it should be the next player's hand
	printf("whoseTurn = %d, expected = %d\n", whoseTurn(&G), 1);
	assert(whoseTurn(&G) == 1);
	
	// The hand count of the previous player should be 0
	printf("handCount of previous player = %d, expected = %d\n", G.handCount[(thisPlayer-1)%numPlayers], 0);
	assert (G.handCount[(thisPlayer-1)%numPlayers] ==  0);
	
	// The discard count of the previous player should increase by the previous hand count
	printf("discardCount of previous player= %d, expected = %d\n", G.discardCount[(thisPlayer-1)%numPlayers], prevHandCount + prevDiscardCount);
	assert (G.discardCount[(thisPlayer-1)%numPlayers] ==  prevHandCount + prevDiscardCount);
	
	// The hand count of the current player should be 5
	printf("handCount of current player = %d, expected = %d\n", G.handCount[thisPlayer], 5);
	assert (G.handCount[thisPlayer] ==  5);
	
	

	/****************************************************
	Test 2: Second player has 3 cards in hand when turn ends
	****************************************************/	
	printf("\nTEST 2: Second player has 3 cards in hand when turn ends \n");

	// Make sure that the second player has only 3 cards in hand
	G.handCount[thisPlayer] -=2;
	G.hand[thisPlayer][4] = -1;
	G.hand[thisPlayer][3] = -1;
	
	// Store previous hand count and previous discard count
	prevHandCount = G.handCount[thisPlayer];
	printf("prevHandCount of previous player = %d\n", prevHandCount);
	prevDiscardCount = G.discardCount[thisPlayer];
	printf("prevDiscard of previous player = %d\n", prevDiscardCount);
	
	// End the second player's turn
	endTurn(&G);
	thisPlayer = (thisPlayer + 1)%3;
	
	// At the end of the turn, it should be the next player's hand
	printf("whoseTurn = %d, expected = %d\n", whoseTurn(&G), thisPlayer);
	assert(whoseTurn(&G) == thisPlayer);
	
	// The hand count of the previous player should be 0
	printf("handCount of previous player = %d, expected = %d\n", G.handCount[(thisPlayer-1)%numPlayers], 0);
	assert (G.handCount[(thisPlayer-1)%numPlayers] ==  0);
	
	// The discard count of the previous player should increase by the previous hand count
	printf("discardCount of previous player= %d, expected = %d\n", G.discardCount[(thisPlayer-1)%numPlayers], prevHandCount + prevDiscardCount);
	assert (G.discardCount[(thisPlayer-1)%numPlayers] ==  prevHandCount + prevDiscardCount);
	
	// The hand count of the current player should be 5
	printf("handCount of current player = %d, expected = %d\n", G.handCount[thisPlayer], 5);
	assert (G.handCount[thisPlayer] ==  5);
	

	/****************************************************
	Test 3: Third player has no cards in hand when turn ends
	****************************************************/	
	printf("\nTEST 3: Third player has no cards in hand when turn ends \n");

	// Make sure that the second player has only 3 cards in hand
	G.handCount[thisPlayer] -=5;
	for (i = 0; i < G.handCount[thisPlayer]; i++){
		G.hand[thisPlayer][i] = -1;
	}

	// Store previous hand count and previous discard count
	prevHandCount = G.handCount[thisPlayer];
	printf("prevHandCount of previous player = %d\n", prevHandCount);
	prevDiscardCount = G.discardCount[thisPlayer];
	printf("prevDiscard of previous player = %d\n", prevDiscardCount);
	
	// End the third player's turnf
	endTurn(&G);
	thisPlayer = (thisPlayer + 1)%3;
	
	// At the end of the turn, it should be the next player's hand
	printf("whoseTurn = %d, expected = %d\n", whoseTurn(&G), thisPlayer);
	assert(whoseTurn(&G) == thisPlayer);
	
	// The hand count of the previous player should be 0
	printf("handCount of previous player = %d, expected = %d\n", G.handCount[(thisPlayer-1)%numPlayers], 0);
	assert (G.handCount[(thisPlayer-1)%numPlayers] ==  0);
	
	// The discard count of the previous player should increase by the previous hand count
	printf("discardCount of previous player= %d, expected = %d\n", G.discardCount[(thisPlayer-1)%numPlayers], prevHandCount + prevDiscardCount);
	assert (G.discardCount[(thisPlayer-1)%numPlayers] ==  prevHandCount + prevDiscardCount);
	
	// The hand count of the current player should be 5
	printf("handCount of current player = %d, expected = %d\n", G.handCount[thisPlayer], 5);
	assert (G.handCount[thisPlayer] ==  5);
	
	
	printf("\n >>>>> SUCCESS: Testing complete of %s <<<<<\n\n\n", UNITTEST);


	return 0;
}
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;
}
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;

}
int main (int argc, char** argv) {
  struct gameState G;
  struct gameState *p = &G;
  int *k,i, players, money = 0, chosenCard=0, playedCard=0, flag=0, buyc=0;
  

  printf ("Starting game.\n");
  k=randomKingdom();
 /* for(i=0; i<10; i++){
    printf("%d\n",k[i]);
  }*/
  players = rand() % 2 + 2;

  initializeGame(players, k, rand(), p);
  

  while (!isGameOver(p)) { //loop for every turn
    printf("#Player%d's turn ###################################\n", whoseTurn(p)+1);
    money = 0;

    printf("-Check hand for treaure.\n");
    for (i = 0; i < numHandCards(p); i++) { //check if the current player has any treasures
      if (handCard(i, p) == copper){
        money++;
        printf("get 1 coin with copper\n");
      }
      else if (handCard(i, p) == silver){
        money += 2;
        printf("get 2 coins with silver\n");
      }
      else if (handCard(i, p) == gold){
        money += 3;
        printf("get 3 coins with gold\n");

      }
    }
    printf("Current coins: %d\n", money);
    printf("Current num of cards (before) %d\n", p->handCount[whoseTurn(p));
      flag=1;
      for(i=0; i< p->handCount[whoseTurn(p)]; i++){
        if (p->hand[whoseTurn(p)][i]!=copper && p->hand[whoseTurn(p)][i] != silver && p->hand[whoseTurn(p)][i] != gold)
          flag=0;
      }
      if(flag==0){ //play a card
        while (flag==0){
          chosenCard=rand() % p->handCount[whoseTurn(p)];
          if (chosenCard!=copper && chosenCard != silver && chosenCard != gold)
            flag=1;
        }
        flag=0;

        playedCard=p->hand[whoseTurn(p)][chosenCard];
          printf("%s played from position %d\n", whichCard(playedCard), chosenCard); 
  	      playCard(chosenCard, 1, 1, 1, p); 
  	      printf("%s played.\n", whichCard(playedCard));
          printf("Current num of cards (after) %d\n", p->handCount[whoseTurn(p));
          if(playedCard==22){
            //printf("\n\n\nembargo\n\n\n\n\n");
            embargoTest(G, whoseTurn(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++;
  	     }
        }
      

      buyc=random()%2-2;
      if(buyc==-1){
        int x=0;
        x=random()%3;
        if(x==0)
          buyc=copper;
        else if(x==1)
          buyc=silver;
        else
          buyc=gold;
      }
      else{
        buyc = k[random()%10];
      }
      printf("attempting to buy %s\n", whichCard(buyc)); 
      buyCard(buyc, p);
      


      printf ("Player %d's current score: %d\n", whoseTurn(p)+1, scoreFor(whoseTurn(p), p));
      printf("Player %d's turn ends.\n", whoseTurn(p)+1);
	    endTurn(p);

  } // end of While

  printf ("Finished game.\n");
  printf(":FINAL RESULT:::::::::::::::::::::::::::::::\n");
  for (i=0; i<players; i++){
    printf ("Player %d's final score: %d\n", i+1, scoreFor(i, p));
  }

  return 0;
}
Exemplo n.º 7
0
int main(int argc, char** argv){

    int i, j, random, boolean, players, temp, money, act_x, vil_x, action, buy;

    if(argc != 2)
    {
        printf("Usage: ./testdominion [random seed]");
        return 0;
    }
    else
    {
        random = atoi(argv[1]);
    }

    srand(random);

    struct gameState g;
    struct gameState* p = &g;

    int k[10];

    for(i = 0; i < 10; i++)
    {
        temp = (rand() % 20) + 7; 
        boolean = 1;
        for(j = 0; j < i; j++)
            if(k[j] == temp)
                boolean = 0;
        if(boolean)     //If the number does not already exist
            k[i] = temp;
        else            //If the number already exists in the array
            i--;
    }

    int not_bought[10];
    int not_played[10];

    for(i = 0; i < 10; i++)
    {
        not_bought[i] = k[i];
        not_played[i] = k[i];
    }

    players = (rand() % 3) + 2;

    int r = initializeGame(players, k, random, p);

    while(!isGameOver(p))        //Loop that continues for game
    {
        for(i = 0; i < players; i++)
        {                                           //Loop the loops through each player
            action = 1;
            buy = 1;
            money = 0;            
            while(action)             //Loop that continues for each players actions
            {
                vil_x = -1;
                act_x = -1;
                for(j = 0; j < numHandCards(p); j++)    //Loop runs through hand
                {
                    if(handCard(j, p) == village)
                    {
                        vil_x = j;
                        break;
                    }
                    else if(!played(not_played, p, handCard(j, p)))
                        act_x = j;
                }
                if(vil_x > -1)
                {
                    playCard(j, -1, -1, -1, p);
                    printf("Player %d played a village\n", j);
                }
                else if(act_x > -1)
                {
                    play(act_x, p);
                    action = 0;
                }
                else
                    action = 0;
            }

            buy_card(p, not_bought, k);

            endTurn(p);
        }
    }
    print_state(p, players);
    printf("\n");
    return 0;
}
Exemplo n.º 8
0
//oracle makes sure returns valid 
int unitTest(struct gameState *post){
    srand(time(NULL));

    //define variables
    int success;
    struct gameState pre;
    memcpy(&pre,post,sizeof(struct gameState));

    //call function
    success=endTurn(post);

    //memcmp game state size
    assert (memcmp(&pre,post, sizeof(struct gameState))==0);


    if( post->playedCardCount != 0){
        #if(NOISY_TEST == 1)
        printf("played card count is not 0 \n");
        #endif
    }

    if(post->handCount[post->whoseTurn] == 0){
        #if(NOISY_TEST == 1)
        printf("Hand count was never increased!\n");
        #endif
    }

    //check to see if whose turn it is has changed
    if(post->numPlayers > 1){
        if( post->whoseTurn != pre.whoseTurn ){
            #if (NOISY_TEST == 1)
            printf ("current players turn has not properly changed.\n");
            #endif
            return 1;
        }
    }

    if (success == -1){
        #if (NOISY_TEST == 1)
        printf ("Error in end turn function.\n");
        #endif
        return 2;
    }

    if(post->outpostPlayed != 0){
		#if (NOISY_TEST ==1)
    	printf("current player not correctly updated, outposts played greater than 0");
		#endif
    }
    if(post->phase != 0){
    	#if (NOISY_TEST ==1)
    	printf("phase correctly updated");
		#endif
    }

    if(post->numActions != 1){
    	#if (NOISY_TEST ==1)
    	printf("current player not correctly updated, outposts played greater than 0");
		#endif
    }

    if(post->numBuys != 1){
    	#if (NOISY_TEST ==1)
    	printf("current player not correctly updated, outposts played greater than 0");
		#endif
    }

    return 0;
}
Exemplo n.º 9
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;
}
int TakeTurn(FILE* fp, int currPlayer, struct gameState* pre, struct gameState* post, int* pass, int* fail){
	int errCount = 0;
	fprintf(fp,"Starting turn for player %d\n", currPlayer);
	fprintf(fp,"Printing GameState Below\n");
	dumpState(fp,pre);
	
	while(numActionCard(currPlayer,pre))
	{
		if(pre->numActions <= 0){break;}
		int NthActCard = (rand() % numActionCard(currPlayer,pre)) + 1;
		int cardPosToPlay = idxNthActCard(currPlayer,pre,NthActCard);
		int cardToPlay = pre->hand[currPlayer][cardPosToPlay];
		
		//make SURE we did this right--Remove this later if it all works
		if(cardToPlay>=adventurer && cardToPlay <= treasure_map)
		{
		
			//REALLY didn't want to do this, but seems to be the only reasonable way to deal with the choice-based cards.
			switch(cardToPlay)
			{
				case feast: ;
					int cardToBuy;
					cardToBuy = randAffordableCard(5,pre);
					if(cardToBuy == -1)
					{
						if(numActionCard(currPlayer,pre) < 2)
						{
							pre->numActions = 0;
							break;
						}
						break;
					}
					fprintf(fp, "Player %d is playing a feast, to buy a %s\n",currPlayer, cardnames[cardToBuy]);
					errCount += fassert(playCard(cardPosToPlay,cardToBuy,0,0,post) == 0, pass, fail, "PlayCard returned 0");
					break;
				
				case mine:
					if(hasHandCard(currPlayer,silver,pre))
					{
						fprintf(fp, "Player %d is using mine to trash a silver for a gold\n",currPlayer);
						errCount+= fassert(playCard(cardPosToPlay,hasHandCard(currPlayer,silver,pre),gold,0,post) == 0, pass, fail, "PlayCard returned 0");
						break;
					}
					else if(hasHandCard(currPlayer,copper,pre))
					{
						fprintf(fp, "Player %d is using mine to trash a copper for a silver\n",currPlayer);
						errCount+= fassert(playCard(cardPosToPlay,hasHandCard(currPlayer,copper,pre),silver,0,post) == 0,pass,fail,"PlayCard returned 0");
						break;
					}
					else
					{
						//hrmm, if we aren't holding a treasure card, we can't play--dont want to stick us in an infinite loop,
						//so going to cheat a bit here, THIS IS ONLY TO PREVENT INFINITE LOOPS IF MINE IS THE ONLY ACTION CARD IN HAND
						//Also, there is no point in checking to trash a gold for another gold. Totally redundant. This covers both cases
						if(numActionCard(currPlayer,pre) <= 1){pre->numActions = 0;}
						break;
					}
				case remodel:
					if(pre->handCount[currPlayer] > 1)
					{
						int cardToTrash;
						while(1)
						{
							cardToTrash = rand() % pre->handCount[currPlayer];
							if(cardToTrash!=cardPosToPlay){break;}
						}
						int trashValue = getCost(cardToTrash);
						int cardToBuy = randAffordableCard(trashValue+2,pre);
						if(cardToBuy == -1)
						{
							if(numActionCard(currPlayer,pre) < 2)
							{
								pre->numActions = 0;
								break;
							}
							break;
						}
						fprintf(fp, "Player %d is using remodel to trash a %s and gain a %s\n",currPlayer,cardnames[pre->hand[currPlayer][cardToTrash]],cardnames[cardToBuy]);
						errCount += fassert(playCard(cardPosToPlay,cardToTrash,cardToBuy,0,post) == 0,pass,fail,"PlayCard Returned 0");
						break;
					}
					else
					{
						//only card is remodel, so we can't play--set actions to 0
						pre->numActions = 0;
						break;
					}
				case baron:
					if(hasHandCard(currPlayer, estate, pre))
					{
						if(rand()%2)
						{
							fprintf(fp,"Player %d is playing baron, discarding an estate\n",currPlayer);
							errCount+=fassert(playCard(cardPosToPlay,1,0,0,post)==0,pass,fail,"PlayCard returned 0");
							break;
						}
					}
					else
					{
						fprintf(fp,"Player %d is playing baron, not discarding estate\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,0,0,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
				case minion: ;
					int rChoice;
					rChoice = (rand() % 2)+1;
					if(rChoice == 1)
					{
						fprintf(fp, "Player %d is playing minion, chose +2 coin\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,1,0,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
					else
					{
						fprintf(fp, "Player %d is playing minion, chose redraw\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,0,1,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
				case steward:
					rChoice = (rand()%3)+1;
					if(rChoice == 1)
					{
						fprintf(fp, "Player %d is playing steward, chose +2 card\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,rChoice,0,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
					else if(rChoice == 2)
					{
						fprintf(fp, "Player %d is playing steward, chose +2 coin\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,rChoice,0,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
					else
					{
						fprintf(fp, "Player %d is playing steward, chose trash 2\n",currPlayer);
						errCount+=fassert(playCard(cardPosToPlay,rChoice,0,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
				case ambassador:
					if(pre->handCount[currPlayer]>1)
					{
						int cardToTrash;
						while(1)
						{
							cardToTrash = rand() % pre->handCount[currPlayer];
							if(cardToTrash!=cardPosToPlay){break;}
						}
						int numToReturn = (rand() % numCardX(currPlayer,pre->hand[currPlayer][cardToTrash],pre))+1;
						fprintf(fp,"Player %d is playing ambassador, trashing a %s, hoping to return %d\n",currPlayer,cardnames[pre->hand[currPlayer][cardToTrash]],numToReturn);
						errCount+=fassert(playCard(cardPosToPlay,cardToTrash,numToReturn,0,post)==0,pass,fail,"PlayCard returned 0");
						break;
					}
					else
					{
						if(numActionCard(currPlayer,pre) < 2)
						{
							pre->numActions = 0;
						}
						break;
					}
				case embargo: ;
					int cardToEmbargo;
					cardToEmbargo = randAffordableCard(999,pre);
					if(cardToBuy == -1)
					{
						if(numActionCard(currPlayer,pre) < 2)
						{
							pre->numActions = 0;
						}
						break;
					}
					fprintf(fp,"Player %d is playing embargo on %s\n",currPlayer,cardnames[cardToEmbargo]);
					errCount+=fassert(playCard(cardPosToPlay,cardToEmbargo,0,0,post)==0,pass,fail,"PlayCard Returned 0");
					break;
				case salvager:
					if(pre->handCount[currPlayer]>1)
					{
						int cardToTrash;
						while(1)
						{
							cardToTrash = rand() % pre->handCount[currPlayer];
							if(cardToTrash!=cardPosToPlay){break;}
						}
						fprintf(fp,"Player %d is playing salvager, trashing %s\n",currPlayer,cardnames[pre->hand[currPlayer][cardToTrash]]);
						errCount+=fassert(playCard(cardPosToPlay,cardToTrash,0,0,post)==0,pass,fail,"PlayCard Returned 0");
						break;
					}
					else
					{
						pre->numActions = 0;
						break;
					}
				default:		//all other cards don't need choices
					fprintf(fp,"Player %d is playing his %s\n",currPlayer,cardnames[cardToPlay]);
					errCount += fassert(playCard(cardPosToPlay,0,0,0, post) == 0, pass, fail, "PlayCard Returned 0");
					break;
			
			}
			errCount += SanityCheck(post,pass,fail,fp);
			if(!errCount)
			{
				fprintf(fp,"Player %d tried to play a card, gamestate is now\n",currPlayer);
				dumpState(fp,post);
				memcpy(pre,post,sizeof(struct gameState));
			}
			else{break;}
		}
		
	}
	
	//OK, now that we played all possible actions, we can try out buying stuff
	while(pre->numBuys > 0 && !errCount)		//doesn't matter how many coins we have, can still buy for ex: copper for 0c
	{
		if(pre->coins < 2)
		{
			int doibuy = rand() % 2;
			if(doibuy){break;}
		}
		
		
		
		int cardToBuy = randAffordableCard(pre->coins,pre);
		if(cardToBuy == -1)
		{
			pre->numBuys = 0;
			break;
		}
		buyCard(cardToBuy,post);
		fprintf(fp,"Player %d tried to buy a %s\n",currPlayer,cardnames[cardToBuy]);
		errCount+=SanityCheck(post,pass,fail,fp);
		if(!errCount)
		{
			//fprintf(fp,"gamestate is now\n");
			//dumpState(fp,post);
			memcpy(pre,post,sizeof(struct gameState));
		}
		else{break;}
	}
	
	
	
	if(!errCount)
	{
		fprintf(fp,"Ending turn for player %d\n", currPlayer);
		errCount += fassert(endTurn(post) == 0, pass, fail, "endTurn Returned 0");
		errCount += SanityCheck(post,pass, fail,fp);
		if(!errCount)
		{
			memcpy(pre,post,sizeof(struct gameState));
		}
	}
	fprintf(fp,"Gamestate at end of turn\n");
	dumpState(fp, post);
	return errCount;
	
	
}