Quaternity
Functions
update.cpp File Reference

This file defines functions for updating the state. More...

#include <cassert>
#include <vector>
#include "graph.h"
#include "match.h"
#include "round.h"
#include "settings.h"
#include "state.h"
#include "update.h"

Go to the source code of this file.

Functions

void update_onturn (const Settings &settings, State &state, const Question &question)
 Update the player onturn such that he has cards for sure. More...
 
bool update_quartet (const Settings &settings, State &state, const Graph &graph, int hand_offset, int player, int set)
 Update the state such that the given player has the provided set. More...
 
void update_quartets (const Settings &settings, State &state, const Question &question)
 Find if there are new quartets and update the state accordingly. More...
 
void update_state (const Settings &settings, State &state, const Question &question, const Answer &answer)
 Update the state of the game according to the question asked and the answer which is given. More...
 

Detailed Description

This file defines functions for updating the state.

Definition in file update.cpp.

Function Documentation

◆ update_onturn()

void update_onturn ( const Settings settings,
State state,
const Question question 
)

Update the player onturn such that he has cards for sure.

If the player onturn does not have any cards left after it got a new quartet, the player who the card was asked from will be onturn. If that player also does not have any cards, we choose the first player in the player list with cards, if there is none we set onturn to -1.

Definition at line 25 of file update.cpp.

26 {
27  // player onturn does not have any cards
28  if (info_num_cards(settings, state, state.onturn) == 0) {
29 
30  // player asked does not have any cards
31  if (info_num_cards(settings, state, question.player) > 0)
32  state.onturn = question.player;
33  else {
34  // choose the first player with cards from the player list
35  for (int player = 0; player < settings.NUM_PLAYERS; player++)
36  if (info_num_cards(settings, state, player) > 0) {
37  state.onturn = player;
38  return;
39  }
40  state.onturn = -1; // no players with cards left
41  }
42  }
43 }
int info_num_cards(const Settings &settings, const State &state, int player)
Get the number of cards the player has without counting the cards from quartets, so the actual number...
Definition: state.cpp:113
int player
The player the question is asked to.
Definition: round.h:22
const int NUM_PLAYERS
The number of players playing the game.
Definition: settings.h:37
int onturn
The player who is on turn.
Definition: state.h:62

◆ update_quartet()

bool update_quartet ( const Settings settings,
State state,
const Graph graph,
int  hand_offset,
int  player,
int  set 
)

Update the state such that the given player has the provided set.

First we check if it is already known the player has the provided set. Next we make sure that in every possible matching the player has the set. To do this we execute the code shown below. If this succeeds we update the state such that every card from the provided set can only be assigned to the player and we set the quartets array entry to the player.

for card in quartet:
    remove all edges from player's hand to the card
    if match exists for graph:
       return
Returns
if the player and set given is a new quartet

Definition at line 63 of file update.cpp.

64 {
65  // check if we already know the quartet belongs to the player
66  if (state.quartets[set] == player)
67  return false;
68 
69  // make sure that in every possible matching this player has the given set
70  for (int card = set * settings.SET_SIZE; card < (set + 1) * settings.SET_SIZE; card++) {
71  Graph graph_restricted = graph_copy(graph);
72  for (int hand_entry = 0; hand_entry < state.players[player].num_cards; hand_entry++)
73  graph_restricted[hand_offset + hand_entry][card] = false;
74  if (match_exists(graph_restricted))
75  return false;
76  }
77 
78  // update the state so only this player can have the cards from the set
79  for (int card = set * settings.SET_SIZE; card < (set + 1) * settings.SET_SIZE; card++) {
80  state.cards[card].players = std::vector<bool>(settings.NUM_PLAYERS, false);
81  state.cards[card].players[player] = true;
82  }
83  state.quartets[set] = player;
84  return true;
85 }
Graph graph_copy(const Graph graph)
Create a copy of the given graph.
Definition: graph.cpp:19
std::vector< std::vector< bool > > Graph
A bipartite graph where true booleans represent edges between a left node denoted as the row and a ri...
Definition: graph.h:19
bool match_exists(const Graph &graph)
Check if there exists a matching for all cards.
Definition: match.cpp:35
const int SET_SIZE
The size of one set, aka the number of cards in one set.
Definition: settings.h:27
std::vector< Player > players
The information on all the players.
Definition: state.h:81
std::vector< Card > cards
The information on all the cards.
Definition: state.h:74
std::vector< int > quartets
The information of who has a certain quartet.
Definition: state.h:90

◆ update_quartets()

void update_quartets ( const Settings settings,
State state,
const Question question 
)

