Quaternity
update.cpp
Go to the documentation of this file.
1 
7 #include <cassert>
8 #include <vector>
9 
10 #include "graph.h"
11 #include "match.h"
12 #include "round.h"
13 #include "settings.h"
14 #include "state.h"
15 #include "update.h"
16 
25 void update_onturn(const Settings &settings, State &state, const Question &question)
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 }
44 
63 bool update_quartet(const Settings &settings, State &state, const Graph &graph, int hand_offset, int player, int set)
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 }
86 
98 void update_quartets(const Settings &settings, State &state, const Question &question)
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 }
128 
155 void update_state(const Settings &settings, State &state, const Question &question, const Answer &answer)
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 }
bool graph_possible(const Settings &settings, const State &state)
Check if it is possible to create a graph.
Definition: graph.cpp:36
Graph graph_copy(const Graph graph)
Create a copy of the given graph.
Definition: graph.cpp:19
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
This file defines the graph type and a creation function header.
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
std::vector< int > match_find(const Graph &graph)
Find a match of the given graph.
Definition: match.cpp:47
bool match_exists(const Graph &graph)
Check if there exists a matching for all cards.
Definition: match.cpp:35
This file defines maximum matching algorithm function headers.
This file defines the structs for a question and typedef of an answer.
bool Answer
An answer returned by a player. The answer is either true or false denoting he/she has or does not ha...
Definition: round.h:39
This file defines the settings struct.
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
This file defines the structs for a card, player and a game state, plus a state initialize and copy f...
A question asked by a player. The player asks another player for a specific card from a certain set.
Definition: round.h:17
int set
The set the card which is asked belongs to.
Definition: round.h:27
int player
The player the question is asked to.
Definition: round.h:22
int card
The card in the set which is asked for.
Definition: round.h:32
The settings which characterize a game.
Definition: settings.h:22
const int NUM_SETS
The number of sets.
Definition: settings.h:32
const int SET_SIZE
The size of one set, aka the number of cards in one set.
Definition: settings.h:27
const int NUM_PLAYERS
The number of players playing the game.
Definition: settings.h:37
The complete game state.
Definition: state.h:57
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
int onturn
The player who is on turn.
Definition: state.h:62
std::vector< int > quartets
The information of who has a certain quartet.
Definition: state.h:90
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
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.
Definition: update.cpp:155
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
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
This file defines the state update function headers.