Skip to content
Snippets Groups Projects
Commit fda1272e authored by marie3003's avatar marie3003
Browse files

adapt gamecontroller

parent 98ea1e31
No related branches found
No related tags found
1 merge request!17finalize common
......@@ -12,7 +12,6 @@
// initialize static members
GameWindow* GameController::_gameWindow = nullptr;
ConnectionPanel* GameController::_connectionPanel = nullptr;
MainGamePanel* GameController::_mainGamePanel = nullptr;
MainGamePanelWizard* GameController::_mainGamePanelWizard = nullptr;
TrickEstimationPanel* GameController::_trickEstimationPanel = nullptr;
......@@ -28,13 +27,13 @@ void GameController::init(GameWindow* gameWindow) {
// Set up main panels
GameController::_connectionPanel = new ConnectionPanel(gameWindow);
GameController::_mainGamePanel = new MainGamePanel(gameWindow);
//GameController::_mainGamePanel = new MainGamePanel(gameWindow);
GameController::_mainGamePanelWizard = new MainGamePanelWizard(gameWindow);
GameController::_trickEstimationPanel = new TrickEstimationPanel(gameWindow);
// Hide all panels
GameController::_connectionPanel->Show(false);
GameController::_mainGamePanel->Show(false);
//GameController::_mainGamePanel->Show(false);
GameController::_mainGamePanelWizard->Show(false);
GameController::_trickEstimationPanel->Show(false);
......@@ -76,7 +75,7 @@ void GameController::connectToServer() {
GameController::showError("Connection error", "Invalid port");
return;
}
uint16_t port = (uint16_t) portAsLong;
auto port = static_cast<uint16_t>(portAsLong);
// convert player name from wxString to std::string
std::string playerName = inputPlayerName.ToStdString();
......@@ -102,40 +101,44 @@ void GameController::updateGameState(game_state* newGameState) {
if(oldGameState != nullptr) {
// check if a new round started, and display message accordingly
if(oldGameState->get_round_number() >= 0 && oldGameState->get_round_number() < newGameState->get_round_number()) {
/*
GameController::showStatus("Round " + std::to_string(newGameState->get_round_number()));
if(oldGameState->get_round_number() > 0)
if(GameController::_currentGameState->is_finished()) {
GameController::showGameOverMessage();
}
else if(GameController::_currentGameState->is_started())
{
int round_number = _currentGameState->get_round_number();
if(round_number != oldGameState->get_round_number())
{
// new round has started
showNewRoundMessage(oldGameState, newGameState);
showStatus("Round " + std::to_string(newGameState->get_round_number()));
}
// estimation phase
if(GameController::_currentGameState->is_estimation_phase()) {
GameController::_gameWindow->showPanel(GameController::_trickEstimationPanel);
GameController::_trickEstimationPanel->buildGameState(GameController::_currentGameState, GameController::_me);
GameController::estimateTrick();
}
// end of trick
if(_currentGameState->get_trick_number() != oldGameState->get_trick_number())
{
GameController::showNewRoundMessage(oldGameState, newGameState);
showTrickOverMessage();
}
GameController::_gameWindow->showPanel(GameController::_trickEstimationPanel);
GameController::_trickEstimationPanel->buildGameState(GameController::_currentGameState, GameController::_me);
GameController::estimateTrick();
*/
}
// delete the old game state, we don't need it anymore
delete oldGameState;
}
if(GameController::_currentGameState->is_finished()) {
GameController::showGameOverMessage();
}
/*
GameController::_gameWindow->showPanel(GameController::_mainGamePanelWizard);
GameController::_mainGamePanelWizard->buildGameState(GameController::_currentGameState, GameController::_me);
*/
/*
// make sure we are showing the main game panel in the window (if we are already showing it, nothing will happen)
GameController::_gameWindow->showPanel(GameController::_mainGamePanel);
GameController::_gameWindow->showPanel(GameController::_mainGamePanelWizard);
GameController::_mainGamePanelWizard->buildGameState(GameController::_currentGameState, GameController::_me);
// command the main game panel to rebuild itself, based on the new game state
GameController::_mainGamePanel->buildGameState(GameController::_currentGameState, GameController::_me);
*/
}
......@@ -176,11 +179,12 @@ void GameController::estimateTrick()
int trickEstimateInt;
if (trickEstimate.ToLong(&trickEstimateValue)) {
trickEstimateInt = static_cast<int>(trickEstimateValue);
GameController::estimateTricks(trickEstimateInt);
} else {
// Handle the error: the string was not a valid integer
GameController::showError("Invalid input!"," Please enter a valid number for the trick estimate.");
}
GameController::estimateTricks(trickEstimateInt);
/*
std::string title = "How many tricks?";
std::string message = "Enter estimated number of tricks";
......@@ -218,17 +222,17 @@ void GameController::showNewRoundMessage(game_state* oldGameState, game_state* n
std::string title = "Round Completed";
std::string message = "The players gained the following minus points:\n";
std::string buttonLabel = "Start next round";
/*
// add the point differences of all players to the messages
for(int i = 0; i < oldGameState->get_players().size(); i++) {
player* oldPlayerState = oldGameState->get_players().at(i);
player* newPlayerState = newGameState->get_players().at(i);
int scoreDelta = newPlayerState->get_score() - oldPlayerState->get_score();
int scoreDelta = newPlayerState->get_scores().back()->get_value() - oldPlayerState->get_scores().back()->get_value();
std::string scoreText = std::to_string(scoreDelta);
if(scoreDelta > 0) {
scoreText = "+" + scoreText;
scoreText.append(scoreText);
}
std::string playerName = newPlayerState->get_player_name();
......@@ -238,11 +242,24 @@ void GameController::showNewRoundMessage(game_state* oldGameState, game_state* n
message += "\n" + playerName + ": " + scoreText;
}
//TODO: what to do with these? Is message just going away or do you have to press okay
//wxMessageDialog dialogBox = wxMessageDialog(nullptr, message, title, wxICON_NONE);
//dialogBox.SetOKLabel(wxMessageDialog::ButtonLabel(buttonLabel));
//dialogBox.ShowModal();
*/
ScoreDialog* dialog = new ScoreDialog(GameController::_gameWindow, title, message);
auto* dialog = new ScoreDialog(GameController::_gameWindow, title, message);
dialog->ShowModal();
}
void GameController::showTrickOverMessage()
{
std::string title = "Trick Completed";
std::string message = " won the trick\n";
player* winner = _currentGameState->get_trick_starting_player();
message = winner->get_player_name() + message;
auto dialog = new ScoreDialog(GameController::_gameWindow, title, message);
dialog->ShowModal();
}
......@@ -251,19 +268,19 @@ void GameController::showGameOverMessage() {
std::string title = "Game Over!";
std::string message = "Final score:\n";
std::string buttonLabel = "Close Game";
/*
// TODO: change logic to determine winner because now we have vector of scores
// sort players by score
std::vector<player*> players = GameController::_currentGameState->get_players();
std::sort(players.begin(), players.end(), [](const player* a, const player* b) -> bool {
return a->get_score() < b->get_score();
return a->get_scores().back()->get_value() < b->get_scores().back()->get_value();
});
// list all players
for(int i = 0; i < players.size(); i++) {
player* playerState = players.at(i);
std::string scoreText = std::to_string(playerState->get_score());
std::string scoreText = std::to_string(playerState->get_scores().back()->get_value());
// first entry is the winner
std::string winnerText = "";
......@@ -281,7 +298,7 @@ void GameController::showGameOverMessage() {
}
message += "\n" + playerName + ": " + scoreText + winnerText;
}
*/
wxMessageDialog dialogBox = wxMessageDialog(nullptr, message, title, wxICON_NONE);
dialogBox.SetOKLabel(wxMessageDialog::ButtonLabel(buttonLabel));
int buttonClicked = dialogBox.ShowModal();
......
......@@ -28,13 +28,13 @@ public:
static void showError(const std::string& title, const std::string& message);
static void showStatus(const std::string& message);
static void showNewRoundMessage(game_state* oldGameState, game_state* newGameState);
static void showTrickOverMessage();
static void showGameOverMessage();
static void showTrickEstimationPanel(int roundNumber);
private:
static GameWindow* _gameWindow;
static ConnectionPanel* _connectionPanel;
static MainGamePanel* _mainGamePanel;
//static MainGamePanel* _mainGamePanel;
static MainGamePanelWizard* _mainGamePanelWizard;
static TrickEstimationPanel* _trickEstimationPanel;
......
......@@ -332,7 +332,7 @@ void MainGamePanelWizard::buildThisPlayer(wxGridBagSizer* sizer, game_state* gam
ImagePanel *cardButton = new ImagePanel(cardPanel, cardFile, wxBITMAP_TYPE_ANY, wxDefaultPosition, scaledCardSize);
if (gameState->get_current_player() == me) {
if (gameState->get_current_player() == me && gameState->is_estimation_phase() == false) {
cardButton->SetToolTip("Play card");
cardButton->SetCursor(wxCursor(wxCURSOR_HAND));
cardButton->Bind(wxEVT_LEFT_UP, [handCard](wxMouseEvent& event) {
......
......@@ -13,7 +13,7 @@ public:
void buildGameState(game_state* gameState, player* me);
private:
void buildThisPlayer(wxGridBagSizer* sizer, game_state* gameState, player* me);
void buildThisPlayer(wxGridBagSizer* sizer, game_state* gameState, player* me);
void buildTurnIndicator(wxGridBagSizer* sizer, game_state* gameState, player* me);
void buildOtherPlayers(wxGridBagSizer* sizer, game_state* gameState, player *me, int myPosition);
......
......@@ -98,6 +98,22 @@ player* game_state::get_current_player() const {
return _players[_current_player_idx->get_value()];
}
player* game_state::get_trick_starting_player() const
{
if(_trick_starting_player_idx == nullptr || _players.empty()) {
return nullptr;
}
return _players[_trick_starting_player_idx->get_value()];
}
player* game_state::get_starting_player() const
{
if(_starting_player_idx == nullptr || _players.empty()) {
return nullptr;
}
return _players[_starting_player_idx->get_value()];
}
trick* game_state::get_trick() const
{
return _trick;
......@@ -116,6 +132,11 @@ bool game_state::is_finished() const {
return _is_finished->get_value();
}
bool game_state::is_estimation_phase() const
{
return _is_estimation_phase->get_value();
}
int game_state::get_round_number() const {
return _round_number->get_value();
}
......
......@@ -75,6 +75,7 @@ public:
bool is_full() const;
bool is_started() const;
bool is_finished() const;
bool is_estimation_phase() const;
bool is_player_in_game(player* player) const;
std::vector<player*>& get_players();
int get_round_number() const;
......@@ -83,6 +84,8 @@ public:
int get_max_round_number() const;
int get_trump_color() const;
player* get_current_player() const;
player* get_trick_starting_player() const;
player* get_starting_player() const;
trick* get_trick() const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment