Ejemplo n.º 1
0
int main() {

	srand(50);
	int k[10];
	makeKingdomCards(k);

	int players, turn = 1, player;
	//struct gameState state;
	struct gameState* s = newGame();

	printf("Running Random Game Test\n");

	players = rand() % 3 + 2;

	initializeGame(2, k, players, s);

	printHand(0, s, "hand");
  	printHand(0, s, "deck");
  	printHand(0, s, "discard");
	printf("score: %d\n", scoreFor(0, s));
  
  	

	return 0;
}
Ejemplo n.º 2
0
//------------------------------------------------------------------------------
// Prints a chart of strategies to a .tex file. 
//------------------------------------------------------------------------------
void printChart (Strategy **chart, const char *filename, int showWinPct, 
            int MAKE_SIMPLE_CHART)
{
  FILE *file = NULL; 
  int i, j; 
  
  file = fopen(filename, "w"); 
  if (file == NULL) throwErr("File could not be opened.", "printChart");
  
  fprintf(file, "\\documentclass{article}\n\n"); 
  fprintf(file, "\\usepackage{amsmath, amssymb}\n"); 
  fprintf(file, "\\pagenumbering{gobble}\n\n"); 
  fprintf(file, "\\addtolength{\\oddsidemargin}{-.5in}\n"); 
  fprintf(file, "\\addtolength{\\evensidemargin}{-.5in}\n"); 
  fprintf(file, "\\addtolength{\\textwidth}{1in}\n"); 
  fprintf(file, "\\addtolength{\\topmargin}{-1.5in}\n"); 
  fprintf(file, "\\addtolength{\\textheight}{2.3in}\n\n"); 

  fprintf(file, "\\begin{document}\n\n\\begin{center}\n\\begin{large}\n"); 
  fprintf(file, "Blackjack Strategy\n\\end{large}\n\\end{center}\n\n"); 
  
  fprintf(file, "\\begin{small}\n"); 
  fprintf(file, "\\begin{center}\n\\emph{Dealer's up card}\n\\end{center}\n\n");
  
  fprintf(file, "\\begin{tabular}{"); 
  for (j = 0; j <= NUM_CARDS; j++)
    fprintf(file, "c|");
  fprintf(file, "}\n"); 
  
  for (j = 2; j <= NUM_CARDS; j++)
    fprintf(file, "& %d ", j); 
  fprintf(file, "& A \\\\\n"); //Note: Ace is printed on the *right* side 
  
  //Print the hands in the following order: 5 - 21, soft 13 - bust, AA, 22, 
  //then 3,3 - 10,10. 
  for (i = FIVE; i <= TWENTYONE; i++) 
    printHand(i, chart, showWinPct, file); 
  for (i = SOFT_THIRTEEN; i <= BUST; i++)
    printHand(i, chart, showWinPct, file); 
  if (!(MAKE_SIMPLE_CHART))
  {
    printHand(SOFT_TWELVE, chart, showWinPct, file); 
    printHand(FOUR, chart, showWinPct, file); 
    for (i = THREES; i <= TENS; i++)
      printHand(i, chart, showWinPct, file); 
  }
  
  fprintf(file, "\\hline\n\\end{tabular}\n\n"); 
  fprintf(file, "\\end{small}\n\n"); 
  fprintf(file, "\\vspace{.1in}\n"); 
  fprintf(file, "\\noindent KEY:\\\\\nH: Hit\\quad S: Stand\\quad DD: "); 
  fprintf(file, "Double down\\quad SPL: Split\\\\\n"); 
  fprintf(file, "X/Y: X\\%% chance of winning, Y\\%% chance of losing. "
          "(May not add up to 100 due to pushes. For splits, this is the");
  fprintf(file, " probability of winning each of the two split hands.)\n\n");
  
  fprintf(file, "\\end{document}\n"); 
  fclose(file); 
}
Ejemplo n.º 3
0
void display(struct gameState *g,int player)
{
	printSupply(g);
	displayPlayers(g,player);
	printDeck(player,g);
	printHand(player,g);
	printDiscard(player,g);
	printDeck(player+1,g);
	printHand(player+1,g);
	printDiscard(player+1,g);
}
Ejemplo n.º 4
0
void print_info(struct gameState state)
{
	printSupply(&state);
	printHand(state.whoseTurn, &state);
	printDeck(state.whoseTurn, &state);
	printDiscard(state.whoseTurn, &state);
	printPlayed(state.whoseTurn, &state);
	printState(&state);
	printScores(&state);
}
Ejemplo n.º 5
0
Archivo: Deck.cpp Proyecto: jbobrow/Set
void Deck::findSet()
{
    for( int i=0; i<cardsDealt.size(); i++ ) {

        for (int j=i+1; j<cardsDealt.size(); j++) {

            //printf("permutation: %i, %i\n", i, j);
            Card *c1 = (Card *) cardsDealt[i];
            Card *c2 = (Card *) cardsDealt[j];
            Card *c3 = c1->findCardToCompleteSet(c2);

            if(c3 != NULL) {
                for( int k=0; k<cardsDealt.size(); k++ ) {

                    Card *s = (Card *) cardsDealt[k];

                    if( c3->isCardEqual(s)) {

                        printf("--------removing found set---------\n");
                        c1->printCard();
                        c2->printCard();
                        c3->printCard();
                        printf("-----------------------------------\n");

                        removeCard(c1, &cardsDealt);
                        removeCard(c2, &cardsDealt);
                        removeCard(c3, &cardsDealt);

                        printHand();
                    }
                }
            }
            else {
                printf("Null Card!!!!\n");
            }
        }
    }
}
Ejemplo n.º 6
0
int main(int argc, char* argv[]) {
		char *add  = "add";
	char *buyC = "buy";
	char *endT = "end";
	char *exit = "exit";
	char *help = "help";
	char *init = "init";
	char *numH = "num";
	char *play = "play";
	char *resign  = "resi";
	char *show = "show";
	char *stat = "stat";
	char *supply = "supp";
	char *whos = "whos";
		
	char command[MAX_STRING_LENGTH];
	char line[MAX_STRING_LENGTH];
	char cardName[MAX_STRING_LENGTH];

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

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

	int randomSeed = atoi(argv[1]);

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

	gameState g;
	gameState * game = &g;

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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


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

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

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

		} // end of While

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

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

		}


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

	return 0;
}
Ejemplo n.º 8
0
bool randomTest(struct gameState *originalGame, int kingdomCards[KINGDOM_CARDS_SIZE]) {
    int player = 0;
    struct gameState testGame;
    bool allPassed = true;
    int handPos = 0;
    int bonus = 0;
    
    memcpy(&testGame, originalGame, sizeof(struct gameState));
    
    randomDeck(player, DECK_SIZE, kingdomCards, &testGame);

    // empty discard pile
    int playedCardCountBefore = testGame.playedCardCount;
    int deckCountBefore = testGame.deckCount[player];
    
    int handCountBefore = 1;
    testGame.handCount[player] = handCountBefore;
    testGame.hand[player][handPos] = village;

    int numActionsBefore = Random() * MAX_ACTIONS;
    testGame.numActions = numActionsBefore;
    
    printf("Before Play Village\n");
    printDeck(player, &testGame);
    printHand(player, &testGame);
    printPlayedCards(&testGame);
    
    printf("After Play Village\n");
    
    cardEffect(village, 0, 0, 0, &testGame, handPos, &bonus);

    
    /* Test Failed Statements */
    /* Test 1 */
    // deck decreases by 1
    printDeck(player, &testGame);
    
    int actualDeckCountAfter = testGame.deckCount[player];
    int expectedDeckCountAfter = deckCountBefore - 1;
    printf("Deck count: %d, Expected: %d\n", actualDeckCountAfter, expectedDeckCountAfter);
    
    if(actualDeckCountAfter != expectedDeckCountAfter){
        printf(">>>>>>> ERROR: TEST 1 FAILED: >>>>>>>\n");
        allPassed &= false;
    }
    
    /* Test 2 */
    // played card count increases by 1 to indicated discardCard is called.
    printPlayedCards(&testGame);
    
    int actualPlayedCardCountAfter = testGame.playedCardCount;
    int expectedPlayedCardCountAfter = playedCardCountBefore + 1;
    printf("Played card count: %d, Expected: %d\n", actualPlayedCardCountAfter, expectedPlayedCardCountAfter);
    
    if(actualPlayedCardCountAfter != expectedPlayedCardCountAfter){
        printf(">>>>>>> ERROR: TEST 2 FAILED: >>>>>>>\n");
        allPassed &= false;
    }
    
    /* Test 3 */
    // hand count should stay the same because card is drawn and village is discarded
    printHand(player, &testGame);
    
    int actualHandCountAfter = testGame.handCount[player];
    int expectedHandCountAfter = handCountBefore;
    printf("Hand count: %d, Expected: %d\n", actualHandCountAfter, expectedHandCountAfter);
    
    if(actualHandCountAfter != expectedHandCountAfter){
        printf(">>>>>>> ERROR: TEST 3 FAILED >>>>>>>\n");
        allPassed &= false;
    }
    
    /* Test 4 */
    // number of actions increased by 2
    printActions(&testGame);
    
    int actualActionsCountAfter = testGame.numActions;
    int expectedActionsCountAfter = numActionsBefore + 2;
    printf("Action count: %d, Expected: %d\n", actualActionsCountAfter, expectedActionsCountAfter);
    
    if(actualActionsCountAfter != expectedActionsCountAfter){
        printf(">>>>>>> ERROR: TEST 4 FAILED >>>>>>>\n");
        allPassed &= false;
    }
    
    return allPassed;
}
Ejemplo n.º 9
0
int main(void){
        srand(time(NULL));

        unsigned deck[52] = { 1,1,1,1,          // ACE
                              2,2,2,2,          // TWO
                              3,3,3,3,          // THREE
                              4,4,4,4,          // FOUR
                              5,5,5,5,          // FIVE
                              6,6,6,6,          // SIX
                              7,7,7,7,          // SEVEN
                              8,8,8,8,          // EIGHT
                              9,9,9,9,          // NINE
                              10,10,10,10,      // TEN
                              10,10,10,10,      // JACK
                              10,10,10,10,      // QUEEN
                              10,10,10,10       // KING
                              };

        unsigned quit = 0;      // stop the game

        unsigned player1_hand[26];
        unsigned computer_hand[26];

        do{
                removeCardsFromHand(player1_hand);
                removeCardsFromHand(computer_hand);

                shuffleDeck(deck);
                size_t deckIndex = 0;

                size_t player1Index = 0;
                player1_hand[player1Index] = deck[deckIndex];
                deckIndex++;
                player1Index++;
                player1_hand[player1Index] = deck[deckIndex];
                deckIndex++;
                player1Index++;
                
                size_t computerIndex = 0;
                computer_hand[computerIndex] = deck[deckIndex];
                deckIndex++;
                computerIndex++;
                computer_hand[computerIndex] = deck[deckIndex];
                deckIndex++;
                computerIndex++;

                do{
                        printf("Player 1's hand: ");
                        printHand(player1_hand);

                        printf("Computer's hand: [unknown] ");
                        printHand(computer_hand + 1);

                        printf("0 for Hit, 1 for Stay: ");
                        unsigned stay;
                        scanf("%u", &stay);
                        if(stay){
                                if(calculateHandValue(computer_hand) < 18){
                                        computer_hand[computerIndex] = deck[deckIndex];
                                        deckIndex++;
                                        computerIndex++;
                                }
                                // staying so leave
                                break;
                        }

                        // hitting
                        player1_hand[player1Index] = deck[deckIndex];
                        deckIndex++;
                        player1Index++;

                        if(calculateHandValue(computer_hand) < 18){
                                computer_hand[computerIndex] = deck[deckIndex];
                                deckIndex++;
                                computerIndex++;
                        }
                }while(1);

                putchar('\n');

                printf("Player 1's hand: ");
                printHand(player1_hand);

                unsigned player1_value = calculateHandValue(player1_hand);
                printf("Player 1's Value: %u\n", player1_value);

                printf("Computer's hand: ");
                printHand(computer_hand);

                unsigned computer_value = calculateHandValue(computer_hand);
                printf("Computer's Value: %u\n", computer_value);

                putchar('\n');

                if(player1_value > 21){
                        printf("Player 1 bust!\n");
                        if(computer_value > 21){
                                printf("Computer bust!\n");
                        }else{
                                printf("Computer wins!\n");
                        }
                }else if(computer_value > 21){
                        printf("Computer bust!\n");
                        if(player1_value > 21){
                                printf("Player 1 bust!\n");
                        }else{
                                printf("Player 1 wins!\n");
                        }
                }else if(player1_value > computer_value){
                        printf("Player 1 wins!\n");
                }else{
                        printf("Computer wins!\n");
                }

                putchar('\n');

                printf("0 for Quit, 1 for New Game: ");
                scanf("%u", &quit);

                putchar('\n');
        }while(quit != 0);

        return 0;
}
Ejemplo n.º 10
0
/*
 * aLeftPlayer: next in turn
 * aRightPlayer: prev in turn
 * 0, 1th
 * 1th, 2nd
 */
