void Trivia::Update() { const clock_t timeDiff = clock() - m_ctLastUpdate; // We need at least 1 ms change since we aren't dealing with nanoseconds here! if (!timeDiff) return; m_ctLastUpdate = clock(); // Process delayed messages before anything below. // What we don't want is to queue something then subtract time right after. auto itr = m_vDelayedMsgs.begin(); while (itr != m_vDelayedMsgs.end()) { (*itr).timer -= int32(timeDiff); if ((*itr).timer <= 0) { m_pOwner->SendChatMsg((*itr).msg); itr = m_vDelayedMsgs.erase(itr); } else ++itr; } // The timer for when a question expires (no one answered in time) if (m_iQuestionExpireTimer > 0) { m_iQuestionExpireTimer -= int32(timeDiff); if (m_iQuestionExpireTimer <= 0) { const TriviaQA& triviaRef = m_vQuestions[m_uiCurQuestion]; std::string answerString; for (size_t i = 0; i < triviaRef.answers.size(); ++i) answerString.append(triviaRef.answers[i]); m_vDelayedMsgs.clear(); // Some randomization so that twitch doesn't see us sending messages at exact intervals m_pOwner->SendChatMsg("Time has expired!"); queueMessage("The answer(s): " + answerString, randomChoice(1000, 2000)); queueNextQuestion(); } } // Is it time for another question? if (m_iNextQuestionTimer > 0) { m_iNextQuestionTimer -= int32(timeDiff); if (m_iNextQuestionTimer <= 0) BeginNewQA(); } }
void Trivia::BeginNewQA() { m_vDelayedMsgs.clear(); // Move on to next question ++m_uiCurQuestion; if (m_uiCurQuestion >= m_vQuestions.size()) m_uiCurQuestion = 0; // You get 60 seconds to answer, but it takes 10 seconds for the preamble to occur. m_iQuestionExpireTimer = 70000; // Send the header for a new trivia m_pOwner->SendChatMsg(getTopScorePrint()); queueMessage("Trivia #" + std::to_string(m_uiCurQuestion) + " is worth " + std::to_string(m_vQuestions[m_uiCurQuestion].points) + " points!", randomChoice(2000, 3000)); queueMessage("-- TRIVIA #" + std::to_string(m_uiCurQuestion) + " --", randomChoice(6000, 7000)); queueMessage(m_vQuestions[m_uiCurQuestion].question, randomChoice(10000, 11000)); // These messages would let people know how much time is left. queueMessage("Time will expire in 30 seconds!", 10000 + randomChoice(28000, 30000)); queueMessage("Hurry, time will expire in 15 seconds!", 10000 + randomChoice(43000, 44000)); queueMessage("Only five seconds remain!", 10000 + randomChoice(52000, 54000)); }
void Trivia::ProcessAnswer(const std::string name, const std::string answer) { if (!isQuestionInProgress()) return; if (isAnswerCorrect(m_uiCurQuestion, answer)) { m_vDelayedMsgs.clear(); uint8 points = m_vQuestions[m_uiCurQuestion].points; m_uoScore[name] += points; std::string answerString; for (size_t i = 0; i < m_vQuestions[m_uiCurQuestion].answers.size(); ++i) answerString.append(m_vQuestions[m_uiCurQuestion].answers[i]); queueMessage("He has done it! User " + name + " gains " + std::to_string(points) + " points for a total of " + std::to_string(m_uoScore[name]) + "!", 1000); queueMessage("The answer(s): " + answerString, randomChoice(3000, 4000)); SaveScore(); queueNextQuestion(false); } }
int main(int argc, char* argv[]){ int i,j,l,k, seed; srand(time(NULL)); if(argv[1]!=NULL) seed = atoi(argv[1]); else seed = rand()%10 + 1; for(i=0; i<TEST_NUM; i++) { struct gameState g; int numPlayer, kingdomcards[10]; SelectStream(1); PutSeed(seed); numPlayer = rand()%2+2; kingdomcards[0]=rand()%(treasure_map - 7 + 1) + 7; for(k=1; k<10; k++){ do{ kingdomcards[k]=rand()%(treasure_map - 7 + 1) + 7; } while(checkKingdomcard(kingdomcards, k)); } initializeGame(numPlayer, kingdomcards, seed, &g); printf("\n******Information of Game********\n"); printf("The seed is %d\n", seed); printf("Number of Player: %d\n", g.numPlayers); printf("Kingdom Cards: \n"); for(l=0; l<5; l++) printf("%d ", kingdomcards[l]); printf("\n"); for(l=5; l<10; l++) printf("%d ", kingdomcards[l]); printf("\n"); printf("*********Game Begins**********\n"); while(!isGameOver(&g)){ int x, index; int handPos=0, gainCard, result=1; int handCount = numHandCards(&g); int choice1, choice2, choice3; if(checkNumAction(&g)>0){ do{ handPos = rand()%handCount; gainCard = g.hand[g.whoseTurn][handPos]; if(gainCard >= adventurer && gainCard <= treasure_map) result = 0; }while(result==1); randomChoice(&g, gainCard, &choice1, &choice2, &choice3); playCard(handPos, choice1, choice2, choice3, &g); printf("Player %d: played %d\n", g.whoseTurn+1, gainCard); } else printf("Player %d: skip play phase\n", g.whoseTurn+1); int size=0, money=checkMoney(&g); int remain_supply[treasure_map+1]; for(x=0; x<treasure_map+1; x++){ if(money >= getCost(x) && supplyCount(x, &g)>0){ remain_supply[size] = x; size++; } } if(size>0){ index = rand()%size; gainCard = remain_supply[index]; if(buyCard(gainCard, &g)==0) printf("Player %d: bought %d\n", g.whoseTurn+1, gainCard); } printf("Player %d: end turn\n", g.whoseTurn+1); endTurn(&g); } printf("\n********Game Over**********\n"); for(j=0; j<g.numPlayers; j++) printf("Player %d: %d\n", j+1, scoreFor(j, &g)); int players[g.numPlayers]; getWinners(players, &g); for(j=0; j<g.numPlayers; j++) if(players[j]==1) printf("Player %d is the winner\n", j+1); } return 0; }