-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRules.cpp
More file actions
178 lines (155 loc) · 4.78 KB
/
Rules.cpp
File metadata and controls
178 lines (155 loc) · 4.78 KB
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
#include "Rules.hpp"
#include "SnakeException.hpp"
#include "CellOccupier.hpp"
#include <iostream>
#include <string.h>
#include <stdio.h>
Rules::Rules(shared_ptr<Board> board, bool through_walls)
: m_board(board), m_coord_space(board->get_width(), board->get_height()),
m_wall(new WallOccupier()), m_teleport(new TeleportOccupier()), m_food(new FoodOccupier()){
m_prev_snake_size = 0;
m_through_walls = through_walls;
srand(time(NULL));
}
shared_ptr<Board> Rules::get_board(){
return m_board;
}
int Rules::snake_count(){
return m_snakes.size();
}
const Snake* Rules::get_snake(int index){
const Snake* snake = &m_snakes[index];
return snake;
}
void Rules::place_food(){
int x = rand() % m_board->get_width();
int y = rand() % m_board->get_height();
if (m_board->lookup(Coord(x,y))->get_type() == CellOccupier::EMPTY){
m_board->insert(m_food.get(), Coord(x,y));
} else {
place_food();
}
}
bool Rules::coord_out_of_bounds(Coord coord){
return (m_board->lookup(coord)->get_type() == CellOccupier::WALL);
}
bool Rules::move_snake(int index, Vector::Direction direction){
Snake& snake = m_snakes[index];
if (Vector::inverse(direction) == snake.get_direction()){
return compute_move(snake, snake.get_direction());
}
return compute_move(snake, direction);
}
bool Rules::move_snake(int index){
Snake& snake = m_snakes[index];
return compute_move(snake, snake.get_direction());
}
bool Rules::snake_dead(){
for (ptr_vector<Snake>::iterator itr = m_snakes.begin(); itr != m_snakes.end(); ++itr){
if (!(*itr).is_alive()){
return true;
}
}
return false;
}
bool Rules::compute_move(Snake& snake, Vector::Direction direction){
Coord old_front = m_board->find(&snake);
Coord new_front = m_coord_space.move(old_front, direction);
snake.set_direction(direction); // Need to set this here so grow moves snake in right direction
return m_board->lookup(new_front)->handle_move(new_front, direction, m_coord_space, m_board, &snake, this);
}
void Rules::build_wall(){
CellOccupier* wall;
if (m_through_walls){
wall = m_teleport.get();
} else{
wall = m_wall.get();
}
int width = m_board->get_width();
int height = m_board->get_height();
for (int x = 0; x < width; ++x){
m_board->insert(wall, Coord(x, 0));
m_board->insert(wall, Coord(x, height-1));
}
for (int y = 0; y < height; ++y){
m_board->insert(wall, Coord(0, y));
m_board->insert(wall, Coord(width-1, y));
}
}
void Rules::set_snakes(int players){
set_snakes(players, m_prev_snake_size);
}
void Rules::set_snakes(int players, int snake_size){
m_prev_snake_size = snake_size;
Coord board_middle(m_board->get_width()/2, m_board->get_height()/2);
for (int player = 0; player < players; ++player){
Coord snake_start(board_middle.get_x() - player, board_middle.get_y());
Snake* snake = new Snake(snake_size, Vector::DOWN);
m_snakes.push_back(snake);
m_board->insert(snake, snake_start);
snake->build_tail(m_board, m_coord_space);
}
}
void Rules::reset(){
m_board->clear();
build_wall();
int players = m_snakes.size();
m_snakes.clear();
set_snakes(players);
place_food();
}
CellOccupier* Rules::get_food(){
return m_food.get();
}
vector<CellOccupier*> Rules::get_walls(){
vector<CellOccupier*> walls;
walls.push_back(m_wall.get());
if (m_through_walls){
walls.push_back(m_teleport.get());
}
return walls;
}
RuleBuilder::RuleBuilder(){
m_through_walls = false;
m_player_count = 0;
m_snake_size = 0;
}
RuleBuilder& RuleBuilder::set_board(shared_ptr<Board> board){
m_board = board;
return *this;
}
RuleBuilder& RuleBuilder::set_snake_size(int size){
m_snake_size = size;
return *this;
}
RuleBuilder& RuleBuilder::set_player_count(int count){
m_player_count = count;
return *this;
}
RuleBuilder& RuleBuilder::set_through_walls(bool through_walls){
m_through_walls = through_walls;
return *this;
}
shared_ptr<Rules> RuleBuilder::create(){
BoardBuilder board_builder;
if (m_board.get() == NULL || m_player_count == 0){
throw RuleBuilderException();
}
//DEFAULT
if (m_snake_size == 0){
m_snake_size = 3;
}
//Subtract two for board perimiter
if (m_snake_size >= (m_board->get_width()/2 - 2)){
throw SnakeTooBigException();
}
// Hand in initialiser object that can build wall and snakes.
shared_ptr<Rules> rules = shared_ptr<Rules> (new Rules(m_board, m_through_walls));
rules->build_wall();
rules->set_snakes(m_player_count, m_snake_size);
rules->place_food();
return rules;
}
int Rules::get_player_count(){
return m_snakes.size();
}