Quaternity
validate.cpp
Go to the documentation of this file.
1 
8 #include <algorithm>
9 #include <iostream>
10 #include <vector>
11 
12 #include "debug.h"
13 #include "graph.h"
14 #include "match.h"
15 #include "round.h"
16 #include "settings.h"
17 #include "state.h"
18 #include "update.h"
19 #include "validate.h"
20 
28 bool valid_state(const Settings &settings, const State &state)
29 {
30  if (!graph_possible(settings, state)) return false;
31  Graph graph = graph_create(settings, state);
32 
33  return match_exists(graph);
34 }
35 
50 bool valid_answer(const Settings &settings, const State &state, const Question &question, const Answer &answer)
51 {
52  if (answer) { // = yes
53  // make sure the asked player can have the card being asked
54  const int card = question.set * settings.SET_SIZE + question.card;
55  if (!state.cards[card].players[question.player]) return false;
56 
57  State state_copy = copy_state(state);
58 
59  // the player onturn has at least one card from the asked set
60  int *set = &state_copy.players[state_copy.onturn].sets[question.set];
61  if (*set == 0) *set = 1;
62 
63  // the asked player has the card being asked
64  state_copy.cards[card].players = std::vector<bool>(settings.NUM_PLAYERS, false);
65  state_copy.cards[card].players[question.player] = true;
66 
67  if (!valid_state(settings, state_copy)) return false;
68  }
69 
70  State state_copy = copy_state(state);
71  update_state(settings, state_copy, question, answer);
72 
73  return valid_state(settings, state_copy);
74 }
75 
89 bool *valid_question(const Settings &settings, const State &state, const Question &question)
90 {
91  if (question.player < 0 || question.player >= settings.NUM_PLAYERS) {
92  std::cerr << "Player is out of bounds." << std::endl;
93  return NULL;
94  }
95 
96  if (question.set < 0 || question.set >= settings.NUM_SETS) {
97  std::cerr << "Set is out of bounds." << std::endl;
98  return NULL;
99  }
100 
101  if (question.card < 0 || question.card >= settings.SET_SIZE) {
102  std::cerr << "Card is out of bounds." << std::endl;
103  return NULL;
104  }
105 
106  if (state.onturn == question.player) {
107  std::cerr << "You cannot ask yourself a question." << std::endl;
108  return NULL;
109  }
110 
111  if (state.quartets[question.set] != -1) {
112  std::cerr << "You cannot ask a card from a quartet." << std::endl;
113  return NULL;
114  }
115 
116  if (info_num_cards(settings, state, question.player) < 1) {
117  std::cerr << "Player " << question.player << " does not have any cards." << std::endl;
118  return NULL;
119  }
120 
121  bool *valid_answers = new bool[2];
122 
123  valid_answers[0] = valid_answer(settings, state, question, false);
124  valid_answers[1] = valid_answer(settings, state, question, true);
125 
126  return !valid_answers[0] && !valid_answers[1] ? NULL : valid_answers;
127 }
This file defines print operator headers for custom structs and a template for printing a vector....
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
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
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.
State copy_state(const State &state)
Copy the old state to a new state.
Definition: state.cpp:96
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_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
This file defines the state update function headers.
bool valid_state(const Settings &settings, const State &state)
Check if there exists a matching for the given state.
Definition: validate.cpp:28
bool valid_answer(const Settings &settings, const State &state, const Question &question, const Answer &answer)
Check if the question asked and the answer given are valid.
Definition: validate.cpp:50
bool * valid_question(const Settings &settings, const State &state, const Question &question)
Check if the question asked is valid.
Definition: validate.cpp:89
This file defines function headers for functions that can check if a question or state is valid.