Find if there are new quartets and update the state accordingly.

First we generate a matching. We check for every player if they have all cards from a set (i.e. a quartet) in the found matching, if so we call update_quartet with the found player and set. If a new quartet is found, we use update_onturn to make sure that the player onturn has cards.

See also
update_quartet
update_onturn

Definition at line 98 of file update.cpp.

99 {
100  assert(graph_possible(settings, state) && match_exists(graph_create(settings, state)));
101 
102  // find any matching of the current state graph for checking of quartets
103  Graph graph = graph_create(settings, state);
104  std::vector<int> match = match_find(graph);
105 
106  bool newquartet = false; // indicates whether any new quartet was found
107  int hand_offset = 0;
108  for (int player = 0; player < settings.NUM_PLAYERS; player++) {
109 
110  // count the number of cards this player has of every set
111  std::vector<int> sets(settings.NUM_SETS, 0);
112  for (int card = 0; card < state.players[player].num_cards; card++)
113  sets[match[hand_offset + card] / settings.SET_SIZE]++;
114 
115  // check if there are any new possible quartets, if so update the state
116  for (int set = 0; set < settings.NUM_SETS; set++)
117  if (sets[set] == settings.SET_SIZE)
118  if (update_quartet(settings, state, graph, hand_offset, player, set))
119  newquartet = true;
120 
121  hand_offset += state.players[player].num_cards;
122  }
123 
124  // make sure that the player who is onturn has cards left
125  if (newquartet)
126  update_onturn(settings, state, question);
127 }
bool graph_possible(const Settings &settings, const State &state)
Check if it is possible to create a graph.
Definition: graph.cpp:36
Graph graph_create(const Settings &settings, const State &state)
Create a biparite graph which has a matching if the state is valid.
Definition: graph.cpp:101
std::vector< int > match_find(const Graph &graph)
Find a match of the given graph.
Definition: match.cpp:47
const int NUM_SETS
The number of sets.
Definition: settings.h:32
void update_onturn(const Settings &settings, State &state, const Question &question)
Update the player onturn such that he has cards for sure.
Definition: update.cpp:25
bool update_quartet(const Settings &settings, State &state, const Graph &graph, int hand_offset, int player, int set)
Update the state such that the given player has the provided set.
Definition: update.cpp:63

◆ update_state()

void update_state ( const Settings settings,
State state,
const Question question,
const Answer answer 
)

Update the state of the game according to the question asked and the answer which is given.

  • If the player onturn does not have have a set restriction for the asked set yet, give it a set restriction of one.
  • If the answer is true.
    • Update the set restrictions.
      • Increase the player onturn's set restriction by one.
      • Decrease the player asked set restriction by one (not lower than 0).
    • Update who has the card which is asked.
      • Set the card's player status only to true for the onturn player.
    • Update the number of cards the players have.
      • Increase the player onturn's card count by one.
      • Decrease the player asked card count by one.
    • Update the quartets.
      • Call function update_quartets.
  • If the answer is false.
    • Update who has the card which is asked.
      • Set the card's player status false for the onturn player.
      • Set the card's player status false for the asked player.
    • Change onturn.
      • Switch onturn to the player asked.
See also
update_quartets

Definition at line 155 of file update.cpp.

156 {
157  const int card = question.set * settings.SET_SIZE + question.card;
158 
159  // update the set restriction of the player onturn
160  int *set = &state.players[state.onturn].sets[question.set];
161  if (*set == 0) *set = 1;
162 
163  if (answer) {
164  // update the set restrictions
165  state.players[state.onturn].sets[question.set] += 1;
166  int *set = &state.players[question.player].sets[question.set];
167  if (*set > 0) *set -= 1;
168 
169  // update who has the card which is asked
170  state.cards[card].players = std::vector<bool>(settings.NUM_PLAYERS, false);
171  state.cards[card].players[state.onturn] = true;
172 
173  // update the number of cards the players have
174  state.players[state.onturn].num_cards += 1;
175  state.players[question.player].num_cards -= 1;
176 
177  // update the quartets
178  update_quartets(settings, state, question);
179  } else {
180  // update who could have the card which is asked
181  state.cards[card].players[state.onturn] = false;
182  state.cards[card].players[question.player] = false;
183 
184  // update the turn
185  state.onturn = question.player;
186  }
187 }
int set
The set the card which is asked belongs to.
Definition: round.h:27
int card
The card in the set which is asked for.
Definition: round.h:32
void update_quartets(const Settings &settings, State &state, const Question &question)
Find if there are new quartets and update the state accordingly.
Definition: update.cpp:98