コード例 #1
0
int getWinners(int players[MAX_PLAYERS], struct gameState *state) {
	int i;
	int j;
	int highScore;
	int currentPlayer;

	//get score for each player
	for (i = 0; i < MAX_PLAYERS; i++)
	{
		//set unused player scores to -9999
		if (i >= state->numPlayers)
		{
			players[i] = -9999;
		}
		else
		{
			players[i] = scoreFor(i, state);
		}
	}

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

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

	//find new highest score
 /* MUTANT (rep_const) */	j = 1;
	for (i = 0; i < MAX_PLAYERS; i++)
	{
		if (players[i] > players[j])
		{
			j = i;
		}
	}
	highScore = players[j];

	//set winners in array to 1 and rest to 0
	for (i = 0; i < MAX_PLAYERS; i++)
	{
		if (players[i] == highScore)
		{
			players[i] = 1;
		}
		else
		{
			players[i] = 0;
		}
	}

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


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

  int i;
  int score = 0;
  //score from hand
  for (i = 0; i < state->handCount[player]; i++)
    {
      if (state->hand[player][i] == curse) { score = score - 1; };
      if (state->hand[player][i] == estate) { score = score + 1; };
      if (state->hand[player][i] == duchy) { score = score + 3; };
      if (state->hand[player][i] == province) { score = score + 6; };
      if (state->hand[player][i] == great_hall) { score = score + 1; };
      if (state->hand[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); };
    }

  //score from discard
  for (i = 0; i < state->discardCount[player]; i++)
    {
      if (state->discard[player][i] == curse) { score = score - 1; };
      if (state->discard[player][i] == estate) { score = score + 1; };
      if (state->discard[player][i] == duchy) { score = score + 3; };
      if (state->discard[player][i] == province) { score = score + 6; };
      if (state->discard[player][i] == great_hall) { score = score + 1; };
      if (state->discard[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); };
    }

  //score from deck
  for (i = 0; i < state->discardCount[player]; i++)
    {
      if (state->deck[player][i] == curse) { score = score - 1; };
      if (state->deck[player][i] == estate) { score = score + 1; };
      if (state->deck[player][i] == duchy) { score = score + 3; };
      if (state->deck[player][i] == province) { score = score + 6; };
      if (state->deck[player][i] == great_hall) { score = score + 1; };
 /* MUTANT (del_stmt) */      if (state->deck[player][i] == gardens) { score = score + ( fullDeckCount(player, 0, state) / 10 ); /*  }; */ 
    }

  return score;
}

int getWinners(int players[MAX_PLAYERS], struct gameState *state) {
  int i;	
  int j;
  int highScore;
  int currentPlayer;

  //get score for each player
  for (i = 0; i < MAX_PLAYERS; i++)
    {
      //set unused player scores to -9999
      if (i >= state->numPlayers)
	{
	  players[i] = -9999;
	}
      else
	{
	  players[i] = scoreFor (i, state);
	}
    }

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

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

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

  //set winners in array to 1 and rest to 0
  for (i = 0; i < MAX_PLAYERS; i++)
    {
      if ( players[i] == highScore )
	{
	  players[i] = 1;
	}
      else
	{
	  players[i] = 0;
	}
    }

  return 0;
}
コード例 #4
0
ファイル: unittest4.c プロジェクト: cr8zd/cs362w16
int main() {

	printf("TEST: SCOREFOR\n\n");
	srand(RSEED);

	int players, numCurse, numEstate, numDuchy, numProvince, numGardens, numGreatHall, victoryCount, curseCount;
	players = rand() % (MAX_PLAYERS - 2) + 2;

	if (players == 2) {
		victoryCount = 8;
		curseCount = 10;
	}
	else if (players == 3) {
		victoryCount = 12;
		curseCount = 20;
	}
	else {
		victoryCount = 12;
		curseCount = 30;
	}


	struct gameState state;
	int cards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};
	customInit(players, cards, &state);
	printf("TEST: Cards in hand\n");

	int i, j, k;
	for (i = 0; i < players; i++) {
		printf("Player %d\n", i + 1);
		numCurse = rand() % (curseCount + 1);
		printf("Curse: %d\n", numCurse);
		numEstate = rand() % (victoryCount + 1);
		printf("Estate: %d\n", numEstate);
		numDuchy = rand() % (victoryCount + 1);
		printf("Duchy: %d\n", numDuchy);
		numProvince = rand() % (victoryCount + 1);
		printf("Province: %d\n", numProvince);
		numGardens = rand() % (victoryCount + 1);
		printf("Gardens: %d\n", numGardens);
		numGreatHall = rand() % (victoryCount + 1);
		printf("Great Hall: %d\n", numGreatHall);

		k = 0;
		j = 0;
		while (j < numCurse) {
			state.hand[i][k] = curse;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numEstate) {
			state.hand[i][k] = estate;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numDuchy) {
			state.hand[i][k] = duchy;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numProvince) {
			state.hand[i][k] = province;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGardens) {
			state.hand[i][k] = gardens;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGreatHall) {
			state.hand[i][k] = great_hall;
			state.handCount[i]++;
			j++;
			k++;
		}
		j = 0;

		int deck = state.deckCount[i] + state.handCount[i] + state.discardCount[i];
		printf("Total deck: %d\n", deck);
		int internalsum = internalPointsCalc(numCurse, numEstate, numDuchy, numProvince, numGardens, numGreatHall, deck);
		int score = scoreFor(i, &state);
		printf("Expected score: %d\n", internalsum);
		printf("Calculated score: %d\n", score);
		if (internalsum == score)
			printf("PASSED\n");
		else
			printf("FAILED\n");

	}

	customInit(players, cards, &state);
	printf("TEST: Cards in deck\n");
		for (i = 0; i < players; i++) {
		printf("Player %d\n", i + 1);
		numCurse = rand() % (curseCount + 1);
		printf("Curse: %d\n", numCurse);
		numEstate = rand() % (victoryCount + 1);
		printf("Estate: %d\n", numEstate);
		numDuchy = rand() % (victoryCount + 1);
		printf("Duchy: %d\n", numDuchy);
		numProvince = rand() % (victoryCount + 1);
		printf("Province: %d\n", numProvince);
		numGardens = rand() % (victoryCount + 1);
		printf("Gardens: %d\n", numGardens);
		numGreatHall = rand() % (victoryCount + 1);
		printf("Great Hall: %d\n", numGreatHall);

		k = state.deckCount[i];
		j = 0;
		while (j < numCurse) {
			state.deck[i][k] = curse;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numEstate) {
			state.deck[i][k] = estate;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numDuchy) {
			state.deck[i][k] = duchy;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numProvince) {
			state.deck[i][k] = province;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGardens) {
			state.deck[i][k] = gardens;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGreatHall) {
			state.deck[i][k] = great_hall;
			state.deckCount[i]++;
			j++;
			k++;
		}
		j = 0;

		
		int deck = state.deckCount[i] + state.handCount[i] + state.discardCount[i];
		printf("Total deck: %d\n", deck);
		int internalsum = internalPointsCalc(numCurse, numEstate, numDuchy, numProvince, numGardens, numGreatHall, deck);
		int score = scoreFor(i, &state);

		printf("Expected score: %d\n", internalsum);
		printf("Calculated score: %d\n", score);
		if (internalsum == score)
			printf("PASSED\n");
		else
			printf("FAILED\n");

	}

	customInit(players, cards, &state);
	printf("TEST: Cards in discard\n");
		for (i = 0; i < players; i++) {
		printf("Player %d\n", i + 1);
		numCurse = rand() % (curseCount + 1);
		printf("Curse: %d\n", numCurse);
		numEstate = rand() % (victoryCount + 1);
		printf("Estate: %d\n", numEstate);
		numDuchy = rand() % (victoryCount + 1);
		printf("Duchy: %d\n", numDuchy);
		numProvince = rand() % (victoryCount + 1);
		printf("Province: %d\n", numProvince);
		numGardens = rand() % (victoryCount + 1);
		printf("Gardens: %d\n", numGardens);
		numGreatHall = rand() % (victoryCount + 1);
		printf("Great Hall: %d\n", numGreatHall);

		k = 0;
		j = 0;
		while (j < numCurse) {
			state.discard[i][k] = curse;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numEstate) {
			state.discard[i][k] = estate;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numDuchy) {
			state.discard[i][k] = duchy;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numProvince) {
			state.discard[i][k] = province;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGardens) {
			state.discard[i][k] = gardens;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;
		while (j < numGreatHall) {
			state.discard[i][k] = great_hall;
			state.discardCount[i]++;
			j++;
			k++;
		}
		j = 0;

		
		int deck = state.deckCount[i] + state.handCount[i] + state.discardCount[i];
		printf("Total deck: %d\n", deck);
		int internalsum = internalPointsCalc(numCurse, numEstate, numDuchy, numProvince, numGardens, numGreatHall, deck);
		int score = scoreFor(i, &state);

		printf("Expected score: %d\n", internalsum);
		printf("Calculated score: %d\n", score);
		if (internalsum == score)
			printf("PASSED\n");
		else
			printf("FAILED\n");

	}

	customInit(players, cards, &state);
	printf("TEST: random distribution\n");
		for (i = 0; i < players; i++) {
		printf("Player %d\n", i + 1);
		numCurse = rand() % (curseCount + 1);
		printf("Curse: %d\n", numCurse);
		numEstate = rand() % (victoryCount + 1);
		printf("Estate: %d\n", numEstate);
		numDuchy = rand() % (victoryCount + 1);
		printf("Duchy: %d\n", numDuchy);
		numProvince = rand() % (victoryCount + 1);
		printf("Province: %d\n", numProvince);
		numGardens = rand() % (victoryCount + 1);
		printf("Gardens: %d\n", numGardens);
		numGreatHall = rand() % (victoryCount + 1);
		printf("Great Hall: %d\n", numGreatHall);

		for (j = 0; j < numCurse; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = curse;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = curse;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = curse;
				state.handCount[i]++;
			}
		}

		for (j = 0; j < numEstate; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = estate;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = estate;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = estate;
				state.handCount[i]++;
			}
		}

		for (j = 0; j < numDuchy; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = duchy;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = duchy;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = duchy;
				state.handCount[i]++;
			}
		}

		for (j = 0; j < numProvince; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = province;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = province;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = province;
				state.handCount[i]++;
			}
		}

		for (j = 0; j < numGardens; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = gardens;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = gardens;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = gardens;
				state.handCount[i]++;
			}
		}

		for (j = 0; j < numGreatHall; j++) {
			k = rand() % 3;
			if (k == 0) {
				state.deck[i][state.deckCount[i]] = great_hall;
				state.deckCount[i]++;
			}
			else if (k == 1) {
				state.discard[i][state.discardCount[i]] = great_hall;
				state.discardCount[i]++;
			}
			else {
				state.hand[i][state.handCount[i]] = great_hall;
				state.handCount[i]++;
			}
		}
		

		
		int deck = state.deckCount[i] + state.handCount[i] + state.discardCount[i];
		printf("Total deck: %d\n", deck);
		int internalsum = internalPointsCalc(numCurse, numEstate, numDuchy, numProvince, numGardens, numGreatHall, deck);
		int score = scoreFor(i, &state);

		printf("Expected score: %d\n", internalsum);
		printf("Calculated score: %d\n", score);
		if (internalsum == score)
			printf("PASSED\n");
		else
			printf("FAILED\n");

	}	
	return 0;
}
コード例 #5
0
int main(){

	srand(1245);

	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;
		ran = 0;
		while(p->numActions != 0)
		{
			cont = 0;
			for(j = 0; j < p->handCount[p->whoseTurn]; j++)
			{
				if((p->hand[p->whoseTurn][j] != curse) && (p->hand[p->whoseTurn][j] != estate) && (p->hand[p->whoseTurn][j] != duchy) && (p->hand[p->whoseTurn][j] != province) && (p->hand[p->whoseTurn][j] != copper) && (p->hand[p->whoseTurn][j] != silver) && (p->hand[p->whoseTurn][j] != gold))
				{
					cont = 1;
				}
			}
			if(cont == 0)
				break;

			r = -1;
			for(i = 0; i < p->handCount[p->whoseTurn]; i++)
			{
				for(j = 0; j < p->handCount[p->whoseTurn]; j++)
				{
					card = p->hand[p->whoseTurn][ran];
					r = playCard(ran, i, j, 1, p);
					fprintf(fp, "%d,%d ", i, j);
					if(r != -1)
						break;
				}
				if(r != -1)
					break;
			}

			if(r == -1)
			{
				printf("Player %d attempted to play %d, but failed\n", whoseTurn(p), card);
				fprintf(fp, "Player %d attempted to play %d, but failed\n", whoseTurn(p), card);
				ran += 1;
			}
			else
			{
				printf("Player %d played %d\n", whoseTurn(p), card);
				fprintf(fp, "Player %d played %d\n", whoseTurn(p), card);
				ran = 0;
			}
		}

		//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)
			{
				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 == 7 || p->coins > 8)
			{
				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;
}
コード例 #6
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;
}
コード例 #7
0
ファイル: playdom.c プロジェクト: cs362sp16/cs362sp16_pengs
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;
}
コード例 #8
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;

}
コード例 #9
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;
}
コード例 #10
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;
}
コード例 #11
0
ファイル: unittest4.c プロジェクト: wmaillard/cs362sp16
int main()
{
    const char* funcName = "scoreFor()";
    struct gameState* game = newGame();
    int* cardsInPlay = kingdomCards(adventurer, baron, smithy, mine, remodel, feast, steward, village, salvager, treasure_map);

    if(initializeGame(2, cardsInPlay, time(NULL), game) == -1)
    {
        printf("Couldn't intialize game!\n");
        exit(0);
    }

    int thisPlayer = whoseTurn(game);
    int otherPlayer = whoseTurn(game)^1;

    printf("**************Starting test on method: %s**************\n\n", funcName);
    // Initial Test to see if there are unexpected changes to the game
    struct gameState* initialState = malloc(sizeof(struct gameState));
    memcpy(initialState, game, sizeof(struct gameState));

    // Each player starts with three estate cards, so their score should be 3
    if( scoreFor(thisPlayer, game) != 3)
        printf("Error -%s shows this player's initial score is not 3!\n", funcName);

    /* Begin testing for unexpected changes in game state */
    if( initialState->numPlayers != game->numPlayers )
        printf("Error -%s changed the number of players!\n", funcName);

    int i;        // There are 26 possible cards, make sure the same are still in play
    for(i=0; i<=26; i++)
    {
        if( initialState->supplyCount[i] != game->supplyCount[i] )
        {
            if(i != adventurer)
                printf("Error -%s changed the number of other kingdom cards!\n", funcName);
        }
    }
    for(i=0; i<=26; i++)
    {
        if(initialState->embargoTokens[i] != game->embargoTokens[i])
            printf("Error -%s changed the Embargo Tokens in play!\n", funcName);
    }
    if( initialState->outpostPlayed != game->outpostPlayed)
        printf("Error -%s changed the \"Outpost Played\" status!\n", funcName);
    if( initialState->outpostTurn != game->outpostTurn)
        printf("Error -%s changed the \"Outpost Turn\" status!\n", funcName);
    if( initialState->whoseTurn != game->whoseTurn)
        printf("Error -%s changed the whose turn it is!\n", funcName);
    if( initialState->phase != game->phase)
        printf("Error -%s changed the game phase!\n", funcName);
    if( initialState->numActions != game->numActions)
        printf("Error -%s changed the number of actions for this turn!\n", funcName);
    if( initialState->coins != game->coins)
        printf("Error -%s changed the \"coins\" variable!\n", funcName);
    if( initialState->numBuys != game->numBuys)
        printf("Error -%s changed the number of Buy phases available!\n", funcName);
    if( initialState->handCount[thisPlayer] != game->handCount[thisPlayer])
        printf("Error -%s changed the number of cards in this player's hand!\n", funcName);
        // Toggle the bit to check for the other player's hand
    if( initialState->handCount[otherPlayer] != game->handCount[otherPlayer])
        printf("Error -%s changed the number of cards in the other player's hand!\n", funcName);

    for(i=0; i< game->handCount[thisPlayer]; i++)
    {
        if( initialState->hand[thisPlayer][i] != game->hand[thisPlayer][i])
            printf("Error -%s changed the cards in this player's hand!\n(If turn isn't changed)\n", funcName);
    }
    for(i=0; i<game->handCount[otherPlayer]; i++)
    {
        if( initialState->hand[otherPlayer][i] != game->hand[otherPlayer][i])
            printf("Error -%s changed the cards in the other player's hand!\n(If turn isn't changed)\n", funcName);
    }

    if(initialState->deckCount[thisPlayer] != game->deckCount[thisPlayer])
        printf("Error -%s changed the deck count for this player!\n", funcName);
    if(initialState->deckCount[otherPlayer] != game->deckCount[otherPlayer])
        printf("Error -%s changed the deck count for the other player!\n", funcName);


    for(i=0; i<game->deckCount[thisPlayer]; i++)
    {
        if(initialState->deck[thisPlayer][i] != game->deck[thisPlayer][i])
            printf("Error -%s changed the contents of this player's deck!\n", funcName);
    }
    for(i=0; i<game->deckCount[otherPlayer]; i++)
    {
        if(initialState->deck[otherPlayer][i] != game->deck[otherPlayer][i])
            printf("Error -%s changed the contents of the other player's deck!\n", funcName);
    }

    if(game->discardCount[thisPlayer] != initialState->discardCount[thisPlayer])
        printf("Error -%s changed the count for this player's discard pile!\n", funcName);
    if(initialState->discardCount[otherPlayer] != game->discardCount[otherPlayer])
        printf("Error -%s changed the count of the other player's discard pile!\n", funcName);


    for(i=0; i<initialState->discardCount[thisPlayer]; i++)
    {
        if(initialState->discard[thisPlayer][i] != game->discard[thisPlayer][i])
            printf("Error -%s changed the contents of this player's discard!\n", funcName);
    }
    for(i=0; i<game->discardCount[otherPlayer]; i++)
    {
        if(initialState->discard[otherPlayer][i] != game->discard[otherPlayer][i])
            printf("Error -%s changed the contents of the other player's discard!\n", funcName);
    }

    if(initialState->playedCardCount != game->playedCardCount)
        printf("Error -%s changed the count of played cards!\n", funcName);

    for(i=0; i<game->playedCardCount; i++)
    {
        if(initialState->playedCards[i] != game->playedCards[i])
            printf("Error -%s changed the cards in the \"Played Cards\" stack!\n", funcName);
    }

    /* End testing for unexpected changes in game state */

    // Testing for scores in the cards in hand.  Set all cards to copper and
    // record the score to compare against.
    for(i=0; i<5; i++)
        game->hand[thisPlayer][i] = copper;
    int previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][0] = curse;
    if( scoreFor(thisPlayer, game) != previousScore - 1 )
        printf("Error -%s didn't properly subtract for a curse card in hand!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][1] = estate;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for an estate card in hand!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][2] = duchy;
    if( scoreFor(thisPlayer, game) != previousScore + 3 )
        printf("Error -%s didn't properly add for a duchy card in hand!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][3] = province;
    if( scoreFor(thisPlayer, game) != previousScore + 6 )
        printf("Error -%s didn't properly add for a province card!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][4] = great_hall;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for a duchy card in hand!\n", funcName);

    // Reset to score 0 for hand
    for(i=0; i<game->handCount[thisPlayer]; i++)
        game->hand[thisPlayer][i] = copper;
    previousScore = scoreFor(thisPlayer, game);

    //Should have 10 cards (none gained or trashed), so gardens should add 1
    game->hand[thisPlayer][0] = gardens;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add 1 to score for a gardens card in hand!\n", funcName);

    // Trash a copper from the hand and gardens should add none
    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][game->handCount[thisPlayer]] = -1;
    game->handCount[thisPlayer]--;

    if( scoreFor(thisPlayer, game) != previousScore - 1)
        printf("Error -%s didn't properly add 0 to score for a gardens card in hand!\n", funcName);

    // Add it back for ease of testing (even 10 cards)
    game->hand[thisPlayer][game->handCount[thisPlayer]] = copper;
    game->handCount[thisPlayer]++;

    // Put all 10 adventurer cards in deck
    previousScore = scoreFor(thisPlayer, game);
    for(i=0; i < 10; i++)
    {
        game->deck[thisPlayer][game->deckCount[thisPlayer]] = adventurer;
        game->deckCount[thisPlayer]++;
    }
    //Should have 20 cards now, so gardens should add 2
    if( scoreFor(thisPlayer, game) != previousScore + 2 )
        printf("Error -%s didn't properly add 2 for a gardens card in hand!\n", funcName);

    // Reset to 10 cards again
    for(i=0; i < 10 ; i++)
    {
        game->deckCount[thisPlayer]--;
        game->deck[thisPlayer][game->deckCount[thisPlayer]] = -1;
    }


    /* Repeat all tests for cards in the discard */

    memcpy(initialState, game, sizeof(struct gameState));

    // Set up to only have 10 cards by removing one from the deck count and adding a gardens to the discard
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = -1;
    game->deckCount[thisPlayer]--;

    // Reset to score 0 for hand
    for(i=0; i<game->handCount[thisPlayer]; i++)
        game->hand[thisPlayer][i] = copper;
    previousScore = scoreFor(thisPlayer, game);

    //Should have 10 cards so gardens should add 1
    game->discard[thisPlayer][0] = gardens;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add 1 to score for a gardens card in discard!\n", funcName);

    // Trash a copper from the hand and gardens should add none
    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][game->handCount[thisPlayer]] = -1;
    game->handCount[thisPlayer]--;

    if( scoreFor(thisPlayer, game) != previousScore - 1)
        printf("Error -%s didn't properly add 0 to score for a gardens card in discard!\n", funcName);

    // Add it back for ease of testing (even 10 cards)
    game->hand[thisPlayer][game->handCount[thisPlayer]] = copper;
    game->handCount[thisPlayer]++;

    // Put all 10 adventurer cards in deck
    previousScore = scoreFor(thisPlayer, game);
    for(i=0; i < 10; i++)
    {
        game->deck[thisPlayer][game->deckCount[thisPlayer]] = adventurer;
        game->deckCount[thisPlayer]++;
    }

    //Should have 20 cards now, so gardens should add 2
    if( scoreFor(thisPlayer, game) != previousScore + 2 )
        printf("Error -%s didn't properly add 2 for a gardens card in discard!\n", funcName);

    // Done testing gardens, on to the simpler tests
    previousScore = scoreFor(thisPlayer, game);
    game->discard[thisPlayer][0] = curse;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore - 1 )
        printf("Error -%s didn't properly subtract for a curse card in discard!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->discard[thisPlayer][1] = estate;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for an estate card in discard!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->discard[thisPlayer][2] = duchy;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 3 )
        printf("Error -%s didn't properly add for a duchy card in discard!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->discard[thisPlayer][3] = province;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 6 )
        printf("Error -%s didn't properly add for a province card in discard!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->discard[thisPlayer][4] = great_hall;
    game->discardCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for a great_hall card in discard!\n", funcName);

    // We added cards to the discard and removed from deck, so reset state to the initial state
    free(game);
    game = initialState;
    initialState = NULL;

    /* Repeat all tests for cards in the deck */

    game->deck[thisPlayer][game->deckCount[thisPlayer]] = copper; // So when we put a gardens in this position we don't remove an estate

    // Reset to score 0 for hand
    for(i=0; i<game->handCount[thisPlayer]; i++)
        game->hand[thisPlayer][i] = copper;
    previousScore = scoreFor(thisPlayer, game);

    //Should have 10 cards so gardens should add 1
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = gardens;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add 1 to score for a gardens card in deck!\n", funcName);

    // Trash a copper from the hand and gardens should add none
    previousScore = scoreFor(thisPlayer, game);
    game->hand[thisPlayer][game->handCount[thisPlayer]] = -1;
    game->handCount[thisPlayer]--;

    if( scoreFor(thisPlayer, game) != previousScore -1 )
        printf("Error -%s didn't properly add 0 to score for a gardens card in deck!\n", funcName);

    // Add it back for ease of testing (even 10 cards)
    game->hand[thisPlayer][game->handCount[thisPlayer]] = copper;
    game->handCount[thisPlayer]++;

    // Put all 10 adventurer cards in deck
    previousScore = scoreFor(thisPlayer, game);
    for(i=0; i < 10; i++)
    {
        game->deck[thisPlayer][game->deckCount[thisPlayer]] = adventurer;
        game->deckCount[thisPlayer]++;
    }

    //Should have 20 cards now, so gardens should add 2
    if( scoreFor(thisPlayer, game) != previousScore + 2 )
        printf("Error -%s didn't properly add 2 for a gardens card in deck!\n", funcName);

    // On to the other victory cards...
    previousScore = scoreFor(thisPlayer, game);
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = curse;
    game->deckCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore - 1 )
        printf("Error -%s didn't properly subtract for a curse card in deck!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = estate;
    game->deckCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for an estate card in deck!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = duchy;
    game->deckCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 3 )
        printf("Error -%s didn't properly add for a duchy card in deck!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = province;
    game->deckCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 6 )
        printf("Error -%s didn't properly add for a province card in deck!\n", funcName);

    previousScore = scoreFor(thisPlayer, game);
    game->deck[thisPlayer][game->deckCount[thisPlayer]] = great_hall;
    game->deckCount[thisPlayer]++;
    if( scoreFor(thisPlayer, game) != previousScore + 1 )
        printf("Error -%s didn't properly add for a great_hall card in deck!\n", funcName);






    free(initialState);
    free(game);
    printf("\n\n********** End of test reached for function %s. Any errors found are shown above.**********\n", funcName);
    return 0;
}
コード例 #12
0
ファイル: unittest4.c プロジェクト: csDaniel/cs362sp16
void testScoreFor() {
    struct gameState gStateTest;
    int seed = 7;
    int discard = 1;
    int numPlayers = 2;
    int p = 0;
    int choice1 = 0, choice2 = 0, choice3 = 0, bonus = 0, handPos = 0;
    int cardCount = 0, cardCountTest = 0;
    int i, j, total;

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

    initializeGame(numPlayers, k, seed, &gStateTest);
    for (i = 0; i < gStateTest.handCount[p]; i++) {
            gStateTest.hand[p][i] = province;
    }

    printf("==== TESTING: %s ====\n", TESTCARD);
    printf("\nCASE 1: Full hand of provinces\n");
    printf("        Expected vs Real\n");
    total = gStateTest.handCount[p] * 6;
    printf(" full hand province = %d vs %d\n", total, scoreFor(p, &gStateTest));
    assert(total == scoreFor(p, &gStateTest));

    initializeGame(numPlayers, k, seed, &gStateTest);
    for (i = 0; i < gStateTest.handCount[p]; i++) {
            gStateTest.hand[p][i] = great_hall;
    }

    printf("\nCASE 2: Full hand of great_hall\n");
    printf("        Expected vs Real\n");
    total = gStateTest.handCount[p] * 1;
    printf(" full hand province = %d vs %d\n", total, scoreFor(p, &gStateTest));
    assert(total == scoreFor(p, &gStateTest));

    initializeGame(numPlayers, k, seed, &gStateTest);

    gStateTest.handCount[p] = 6;
    gStateTest.discardCount[p] = 6;
    gStateTest.deckCount[p] = 6;

    for (i = 0; i < 6; i++) {
        if (i == 0) {
            gStateTest.hand[p][i] = curse;
            gStateTest.deck[p][i] = curse;
            gStateTest.discard[p][i] = curse;
        }
        if ( i == 1) {
            gStateTest.hand[p][i] = estate;
            gStateTest.deck[p][i] = estate;
            gStateTest.discard[p][i] = estate;
        }
        if ( i == 2) {
            gStateTest.hand[p][i] = duchy;
            gStateTest.deck[p][i] = duchy;
            gStateTest.discard[p][i] = duchy;
        }
        if ( i == 3) {
            gStateTest.hand[p][i] = province;
            gStateTest.deck[p][i] = province;
            gStateTest.discard[p][i] = province;
        }
        if (i == 4) {
            gStateTest.hand[p][i] = great_hall;
            gStateTest.deck[p][i] = great_hall;
            gStateTest.discard[p][i] = great_hall;
        }
        if (i == 5) {
            gStateTest.hand[p][i] = gardens;
            gStateTest.deck[p][i] = gardens;
            gStateTest.discard[p][i] = gardens;
        }
    }
    printf("\nCASE 3: 1 of every score card in hand, discard, deck\n");
    printf("        Expected vs Real\n");
    total = 0 -1 +1 + 3 + 6 + 1;
    total *= 3;
    total += (fullDeckCount(p, 0, &gStateTest) / 10);


    printf(" full hand province = %d vs %d\n", total, scoreFor(p, &gStateTest));
    assert(total == scoreFor(p, &gStateTest));

    printf("\nCASE 4: hand = estate, deck = duchy, discard = province\n");
    initializeGame(numPlayers, k, seed, &gStateTest);
    gStateTest.discardCount[p] = 3;
    gStateTest.deckCount[p] = 2;

    for (i = 0; i < gStateTest.handCount[p]; i++) {
            gStateTest.hand[p][i] = estate;
            gStateTest.deck[p][i] = duchy;
            gStateTest.discard[p][i] = province;
    }

    total = (1 * gStateTest.handCount[p]) + (3 * 2) + (6 * 3);
    printf(" full hand province = %d vs %d\n", total, scoreFor(p, &gStateTest));
    assert(total == scoreFor(p, &gStateTest));

    printf("\n      testScoreFor(): PASS\n");
}