Quaternity
Functions
validate.cpp File Reference

This file defines functions which can check if a question, optionally plus answer, or state is valid. More...

#include <algorithm>
#include <iostream>
#include <vector>
#include "debug.h"
#include "graph.h"
#include "match.h"
#include "round.h"
#include "settings.h"
#include "state.h"
#include "update.h"
#include "validate.h"

Go to the source code of this file.

Functions

bool valid_state (const Settings &settings, const State &state)
 Check if there exists a matching for the given state. More...
 
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. More...
 
bool * valid_question (const Settings &settings, const State &state, const Question &question)
 Check if the question asked is valid. More...
 

Detailed Description

This file defines functions which can check if a question, optionally plus answer, or state is valid.

Definition in file validate.cpp.

Function Documentation

◆ valid_answer()

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.

  • If answer is yes we need to check the player does not already have the card which is being asked for.
    • If the asked player cannot have the card, return false.
    • Update the state according to question.
      • The player onturn has at least one card from asked set.
      • Only the asked player could have the asked card.
    • If there exists no matching, return false.
  • Update state according to question and answer.
  • Return if matching exists.

Definition at line 50 of file validate.cpp.

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 }
State copy_state(const State &state)
Copy the old state to a new state.
Definition: state.cpp:96
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
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
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
bool valid_state(const Settings &settings, const State &state)
Check if there exists a matching for the given state.
Definition: validate.cpp:28

◆ valid_question()

bool* valid_question ( const Settings settings,
const State state,
const Question question 
)

Check if the question asked is valid.

Check if the values (player, set, card) are all within the bounds they can be. Additionally check if the player whom a question is asked has at least one card, check that the question is not pointed towards oneself and check that we do not ask for a card from a quartet. We also make sure that there is at least one valid answer possible.

Returns
array with two boolean values, the first one indicates if 0/false is a valid answer to the question, the second one indicates if 1/true is valid, if there is no valid answer possible NULL is returned

Definition at line 89 of file validate.cpp.

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 }
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
const int NUM_SETS
The number of sets.
Definition: settings.h:32
std::vector< int > quartets
The information of who has a certain quartet.
Definition: state.h:90
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

◆ valid_state()

bool valid_state ( const Settings settings,
const State state 
)

Check if there exists a matching for the given state.

See also
graph_possible
graph_create
match_exists

Definition at line 28 of file validate.cpp.

29 {
30  if (!graph_possible(settings, state)) return false;
31  Graph graph = graph_create(settings, state);
32 
33  return match_exists(graph);
34 }
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< 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