root/lang/cplusplus/hebilife/snake.cpp @ 16006

Revision 12838, 5.1 kB (checked in by tosik, 5 years ago)

hebilife : updated some comments

  • Property svn:eol-style set to CRLF
Line 
1#include <iostream>
2
3using namespace std;
4
5
6#include "field.h"
7#include "snake.h"
8
9
10Snake::Snake(int _id, Field *_field)
11        : id(_id),
12        field(_field),
13        is_dead(false)
14{
15
16        // ����������   int r = field->random(4);
17        switch ( r )
18        {
19                case 0:
20                        direction_x = 0;
21                        direction_y = 1;
22                        break;
23                case 1:
24                        direction_x = 1;
25                        direction_y = 0;
26                        break;
27                case 2:
28                        direction_x = 0;
29                        direction_y = -1;
30                        break;
31                case 3:
32                        direction_x = -1;
33                        direction_y = 0;
34                        break;
35                default:
36                        break;
37        }
38
39        // ��W�̏���     length = field->random(field->LENGTH_MAX-1) + 1;
40        body_x[0] = field->random(field->size_x-length);
41        body_y[0] = field->random(field->size_y-length);
42        for ( int i = 1 ; i < length ; i ++ )
43        {
44                body_x[i] = body_x[i-1] - direction_x;
45                body_y[i] = body_y[i-1] - direction_y;
46        }
47        for ( int i = length ; i < 100 ; i ++ )
48        {
49                body_x[i] = -1;
50                body_y[i] = -1;
51        }
52}
53
54// ������Ƃ��Ďւ𐶐�
55Snake::Snake(int _id, Field *_field, Snake *s)
56        : id(_id),
57        field(_field),
58        is_dead(false)
59{
60        length = s->length / 2;
61        for ( int i = 0 ; i < length ; i ++ )
62        {
63                body_x[i] = s->pre_body_x[i + length];
64                body_y[i] = s->pre_body_y[i + length];
65                pre_body_x[i] = body_x[i];
66                pre_body_y[i] = body_y[i];
67        }
68        for ( int i = length ; i < 100 ; i ++ )
69        {
70                body_x[i] = -1;
71                body_y[i] = -1;
72        }
73
74        // ����������   direction_x = s->pre_body_x[length - 1] - s->pre_body_x[length];
75        direction_y = s->pre_body_y[length - 1] - s->pre_body_y[length];
76
77        brain.copy(&s->brain);
78}
79
80// step�����Ɉ��q�ɂ��s����肵�A���ɍs������void Snake::pre_move()
81{
82        if ( is_dead )
83                return;
84
85        // body��_body�ɃR�s�[
86        for ( int i = 0 ; i < length ; i ++ )
87        {
88                pre_body_x[i] = body_x[i];
89                pre_body_y[i] = body_y[i];
90        }
91
92        // ����������   int x = body_x[0];
93        int y = body_y[0];
94        int input[NN::N_NEURON];
95
96        // ������ėV�ԏꏊ
97//    input[0] = field->getHazard(x+direction_y,y-direction_x+direction_y) ? 1 : 0;
98//    input[1] = field->getHazard(x+direction_y,y+direction_x+direction_y) ? 1 : 0;
99        input[0] = field->getHazard(x+direction_y,y-direction_x) ? 1 : 0;
100        input[1] = field->getHazard(x+direction_x*3,y+direction_y*3) ? 1 : 0;
101        input[2] = field->getHazard(x+direction_y,y+direction_x) ? 1 : 0;
102//    input[3] = field->getHazard(x+direction_x,y+direction_y*2) ? 1 : 0;
103//    input[4] = field->getHazard(x+direction_x,y+direction_y*3) ? 1 : 0;
104        int output = brain.input_and_output(input);
105
106        if ( output == 1 )
107        {
108                int temp = direction_x;
109                direction_x = direction_y;
110                direction_y = - temp;
111        }
112        else if ( output == 2 )
113        {
114                int temp = direction_x;
115                direction_x = - direction_y;
116                direction_y = temp;
117        }
118
119        // �����_���ɓ���
120//    if ( field->random(10) == 0 )
121//    {
122//        int r = field->random(4);
123//        switch ( r )
124//        {
125//            case 0:
126//                if ( direction_x == 0 && direction_y == -1 )
127//                    break;
128//                direction_x = 0;
129//                direction_y = 1;
130//                break;
131//            case 1:
132//                if ( direction_x == -1 && direction_y == 0 )
133//                    break;
134//                direction_x = 1;
135//                direction_y = 0;
136//                break;
137//            case 2:
138//                if ( direction_x == 0 && direction_y == 1 )
139//                    break;
140//                direction_x = 0;
141//                direction_y = -1;
142//                break;
143//            case 3:
144//                if ( direction_x == 1 && direction_y == 0 )
145//                    break;
146//                direction_x = -1;
147//                direction_y = 0;
148//                break;
149//            default:
150//                break;
151//        }
152//    }
153
154        // ���̕�����
155        for ( int i = length ; i > 0 ; i -- )
156        {
157                pre_body_x[i] = pre_body_x[i-1];
158                pre_body_y[i] = pre_body_y[i-1];
159        }
160        pre_body_x[0] += direction_x;
161        pre_body_y[0] += direction_y;
162
163        // wrap
164        // �[�̓��b�v����        if ( pre_body_x[0] == -1 )
165                pre_body_x[0] = field->size_x - 1;
166        if ( pre_body_y[0] == -1 )
167                pre_body_y[0] = field->size_y - 1;
168        if ( pre_body_x[0] == field->size_x )
169                pre_body_x[0] = 0;
170        if ( pre_body_y[0] == field->size_y )
171                pre_body_y[0] = 0;
172
173//    if ( output == 3 && length >= field->length_max )
174//        segmentation();
175}
176
177void Snake::step()
178{
179        if ( is_dead )
180                return;
181
182        if ( field->isDead(id) )
183        {
184                die();
185                return;
186        }
187        else if ( field->canEat(id) )
188        {
189                eat();
190                if ( length >= Field::LENGTH_MAX )
191                        segmentation();
192        }
193        move();
194}
195
196// pre_move �Ō��肵���s����ۂɍs��
197void Snake::move()
198{
199        if ( is_dead )
200                return;
201
202        for ( int i = 0 ; i < length ; i ++ )
203        {
204                body_x[i] = pre_body_x[i];
205                body_y[i] = pre_body_y[i];
206        }
207}
208
209void Snake::die()
210{
211        is_dead = true;
212
213        for ( int i = 0 ; i < length; i ++ )
214        {
215                field->foods[body_x[i]][body_y[i]] = true;
216        }
217}
218
219void Snake::eat()
220{
221        length++;
222        if ( length == 100 )
223                length --;
224        field->foods[pre_body_x[0]][pre_body_y[0]] = false;
225}
226
227// ����oid Snake::segmentation()
228{
229        field->makeSnake(this);
230        length /= 2;
231}
232
Note: See TracBrowser for help on using the browser.