| 1 | #include "snake.h"
|
|---|
| 2 | #include "field.h"
|
|---|
| 3 |
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <iomanip>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <time.h>
|
|---|
| 8 | using namespace std;
|
|---|
| 9 |
|
|---|
| 10 | Field::Field()
|
|---|
| 11 | : cnt(0),
|
|---|
| 12 | steps(0)
|
|---|
| 13 | {
|
|---|
| 14 | srand((unsigned int)time(NULL));
|
|---|
| 15 | for ( int y = 0 ; y < size_y ; y ++ )
|
|---|
| 16 | {
|
|---|
| 17 | for ( int x = 0 ; x < size_x ; x ++ )
|
|---|
| 18 | {
|
|---|
| 19 | foods[x][y] = false;
|
|---|
| 20 | wall[x][y] = false;
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | // if(false)
|
|---|
| 25 | // ����
|
|---|
| 26 | for ( int i = 0 ; i < 20 ; i ++ )
|
|---|
| 27 | {
|
|---|
| 28 | int r = rand()%3;
|
|---|
| 29 |
|
|---|
| 30 | if ( r == 0 )
|
|---|
| 31 | {
|
|---|
| 32 | int x = rand()%(size_x-8), y = rand()%(size_y-8);
|
|---|
| 33 |
|
|---|
| 34 | wall[x][y + 0] = true;
|
|---|
| 35 | wall[x][y + 1] = true;
|
|---|
| 36 | wall[x][y + 2] = true;
|
|---|
| 37 | wall[x][y + 3] = true;
|
|---|
| 38 | wall[x][y + 4] = true;
|
|---|
| 39 | wall[x][y + 5] = true;
|
|---|
| 40 | wall[x][y + 6] = true;
|
|---|
| 41 | wall[x][y + 7] = true;
|
|---|
| 42 | wall[x][y + 8] = true;
|
|---|
| 43 |
|
|---|
| 44 | wall[x + 1][y] = true;
|
|---|
| 45 | wall[x + 2][y] = true;
|
|---|
| 46 | wall[x + 3][y] = true;
|
|---|
| 47 | wall[x + 4][y] = true;
|
|---|
| 48 | wall[x + 5][y] = true;
|
|---|
| 49 | wall[x + 6][y] = true;
|
|---|
| 50 | wall[x + 7][y] = true;
|
|---|
| 51 | wall[x + 8][y] = true;
|
|---|
| 52 |
|
|---|
| 53 | wall[x + 8][y + 1] = true;
|
|---|
| 54 | wall[x + 8][y + 2] = true;
|
|---|
| 55 | wall[x + 8][y + 3] = true;
|
|---|
| 56 | wall[x + 8][y + 4] = true;
|
|---|
| 57 | wall[x + 8][y + 5] = true;
|
|---|
| 58 | wall[x + 8][y + 6] = true;
|
|---|
| 59 | wall[x + 8][y + 7] = true;
|
|---|
| 60 | wall[x + 8][y + 8] = true;
|
|---|
| 61 |
|
|---|
| 62 | wall[x + 1][y + 8] = true;
|
|---|
| 63 | wall[x + 7][y + 8] = true;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if ( r == 1 )
|
|---|
| 67 | {
|
|---|
| 68 | int x = rand()%(size_x-2), y = rand()%(size_y-2);
|
|---|
| 69 |
|
|---|
| 70 | wall[x + 0][y + 0] = true;
|
|---|
| 71 | wall[x + 0][y + 1] = true;
|
|---|
| 72 | wall[x + 1][y + 0] = true;
|
|---|
| 73 | wall[x + 1][y + 1] = true;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | if ( r == 2 )
|
|---|
| 77 | {
|
|---|
| 78 | int x = rand()%(size_x-5), y = rand()%(size_y-3);
|
|---|
| 79 |
|
|---|
| 80 | wall[x + 0][y + 0] = true;
|
|---|
| 81 | wall[x + 1][y + 0] = true;
|
|---|
| 82 | wall[x + 2][y + 0] = true;
|
|---|
| 83 | wall[x + 3][y + 0] = true;
|
|---|
| 84 | wall[x + 4][y + 0] = true;
|
|---|
| 85 |
|
|---|
| 86 | wall[x + 0][y + 2] = true;
|
|---|
| 87 | wall[x + 1][y + 2] = true;
|
|---|
| 88 | wall[x + 2][y + 2] = true;
|
|---|
| 89 | wall[x + 3][y + 2] = true;
|
|---|
| 90 | wall[x + 4][y + 2] = true;
|
|---|
| 91 |
|
|---|
| 92 | wall[x + 4][y + 1] = true;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void Field::makeSnake(Snake *s)
|
|---|
| 98 | {
|
|---|
| 99 | // ��������ake��D
|
|---|
| 100 | int id;
|
|---|
| 101 |
|
|---|
| 102 | // ����nake���bool flag = false;
|
|---|
| 103 | for ( int i = 0 ; i < cnt ; i ++ )
|
|---|
| 104 | {
|
|---|
| 105 | if ( snake[i]->is_dead )
|
|---|
| 106 | {
|
|---|
| 107 | id = i;
|
|---|
| 108 | delete snake[i];
|
|---|
| 109 |
|
|---|
| 110 | flag = true;
|
|---|
| 111 | break;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | // ����q�b�g���Ȃ������ if ( !flag )
|
|---|
| 115 | id = cnt++;
|
|---|
| 116 |
|
|---|
| 117 | snake[id] = new Snake(id, this, s);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | void Field::makeSnake()
|
|---|
| 121 | {
|
|---|
| 122 | // ��������ake��D
|
|---|
| 123 | int id;
|
|---|
| 124 |
|
|---|
| 125 | // ����nake���bool flag = false;
|
|---|
| 126 | for ( int i = 0 ; i < cnt ; i ++ )
|
|---|
| 127 | {
|
|---|
| 128 | if ( snake[i]->is_dead )
|
|---|
| 129 | {
|
|---|
| 130 | id = i;
|
|---|
| 131 | delete snake[i];
|
|---|
| 132 |
|
|---|
| 133 | flag = true;
|
|---|
| 134 | break;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | // ����q�b�g���Ȃ������ if ( !flag )
|
|---|
| 138 | id = cnt++;
|
|---|
| 139 |
|
|---|
| 140 | snake[id] = new Snake(id, this);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | void Field::step()
|
|---|
| 144 | {
|
|---|
| 145 | steps ++;
|
|---|
| 146 | for ( int i = 0 ; i < cnt; i ++ )
|
|---|
| 147 | {
|
|---|
| 148 | snake[i]->pre_move();
|
|---|
| 149 | }
|
|---|
| 150 | for ( int i = 0 ; i < cnt; i ++ )
|
|---|
| 151 | {
|
|---|
| 152 | snake[i]->step();
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | void Field::view()
|
|---|
| 157 | {
|
|---|
| 158 | cout << " --- snake --- ";
|
|---|
| 159 |
|
|---|
| 160 | {
|
|---|
| 161 | int population = 0;
|
|---|
| 162 | for ( int i = 0 ; i < cnt ; i ++ )
|
|---|
| 163 | if ( !snake[i]->is_dead )
|
|---|
| 164 | population ++;
|
|---|
| 165 |
|
|---|
| 166 | cout << " population = " << population;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | cout << " steps = " << steps;
|
|---|
| 170 | cout << endl;
|
|---|
| 171 |
|
|---|
| 172 | for ( int y = 0 ; y < size_y ; y ++ )
|
|---|
| 173 | {
|
|---|
| 174 | for ( int x = 0 ; x < size_x ; x ++ )
|
|---|
| 175 | {
|
|---|
| 176 | bodies[x][y] = -1;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | for ( int j = 0 ; j < cnt ; j ++ )
|
|---|
| 180 | {
|
|---|
| 181 | if ( snake[j]->is_dead )
|
|---|
| 182 | continue;
|
|---|
| 183 | for ( int i = 0 ; i < snake[j]->length ; i ++ )
|
|---|
| 184 | {
|
|---|
| 185 | bodies[snake[j]->body_x[i]][snake[j]->body_y[i]] = j;
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | for ( int y = 0 ; y < size_y ; y ++ )
|
|---|
| 189 | {
|
|---|
| 190 | for ( int x = 0 ; x < size_x ; x ++ )
|
|---|
| 191 | {
|
|---|
| 192 | if ( bodies[x][y] != -1 )
|
|---|
| 193 | // cout << setw(2) << bodies[x][y] ;
|
|---|
| 194 | cout << "o " ;
|
|---|
| 195 | else if ( wall[x][y] )
|
|---|
| 196 | cout << "@ " ;
|
|---|
| 197 | else if ( foods[x][y] )
|
|---|
| 198 | cout << ". " ;
|
|---|
| 199 | else
|
|---|
| 200 | cout << " " ;
|
|---|
| 201 | }
|
|---|
| 202 | cout << endl;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | bool Field::isDead(int id)
|
|---|
| 207 | {
|
|---|
| 208 | int x = snake[id]->pre_body_x[0];
|
|---|
| 209 | int y = snake[id]->pre_body_y[0];
|
|---|
| 210 |
|
|---|
| 211 | if ( x < 0 || x >= size_x || y < 0 || y >= size_y )
|
|---|
| 212 | return true;
|
|---|
| 213 | else if ( wall[x][y] )
|
|---|
| 214 | return true;
|
|---|
| 215 | else
|
|---|
| 216 | {
|
|---|
| 217 | for ( int j = 0 ; j < cnt ; j ++ )
|
|---|
| 218 | {
|
|---|
| 219 | if ( snake[j]->is_dead )
|
|---|
| 220 | continue;
|
|---|
| 221 | for ( int i = 0 ; i < snake[j]->length ; i ++ )
|
|---|
| 222 | {
|
|---|
| 223 | if ( snake[j]->pre_body_x[i] == x && snake[j]->pre_body_y[i] == y )
|
|---|
| 224 | {
|
|---|
| 225 | if ( j != id || i != 0 )
|
|---|
| 226 | return true;
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 | return false;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | bool Field::canEat(int id)
|
|---|
| 235 | {
|
|---|
| 236 | int x = snake[id]->pre_body_x[0];
|
|---|
| 237 | int y = snake[id]->pre_body_y[0];
|
|---|
| 238 | return foods[x][y];
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | int Field::random(int n)
|
|---|
| 242 | {
|
|---|
| 243 | return rand() % n;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | bool Field::getHazard(int x, int y)
|
|---|
| 247 | {
|
|---|
| 248 | // wrap
|
|---|
| 249 | if ( x == -1 )
|
|---|
| 250 | x = size_x - 1;
|
|---|
| 251 | if ( y == -1 )
|
|---|
| 252 | y = size_y - 1;
|
|---|
| 253 | if ( x == size_x )
|
|---|
| 254 | x = 0;
|
|---|
| 255 | if ( y == size_y )
|
|---|
| 256 | y = 0;
|
|---|
| 257 |
|
|---|
| 258 | return bodies[x][y] != -1 || wall[x][y];
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | int Field::getBodies(int x, int y)
|
|---|
| 262 | {
|
|---|
| 263 | // wrap
|
|---|
| 264 | if ( x == -1 )
|
|---|
| 265 | x = size_x - 1;
|
|---|
| 266 | if ( y == -1 )
|
|---|
| 267 | y = size_y - 1;
|
|---|
| 268 | if ( x == size_x )
|
|---|
| 269 | x = 0;
|
|---|
| 270 | if ( y == size_y )
|
|---|
| 271 | y = 0;
|
|---|
| 272 |
|
|---|
| 273 | return bodies[x][y];
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | int Field::getFoods(int x, int y)
|
|---|
| 277 | {
|
|---|
| 278 | // wrap
|
|---|
| 279 | if ( x == -1 )
|
|---|
| 280 | x = size_x - 1;
|
|---|
| 281 | if ( y == -1 )
|
|---|
| 282 | y = size_y - 1;
|
|---|
| 283 | if ( x == size_x )
|
|---|
| 284 | x = 0;
|
|---|
| 285 | if ( y == size_y )
|
|---|
| 286 | y = 0;
|
|---|
| 287 |
|
|---|
| 288 | return foods[x][y];
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | Field::~Field()
|
|---|
| 292 | {
|
|---|
| 293 | // release����
|
|---|
| 294 | for ( int i = 0 ; i < cnt ; i ++ )
|
|---|
| 295 | delete snake[i];
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|