Example #1
0
int main(){
    struct player *players;
    struct card *deck;
    //struct player computer;
    int numPlayers;
    int i;


    deck = createDeck();
    shuffleDeck(deck);
    printf("__WELCOME_TO_HENRY'S_BLACK_JACK_GAME__\n");     
    printf("How many players? (MAX 5)\n");
    scanf("%d", &numPlayers);
     while(numPlayers < 1 || numPlayers > 5){
       printf("Minimum 1, Maximum 5\n");
       scanf("%d", &numPlayers);
     }
    players = makePlayers(numPlayers);
    printf("number of players %d\n", numPlayers);
    printf("_________LETS_PLAY_BLACK_JACK!________\n");
    for(i=0;i<numPlayers;i++){
      players[i].playersCards = NULL;
      players[i].amountCard = 2; //varför fungerar du inte?=
      printf("player: %s\n", players[i].name);
      dealCards(players[i].playersCards,deck,players[i].amountCard);
    }

    free(deck);
    free(players);
    return 0;
}
Example #2
0
int main()
{
	int deckSize = DECKCOUNT;
	int deck[deckSize];
	int code = shuffleDeck(deck, deckSize);
	return code;
}
void BlackJack::dealHand()
{
    qDebug() << "Deal hand...";
    ui->actionDeal_Hand->setDisabled(true);

    // if disabled by handOver() set enabled
    ui->actionHit_Me->setEnabled(true);
    ui->actionStay->setEnabled(true);

    // if already instantiated, delete to prevent memory leak
    if(playerHand != nullptr || dealerHand != nullptr) {
        delete playerHand;
        delete dealerHand;
        playerHand = nullptr;
        dealerHand = nullptr;
    }

    if(deck->count() <= 4) {
        qDebug() << "insufficient amount of cards...";
        shuffleDeck();
    }

    playerHand = deck->deal(2);
    dealerHand = deck->deal(2);

    refreshImages(playerHand, ui->layout_player);
    refreshImages(dealerHand, ui->layout_dealer);

    qDebug() << "player hand: " << playerHand->toString();
    qDebug() << QString("dealer hand: %1 thus value of %2")
                .arg(dealerHand->toString())
                .arg(dealerHand->getValue());

    ui->sb_cardsLeft->setValue(deck->getCardsLeft());
}
BlackJack::BlackJack(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::BlackJack)
{
    ui->setupUi(this);
    deck = new CardDeck();

    // initialize card hands as null pointers
    playerHand = nullptr;
    dealerHand = nullptr;

    // connect QActions with Toolbar buttons
    ui->tb_newGame->setDefaultAction(ui->actionNewGame);
    ui->tb_dealHand->setDefaultAction(ui->actionDeal_Hand);
    ui->tb_shuffleDeck->setDefaultAction(ui->actionShuffle_Deck);
    ui->tb_hit_me->setDefaultAction(ui->actionHit_Me);
    ui->tb_stay->setDefaultAction(ui->actionStay);

    // connect QActions with proper slots
    connect(ui->actionNewGame, SIGNAL(triggered()), this, SLOT(newGame()));
    connect(ui->actionDeal_Hand, SIGNAL(triggered()), this, SLOT(dealHand()));
    connect(ui->actionShuffle_Deck, SIGNAL(triggered()), this, SLOT(shuffleDeck()));
    // new signal slot syntax:
    connect(ui->actionHit_Me, &QAction::triggered, this, &BlackJack::hitMe);
    connect(ui->actionStay, &QAction::triggered, this, &BlackJack::stay);

    // start!
    dealHand();
}
int main () {
	srand (time (NULL));
	int numGames, numPlayers;
	Card deck[NUM_CARDS];
	Card hand[MAX_PLAYER][HAND_SIZE];
	getGameInfo (&numGames);
	initFile (numGames);
	for (int i = 0; i < numGames; i++) {
		getNumPlayers (&numPlayers);
		int handScores[numPlayers][1 + HAND_SIZE];
		memset(handScores, 0, numPlayers * (1 + HAND_SIZE) * sizeof(int));
		int bestHandIndices[numPlayers];
		memset (bestHandIndices, 0, numPlayers*sizeof(int));
		int *numTied = (int *)malloc(sizeof(int));
		*numTied = 0;
		printfName (i + 1, numPlayers);
		initDeck (deck);
		shuffleDeck (deck);
		dealHands (deck, hand, numPlayers);
		evaluateWinner (hand, numPlayers, handScores, bestHandIndices, numTied);
		for (int j = 0; j < numPlayers; j++) {
			printResults (&hand[j][0], HAND_SIZE, j, handScores);
		}
		printWinner (i + 1, hand, numPlayers, handScores, bestHandIndices, *numTied);
	}
}
Example #6
0
/* Note:
	In our simplified version of Blackjack, we’re not going to keep track of which specific
	cards the player and the dealer have been dealt. We’ll only track the sum of the values of
	the cards they have been dealt for the player and dealer. This keeps things simpler.
*/
int main()
{
	// call this before calling getRandomNumber()
	// set initial seed value to system clock
	// time() returns the number of seconds since midnight on Jan 1, 1970.
	srand(static_cast<unsigned int>(time(0)));
	
	// first result discarded to overcome a flaw in how rand() is implemented by the compiler
	rand();

	std::array<Card, 52> deck;

	// initialize deck with each card
	int index = 0;
	for (int suit = SUIT_CLUB; suit < MAX_SUITS; ++suit)
	{
		for (int rank = CARD_2; rank < MAX_RANKS; ++rank)
		{
			deck.at(index).suit = static_cast<CardSuit>(suit);
			deck.at(index).rank = static_cast<CardRank>(rank);
			++index;
		}
	}
	
	// printDeck(deck);

	shuffleDeck(deck);

	if (playBlackJack(deck))
		std::cout << "You win!\n";
	else
		std::cout << "You lose!\n";
	
	return 0;
}
void BlackJack::newGame()
{
    qDebug() << "new game";

    ui->sb_dealerScore->setValue(0);
    ui->sb_playerScore->setValue(0);

    shuffleDeck();
    dealHand();
}
Example #8
0
/* Usage: deck hand_size number_of_hands */
int main(int argc, char *argv[]) {

    /* input */
    int nhands;                   /* stores number of hands from input */
    int handSize;                 /* stores hand size from input */
    int canDisplayHands;          /* flag to indicate if the hands can be displayed */

    int foundErrors = NO_ERRORS;  /* indicate whether errors are found */

    /* validate input */
    if (validInput(argc, argv, &handSize, &nhands)) {

        Card deck[DECK_SIZE];     /* the cards deck */
        Hand hands[nhands];       /* the hands */
        Hand sortedHands[nhands];

        canDisplayHands = (handSize && nhands) > 0 ? TRUE : FALSE;

        /* create a deck of cards. */
        printf("\n   Create and display the deck.\n\n");
        createDeck(deck);
        displayDeck(deck, DECK_SIZE);

        /* shuffle deck of cards */
        printf("\n   Shuffle and display the deck.\n\n");
        shuffleDeck(deck, DECK_SIZE);
        displayDeck(deck, DECK_SIZE);

        if (canDisplayHands) {
            /* create and display hands */
            printf("\n   Create and display hands.\n\n");
            createHands(deck, hands, handSize, nhands);
            sortHands(hands, sortedHands, handSize, nhands);

            displayHands(hands, sortedHands, handSize, nhands);

            /* rank hands */
            printf("\n   Rank and display ranked hands.\n\n");
            displayRankedHands(sortedHands, nhands);

            printf("\n   Display winners.\n\n");
            displayWinners(sortedHands, nhands);
        }
        else {
            printf("\nCould not display hands because either the hand size or the number of hands is zero.\n");
        }
    }
    else {
        /* invalid input */
        printf("Usage: deck hand_size number_of_hands\n");
        foundErrors = ERRORS;
    }
    return foundErrors;
}
Example #9
0
void simGame (FILE *f, int numPlayers, int numGames) {
	cardType deck[NUM_CARDS];
	handType players[numPlayers];
	createDeck(deck);
	
	int winners[numPlayers]; 
	int numWinners; 
	char *type[] = {"Bust", "Pair", "Two Pair", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush", "Royal Flush"};
	char suitSym [] = {'H', 'D', 'S', 'C'};
	
	for (int i = 0; i < numGames; i++) {
		shuffleDeck (deck);
		numWinners = 0;
		
		fprintf (f, "GAME %i", i + 1);
		
		for (int j = 0; j < numPlayers; j++) {
			
			fprintf (f, "\nPlayer %i's hand: ", j + 1);	
			
			for (int k = 0; k < HAND_SIZE; k++) {
				players[j].cards[k] = deck[j * HAND_SIZE + k];
				
				fprintf (f, "%i%c ", players[j].cards[k].rank + 1, suitSym[players[j].cards[k].suit]);
			}
			checkHand (&players[j]);		
			fprintf (f, "(%s)", type[players[j].type]);
			
			
			if (j == 0) {
				winners[0] = j;
				numWinners = 1;
			}			
			else if (players[j].type > players[winners[numWinners - 1]].type || 
			(players[j].type == players[winners[numWinners - 1]].type &&  players[j].value > players [winners[numWinners - 1]].value)) {
				winners[0] = j;		
				numWinners = 1;				
			}
			else if (players[j].type == players [winners[numWinners - 1]].type && players[j].value == players [winners[numWinners - 1]].value) {
				winners [numWinners++] = j;
			}
						
		}
		
		fprintf (f, "\nThe %s ", (numWinners == 1)? "winner is" : "winners are");
		for (int j = 0; j < numWinners; j++) {
			fprintf (f, "%splayer %i", j == 0? "" : " and ",  winners[j] + 1);
		}
		fprintf (f, "!\n\n");	
		
	}
}
void BlackJack::hitMe()
{
    if(deck->count() <= 1) {
        qDebug() << "insufficient amount of cards...";
        shuffleDeck();
    }
    qDebug() << "Adding card...";
    playerHand->append(deck->last());
    deck->removeLast();
    qDebug() << "playerHand now has " << playerHand->toString();
    ui->sb_cardsLeft->setValue(deck->getCardsLeft());
    refreshImages(playerHand, ui->layout_player);
}
Example #11
0
DeckOfCards::DeckOfCards(void){
	suit[0]="spades";
	suit[1]="clubs";
	suit[2]="hearts";
	suit[3]="diamonds";

	short handValue = 0;

	int pointerToDeck = 0;

	//inits card 0-51 with values 1-13
	createDeck();
	shuffleDeck();
}
Example #12
0
int main(void) {
	fillDeck();
	shuffleDeck();
	passCard(playerHand, Player);
	passCard(playerHand, Player);
	PrintHand();
	printf("You score is (%i).        ", scorePlayerHand);
	do {
		CheckHand();
		Ask();
		Choice();
		PrintHand();
		printf("You score is (%i).    ", scorePlayerHand);
	} while ((userAnswer == 'y' || userAnswer == 'Y'));
	getWinner();
	return 0;
}
/* function name: test_shuffleDeck()
   function prototype: test_shuffleDeck()
   Description: checks if shuffleDeck works properly by making sure
   				there are no repeats in the sequence of cards. This 
   				will ensure that shuffleDeck is correctly swapping 
   				cards.
   	Returns: FALSE on duplicates found (BAD)
   			TRUE if duplicates are not found (GOOD)
 */
int test_shuffleDeck(){
	shuffleDeck(test_array, dcptr);
	/*check if cards are repeated*/
	int i = 0;
	int duplicate = 0;
	for(i=0; i<51; i++){
		int j;
		for (j=i+1; j<52; j++){
			if (test_array[i] == test_array[j]){
				duplicate=1;
				fprintf(stderr, "Duplicates found at indexes %d and %d", i, j);

			}
		}
	}
	if (duplicate == 1){
		return 0;
	}
	else return 1;
}
Example #14
0
void deck::refill()
{
    the_deck.clear();
    suitType suit = spade;
    for (; suit < 4; suit = static_cast<suitType>(static_cast<int>(suit) + 1))
    {
        for (int value = 2; value < 14; value++)
        {
            card newCard;
            newCard.suit = suit;
            newCard.value = value;
            the_deck.push_back(newCard);
        }
        card newAce;
        newAce.suit = suit;
        newAce.value = 11;
        newAce.isAce = true;
        the_deck.push_back(newAce);
    }
    shuffleDeck();
    
}
Example #15
0
int getNumberOfRounds()
{
	char deck[52];
	int i = 0;
	int numberOfRounds = 0;
	
	for(i=0;i<13;i++)
	{
		deck[i*4] = i;
		deck[i*4 + 1] = i;
		deck[i*4 + 2] = i;
		deck[i*4 + 3] = i;
	}
	
	shuffleDeck(deck);
	
	char p1 [52];
	char p2 [52];
	
	int p1Counter = 0;
	int p2Counter = 0;
	
	for(i=0;i<26;i++)
	{
		p1[i] = deck[i];
		p2[i] = deck[i + 26];
	}
	p1Counter = 26;
	p2Counter = 26;
	
	int p1Index = 0;
	int p2Index = 0;
	
	while(p1Index != p1Counter && p2Index != p2Counter)
	{
		numberOfRounds++;
		if(p1[p1Index] > p2[p2Index])
		{
			p1[p1Counter++] = p1[p1Index++];
			if(p1Counter >= 52)
				p1Counter = 0;
			if(p1Index >= 52)
				p1Index = 0;
				
			p1[p1Counter++] = p2[p2Index++];
			if(p1Counter >= 52)
				p1Counter = 0;
			if(p2Index >= 52)
				p2Index = 0;
		}
		else if(p1[p1Index] < p2[p2Index])
		{
			p2[p2Counter++] = p2[p2Index++];
			if(p2Counter >= 52)
				p2Counter = 0;
			if(p2Index >= 52)
				p2Index = 0;
			
			p2[p2Counter++] = p1[p1Index++];
			if(p2Counter >= 52)
				p2Counter = 0;
			if(p1Index >= 52)
				p1Index = 0;
		}
		else
		{
			int p1WarIndex = p1Index;
			int p2WarIndex = p2Index;
			int equalCount = 0;
			
			if(p1Index > p1Counter)
				p1Counter = 52 + p1Counter;
			if(p2Index > p2Counter)
				p2Counter = 52 + p2Counter;	
			do
			{
				p1WarIndex += 4;
				p2WarIndex += 4;
					
				if(p1WarIndex >= p1Counter)
					break;
				if(p2WarIndex >= p2Counter)
					break;
				equalCount++;
			}while(p1[p1WarIndex % 52] == p2[p2WarIndex % 52]);
			
			//The war was ended
			if(p1WarIndex < p1Counter && p2WarIndex < p2Counter)
			{
				p1Counter = p1Counter % 52;
				p2Counter = p2Counter % 52;
				if(p1[p1WarIndex % 52] > p2[p2WarIndex % 52])
				{
					while(p1Index < p1WarIndex)
					{
						p1[p1Counter++] = p1[(p1Index++) % 52];
						if(p1Counter >= 52)
							p1Counter = 0;
							
						p1[p1Counter++] = p2[(p2Index++) % 52];
						if(p1Counter >= 52)
							p1Counter = 0;
					}
				}
				else
				{
					while(p2Index < p2WarIndex)
					{
						p2[p2Counter++] = p2[(p2Index++) % 52];
						if(p2Counter >= 52)
							p2Counter = 0;
							
						p2[p2Counter++] = p1[(p1Index++) % 52];
						if(p2Counter >= 52)
							p2Counter = 0;
					}
				}
				p1Index = p1Index % 52;
				p2Index = p2Index % 52;
			}
			else
				break;
		}
	}
	
	return numberOfRounds;
}
Example #16
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;
}
Example #17
0
File: sol.c Project: ocept/Klondike
int playGame() //simulates 1 game
{
	int table[7][20];
	int deck[52];
	int founds[4][13];
	int i,j,h;
	int wastePos;
	int win;
	
	//clear table
	for(i=0;i<7;i++){ 
		for(j=0;j<20;j++){
			table[i][j]=0;
		}
	}
	//clear foundations
	for(i=0;i<4;i++){ 
		for(j=0;j<13;j++){
			founds[i][j]=0;
		}
	}
	shuffleDeck(deck);
	layTable(table,deck);
	if(DEBUG >= 10) 
	{
		printf("Table at start of game:\n");
		printTable(table);
	}

	//solve
	for(i=0;i<20;i++){
		checkTable(table,founds);
		checkTable(table,founds);
		for(wastePos=0; wastePos<52; wastePos++) //check waste cards
		{
			if(deck[wastePos]!=99) checkWaste(table,deck, wastePos, founds);
		}
		
		//check for win
		win=1;
		for(j=0;j<4;j++){
			if(founds[j][12] == 0) win = 0;
		}
		if(win)
		{
			if(DEBUG >= 10) printf("Win after %d cycles", i);
			return 1;
		}
	}
	
	//print results
	if(DEBUG >= 10)
	{
		printf("\nFoundations at finish:\n");
		printFounds(founds);
		printf("\nTable at finish:\n");
		printTable(table);
	}

	if(DEBUG >= 10)
	{
		printf("\nRemaining cards in waste:\n");
		for(i=0;i<52;i++)
		{
			if(deck[i]!=99)
			{
				printCard(deck[i]);
				printf("\n");
			}
			//printf("%d\n",deck[i]%13);
		}
	}
	return 0;
}
Example #18
0
void Game::createGame() {
  setRules();
  createPlayers();
  shuffleDeck();
  dealCards();
}
Example #19
0
Deck::Deck( QObject* parent ) :
    QObject( parent )
{
    createDeck();
    shuffleDeck();
}
Example #20
0
void deal_cards(PLAYER usr[],DECK card[], THREAD tdata[], int* deckPosition){

    int i,j;
// Player score, handposition
    usr[0].score = 0;
    usr[1].score = 0;
    usr[2].score = 0;
    usr[0].handPos = 0;
    usr[1].handPos = 0;
    usr[2].handPos = 0;

//player score positions
    usr[0].x2 = 565;
    usr[1].x2 = 685;
    usr[2].x2 = 425;

    usr[0].y2 = 10;
    usr[1].y2 = 500;
    usr[2].y2 = 500;

//player card positions
    usr[0].x1 = 565;
    usr[1].x1 = 685;
    usr[2].x1 = 425;

    usr[0].y1 = 120;
    usr[1].y1 = 380;
    usr[2].y1 = 380;

//win, lose or busted message
    usr[0].x3 = 730;
    usr[0].y3 = 5;
    usr[1].x3 = 820;
    usr[1].y3 = 400;
    usr[2].x3 = 300;
    usr[2].y3 = 400;


    //*deckPosition = 0;
    for(i=0;i<5;i++){

        if(*deckPosition > 51) {
            shuffleDeck(card);
            *deckPosition = 0;
        }

        // Rectangles for positioning
        if(i==0) { //dealar first card
            cardRect(card,usr,deckPosition,0);
            checkHandValue(usr, card, 0, deckPosition);
        }

        if(i>0 && i < 3) { //player second and third card
            cardRect(card,usr,deckPosition,1);
            checkHandValue(usr, card, 1, deckPosition);
        }

        if(i>2 && i < 5) { //player second and third card
            cardRect(card,usr,deckPosition,2);
            checkHandValue(usr, card, 2, deckPosition);
        }

        for(j=0;j<2;j++) {
            sendDeckStruct(card, deckPosition, tdata[0].tconsocket[j]); // send current card to client
            printf("Deckposition: %d\n", *deckPosition);
        }
    }
    for(i=0;i<2;i++) {
        for(j=0;j<3;j++)
            sendUsrStruct(usr,j, tdata[0].tconsocket[i]);
    }
}
 int main (int argc, char **argv) {
  #ifdef CLEAR
  strcpy(table_path, dirname(argv[0]));
  strcat(table_path, "/");
  strcat(table_path, argv[2]);
  shm_unlink(table_path);
  #else
  // verifies if the number of arguments is correct
  if (argc != 4) {
    printf("usage: %s <player's name> <table's name> <nr. players>\n", argv[0]);
    return -1;
  }
  
  if (verifyCmdArgs(argv) == -1) {
    return -1;
  }
  
  // installs an exit handler
  if (atexit(exitHandler) == -1) {
    perror("atexit()");
    exit(-1);
  }
  
  blockSignals();
  
  initFIFO(argv[1]);
  
  initSharedMem(argv);
  
  waitForPlayers();
  
  pthread_t tid;
  
  if (is_dealer) {

    initDefaultDeck();
    printCardsList(cards, NULL);
    shuffleDeck();
    randomiseFirstPlayer();
    
    if ((errno = pthread_create(&tid, NULL, dealCards, NULL)) != 0) {
      perror("pthread_create()");
      exit(-1);
    }
  }
  receiveCards();
  reorderCardsList(hand);
  
  if (is_dealer) {
    pthread_join(tid, NULL);
  }
  //call thread responsible for showing info about the game and manage the game
  pthread_t tidG;
  if ((errno = pthread_create(&tidG, NULL, playGame, NULL)) != 0) {
    perror("pthread_create()");
    exit(-1);
  }
  
  pthread_join(tidG, NULL);
  
  #endif
  return 0;
}
Example #22
0
int main(){
	float result;
	char userName [40];
	int i, count, choice, betMarker, pLeft;
	double bet, pot;
	Player newPlayers [PLAYERS];

	/*Intro for the game*/
	setup(newPlayers, userName);
	Deck newDeck;
	srand(time(NULL));
	
	/*MAIN GAME LOOP, BASED ON GALLEONS*/

	while (newPlayers[0].galleons >= 5){
		printf("\n\n========  NEW ROUND  ========\n\n\n");
		pot = bet = 0;

		/*Always update each player to be in the game initially, unless they have less than 5 galleons*/
		for(i = 0; i<PLAYERS; i++){
			newPlayers[i].inGame = 1;
			if (newPlayers[i].galleons < 5){
				newPlayers[i].galleons = 0;
				newPlayers[i].inGame = 0;
				newPlayers[i].checkBet = 0;
			}
			newPlayers[i].checkBet = 0;
			newPlayers[i].galleons = newPlayers[i].galleons - 5;
			if(newPlayers[i].inGame == 1){
				pot = pot + 5;
			}
		}
		/*if the three computers are out of the game, user has won, so end the game!*/
		pLeft = 0;
		for (i = 1; i<PLAYERS; i++){
			if (newPlayers[i].inGame == 0){
				pLeft++;
			}
			if (pLeft == 3){
				printf("\n\nCONGRATULATIONS %s, you won! Harry, Voldy and Dumbly seem to have lost all their money!!!\n\n", newPlayers[0].name);
				return 0;
			}
		}


		sleep(2);
		/*Print out current standings, how many galleons each player has and if they are still in*/	
		printf("Current Standings:\n\n");
		for (i = 0; i<PLAYERS; i++){
			if (newPlayers[i].inGame == 1){
				printf("%s has %.2f Galleons\n", newPlayers[i].name, newPlayers[i].galleons);
			}
		}
		for (i = 0; i<PLAYERS; i++){
			if (newPlayers[i].inGame == 0){
				printf("%s is out of the game\n", newPlayers[i].name);
			}
		}

		/*Initialize Game*/
		fillDeck(&newDeck);
		shuffleDeck(&newDeck);
		dealOut(&newDeck, newPlayers);
		printf("\nYou are about to receive your hand!\n");
		
		/*User specifics*/
		sleep(1);
		displayHand(newPlayers);	
		result = MC_rate(newPlayers, 0);
		sleep(1);

		/*Monte Carlo Advice in MC.c*/
		advice(&newPlayers[0]);

		/*This exchange is only for the user, computers have its own function*/
		exchangeCards(newPlayers, &newDeck);
		displayHand(newPlayers);
		printf("\nYou can now either: \n1) Place a Bet\n2) Pass\n3) Fold\nPlease type 1,2, or 3: ");
		scanf("%d", &choice);
		if (choice == 1){
			bet = placeBet(newPlayers, &pot);
		}
		else if (choice == 3){
			endPlayer(newPlayers, 0);
		}
		else if (choice == 2){
			printf("You have chosen to pass.\n");
		}

		/*run Computer exchanges and whether they decide to raise, pass or fold*/	
		count = 1;
		while (count <= 3){
			if(newPlayers[count].inGame == 1){
				
				sleep(1);
				/*result value is more for testing purposes, but still needs to run to fill in the correct*/
				/*int array mcAdivce for Monte carlo advice for each computer*/
				result = MC_rate(newPlayers, count);
				
				/*This can be found in exchange.c, just exchange based on MC advice*/
				computerExchange(newPlayers, &newDeck, count);
				
				/*This can be found in display.c, determines for the computer what to do (raise, pass, fold)*/
				comp_decision(newPlayers, count, &bet, &pot);
				
				/*keep track of which computer raises for later*/
				if (newPlayers[count].checkBet > 0){
					betMarker = count;
				}
			}

			count++;
		}


		/*if any computer raises, then go back to user one time to either match or fold, then each computer*/

		if (betMarker > 0){
			if (newPlayers[0].galleons < newPlayers[betMarker].checkBet){
				printf("\nOH NOOOO, the bet is more than you can afford, and by wizarding rules, you lose the game!!!\n\n");
					return 0;
			}
		if (newPlayers[0].inGame != 0){	
		printf("%s raised by %.2lf. Would you like to either:\n1) Match\n2) Fold \nPlease type 1 or 2: ", newPlayers[betMarker].name, newPlayers[betMarker].checkBet);
		scanf("%d", &choice);
			if (choice == 1){
				newPlayers[0].galleons = newPlayers[0].galleons - newPlayers[betMarker].checkBet;
				pot = pot + newPlayers[betMarker].checkBet;
				printf("You have matched the raise\n");
			}
			else {
				newPlayers[0].inGame = 0;
				printf("You Folded\n");
			}
			}

	/*Determine whether each computer should match or fold*/	
		for (i = 1; i<betMarker; i++){
			if (newPlayers[i].inGame == 1){
				if (newPlayers[i].galleons > newPlayers[betMarker].checkBet){
					result = analyze_hand(newPlayers[i]);
					if (result < 23){
						newPlayers[i].inGame = 0;
						printf("%s has folded\n", newPlayers[i].name);
					}
					else{	
						newPlayers[i].galleons = newPlayers[i].galleons - newPlayers[betMarker].checkBet;	
						pot = pot + newPlayers[betMarker].checkBet;
						printf("%s has matched the raise\n",newPlayers[i].name);
						}
					}
				}
			}
		}
		
		sleep(2);
		/*find winner of the game, give winning's to rightful winner, display winner's hand*/
		findWinner(newPlayers, &pot);
		
		/*reset betMarker for next game*/
		betMarker = 0;
		sleep(2);
	}

	/*After game loop, you have lost all your money or you have less than the 5 Galleon entrance amount*/	
		printf("\n\nOH NO, you lost at Wizard's poker!!!!\n");
		printf("Final Galleon Count for each player:\n");
		for (i = 0; i < PLAYERS; i++){
			if (newPlayers[i].galleons < 0){
				newPlayers[i].galleons = 0;
			}
			printf("\t%s has %.2f Galleons\n", newPlayers[i].name, newPlayers[i].galleons);
		}

	return 0;
}
Example #23
0
/**
 * 
 * @param argc
 * @param argv
 * @return 
 */
