Node *SortHand(Node* head){ //loops through the deck and finds the smallest card in each case, creating a temporary deck to keep track of their order Node* newDeck = NULL; int oldDeckSize = DeckSize(head); int cnts = 0; Node* prevSmallestCard = NULL; for(int x = 0; x < oldDeckSize; x++){ resetDeck(head); if(newDeck != NULL) resetDeck(newDeck); Node* currCard = head; Node* smallestCard = NULL; while(currCard != NULL){ if(cardAlreadyPresent(newDeck, currCard) != true){ if(smallestCard == NULL ){ smallestCard = currCard; } else if(currCard->getCard() < smallestCard->getCard()){ smallestCard = currCard; } } currCard = currCard->after; } //this was used in a privious attempt at sorting, it remains the same because it works to a degree in sorting if(smallestCard != NULL){ if(cardAlreadyPresent(newDeck, smallestCard) != true){ Node* newCard = new Node(smallestCard->getCard()); if(newDeck != NULL){ fetchHead(newDeck); newDeck->after = newCard; newCard->before = newDeck; } smallestCard->sorted_prev = prevSmallestCard; if(prevSmallestCard != NULL) prevSmallestCard->sorted_next = smallestCard; prevSmallestCard = smallestCard; newDeck = newCard; cnts++; } } else{ Node* newCard = new Node(currCard->getCard()); fetchHead(newDeck); newDeck->after = newCard; newCard->before = newDeck; cnts++; smallestCard->sorted_prev = prevSmallestCard; if(prevSmallestCard != NULL) prevSmallestCard->sorted_next = smallestCard; prevSmallestCard = smallestCard; } } resetDeck(newDeck); DeleteAllCards(newDeck); findSortedHead(head); return head; }
void Deal(Node* &head, Node** hands, int numHands, std::string numCardsToDeal, int numCardsPerHand){ //this works by looping through the deck and giving each hand a card untill either the deck is out or the hand has all the cards they need Node* dealer = head; Node* newHead = NULL; bool first_card = true; while(dealer != NULL){ for(int x = 0; x < numHands; ++x){ if(dealer != NULL){ if(first_card){ delete hands[x]; hands[x] = new Node(dealer->getCard()); dealer = dealer->after; } else{ Node* card = hands[x]; int cnts = 0; while(card->after != NULL){ card = card->after; cnts++; } if(cnts < numCardsPerHand - 1){ Node* new_card = new Node(dealer->getCard()); card->after = new_card; new_card->before = card; } else{ if(newHead == NULL){ delete newHead; newHead = new Node(dealer->getCard()); } else{ Node* new_card = new Node(dealer->getCard()); newHead->after = new_card; new_card->before = newHead; newHead = new_card; } } resetDeck(hands[x]); dealer = dealer->after; } } } first_card = false; } if(newHead != NULL) resetDeck(newHead); DeleteAllCards(head); head = newHead; }
CDeck::CDeck(ID3D10Device* pD3D10Device, bool bFull) { int i; setDevice(pD3D10Device); for (i=0;i<NUM_CARD_VIEWS;i++) { m_cardGraphics[i].pShaderResource = NULL; m_cardGraphics[i].lpFileName[0] = '\0'; m_cardGraphics[i].lpCardDesc[0] = '\0'; m_cardGraphics[i].ID = 0; } resetDeck(bFull); #if defined(_DEBUG) //////////////////////////////// // Testing // Bang on the list swap functions //////////////////////////////// { bool bTestResult = true; //bTestResult = m_deck.testDriveSwap(); if (!bTestResult) { std::cout << "ERROR in testDriveSwap()!!\n" << std::flush; } } #endif // _DEBUG InitSprite(getDevice(), bFull); return; }
void SolitaireCrypto::keydeck() { resetDeck(); for(uint8_t i=0;i<passkey.length();i++) { solitaire(); countcut(charCodeAt(passkey[i])-64); } }
SolitaireCrypto::SolitaireCrypto() { // initialize random seed: srand (time(NULL)); resetDeck(); keystream.assign(""); cleartext.assign(""); ciphertext.assign(""); passkey.assign(""); }
Node* Shuffle(Node* &top, Node* &bottom, std::string keyString){ //this is where my memory leaks if(keyString == "perfect"){ //Node* shuffledDeck = NULL; Node* topTrace = top; Node* bottomTrace = bottom; Node* prevNode = NULL; while(topTrace != NULL && bottomTrace != NULL){ if(prevNode != NULL) prevNode->after = topTrace; topTrace->before = prevNode; prevNode = topTrace; topTrace = topTrace->after; prevNode->after = bottomTrace; bottomTrace->before = prevNode; prevNode = bottomTrace; bottomTrace = bottomTrace->after; /* shuffledDeck = new Node(topTrace->getCard()); shuffledDeck->before = prevNode; if(prevNode != NULL) prevNode->after = shuffledDeck; prevNode = shuffledDeck; shuffledDeck = new Node(bottomTrace->getCard()); shuffledDeck->before = prevNode; prevNode->after = shuffledDeck; prevNode = shuffledDeck; topTrace = topTrace->after; bottomTrace = bottomTrace->after; */ } topTrace = NULL; bottomTrace = NULL; resetDeck(top); resetDeck(bottom); bottom = NULL; std::cout << " BOTTOM IS EQUAL TO: " << bottom << std::endl; //DeleteAllCards(top); //DeleteAllCards(bottom); //resetDeck(shuffledDeck); return top; } else return NULL; }
//* * * * * * * * * * * * * * * * * * * * * * * * // dealCard() removes a card from the deck * //* * * * * * * * * * * * * * * * * * * * * * * * Card BlackJackDeck::dealCard() { if (deck.empty() == false) { lastCard = deck.back(); deck.pop_back(); } else { std::cout << "The deck is empty... reshuffling"; resetDeck(); lastCard = deck.back(); deck.pop_back(); } return lastCard; }
RoundManager::RoundManager(std::vector<MyPlayerP*> playersOfThisRound, APlayerControllerP* pc, int amountOfPlayersRemaining, int dealerIndex, int smallBlind, int bigBlind) { playerController = pc; this->amountOfPlayersRemaining = amountOfPlayersRemaining; resetDeck(); players = std::vector<MyPlayerP*>(8); for (int i = 0; i < amountOfPlayersRemaining; ++i) { players[i] = playersOfThisRound[i]; int card0[2] = { FMath::RandRange(0, 3), FMath::RandRange(0, 12) }; int card1[2] = { FMath::RandRange(0, 3), FMath::RandRange(0, 12) }; while (!controlDeck(card0[0], card0[1])) { card0[0] = FMath::RandRange(0, 3); card0[1] = FMath::RandRange(0, 12); } while (!controlDeck(card1[0], card1[1])) { card1[0] = FMath::RandRange(0, 3); card1[1] = FMath::RandRange(0, 12); } players[i]->initializeNewRound(card0[0], card0[1], card1[0], card1[1]); } this->dealerIndex = dealerIndex; currentPlayerIndex = (dealerIndex + 3 ) % amountOfPlayersRemaining; addPot(); pot = 0; lastBet = bigBlind; roundState = PREFLOP; this->smallBlind = smallBlind; this->bigBlind = bigBlind; currentMaxBet = bigBlind; playersDidActions = 0; settingBlinds(); }
Deck::Deck() { resetDeck(); qsrand(QDateTime::currentMSecsSinceEpoch()); }
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; }
// triggers winning calculation and manages winning pot(s) void RoundManager::roundOver() { // can be used for debugging calculator: //players[0]->initializeNewRound(3, 10, 3, 6); //players[1]->initializeNewRound(2, 12, 1, 12); //amountOfPlayersRemaining = 2; //flop[0] = new Card(0,5); //flop[1] = new Card(0,2); //flop[2] = new Card(0,3); //turn = new Card(2,5); //river = new Card(1,9); // -- debugging if (amountOfPlayersRemaining > 1) { Calculator* calc = new Calculator(); // debugging: calc->setPlayerController(playerController); int value = -1; int player = 0; int highestPlayerSoFar[2] = { -1, -1 }; // [0] = quality of cards, [1] player's index within players[]; int currentPlayer[2]; // [0] = quality of cards, [1] player's index within players[]; int comparisonKeyCardsArray[5]; TArray<int> splitPotCandidates; for (int k = 0; k < pots.Num(); ++k) // going throught this procedure for each pot { for (int i = 0; i < amountOfPlayersRemaining; ++i) // checking each player { if (players[i]->getPotAssignment() >= k) // checking to which pot player belongs to { // the player with the highest potAssignment, obviously plays for each pot available currentPlayer[0] = calc->qualityOfCards(players[i]->getCard0(), players[i]->getCard1(), flop[0], flop[1], flop[2], turn, river); currentPlayer[1] = i; if (currentPlayer[0] > highestPlayerSoFar[0]) { highestPlayerSoFar[0] = currentPlayer[0]; highestPlayerSoFar[1] = currentPlayer[1]; splitPotCandidates.Empty(); for (int n = 0; n < 5; ++n) comparisonKeyCardsArray[n] = calc->keyCards[n]->getValue(); // so keyCards[] will always be the current array, and comparisonKeyCards[] will always be the highest player's one } else if (currentPlayer[0] == highestPlayerSoFar[0]) // checking for possible splitPot { int splitPotCounter = 0; for (int n = 0; n < 5; ++n) { if (calc->keyCards[n]->getValue() > comparisonKeyCardsArray[n]) { highestPlayerSoFar[0] = currentPlayer[0]; highestPlayerSoFar[1] = currentPlayer[1]; splitPotCandidates.Empty(); break; } else if (calc->keyCards[n]->getValue() == comparisonKeyCardsArray[n]) { splitPotCounter++; } } if (splitPotCounter == 5) { splitPotCandidates.Add(highestPlayerSoFar[1]); splitPotCandidates.Add(currentPlayer[1]); } } } } #pragma region defining winner String FString winner; if (highestPlayerSoFar[0] == 0) winner = "High Card!"; if (highestPlayerSoFar[0] == 1) winner = "Pair!"; if (highestPlayerSoFar[0] == 2) winner = "Two Pair!"; if (highestPlayerSoFar[0] == 3) winner = "Triple!"; if (highestPlayerSoFar[0] == 4) winner = "Straight!"; if (highestPlayerSoFar[0] == 5) winner = "Flush!"; if (highestPlayerSoFar[0] == 6) winner = "Full House!"; if (highestPlayerSoFar[0] == 7) winner = "Quads!"; if (highestPlayerSoFar[0] == 8) winner = "Straight Flush!"; #pragma endregion if (splitPotCandidates.Num() > 0) // that means split pot { playerController->debugMessage("split pot ! splitting pot " + FString::FromInt(k) + " with: "); for (int i = 0; i < splitPotCandidates.Num(); ++i) { players[splitPotCandidates[i]]->increaseChips(pots[k] / splitPotCandidates.Num()); playerController->debugMessage("" + players[splitPotCandidates[i]]->getName() + " (" + winner + ")"); } } else { playerController->debugMessage("" + players[player]->getName() +" with: " + winner + "wins pot " + FString::FromInt(k)); players[player]->increaseChips(pots[k]); } } calc->~Calculator(); } else playerController->debugMessage("aaaaand the winner is: " + players[currentPlayerIndex]->getName() + " !"); pots.Empty(); // should be redundant resetDeck(); playerController->roundFinished(); }
int main (int argc, char *argv[]) { int numClients = argc - 1; fd_set readfds, activefds, tempfds; char buffer[BUFFSIZE]; struct hand *myHand = NULL; struct hand *temp; struct client clients[numClients]; struct timeval timeout; int clientsPlaying = 0; int numRead; int i; resetDeck(); //have the dealer get its first 2 cards myHand = initCard(); myHand->next = initCard(); //have the clients get their first 2 cards for (i = 0; i < numClients; i++) { clients[i].hand = initCard(); clients[i].hand->next = initCard(); } //initialize the active client list FD_ZERO (&activefds); FD_ZERO (&tempfds); for (i = 1; i < argc; i++) { int socket = atoi(argv[i]); FD_SET (socket, &activefds); clients[clientsPlaying].socket = socket; clients[clientsPlaying].countdown = 10; clients[clientsPlaying].finished = 0; //send the initial cards to the client temp = clients[clientsPlaying].hand; strcpy(buffer, temp->card); strcat(buffer, temp->next->card); strcat(buffer, myHand->card); if (write(socket, buffer, sizeof(buffer)) < 0) { fprintf(stderr, "ERROR contacting client.\n"); FD_CLR(socket, &activefds); } clientsPlaying++; } while (clientsPlaying > 0) { fflush(stderr); //disconnect any clients who have timed out for (i = 0; i < numClients; i++) { if ((clients[i].countdown < 1) && (clients[i].finished == 0)) { fprintf(stderr, "A client has timed out.\n"); FD_CLR(clients[i].socket, &activefds); close(clients[i].socket); clients[i].finished = 1; clientsPlaying--; } } readfds = activefds; timeout.tv_sec = 1; timeout.tv_usec = 0; numRead = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout); if (numRead < 0) { fprintf(stderr, "ERROR with select %d\n", errno); continue; } //keep track of the inactivity for each client if (numRead == 0) { fprintf(stderr, "."); for (i = 0; i < numClients; i++) if (clients[i].finished == 0) clients[i].countdown--; continue; } //check for the client input for (i = 0; i < numClients; i++) { if (FD_ISSET (clients[i].socket, &readfds) && (clients[i].finished == 0)) { if (read(clients[i].socket, buffer, sizeof(buffer)) < 0) { fprintf(stderr, "ERROR reading the client's message.\n"); continue; } //give the player another card if (strcasecmp(buffer, "HIT") == 0) { clients[i].countdown = 10; struct hand *last = clients[i].hand; while (last->next != NULL) last = last->next; last->next = initCard(); write(clients[i].socket, last->next->card, sizeof(last->next->card)); //check to see if the client bust if (handValue(clients[i].hand) > 21) { fprintf(stderr, "A client has bust.\n"); FD_CLR(clients[i].socket, &activefds); close(clients[i].socket); clients[i].finished = 1; clientsPlaying--; } } else if (strcasecmp(buffer, "STAND") == 0) { //remove the client from ongoing gameplay fprintf(stderr, "A client has chosen to stand.\n"); clients[i].finished = 1; FD_CLR(clients[i].socket, &activefds); FD_SET(clients[i].socket, &tempfds); clientsPlaying--; } } } } activefds = tempfds; //calculate the rest of the dealer's hand, first check for soft 17 if ((getValue(myHand->card) == 1 || getValue(myHand->next->card) == 1) && dealerValue(myHand) == 17) myHand->next->next = initCard(); //keep hitting until 17 or higher while (dealerValue(myHand) < 17) { struct hand *last = myHand; while (last->next != NULL) last = last->next; last->next = initCard(); } //output the rest of the dealers hand to the buffer temp = myHand->next; strcpy(buffer, temp->card); temp = temp->next; while (temp != NULL) { strcat(buffer, temp->card); temp = temp->next; } //send the rest of the dealers hand to the clients for (i = 0; i < numClients; i++) { if (FD_ISSET (clients[i].socket, &activefds)) { write(clients[i].socket, buffer, sizeof(buffer)); close(clients[i].socket); } } return EXIT_SUCCESS; }