void TicTacToeGame::setUserInputContext(IInputValueContext *pInputContext) { if (pInputContext && getPlayer1() && getPlayer2()) { getPlayer1()->setInputContext(pInputContext); getPlayer2()->setInputContext(pInputContext); } else { if (!pInputContext) { std::string("Invalid IInputValueContext passed to setUserInputContext"); } if (!pInputContext) { std::string("Game Players are not initialized"); } } }
bool PongGameStatus::checkIfAnyPlayerWon() { if (getPlayer1()->getPoints() == POINTS_TO_WIN || getPlayer2()->getPoints() == POINTS_TO_WIN) return true; return false; }
bool BoardState::checkGameEnded() { // Store in turn the winner if (getPlayer1()->isDead()) { if (getPlayer2()->isDead()) turn = TIE; else turn = PLAYER2; gameState = ENDED; return true; } // player 1 is alive if (getPlayer2()->isDead()) { turn = PLAYER1; gameState = ENDED; return true; } return false; }
void BoardState::startGame(DeckList playerDeck, DeckList oppenentDeck) { // Load deck lists getPlayer1()->loadDeck(playerDeck); getPlayer2()->loadDeck(oppenentDeck); // Init players for (int i = 0; i < NUM_PLAYERS; ++i) player[i]->init(); if (RNG::rollPct(50.0)) turn = PLAYER1; else turn = PLAYER2; std::cout << "Started Game with starting player: " << turn << std::endl; gameState = IN_PROGRESS; getActivePlayer()->startTurn(); }
void TicTacToeGame::updateBoard(int iPlayer, int iChoice) { int i = (iChoice / BOARD_SIZE) + ((iChoice % BOARD_SIZE) != 0 ? 1 : 0); int j = (iChoice % BOARD_SIZE) == 0 ? BOARD_SIZE : (iChoice % BOARD_SIZE); //iChoice is 1 indexed based and i,j are 0 indexed based. So we need to do -1 from both --i; --j; if (getBoardElement(i, j) != EMPTY) { throw std::string("Trying to update already assigned field"); } if (iPlayer == Player1) { setBoardValue(getPlayer1()->getSign(), i, j); } else { setBoardValue(getPlayer2()->getSign(), i, j); } }
int TicTacToeGame::playTurn(PlayerID player) { try { //Now only supported for max two players int iChoice = INVALID_INPUT; if (player == Player1) { iChoice = getPlayer1()->getChoice(); } else if(player == Player2) { iChoice = getPlayer2()->getChoice(); } updateBoard(player, iChoice); return iChoice; } catch (...) { return INVALID_INPUT; } }