int main(int argc, char** argv) {
    
    //Command line input
    int players;
    int cards;
    
    cards = atoi(argv[1]);
    players = atoi(argv[2]);
    
    cards = 5; //Force cards to 5
//    players = 5;

    //Validate
    if (players < MINPLAYERS || players > MAXPLAYERS) {
        printf("Players too high or too low!!!\n");
        return 0;
    }
    if (cards < MINCARDS || cards > MAXCARDS) {
        printf("Cards too high or too low!!!\n");
        return 0;
    }
        
    //Create
    struct card deck[NUMCARDS]; //Create the deck
    createDeck(deck); //Fill the deck
    
    //Display
    printf("Unshuffled deck(suit, val):\n");
    //printDeck(deck);
    printDeckColumned(deck);
    printf("\n");
    
    //Shuffle
    shuffleDeck(deck);
    
    //Display
    printf("Shuffled deck(suit, val):\n");
    //printDeck(deck);
    printDeckColumned(deck);
    printf("\n");
    
    //Deal
    printf("Dealing.\n\n");
    struct hand * hands = dealDeck(players, cards, deck);
    
    //Show Hands
    printf("Player's hands(unsorted):\n");
    printHandsNamed(players, cards, hands);
    printf("\n");
    
    //Sort Hands
    sortHands(players, cards, hands);
    
    //Show Hands
    printf("Player's hands(sorted):\n");
    printHandsNamed(players, cards, hands);
    printf("\n");
    
//    //Fixed cards for testing (suit, value)
//    struct card card1 = makeCard(4,3);
//    struct card card2 = makeCard(4,4);
//    struct card card3 = makeCard(4,5);
//    struct card card4 = makeCard(4,6);
//    struct card card5 = makeCard(4,7);
//    
//    //Fixed hand for testing
//    struct hand fixedHand;
//    fixedHand.cards[0] = card1;
//    fixedHand.cards[1] = card2;
//    fixedHand.cards[2] = card3;
//    fixedHand.cards[3] = card4;
//    fixedHand.cards[4] = card5;
//    
//    printHandsNamed(1,5,&fixedHand);
//    rankHand(&fixedHand);
//    printHandsNamed(1,5,&fixedHand);
//    printf("%i",fixedHand.rank);
    
    //Rank hands
    rankHands(players, hands);
    
    //Show hands with ranks
    printf("Players hands' ranks:\n");
    printHandsRanked(players, hands);
    
    //Determine winner
    determineWinner(players, hands);
    
    return (0);
}
Example #24
0
void Field::initializeField(int player_num) {
  setPlayerNum(player_num);
  makeInfomationArrays();
  determinePanelsOrder();
  shuffleDeck();
}
Example #25
0
int main(void){
  printf("WELCOME TO HENRYS BLACKJACK\n");
  //player1
  struct player player1;
  printf("Type name for player1: ");
  scanf("%s",player1.name);
  getchar();
  player1.playerHand = NULL;
  player1.cardValue = 0;
  player1.startCards = 2;
  printf("utan ptr: %d\n", player1.startCards);
  //player2
  struct player player2;
  printf("Type name for player2: ");
  scanf("%s",player2.name);
  getchar();
  player2.playerHand = NULL;
  player2.cardValue = 0;
  player2.startCards = 2;
  //computer
  struct player comp;
  strcpy(comp.name, "the dealer");
  comp.playerHand = NULL;
  comp.cardValue = 0;
  comp.startCards = 1;

  int cardCounter = 0;
  struct card *deck = NULL;
  deck = createDeck(deck);    // deck created
  int i;
  for(i=0; i<20; i++)
    shuffleDeck(deck);   // deck shuffled

  /* Name giving and first cards dealing  */
  printf("\n\nHand of %s:\n", player1.name);
  dealCards(&player1, deck, &cardCounter);
  printCards(player1.playerHand, player1.startCards);
  printf("\n\nHand of %s: \n", player2.name);
  dealCards(&player2, deck, &cardCounter);
  printCards(player2.playerHand, player2.startCards);

  printf("\n\nHand of %s: \n", comp.name);
  dealCards(&comp, deck, &cardCounter);
  printCards(comp.playerHand, comp.startCards);


  /* Hit or stay face  */
 printf("------------------------------------------------------\n");
 player1.cardValue = actDealer(player1,deck, &cardCounter,22);
 printf("------------------------------------------------------\n");
 player2.cardValue = actDealer(player2,deck, &cardCounter,22);
 printf("------------------------------------------------------\n");
    if(player1.cardValue  == -1 && player2.cardValue == -1){  // if both loses dealer does not have to continue
        printf("Both players lost!\n");
        printf("--------------------Game-finished---------------------\n");
        goto masteJagKompletteraNufragetecken;
    }
 comp.cardValue = actDealer(comp,deck,&cardCounter,17);
 printf("------------------------------------------------------\n");
 whoWon(player1, player2, comp);
 printf("--------------------Game-finished---------------------\n");

masteJagKompletteraNufragetecken:
/* Freeing memory */
free(deck);
free(player1.playerHand);
free(player2.playerHand);
free(comp.playerHand);
return 0;
}