implement reading from file. cleanup

This commit is contained in:
Maximilian Keßler 2023-08-05 11:55:46 +02:00
parent 262d909f3d
commit 3b993030ea
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 197 additions and 181 deletions

View file

@ -5,6 +5,8 @@
#include <boost/json/src.hpp>
#include <cpr/cpr.h>
#include <iostream>
#include <fstream>
#include <variant>
#include "game_state.h"
@ -58,15 +60,27 @@ boost::json::object download_game_json(int game_id) {
return boost::json::parse(r.text).as_object();
}
void download_game(int game_id) {
boost::json::object game = download_game_json(game_id);
std::cout << game << std::endl;
const auto deck = parse_deck(game.at("deck"));
const unsigned num_players = game.at("players").as_array().size();
for(const auto& card : deck) {
std::cout << card << std::endl;
boost::json::object open_game_json(const char* filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cout << "Failed to open " << filename << "." << std::endl;
return {};
}
std::string game_json ((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return boost::json::parse(game_json).as_object();
}
void get_game(std::variant<int, const char*> game_spec) {
const boost::json::object game = [&game_spec](){
if (game_spec.index() == 0) {
return download_game_json(std::get<int>(game_spec));
} else {
return open_game_json(std::get<const char*>(game_spec));
}
}();
const std::vector<Hanabi::Card> deck = parse_deck(game.at("deck"));
const unsigned num_players = game.at("players").as_array().size();
std::cout << deck.size() << std::endl;
std::cout << num_players;
}

View file

@ -1,7 +1,3 @@
//
// Created by maximilian on 7/13/23.
//
#ifndef DYNAMIC_PROGRAM_GAME_STATE_H
#define DYNAMIC_PROGRAM_GAME_STATE_H
@ -41,7 +37,7 @@ constexpr player_t draw_pile = -1;
constexpr player_t trash_or_play_stack = -2;
constexpr clue_t max_num_clues = 8;
constexpr std::array<std::string, 5> suit_initials{"r", "y", "g", "b", "p"};
constexpr std::array<std::string, 6> suit_initials{"r", "y", "g", "b", "p", "t"};
struct Card {
suit_t suit;
@ -157,10 +153,11 @@ operator<<(std::ostream &os,
HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>
hanabi_state);
#include "game_state.hpp"
template class HanabiState<5, 3, 4, 20>;
}
#include "game_state.hpp"
#endif // DYNAMIC_PROGRAM_GAME_STATE_H

View file

@ -2,15 +2,14 @@
#include <algorithm>
#include <iterator>
namespace Hanabi {
Card &Card::operator++() {
rank++;
return *this;
}
Card Card::successor() const {
return {suit, static_cast<rank_t>(rank + 1)};
}
Card Card::successor() const { return {suit, static_cast<rank_t>(rank + 1)}; }
const Card Card::operator++(int) {
Card ret = *this;
@ -48,17 +47,20 @@ Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::clue(
}
template<size_t num_suits, size_t num_players, size_t hand_size, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::incr_turn() {
void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_size>::incr_turn() {
_turn = (_turn + 1) % num_players;
}
template<size_t num_suits, size_t num_players, size_t hand_size, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::decr_turn() {
void HanabiState<num_suits, num_players, hand_size,
max_draw_pile_size>::decr_turn() {
_turn = (_turn + num_players - 1) % num_players;
}
template<std::size_t num_suits, std::size_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play(std::uint8_t index) {
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play(
std::uint8_t index) {
assert(index < _hands[_turn].size());
const Card card = _hands[_turn][index];
assert(card.rank == _stacks[card.suit] - 1);
@ -79,7 +81,8 @@ Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::play(
}
template<std::size_t num_suits, std::size_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard(std::uint8_t index) {
Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::discard(
std::uint8_t index) {
assert(index < _hands[_turn].size());
assert(_num_clues != max_num_clues);
@ -93,7 +96,6 @@ Action HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::disca
return ret;
}
template<std::size_t num_suits, std::size_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
std::ostream &operator<<(std::ostream &os, const HanabiState<num_suits, num_players, hand_size, max_draw_pile_size> hanabi_state) {
os << "Stacks: " << hanabi_state._stacks << std::endl;
@ -155,7 +157,8 @@ void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert_
}
template<std::size_t num_suits, std::size_t num_players, std::size_t hand_size, uint8_t max_draw_pile_size>
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert(const Action &action) {
void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert(
const Action &action) {
decr_turn();
switch (action.type) {
case ActionType::clue:
@ -175,3 +178,5 @@ void HanabiState<num_suits, num_players, hand_size, max_draw_pile_size>::revert(
_stacks[action.discarded.suit]++;
}
}
} // namespace Hanabi

View file

@ -38,7 +38,7 @@ void test_game() {
assert(state == state2);
}
void download() { download_game(1000000); }
void download() { get_game("314159.json"); }
void print_sizes() {
std::cout << "size of card -> hand map: " << sizeof(HanabiState<5, 3, 4, 5>)