Quaternity
Functions
graph.cpp File Reference

This file defines the graph creation functions. More...

#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
#include "graph.h"
#include "settings.h"
#include "state.h"

Go to the source code of this file.

Functions

Graph graph_copy (const Graph graph)
 Create a copy of the given graph. More...
 
bool graph_possible (const Settings &settings, const State &state)
 Check if it is possible to create a graph. More...
 
void graph_set (const Settings &settings, const State &state, Graph &graph, int player, int set, int hand_offset, int hand_entry)
 Add all the edges representing the set constraints for a player. More...
 
void graph_player (const Settings &settings, const State &state, Graph &graph, int player, int hand_offset)
 Add all the edges representing the given player. More...
 
Graph graph_create (const Settings &settings, const State &state)
 Create a biparite graph which has a matching if the state is valid. More...
 

Detailed Description

This file defines the graph creation functions.

Definition in file graph.cpp.

Function Documentation

◆ graph_copy()

Graph graph_copy ( const Graph  graph)

Create a copy of the given graph.

Definition at line 19 of file graph.cpp.

20 {
21  Graph graph_clone(graph.size(), std::vector<bool>(graph.size()));
22  for (size_t i = 0; i < graph.size(); i++)
23  for (size_t j = 0; j < graph[i].size(); j++)
24  graph_clone[i][j] = graph[i][j];
25  return graph_clone;
26 }
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

◆ graph_create()

Graph graph_create ( const Settings settings,
const State state 
)

Create a biparite graph which has a matching if the state is valid.

The graph is represented as a 2d array where every cell is an edge between the left node (row number) and the right node (column number). We go through all the players and add the constraints corresponding to that player's possible hands.

See also
graph_player

Definition at line 101 of file graph.cpp.

102 {
103  assert(graph_possible(settings, state)); // make sure we can create a graph
104 
105  // create the graph array and initialize all values to false
106  const int NUM_CARDS = settings.NUM_SETS * settings.SET_SIZE;
107  Graph graph(NUM_CARDS, std::vector<bool>(NUM_CARDS, false));
108 
109  // set the corresponding edges for all players
110  int hand_offset = 0;
111  for (int player = 0; player < settings.NUM_PLAYERS; player++) {
112  graph_player(settings, state, graph, player, hand_offset);
113  hand_offset += state.players[player].num_cards;
114  }
115 
116  return graph;
117 }
bool graph_possible(const Settings &settings, const State &state)
Check if it is possible to create a graph.
Definition: graph.cpp:36
void graph_player(const Settings &settings, const State &state, Graph &graph, int player, int hand_offset)
Add all the edges representing the given player.
Definition: graph.cpp:73
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
std::vector< Player > players
The information on all the players.
Definition: state.h:81

◆ graph_player()

void graph_player ( const Settings settings,
const State state,
Graph graph,
int  player,
int  hand_offset 
)

Add all the edges representing the given player.

First we add all the edges for our set constraints.

After this we assign edges to the leftover cards the player has going to all possible cards the player could have.

See also
graph_set

Definition at line 73 of file graph.cpp.

74 {
75  // go through all set constraints and make edges for them
76  int hand_entry = 0;
77  for (int set = 0; set < settings.NUM_SETS; set++) {
78  graph_set(settings, state, graph, player, set, hand_offset, hand_entry);
79  hand_entry += state.players[player].sets[set];
80  }
81 
82  // after all set constraints are set we can have any card
83  while (hand_entry < state.players[player].num_cards) {
84  for (int card = 0; card < settings.NUM_SETS * settings.SET_SIZE; card++)
85  if (state.cards[card].players[player])
86  graph[hand_offset + hand_entry][card] = true;
87  hand_entry++;
88  }
89 }
void graph_set(const Settings &settings, const State &state, Graph &graph, int player, int set, int hand_offset, int hand_entry)
Add all the edges representing the set constraints for a player.
Definition: graph.cpp:51
std::vector< Card > cards
The information on all the cards.
Definition: state.h:74

◆ graph_possible()

bool graph_possible ( const Settings settings,
const State state 
)

Check if it is possible to create a graph.

More specifically, check that no player's total number of set constraints is bigger than the number of cards that player has.

Returns
true if possible

Definition at line 36 of file graph.cpp.

37 {
38  for (Player player: state.players)
39  if (player.num_cards < accumulate(player.sets.begin(), player.sets.end(), 0))
40  return false;
41  return true;
42 }
The information connected to a player.
Definition: state.h:34
std::vector< int > sets
The number of cards the player at least has from a certain set.
Definition: state.h:48
int num_cards
The number of cards this player has.
Definition: state.h:39

◆ graph_set()

void graph_set ( const Settings settings,
const State state,
Graph graph,
int  player,
int  set,
int  hand_offset,
int  hand_entry 
)

Add all the edges representing the set constraints for a player.

For every set constraint corresponding to the given set and player, add edges to the current player card (hand_offset + hand_entry) to all cards the player can have from the given set.

Definition at line 51 of file graph.cpp.

52 {
53  int minimum = state.players[player].sets[set];
54  while (minimum-- > 0) {
55  int card_start = set * settings.SET_SIZE;
56  for (int card = card_start; card < card_start + settings.SET_SIZE; card++)
57  if (state.cards[card].players[player])
58  graph[hand_offset + hand_entry][card] = true;
59  hand_entry++;
60  }
61 }