int shuffleDeck(int *deck, int deckSize) { for (int i = 0; i < deckSize; ++i) deck[i] = i; printDeck(deck, deckSize); srand(time(NULL)); int *buf = (int *)malloc(deckSize * sizeof(int)); int mixCount = MIXCOUNT; if (mixCount <= 0) return ERRINPUT; for (;mixCount > 0; --mixCount) { int shift = rand() % deckSize; int shiftCycles = MIXCYCLMIN + rand() % (MIXCYCLMAX - MIXCYCLMIN); memcpy(buf, deck, shift * sizeof(int)); memmove(deck, deck + shift, (deckSize - shift) * sizeof(int)); for (int i = shiftCycles; (shift > 0) && (i > 0); --i) { int miniShift = rand() % (shift + 1); memcpy(deck + deckSize - shift, buf + shift - miniShift, miniShift * sizeof(int)); shift -= miniShift; } memcpy(deck + deckSize - shift, buf, shift * sizeof(int)); } printDeck(deck, deckSize); free(buf); return 0; }
int main(){ // int i, j; Deck deck = initDeck(); printDeck( deck ); Deck deck2 = initDeck(); printDeck( deck2 ); /* shuffle( deck ); */ /* printDeck( deck ); */ /* printf("\n"); */ /* printf("Suppose user is player 4 and other players are AI. \n"); */ /* Hand h[player_number]; */ /* for(i=0; i<player_number; i++){ */ /* h[i] = initHand(i); */ /* for(j=0; j<hand_size; j++){ */ /* h[i]->handCard[j] = getCard( deck ); */ /* } */ /* h[i]->size = 5; */ /* } */ /* for(i=0; i<player_number; i++){ */ /* printf("player %d: ", h[i]->userLable); */ /* printHand(h[i]); */ /* } */ /* for(i=0;i<player_number - 1;i++){ */ /* printf("player %d throw card\n", h[i]->userLable); */ /* h[i]->grade = gradeHand(h[i]); */ /* printf("Grade before throw is: %Lf \n", h[i]->grade); */ /* h[i]->grade = AIThrow(h[i]); */ /* printf("Grade after throw is: %Lf \n", h[i]->grade); */ /* } */ /* printf("User throw card now\n"); */ /* printf("test\n"); */ return 0; }
int main(int argc, const char * argv[]) { // insert code here... char *trump = "false"; deck *Standard, *shuffled, *reshuffled; card *popper; printf("Hello, World!\n"); printf("welcome to BLACK JIGGITY JACK, JACK!\n\n\n"); Standard = createDeck(); printDeck(Standard); setTrumpSuit(suitArr[3]); if (isTrump(Standard->cards[1]->suit)) {trump = "true";} printf("\nHearts are trump: %s\n", trump); if (isTrump(Standard->cards[51]->suit)) {trump = "true";} else trump = "false"; printf("Clubs are trump: %s\n", trump); desetTrumpSuit(suitArr[3]); printf("\ndesetting trump suit\n"); if (isTrump(Standard->cards[51]->suit)) {trump = "true";} else trump = "false"; printf("Clubs are trump: %s\n", trump); printf("\nFirst Ace is: %s\tvalue: %d\nSecond Ace is: %s\tvalue: %d\n", Standard->cards[12]->name, Standard->cards[12]->value->weight, Standard->cards[25]->name, Standard->cards[25]->value->weight); setLoAce(Standard->cards[12]); printf("Changing first ace to lo.\n"); printf("\nFirst Ace is: %s\tvalue: %d\nSecond Ace is: %s\tvalue: %d\n", Standard->cards[12]->name, Standard->cards[12]->value->weight, Standard->cards[25]->name, Standard->cards[25]->value->weight); setLoAce(Standard->cards[12]); printf("Is first ace hi? %d\tIs second ace hi? %d\n", isHiAce(Standard->cards[12]), isHiAce(Standard->cards[25])); isHiAce(Standard->cards[25]); setHiAce(Standard->cards[12]); printf("Is first ace hi? %d\tIs second ace hi? %d\n", isHiAce(Standard->cards[12]), isHiAce(Standard->cards[25])); shuffled = shuffle(Standard); reshuffled = shuffle(shuffled); printDeck(reshuffled); popper = _popCard(reshuffled); printf("first card from the top: \n\t%s\n", popper->name); popper = _popCard(reshuffled); printf("second card from the top: \n\t%s\n", popper->name); _deleteDeck(reshuffled); return 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); }
int main(){ char user; printf("\nTo Play: "); printf("\n Black Jack-"); printf("enter 'b'"); printf("\n Poker-"); printf("enter 'p'"); printf("\n Neither-"); printf("enter 'n'"); printf("\nWhich do you choose: "); user = getchar(); if(user == 'b'){ initialize(); playBlackJack(); main(); }else if (user == 'p'){ printDeck(); playPoker(); main(); }else if(user == 'n'){ main(); } }
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); }
/* deal function which accepts the 2 arguments passed by the command-line, already converted to int, create a new deck, shuffle the deck, print the deck, then deal and display the cards that each player has. */ void deal(int numberOfCards, int numberOfPlayers) { int forPlayer, forCard, currentCard = 0; printf("\n\nReady to deal %d card(s) to %d player(s)...\n\n", numberOfCards, numberOfPlayers); struct deck cardDeck, *point = &cardDeck; cardDeck = createDeck(); /* Create the cardDeck with all cards */ shuffle(point); printDeck(point); struct card players[numberOfPlayers]; struct card *playersPoint; playersPoint = players; printf("\n"); for(forPlayer = 0; forPlayer < numberOfPlayers; ++forPlayer) { if (numberOfCards > 1) /* If numberOfCards is greater than 1 then we must display all of the cards the players have */ { for (forCard = 0; forCard < numberOfCards; ++forCard) { playersPoint[forCard] = cardDeck.deckOfCards[currentCard]; currentCard++; } } else { /* Otherwise, the number of cards is 1. */ playersPoint[forPlayer] = cardDeck.deckOfCards[currentCard]; currentCard++; } } printf("\n"); printHands(numberOfCards, numberOfPlayers, point); }
void testDeckShuffle(struct gameState* G){ int deckCount; int player= G->whoseTurn; int empty_deck[MAX_DECK]; int i; for (i = 0; i < MAX_DECK; i++) { empty_deck[i]=""; } int j; G->discardCount[player]=G->deckCount[player]; for(j=0;j<G->discardCount[player]; j++){ G->discard[player][j]= j; } printf("Test 1) Testing when deck is empty, discard pile is shuffled, then added to deck\npreconditions: deck is empty, discard is full of unique cards (to check shuffle)\npostconditions: deck has cards\n"); deckCount=G->deckCount[player]; //deck count stored G->deckCount[player]=0; // deck count in state set to zero G->hand[player][0]=7; // adventurer card put in player's hand at pos zero memcpy(G->deck[player], empty_deck, sizeof(int) * deckCount); // player's deck is emptied printf("\n(before function call, to compare with deck cards post function call)\n"); printDiscard(player,G); printf("\n(deckCount before calling play_adventurer: %d)\n",G->deckCount[player]); printf("results: \n"); play_adventurer(player,G,0); //takes player, game ptr, and hand position of adventurer card printf("(deck count after calling play_adventurer)\n - Expected: at least 1 Actual: %d\n\n", G->deckCount[player]); printf("(Visual check to see that new cards in deck are shuffled)\n"); printDeck(player,G); }
int main(void) /* * function is used mainly to call the other functions. It also allocates the memory for the deck * prints a message and exits if it can not be done. It also intializes the pointer-arrays that are * used by other functions to create the deck that cards are printed from. */ { Card *deck = NULL; const char *faces[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight", "Nine","Ten","Jack","Queen","King"}; const char *suits[] = {"Spades","Diamonds","Clubs","Hearts"}; // allocate memory for the deck of cards if ((deck = (Card *)calloc(DECK_SIZE,sizeof(Card))) == NULL) { printf("Unable to allocate card structure"); return 0; } // initializes the deck of cards from the pointers/struct initDeck(deck); // shuffles the deck based on the rand() function shuffle(deck); // print the deck of cards printDeck(deck,faces,suits); readCardsPrinted(); // free the memory used by the deck of cards free(deck); return 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; }
int main(void) { std::srand( std::time(0) ); CardPrinter cp; std::string std_suits = "S,H,C,D"; std::string std_values = "A,2,3,4,5,6,7,8,9,T,J,Q,K"; std::string long_suits = " of Spades, of Hearts, of Clubs, of Diamonds"; std::string long_values = "Ace,2,3,4,5,6,7,8,9,Ten,Jack,Queen,King"; cp.loadFromString(long_suits, long_values); Playmat pm; std::string input; std::deque<std::string> cmd; std::string drawDeck = "deck"; std::string handDeck = "hand"; while( input != "quit" ){ std::cout << ":"; getline(std::cin, input); cmd = split(input); if(cmd[0] == "d" || cmd[0] == "draw"){ Card c; c = pm.getDeck(drawDeck).draw(); if(c.isValid()){ pm.getDeck(handDeck).add(c); std::cout << cp.print(pm.getDeck(handDeck).peek(1)) << std::endl; } else { std::cout << "Out of cards in " << drawDeck; } } else if (cmd[0] == "long"){ cp.loadFromString(long_suits, long_values); } else if (cmd[0] == "list"){ for(int i = 0; i < pm.numDecks(); i++){ if(pm.getDeckName(i) == drawDeck){ std::cout << "d "; } if(pm.getDeckName(i) == handDeck){ std::cout << "h "; } std::cout << pm.getDeckName(i) << " "; std::cout << pm.getDeck(i).cardsLeft() << std::endl; } } else if (cmd[0] == "short"){ cp.loadFromString(std_suits, std_values); } else if (cmd[0] == "show" && cmd.size() > 1){ printDeck(pm.getDeck(cmd[1]), cp); } else if (cmd[0] == "rebuild" && cmd.size() > 1){ pm.getDeck(cmd[0]).buildDeck(4, 13); } else if (cmd[0] == "shuffle" && cmd.size() > 1){ pm.getDeck(cmd[1]).shuffle(); } else if (cmd[0] == "add" && cmd.size() > 1){ pm.addDeck(cmd[1]); } else if (cmd[0] == "clear" && cmd.size() > 1){ pm.getDeck(cmd[1]).clearDeck(); } else if (cmd[0] == "put" && cmd.size() > 1){ Card c; int card = atoi(cmd[1].c_str()); c = pm.getDeck(handDeck).take(card); pm.getDeck(drawDeck).add(c); } else if (cmd[0] == "set" && cmd.size() > 2){ if(cmd[1] == "draw"){ drawDeck = cmd[2]; } if(cmd[1] == "hand"){ handDeck = cmd[2]; } } } return 0; }
int main(int argc, const char * argv[]) { /* local variables used here */ int max, numCardsPerHand, numOfPlayers, i, j, cardsPtr = 0, *ptr = &cardsPtr; struct deck thedeck, *pointer = &thedeck; thedeck = createDeck(); struct player thePlayers[MAX_NUM_PLAYERS]; time_t t; srand((unsigned)time(&t)); // generate seed for random value /* validation of user input. No more than 3 args. */ if( argc == ARGNUM ){ /* parse the arguments to integer values and compute maximum legal values allowed for game */ numCardsPerHand = atoi(argv[CARD_INPUT]); numOfPlayers = atoi(argv[PLAYER_INPUT]); if(validateInput(numCardsPerHand, numOfPlayers)) /* If the validateInput function returns a 1 then the inputs were correct and the game of cards can begin */ { /* create and display deck, shuffle the deck, deal and display hands */ thedeck = createDeck(); printf("\nThe standard deck:\n\n"); printDeck(pointer); printf("\nThe shuffled deck:\n\n"); shuffle(pointer); printDeck(pointer); //deal(pointer, numCardsPerHand, numOfPlayers); //sortHands(pointer, numCardsPerHand, numOfPlayers); //rankHands(gamer, numCardsPerHand, numOfPlayers); for (i = 0; i < numOfPlayers; i++) { thePlayers[i] = deal2(numCardsPerHand, pointer); } displayHands2(thePlayers, numCardsPerHand, numOfPlayers, numCardsPerHand); //deal(pointer, numCardsPerHand, numOfPlayers); //sortHands(pointer, numCardsPerHand, numOfPlayers); sortHands2(thePlayers, numCardsPerHand, numOfPlayers); displayHands2(thePlayers, numCardsPerHand, numOfPlayers, numCardsPerHand); printDeck(pointer); //hasHighCard(pointer, numCardsPerHand, numOfPlayers); findHighCard(thePlayers, numOfPlayers, numCardsPerHand); } }else{ /* here as specified, display error and terminate due to incorrect number of args entered by user */ printf("Sorry that is incorrect. You must enter the number of cards per hand\n"); printf("followed by the number of players separated by a space.\n\n"); } return 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; }