Card *AlphaBetaPlayer::makeMove (Card *lMove, Card *rMove, Player *aLeftPlayer, Player *aRightPlayer, bool isPassOut) {
  qDebug() << type() << "("<< mPlayerNo << ") moves";
  
  card_t hands[3][10];
  card_t desk[3];
  int crdLeft = 0;
  int trumpSuit = 0;
  Player *plst[3];

//again:
  plst[0] = plst[1] = plst[2] = 0;
  plst[mPlayerNo-1] = this;
  plst[aLeftPlayer->number()-1] = aLeftPlayer;
  plst[aRightPlayer->number()-1] = aRightPlayer;

  // build hands
  for (int c = 0; c < 3; c++) {
    Q_ASSERT(plst[c]);
    CardList *clst = &(plst[c]->mCards);
    //for (int f = 0; f < 10; f++) hds[c][f] = hands[c][f] = 0;
    int pos = 0;
    for (int f = 0; f < clst->size(); f++) {
      Card *ct = clst->at(f);
      if (!ct) continue;
      hands[c][pos++] = CARD(ct->face(), ct->suit()-1);
      if (pos > crdLeft) crdLeft = pos;
    }
    for (int f = pos; f < 10; f++) hands[c][f] = 0;
    xsortCards(hands[c], pos);
  }


  if (!lMove && !rMove && crdLeft == 10) { 
    return AiPlayer::makeMove(lMove, rMove, aLeftPlayer, aRightPlayer, isPassOut);
  }


  // find game
  const eGameBid bid = m_model->currentGame();

  gPassOutOrMisere = (bid == g86 || bid == g86catch || bid == raspass);
  trumpSuit = bid%10-1;//(bid-(bid/10)*10)-1;
/*
  if (bid == g86catch || bid == g86 || bid == raspass) {
    return Player::moveSelectCard(lMove, rMove, aLeftPlayer, aRightPlayer);
  }
*/
  if (bid == g86catch || bid == g86 || bid == raspass) {
    trumpSuit = 4;
  }
  if (trumpSuit < 0) trumpSuit = 4;

  fprintf(stderr, "po:%s; lm:%s, rm:%s\n", isPassOut?"y":"n", lMove?"y":"n", rMove?"y":"n");
  if (isPassOut && rMove && !lMove) {
    // это распасы, первый или второй круг, первый ход
    gPassOutSuit = rMove->suit()-1;
    fprintf(stderr, "pass-out: %i\n", gPassOutSuit);
    rMove = 0;
  } else gPassOutSuit = -1;

  // build desk
  int turn = 0;
  if (lMove) {
    desk[turn++] = CARD(lMove->face(), lMove->suit()-1);
    if (rMove) desk[turn++] = CARD(rMove->face(), rMove->suit()-1);
  } else if (rMove) {
    desk[turn++] = CARD(rMove->face(), rMove->suit()-1);
  }

  // build hands
  for (int f = 0; f < 3; f++) {
    xHands[f].suitCount[0] = xHands[f].suitCount[1] = xHands[f].suitCount[2] = xHands[f].suitCount[3] = 0;
    xHands[f].suitStart[0] = xHands[f].suitStart[1] = xHands[f].suitStart[2] = xHands[f].suitStart[3] = 11;
    xHands[f].tricks = plst[f]->tricksTaken();
    int st;
    for (int z = 0; z < 10; z++) {
      if (hands[f][z]) {
        xHands[f].faces[z] = FACE(hands[f][z]);
        st = xHands[f].suits[z] = SUIT(hands[f][z]);
        if (xHands[f].suitCount[st]++ == 0) xHands[f].suitStart[st] = z;
      } else xHands[f].faces[z] = 0;
    }
  }

  // build desk
  for (int f = 0; f < turn; f++) {
    xDeskFaces[f] = FACE(desk[f]);
    xDeskSuits[f] = SUIT(desk[f]);
  }

  int a, b, c, move;
  int me = this->number()-1;
  xCardsLeft = crdLeft;
  gTrumpSuit = trumpSuit;
  gIterations = 0;

  printf("%shand 0:", this->number()==0?"*":" ");
  printHand(&(xHands[0]));
  printf("%shand 1:", this->number()==1?"*":" ");
  printHand(&(xHands[1]));
  printf("%shand 2:", this->number()==2?"*":" ");
  printHand(&(xHands[2]));
  printDesk(turn);

  // оптимизации
/*
  if (turn > 0) {
    // можем вообще взять?
    if (hands[me].suitCount(
  }
*/

  stTime = QTime::currentTime();
  stTime.start();
  abcPrune(turn, me, -666, 666, 666, &a, &b, &c, &move);

  qDebug() <<
    "face:" << FACE(hands[me][move]) <<
    "suit:" << SUIT(hands[me][move])+1 <<
    "move:" << move <<
    "turn:" << turn <<
    "moves:" << crdLeft <<
    "trump:" << trumpSuit <<
    "iters:" << gIterations <<
    "";

/*
  for (int h = 0; h < 3; h++) {
    fprintf(stderr, (h == me)?"*":" ");
    fprintf(stderr, "hand %i:", h);
    for (int f = 0; f < 10; f++) {
      if (hands[h][f]) {
        fprintf(stderr, " %2i.%i(%3i)", FACE(hands[h][f]), SUIT(hands[h][f]), hands[h][f]);
      } else {
        fprintf(stderr, " %2i.%i(%3i)", 0, 0, hands[h][f]);
      }
    }
    fprintf(stderr, "\n");
  }
  fprintf(stderr, "desk:");
  for (int f = 0; f < turn; f++) {
    fprintf(stderr, " %2i.%i(%3i)", FACE(desk[f]), SUIT(desk[f]), desk[f]);
  }
  fprintf(stderr, "\n");
*/
  Q_ASSERT(move >= 0);

  Card *moveCard = getCard(FACE(hands[me][move]), SUIT(hands[me][move])+1);

  qDebug() << "move:" << moveCard->toString();

  mCards.remove(moveCard);
  mCardsOut.insert(moveCard);

  return moveCard;
}
Ejemplo n.º 11
0
int main (void)
{
	/* initialize suit array */
	const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

	/* initialize face array */
	const char *face[NUM_FACES] = {"Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight",
		"Nine", "Ten", "Jack", "Queen", "King", "Ace"};

	/* initalize deck array */
	int deck[SUITS][NUM_FACES] = {0};

	/* initialize winning card arrays */
	int winningCards[13] = {0}, winningCardsComp[13] = {0};

	// Initialize suit and face arrays to store current hand info
	int numSuits[SUITS] = {0};
	int numFaces[NUM_FACES] = {0};
	int numSuitsComp[SUITS] = {0};
	int numFacesComp[NUM_FACES] = {0};
	int keepCards[HAND_SIZE] = {0};
	int winner = -1, wins = 0, losses = 0, choice = 0;

	// initialize 2 hands
	Card hand1[HAND_SIZE] = {{0, 0, 0}};
	Card hand2[HAND_SIZE] = {{0, 0, 0}};
	srand ((unsigned) time (NULL)); /* seed random-number generator */

	while(choice != 3){
		displayMenu();
		choice = getMenuChoice();
		if(choice == 1){
			system("cls");
			winner = -1;
			// Shuffle deck
			resetDeck(deck);
			shuffle (deck);
			resetKeepCards(keepCards);
	
			// Deal player hand
			deal (deck, hand1, 1, keepCards);

			// Deal computer hand
			deal(deck, hand2, 0, keepCards);
			resetCardArrays(numSuits, numFaces);
			resetCardArrays(numSuitsComp, numFacesComp);
			setCardArrays(numSuitsComp, numFacesComp, hand2);

			// Print player and computer hands
			printf("Your hand:\n");
			printf("-------------------------\n");
			printHand(hand1, face, suit);
				/* **For testing**
				printf("\nComputer's hand:\n");
				printf("-------------------------\n");
				printHand(hand2, face, suit);*/
			printSecretHand();

			// Get cards player would like to keep
			getKeepCards(keepCards);

			// Deal replacement cards to player
			deal(deck, hand1, 0, keepCards);

			// Determine which cards computer will keep
			resetKeepCards(keepCards);
			determineCompKeepCards(keepCards, numSuitsComp, numFacesComp, hand2);

			// Deal replacement cards to computer
			deal(deck, hand2, 0, keepCards);

			// Print new hands
			system("cls");
			printf("Your hand:\n");
			printf("-------------------------\n");
			printHand(hand1, face, suit);
			printf("\nComputer's hand:\n");
			printf("-------------------------\n");
			printHand(hand2, face, suit);

			// Determine winner
			resetCardArrays(numSuits, numFaces);
			resetCardArrays(numSuitsComp, numFacesComp);
			setCardArrays(numSuitsComp, numFacesComp, hand2);
			setCardArrays(numSuits, numFaces, hand1);
			setDetermineWinnerArray(numSuits, numFaces, winningCards);
			setDetermineWinnerArray(numSuitsComp, numFacesComp, winningCardsComp);
			winner = determineWinner(winningCards, winningCardsComp);

			// Display winner
			if(winner == 1){
				printf("\nYou win!\n");
				wins++;
			}
			else if(winner == 0){
				printf("\nComputer wins\n");
				losses++;
			}
			system("pause");
		}
		if(choice == 2){
			displayWinsLosses(wins, losses);
		}
	}
	return 0;
}
Ejemplo n.º 12
0
int main(){
	
	//Initialize Game
	int init;
	
	int k[10] = {adventurer, smithy, remodel, gardens, minion, steward, tribute, 
				ambassador, baron, mine};
	
	struct gameState G;
	
	init = initializeGame(2, k, 1, &G);
	
	printf("\n--------------------------------\n");
	printf("Testing Card: Villages\n");
	printf("--------------------------------\n\n");
	
	//Setup the cards for user 1
	G.deckCount[0] = 5;
	G.handCount[0] = 5;
	G.discardCount[0] = 0;
	
	G.hand[0][0] = 1; //estate
	G.hand[0][1] = 1; //estate
	G.hand[0][2] = 4; //copper
	G.hand[0][3] = 4; //copper
	G.hand[0][4] = 14; //villages

	G.deck[0][0] = 1; //estate
	G.deck[0][1] = 2; //duchy
	G.deck[0][2] = 4; //copper
	G.deck[0][3] = 5; //silver
	G.deck[0][4] = 6; //gold
	
	printf("Top card in deck: %d\n", G.deck[0][G.deckCount[0] - 1]);
	
	printf("\nHand prior to playing Village\n");
	printHand(&G);
	
	//Get the number of actions prior to playing the Village card
	int numActionsprior = G.numActions;
	printf("\nNumber of actions prior to playing Village: %d\n", numActionsprior);

	//Run test #1
	int test1 = playVillage(0, 4, &G);
	
	if(test1 != 0){
		printf("Test #1 Failed to run.");
	}
	
	printf("\nHand after playing Village\n");
	printHand(&G);
	int numActionsafter = G.numActions;
	printf("\nNumber of actions after playing Village: %d\n\n", numActionsafter);
	
	printf("Test #1: Village card removed.\n");
	if(G.hand[0][4]!= 14){
		printf("Result: Pass\n\n");
	}else{
		printf("Result: Fail\n\n");
	}
	
	printf("Test #2: Added card is the top card of the deck.\n");
	if(G.hand[0][4] == 6){
		printf("Result: Pass\n\n");
	}else{
		printf("Result: Fail\n\n");
	}
	
	printf("Test #3: More than 1 card is added.\n");
	
	if(G.hand[0][5] == -1){
		printf("Result: Pass\n\n");
	}else{
		printf("Result: Fail\n\n");
	}
	
	printf("Test #4: Actions should be incremented by 2.\n");
	
	if(numActionsafter == numActionsprior+2){
	
		printf("Result: Pass\n\n");
	}else{
		printf("Result: Fail\n\n");
	}

}