int PlayerState::calculateVictoryPoints() { int victoryPoints = 0; victoryPoints += 1 * deck[CardManager::cardIndexer[ESTATE]]; victoryPoints += 3 * deck[CardManager::cardIndexer[DUCHY]]; victoryPoints += 6 * deck[CardManager::cardIndexer[PROVINCE]]; victoryPoints -= 1 * deck[CardManager::cardIndexer[CURSE]]; victoryPoints += 1 * deck[CardManager::cardIndexer[GARDENS]] * (countCards() / 10); victoryPoints += 1 * hand[CardManager::cardIndexer[ESTATE]]; victoryPoints += 3 * hand[CardManager::cardIndexer[DUCHY]]; victoryPoints += 6 * hand[CardManager::cardIndexer[PROVINCE]]; victoryPoints -= 1 * hand[CardManager::cardIndexer[CURSE]]; victoryPoints += 1 * hand[CardManager::cardIndexer[GARDENS]] * (countCards()/10); victoryPoints += 1 * discard[CardManager::cardIndexer[ESTATE]]; victoryPoints += 3 * discard[CardManager::cardIndexer[DUCHY]]; victoryPoints += 6 * discard[CardManager::cardIndexer[PROVINCE]]; victoryPoints -= 1 * discard[CardManager::cardIndexer[CURSE]]; victoryPoints += 1 * discard[CardManager::cardIndexer[GARDENS]] * (countCards() / 10); victoryPoints += 1 * inPlay[CardManager::cardIndexer[ESTATE]]; victoryPoints += 3 * inPlay[CardManager::cardIndexer[DUCHY]]; victoryPoints += 6 * inPlay[CardManager::cardIndexer[PROVINCE]]; victoryPoints -= 1 * inPlay[CardManager::cardIndexer[CURSE]]; victoryPoints += 1 * inPlay[CardManager::cardIndexer[GARDENS]] * (countCards() / 10); return victoryPoints; }
void PlayerState::drawCards(int cards) { // For each card to be drawn for (int handCounter = 0; handCounter < cards; handCounter ++) { // If there are any top cards, draw them first if (topOfDeckAsIndex.size() > 0) { deck[topOfDeckAsIndex.top()]--; hand[topOfDeckAsIndex.top()]++; topOfDeckAsIndex.pop(); } else { // If deck is empty, shuffle if (countCards(deck) == 0) { // If discard too is empty, then don't draw more cards. if (countCards(discard) == 0) return; else shuffle(); } // Pick a random card int cardIndex = pickRandom(deck); hand[cardIndex] += 1; deck[cardIndex] -= 1; } } }
bool isFourCard(const std::vector<std::string>& vec) { std::vector<int> table = countCards(vec); if (std::count(table.begin(), table.end(), 4) == 1) return true; return false; }
bool isTwoPair(const std::vector<std::string>& vec) { const std::vector<int> table = countCards(vec); if (std::count(table.begin(), table.end(), 2) == 2) return true; return false; }
int PlayerState::pickRandom(const int (&cardPile)[INSUPPLY]) { int randomCounter = rand() % countCards(deck) + 1; for (int index = 0; index < INSUPPLY; index++) { randomCounter -= cardPile[index]; if (randomCounter <= 0) return index; } return -1; }
int PlayerState::flipThiefCards(int& absoluteCardId, int& extraCardId) { // For each card to be flipped for (int index = 0; index < 2; index++) { // If there are any top cards, flip them first if (topOfDeckAsIndex.size() > 0) { deck[topOfDeckAsIndex.top()]--; discard[topOfDeckAsIndex.top()]++; if (index == 0) absoluteCardId = CardManager::cardLookupByIndex[topOfDeckAsIndex.top()].id; else extraCardId = CardManager::cardLookupByIndex[topOfDeckAsIndex.top()].id; topOfDeckAsIndex.pop(); } else { // If deck is empty, shuffle if (countCards(deck) == 0) { // If discard too is empty, then don't draw more cards. if (countCards(discard) == 0) return index; else shuffle(); } int cardIndex = pickRandom(deck); if (index == 0) absoluteCardId = CardManager::cardLookupByIndex[cardIndex].id; else extraCardId = CardManager::cardLookupByIndex[cardIndex].id; deck[cardIndex]--; discard[cardIndex]++; } } return 2; }
int getHighCard(const std::vector<std::string>& vec, const int pflag = 0) { if (pflag != 0) { const std::vector<int> table = countCards(vec); if (pflag == 1) return std::distance(table.begin(), std::find(table.begin(), table.end(), 2)); else if (pflag == 2) { const auto p1 = std::find(table.begin(), table.end(), 2); const auto p2 = std::find(p1+1, table.end(), 2); return std::max(*p1, *p2); } else if (pflag == 3) return std::distance(table.begin(), std::find(table.begin(), table.end(), 3)); else if (pflag == 4) return std::distance(table.begin(), std::find(table.begin(), table.end(), 4)); } else { const std::vector<int> card = sortCards(vec); return *std::max_element(card.begin(), card.end()); } return -1; }
void MTGPack::load(string filename) { //TODO Placeholder until XML format available. TiXmlDocument packfile(filename.c_str()); if (!packfile.LoadFile()) return; TiXmlHandle hDoc(&packfile); TiXmlElement * pPack; pPack = hDoc.FirstChildElement().Element(); if (!pPack) { return; } //root should be "pack" string tag = pPack->Value(); std::transform(tag.begin(), tag.end(), tag.begin(), ::tolower); if (tag != "pack") return; //After validating, handle actual loading. TiXmlElement * pSlot; const char * holder = NULL; holder = pPack->Attribute("price"); if (holder) price = atoi(holder); else price = Constants::PRICE_BOOSTER; holder = pPack->Attribute("pool"); if (holder) pool = holder; else pool = ""; holder = pPack->Attribute("type"); if (holder) { type = holder; } else type = "Booster"; holder = pPack->Attribute("name"); if (holder) name = holder; else name = "Special"; holder = pPack->Attribute("requires"); if (holder) check = holder; holder = pPack->Attribute("sort"); if (holder) sort = holder; else sort = ""; std::transform(sort.begin(), sort.end(), sort.begin(), ::tolower); for (pSlot = pPack->FirstChildElement(); pSlot != NULL; pSlot = pSlot->NextSiblingElement()) { TiXmlElement * pEntry; //Load slot. tag = pSlot->Value(); std::transform(tag.begin(), tag.end(), tag.begin(), ::tolower); if (tag != "slot") continue; MTGPackSlot * s = NEW MTGPackSlot(); slotss.push_back(s); holder = pSlot->Attribute("copies"); if (holder) s->copies = atoi(holder); else s->copies = 1; holder = pSlot->Attribute("pool"); if (holder) s->pool = holder; for (pEntry = pSlot->FirstChildElement(); pEntry != NULL; pEntry = pEntry->NextSiblingElement()) { tag = pEntry->Value(); std::transform(tag.begin(), tag.end(), tag.begin(), ::tolower); if (tag == "card") { //Load specific card MTGPackEntrySpecific * es = NEW MTGPackEntrySpecific(); holder = pEntry->Attribute("copies"); if (holder) es->copies = atoi(holder); else es->copies = 1; es->card = MTGCollection()->getCardByName(pEntry->Value()); s->addEntry(es); } else if (tag == "random_card") { //Load random card MTGPackEntryRandom * er = NEW MTGPackEntryRandom(); holder = pEntry->Attribute("copies"); if (holder) er->copies = atoi(holder); else er->copies = 1; const char * text = pEntry->GetText(); if (text) er->filter = text; s->addEntry(er); } else if (tag == "nothing") { MTGPackEntryNothing * nt = NEW MTGPackEntryNothing(); s->addEntry(nt); } } } bValid = true; countCards(); return; }
void testVillageCard() { struct gameState gs; int errors = 0; int player; int numPlayers = 2; int handCount=5; int deckCount = 10; int randomSeed = rand() % 100 + 1; int test1[5]; int test0[5]; int seeds[4] = { copper, silver, duchy, estate }; int test0Deck[10] = { silver, duchy, copper, estate, estate, silver, copper, silver, duchy, estate }; int test1Deck[10] = { silver, copper, copper, silver, silver, silver, silver, silver, silver, silver }; int numCopper, numSilver, numGold, numEstate, numDuchy, numProvince, numCurse; int kingdom[10]= { adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall }; int i, j, num0, num1; //Run village test 5 times for(i=0;i<5;i++){ printf("\nTEST #%d\n", i+1); for(j=0;j<5;j++){ num0 = rand() % 3; num1 = rand() % 3; if(j==0) test0[j] = village; else test0[j] = seeds[num0]; test1[j] = seeds[num1]; } //set the game memset(&gs, 23, sizeof(struct gameState)); initializeGame(numPlayers, kingdom, randomSeed, &gs); memcpy(gs.hand[0], test0, sizeof(int)*handCount); memcpy(gs.hand[1], test1, sizeof(int)*handCount); memcpy(gs.deck[0], test0Deck, sizeof(int)*deckCount); memcpy(gs.deck[1], test1Deck, sizeof(int)*deckCount); gs.handCount[0]=5; gs.handCount[1]=5; gs.deckCount[0]=10; gs.deckCount[0]=10; //Test for player 0 before the card is played player = 0; printf("BEFORE village card is played\n"); printf("Player %d\n", player); printf("In hand..."); countCards(&gs, player, 0); printf("In Deck..."); countCards(&gs, player, 1); printf("\n"); //Test for player 1 before the card is played player = 1; printf("Player %d\n", player); printf("In Hand..."); countCards(&gs, player, 0); printf("In Deck..."); countCards(&gs, player, 1); printf("\n"); int actionsStart = gs.numActions; printf("Number of Actions: %d\n", gs.numActions); //check the supply count numCopper = gs.supplyCount[copper]; numGold = gs.supplyCount[gold]; numSilver = gs.supplyCount[silver]; numEstate = gs.supplyCount[estate]; numDuchy = gs.supplyCount[duchy]; numProvince = gs.supplyCount[province]; numCurse =gs.supplyCount[curse]; //player 0 plays the village card cardEffect(village, 0, 0, 0, &gs, 0, 0); //check player 0 after card is played player = 0; printf("AFTER village card is played\n"); printf("Player %d\n", player); printf("In Hand..."); countCards(&gs, player, 0); printf("In Deck..."); countCards(&gs, player, 1); if(gs.deckCount[player] != 9){ printf("Fail: "); errors++; }else{ printf("Pass: "******"Deck count actual: %d, expected: 9\n", gs.deckCount[player]); if(gs.handCount[player] != handCount){ printf("Fail: "); errors++; }else{ printf("Pass: "******"Hand count actual: %d, expected: 5\n", gs.handCount[player]); //check player 1 after the card is played player = 1; printf("Player %d\n", player); printf("In Hand..."); countCards(&gs, player, 0); printf("In Deck..."); countCards(&gs, player, 1); printf("Number of Actions: %d\n", gs.numActions); //check that actions are 1 greater //this would mean that the player received 2 actions if(gs.numActions != actionsStart+1){ printf("Fail: "); errors++; }else{ printf("Pass: "******"Actions actual: %d, expected %d\n", gs.numActions, actionsStart+1); if(gs.handCount[player] != handCount){ printf("Fail: "); errors++; }else{ printf("Pass: "******"Hand count actual: %d, expected: 5\n", gs.handCount[player]); if(gs.deckCount[player] != 10){ printf("Fail: "); errors++; }else{ printf("Pass: "******"Deck count actual: %d, expected: 10\n", gs.deckCount[player]); if(numCopper == gs.supplyCount[copper] && numGold == gs.supplyCount[gold] && numSilver == gs.supplyCount[silver] && numEstate == gs.supplyCount[estate] && numDuchy == gs.supplyCount[duchy] && numProvince == gs.supplyCount[province] && numCurse == gs.supplyCount[curse]){ printf("Pass: Supply count is unchanged.\n"); }else{ printf("Fail: Supply count was changed.\n"); } } if(errors == 0){ printf("All villageCard tests passed\n"); }else{ printf("%d villageCard tests failed\n", errors); } }
/* this is the function where all the processing on frames is done and new game state is set */ int ProcessingThread::process(Mat *matrix){ // we reachecd the end of a game and we see the table is empty (from cards/any other things besides the table) if ( (GameState.status == END_GAME) && (is_frame_empty(*matrix, blackVal))){ GameState.status = NEW_GAME; } if (GameState.status==END_GAME) return 0; //don't process cleanTable(&newTable); getTableFromMat(&newTable, matrix); // get info of the table from the frame fill_known_cards(&newTable,&GameState.table,*matrix,Glyphs, keypoints,70,20); //get the letters of the cards // check that te dealer didn't move too much (indicates that the dealer was hidden behind a hand in the frame) if (is_dealer_in_same_location(&newTable, &GameState.table, 70) == 0){ return 0; } // get the number of cards of each value on the table char tempInv[13] = {0}; getTableInventory(&newTable, tempInv); // get the count of the cards on the table int tempCount = countCards(tempInv); // if the game hasn't ended check if it's the dealers turn (has > 1 cards) or the players' turn if (GameState.status != END_GAME){ if ((newTable.players.size() > 0) && (newTable.players.at(0).cards.size() > 1)){ GameState.status = DEALER_TURN; } else { GameState.status = PLAYER_TURN; } } // check if the dealer has any players to play against (if they all bust - go to end game) char flag = 0; if ((newTable.players.size() > 1) && (GameState.status == PLAYER_TURN)){ flag = 1; for (uint i = 1; i < newTable.players.size(); i++){ int val = getHandVal(newTable.players.at(i)); if ((val <= 21) || (val == BLACK_JACK) ){ flag = 0; break; } } } // check on dealers turn if there is a player who didn't bust/lose to the dealer yet if ((newTable.players.size() > 1) && (GameState.status == DEALER_TURN)){ flag = 1; // will stay 1 after the for iff the dealer has the highest hand int dval = getHandVal(newTable.players.at(0)); for (uint i = 1; i < newTable.players.size(); i++){ int val = getHandVal(newTable.players.at(i)); if ((dval == BLACK_JACK) || (dval >= 17)) { flag = 1; break; } if ((val > dval) && (val <= 21) ){ flag = 0; break; } } } // from the last to checks (above) the flag is 1 if the game has ended - update the Game State if (flag == 1){ // a game ended update GameState (nothing new should go on the table until it is empty again GameState.status = END_GAME; //update the game state history for (uint i = 0; i < 13; i++){ GameState.history[i] += tempInv[i]; } //update the game state count with the temp count GameState.deckCount += tempCount; } // store the new processed table and update output text GameState.table = newTable; // display new text on screen updateGameText(tempInv); return 1; }