Quaternity
Functions
validate.h File Reference

This file defines function headers for functions that can check if a question or state is valid. More...

#include "round.h"
#include "settings.h"
#include "state.h"

Go to the source code of this file.

Functions

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 function headers for functions that can check if a question or state is valid.

Definition in file validate.h.

Function Documentation

◆ 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
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 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
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
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