Quaternity
Functions
state.cpp File Reference

This file defines state initialization and copy functions. More...

#include <algorithm>
#include <vector>
#include "settings.h"
#include "state.h"

Go to the source code of this file.

Functions

std::vector< Cardinit_cards (const Settings &settings)
 Initialize the cards corresponding to the given settings. More...
 
std::vector< Playerinit_players (const Settings &settings)
 Initialize the players corresponding to the given settings. More...
 
State init_state (const Settings &settings)
 Initialize the starting state corresponding to the given settings. More...
 
std::vector< Cardcopy_cards (const std::vector< Card > cards)
 Copy the old cards vector to a new cards vector. More...
 
std::vector< Playercopy_players (const std::vector< Player > players)
 Copy the old player vector to a new player vector. More...
 
State copy_state (const State &state)
 Copy the old state to a new state. More...
 
int info_num_quartets (const State &state, int player)
 Get the number of quartets the given player has. More...
 
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 of cards the player has in its hands. More...
 

Detailed Description

This file defines state initialization and copy functions.

Definition in file state.cpp.

Function Documentation

◆ copy_cards()

std::vector<Card> copy_cards ( const std::vector< Card cards)

Copy the old cards vector to a new cards vector.

Definition at line 67 of file state.cpp.

68 {
69  std::vector<Card> cards_copy;
70  for (Card card: cards) {
71  Card card_copy = {card.players};
72  cards_copy.push_back(card_copy);
73  }
74  return cards_copy;
75 }
The information connected to a card.
Definition: state.h:19
std::vector< bool > players
The players which could have this card.
Definition: state.h:28

◆ copy_players()

std::vector<Player> copy_players ( const std::vector< Player players)

Copy the old player vector to a new player vector.

Definition at line 80 of file state.cpp.

81 {
82  std::vector<Player> players_copy;
83  for (Player player: players) {
84  Player player_copy = {player.num_cards, player.sets};
85  players_copy.push_back(player_copy);
86  }
87  return players;
88 }
The information connected to a player.
Definition: state.h:34
int num_cards
The number of cards this player has.
Definition: state.h:39

◆ copy_state()

State copy_state ( const State state)

Copy the old state to a new state.

See also
copy_cards
copy_players

Definition at line 96 of file state.cpp.

97 {
98  return State{state.onturn, copy_cards(state.cards), copy_players(state.players), state.quartets};
99 }
std::vector< Card > copy_cards(const std::vector< Card > cards)
Copy the old cards vector to a new cards vector.
Definition: state.cpp:67
std::vector< Player > copy_players(const std::vector< Player > players)
Copy the old player vector to a new player vector.
Definition: state.cpp:80
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

◆ info_num_cards()

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 of cards the player has in its hands.

Definition at line 113 of file state.cpp.

114 {
115  return state.players[player].num_cards - settings.SET_SIZE * info_num_quartets(state, player);
116 }
int info_num_quartets(const State &state, int player)
Get the number of quartets the given player has.
Definition: state.cpp:104
const int SET_SIZE
The size of one set, aka the number of cards in one set.
Definition: settings.h:27

◆ info_num_quartets()

int info_num_quartets ( const State state,
int  player 
)

Get the number of quartets the given player has.

Definition at line 104 of file state.cpp.

105 {
106  return std::count(state.quartets.begin(), state.quartets.end(), player);
107 }

◆ init_cards()

std::vector<Card> init_cards ( const Settings settings)

Initialize the cards corresponding to the given settings.

The players vector for every card will be initilialized to all true, because at the start every player could have that card. There are a total of SET_SIZE * NUM_SETS cards which will be generated.

Definition at line 20 of file state.cpp.

21 {
22  std::vector<Card> cards;
23  for (int i = 0; i < settings.SET_SIZE * settings.NUM_SETS; i++) {
24  Card card = {std::vector<bool>(settings.NUM_PLAYERS, true)};
25  cards.push_back(card);
26  }
27  return cards;
28 }
const int NUM_SETS
The number of sets.
Definition: settings.h:32
const int NUM_PLAYERS
The number of players playing the game.
Definition: settings.h:37

◆ init_players()

std::vector<Player> init_players ( const Settings settings)

Initialize the players corresponding to the given settings.

The cards are equally distributed among the players which means that every player will start with (NUM_SETS * SET_SIZE) / NUM_PLAYERS amount of cards. They further have the sets vector initialized to all zeros because they do not need to have any specific set at the start.

Definition at line 38 of file state.cpp.

39 {
40  const int NUM_CARDS = (settings.NUM_SETS * settings.SET_SIZE) / settings.NUM_PLAYERS;
41  std::vector<Player> players;
42  for (int i = 0; i < settings.NUM_PLAYERS; i++) {
43  Player player = {NUM_CARDS, std::vector<int>(settings.NUM_SETS, 0)};
44  players.push_back(player);
45  }
46  return players;
47 }

◆ init_state()

State init_state ( const Settings settings)

Initialize the starting state corresponding to the given settings.

The cards and players are defined by their respective functions. The zero'th player starts the game (has the first turn).

See also
init_cards
init_players

Definition at line 58 of file state.cpp.

59 {
60  std::vector<int> quartets(settings.NUM_SETS, -1);
61  return State{0, init_cards(settings), init_players(settings), quartets};
62 }
std::vector< Card > init_cards(const Settings &settings)
Initialize the cards corresponding to the given settings.
Definition: state.cpp:20
std::vector< Player > init_players(const Settings &settings)
Initialize the players corresponding to the given settings.
Definition: state.cpp:38