Quaternity
graph.cpp
Go to the documentation of this file.
1 
7 #include <algorithm>
8 #include <cassert>
9 #include <numeric>
10 #include <vector>
11 
12 #include "graph.h"
13 #include "settings.h"
14 #include "state.h"
15 
19 Graph graph_copy(const Graph graph)
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 }
27 
36 bool graph_possible(const Settings &settings, const State &state)
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 }
43 
51 void graph_set(const Settings &settings, const State &state, Graph &graph, int player, int set, int hand_offset, int hand_entry)
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 }
62 
73 void graph_player(const Settings &settings, const State &state, Graph &graph, int player, int hand_offset)
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 }
90 
101 Graph graph_create(const Settings &settings, const State &state)
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 }
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
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
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
This file defines the settings struct.
This file defines the structs for a card, player and a game state, plus a state initialize and copy f...
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
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