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

update player, setup round, wrap up round

# Conflicts:
#	src/common/game_state/game_state.h
parent 4210d2f4
No related branches found
No related tags found
2 merge requests!17finalize common,!5Draft: Resolve "Adapt Game State Class and add class RoundState"
......@@ -143,34 +143,88 @@ std::vector<player*>& game_state::get_players() {
// state modification functions without diff
void game_state::increase_round_number() {
int number = get_round_number() + 1;
_round_number->set_value(number);
}
void game_state::update_current_player() {
_current_player_idx->set_value((_current_player_idx->get_value() + 1) % _players.size());
void game_state::wrap_up_round(std::string& err){
_starting_player_idx->set_value((_starting_player_idx->get_value() + 1) % _players.size());
for (int i = 0; i < _players.size(); i++) {
_players[i]->wrap_up_round();
}
}
void game_state::setup_round(std::string &err) {
bool game_state::update_current_player(std::string& err){
_current_player_idx->set_value((_current_player_idx->get_value() + 1) % _players.size());
//if current player is last player of round, switch from estimation to playing or end round and send callback to game_state
if (_current_player_idx->get_value() == _trick_starting_player_idx->get_value()){
if (_is_estiamtion_phase->get_value() == true) {
_is_estimation_phase->set_value(false);
} else {
//determine trick winner
player* winner = _trick->wrap_up_trick(err);
winner->set_nof_tricks(get_nof_tricks() + 1);
// round has not ended yet
if (_trick_number->get_value() < _round_number->get_value()){
_trick_number->set_value(_trick_number->get_value() + 1);
_trick->set_up_round();
// winner of trick is starting player of next trick
int winner_index = get_player_index(winner);
if(winner_index == -1){
err = "Player is not part of the game!";
return false;
} else {
_trick_starting_player_idx->set_value(winner_index);
_current_player_idx->set_value(winner_index);
}
} else {
wrap_up_round(err);
_round_number->set_value(_round_number->get_value() + 1);
setup_round(err);
}
}
}
return true;
}
increase_round_number();
update_current_player();
void game_state::determine_trump_color() {
if(_round_number->get_value() * _players.size() == 60){
_trump_color->set_value(0); // there is no trump
} else {
card* trump_card = _deck->draw_trump();
if (trump_card->get_color() != 0){
_trump_color->set_value(trump_card->get_color());
}
else if (trump_card->get_value() == 0) { //jester
_trump_color->set_value(0);
}
else if (trump_card->get_value() == 14){ //wizard
// for now: just randomly generates number
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(1, 4);
_trump_color->set_value(distrib(gen));
// TODO: include get trump_color_request()
}
}
}
//TODO: possibly set up other parts of round (players, deck, trick, ...) --> maybe do in round state
void game_state::setup_round(std::string &err) {
// Provide the callback to be called when the round ends
_round_state->set_on_round_end([this]() { // [this] captures the game_state instance
if (get_round_number() < get_max_round_number()) {
setup_round(err); // Start a new round
} else {
//TODO: possibly more complicated finish game logic
this->_is_finished->set_value(true); // End the game
finish_game(err);
_trick_estimate_sum->set_value(0);
_is_estimation_phase->set_value(true);
_trick_number->set_value(0); //tricks numbers start by 0
_trick_starting_player_idx->set_value(_starting_player_idx->get_value());
_current_player_idx->set_value(_starting_player_idx->get_value());
}
});
_deck->setup_round();
for (int i = 0; i < _players.size(); i++) {
_players[i]->setup_round();
_deck->draw_cards(_players[i], _round_number->get_value(), err);
}
determine_trump_color();
_trick->set_up_round(err, _trump_color->get_value());
}
bool game_state::start_game(std::string &err) {
......@@ -180,15 +234,9 @@ bool game_state::start_game(std::string &err) {
}
if (!_is_started->get_value()) {
this->_is_started->set_value(true);
// TODO: think about whether this will cause a problem since setup_round iteratively calls it self and start_game only ends when game has ended
if (_round_state) {
delete _round_state; // Clean up previous round state if it exists
_round_state = nullptr;
}
_round_state = new round_state(_players, _current_player_idx, _round_number);
this->setup_round(err); //iteratively starts all rounds until game is over
return true;
_is_started->set_value(true);
// TODO: chek if all round setup done
setup_round(err);
} else {
err = "Could not start game, as the game was already started";
return false;
......
......@@ -32,6 +32,10 @@ private:
serializable_value<int>* _starting_player_idx;
serializable_value<int>* _trick_starting_player_idx;
serializable_value<int>* _current_player_idx;
//TODO: possibly add boolean _determine_trump_color that can be used to tell player to decide on a trump color
deck* _deck;
trick* _trick; // only save current trick, won tricks are saved with the players
serializable_value<int>* _trump_color;
serializable_value<int>* _trick_estimate_sum;
......@@ -63,6 +67,7 @@ private:
// returns the index of 'player' in the '_players' vector
int get_player_index(player* player) const;
int get_number_of_turns();
void determine_trump_color();
public:
game_state();
......@@ -83,14 +88,12 @@ public:
// all in block behind ifdef is only included if wizard server is defined
//#ifdef WIZARD_SERVER
// server-side state update functions
void update_current_player(); //starting player of round
bool update_current_player(std::string& err); //starting player of round // server side initialization
bool remove_player(player* player, std::string& err);
bool add_player(player* player, std::string& err);
bool start_game(std::string& err);
bool finish_game(std::string& err);
void determine_trump_color();
void setup_round(std::string& err); // server side initialization
void wrap_up_round(std::string& err);
bool estimate_tricks(player *player, std::string &err, int trick_estimate);
......
......@@ -200,47 +200,9 @@ void round_state::finish_round() {
}
}
void round_state::determine_trump_color() {
if(_round_number->get_value() * _players.size() == 60){
_trump_color->set_value(0); // there is no trump
} else {
card* trump_card = _deck->draw_trump();
if (trump_card->get_color() != 0){
_trump_color->set_value(trump_card->get_color());
}
else if (trump_card->get_value() == 0) { //jester
_trump_color->set_value(0);
}
else if (trump_card->get_value() == 14){ //wizard
// for now: just randomly generates number
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(1, 4);
_trump_color->set_value(distrib(gen));
// TODO: include get trump_color_request()
}
}
}
// set up round
void round_state::setup_round(std::string& err, const int round_number, const int starting_player_idx){
this->_starting_player_idx->set_value(starting_player_idx);
this->_current_player_idx->set_value(starting_player_idx);
this->_round_number->set_value(round_number);
_trick_estimate_sum->set_value(0);
_is_estimation_phase->set_value(true);
_current_trick_number->set_value(1); //tricks have number 1, ..., round_number
_deck->setup_round();
for (int i = 0; i < _players.size(); i++) {
_players[i]->setup_round();
_deck->draw_cards(_players[i], _round_number->get_value(), err);
}
determine_trump_color();
_trick->set_up_round(err, _trump_color->get_value());
}
void round_state::update_current_player(std::string& err){
_current_player_idx->set_value((_current_player_idx->get_value() + 1) % _players.size());
......
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