1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
* Copyright 2016-2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* panaconda.hpp -- example usage of libpmemobj C++ bindings
*/
#ifndef LIBPMEMOBJ_CPP_EXAMPLES_PANACONDA_HPP
#define LIBPMEMOBJ_CPP_EXAMPLES_PANACONDA_HPP
#include <libpmemobj++/make_persistent_array.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj_cpp_examples_list.hpp>
class board_element;
enum direction { UNDEFINED, DOWN, RIGHT, UP, LEFT };
enum object_type { SNAKE_SEGMENT, WALL, FOOD };
enum config_file_symbol { SYM_NOTHING = '0', SYM_WALL = '1' };
enum play_state { STATE_NEW, STATE_PLAY, STATE_GAMEOVER };
enum snake_event { EV_OK, EV_COLLISION };
enum action { ACTION_NEW_GAME = 'n', ACTION_QUIT = 'q' };
typedef pmem::obj::persistent_ptr<examples::list<board_element>> element_list;
struct color_pair {
color_pair();
color_pair(const int col_fg, const int col_bg);
int color_bg;
int color_fg;
};
struct parameters {
bool use_maze;
std::string name;
std::string maze_path;
};
class helper {
public:
static color_pair get_color(const object_type obj_type);
static int parse_params(int argc, char *argv[],
struct parameters *params);
static inline void sleep(int time);
static inline void print_usage(std::string &name);
};
class point {
public:
pmem::obj::p<int> x;
pmem::obj::p<int> y;
point();
point(int x, int y);
friend bool operator==(point &point1, point &point2);
};
bool operator==(point &point1, point &point2);
class element_shape {
public:
element_shape() = default;
element_shape(int shape);
int get_val();
private:
pmem::obj::p<int> val;
int get_symbol(int shape);
};
class board_element {
public:
board_element();
board_element(int px, int py,
pmem::obj::persistent_ptr<element_shape> shape,
direction dir);
board_element(point p, pmem::obj::persistent_ptr<element_shape> shape,
direction dir);
board_element(const board_element &element);
~board_element();
pmem::obj::persistent_ptr<point> calc_new_position(const direction dir);
void print(void);
void print_double_col(void);
void print_single_double_col(void);
pmem::obj::persistent_ptr<point> get_position(void);
void set_position(const pmem::obj::persistent_ptr<point> new_point);
direction get_direction(void);
void set_direction(const direction dir);
private:
pmem::obj::persistent_ptr<point> position;
pmem::obj::persistent_ptr<element_shape> shape;
pmem::obj::p<direction> element_dir;
};
class snake {
public:
snake();
~snake();
void move(const direction dir);
void print(void);
void add_segment(void);
bool check_point_against_segments(point point);
point get_head_point(void);
direction get_direction(void);
point get_next_point(const direction dir);
private:
element_list snake_segments;
pmem::obj::p<point> last_seg_position;
pmem::obj::p<direction> last_seg_dir;
};
class game_board {
public:
game_board();
~game_board();
void print(const int score);
void print_game_over(const int score);
unsigned get_size_row(void);
void set_size_row(const unsigned size_r);
unsigned get_size_col(void);
void set_size_col(const unsigned size_c);
int creat_dynamic_layout(const unsigned row_no, char *const buffer);
int creat_static_layout(void);
bool is_snake_head_food_hit(void);
void create_new_food(void);
bool is_collision(point point);
snake_event move_snake(const direction dir);
direction get_snake_dir(void);
void add_snake_segment(void);
private:
pmem::obj::persistent_ptr<snake> anaconda;
pmem::obj::persistent_ptr<board_element> food;
element_list layout;
pmem::obj::p<unsigned> size_row;
pmem::obj::p<unsigned> size_col;
void set_new_food(const point point);
bool is_snake_collision(point point);
bool is_wall_collision(point point);
};
class game_player {
public:
game_player();
~game_player();
int get_score(void);
void update_score(void);
play_state get_state(void);
void set_state(const play_state st);
private:
pmem::obj::p<int> score;
pmem::obj::p<play_state> state;
};
class game_state {
public:
game_state();
~game_state();
pmem::obj::persistent_ptr<game_board> get_board();
pmem::obj::persistent_ptr<game_player> get_player();
void init(void);
void clean_pool(void);
private:
pmem::obj::persistent_ptr<game_board> board;
pmem::obj::persistent_ptr<game_player> player;
};
class game {
public:
game(struct parameters *par);
~game();
void init(void);
void init_colors(void);
void process_step(void);
void process_key(const int lastkey);
inline bool is_stopped(void);
void process_delay(void);
void clear_screen(void);
bool is_game_over(void);
void game_over(void);
void clear_prog(void);
private:
pmem::obj::pool<game_state> state;
int last_key;
int delay;
struct parameters *params;
direction direction_key;
void clean_pool(void);
void set_direction_key(void);
int parse_conf_create_dynamic_layout(void);
};
#endif /* LIBPMEMOBJ_CPP_EXAMPLES_PANACONDA_HPP */
|