@@ -13,11 +13,23 @@ public class Snake {
1313 private short [] bodyX, bodyY;
1414 private short arrIndex;
1515 private SnakeMove [] movements;
16+ private byte gap;
1617
1718 public Snake () {
1819 init();
1920 }
2021
22+ public short getPositionX (int index ) {
23+ return bodyX[index];
24+ }
25+
26+ public short getPositionY (int index ) {
27+ return bodyY[index];
28+ }
29+
30+ /**
31+ * Init the arrays and run the calc to the screen
32+ */
2133 private void init () {
2234 arrIndex = 0 ;
2335 var pieces = piecesPerScreen();
@@ -26,7 +38,11 @@ public class Snake {
2638 movements = new SnakeMove [pieces];
2739 }
2840
29- public int piecesPerScreen () {
41+ /**
42+ * Calc how much pieces per screen the snake can handle
43+ * @return int result of calc
44+ */
45+ private int piecesPerScreen () {
3046 return (SnakeGame . W * SnakeGame . H ) / (SnakeGame . SNAKE_SIZE_W * SnakeGame . SNAKE_SIZE_H );
3147 }
3248
@@ -48,19 +64,11 @@ public class Snake {
4864 movements[0 ] = movement;
4965 }
5066
51- /**
52- * Gets the first position of the snake.
53- * @return Position The first position of the snake.
54- */
55- public short [] getFirstPosition () {
56- return new short []{bodyX[0 ], bodyY[0 ]};
57- }
58-
5967 /**
6068 * Destroys the snake by clearing its body and positions.
6169 */
6270 public void destroySnake () {
63- init() ;
71+ arrIndex = 0 ;
6472 }
6573
6674 /**
@@ -69,20 +77,20 @@ public class Snake {
6977 * The new body is positioned one movement offset away from the last body.
7078 */
7179 public void newBody () {
72- short [] lastPosition = {bodyX[arrIndex], bodyY[arrIndex]};
73- var movement = movements[arrIndex];
74- var offset = movement. getOffset();
80+ SnakeMove movement = movements[arrIndex];
81+ bodyX[arrIndex+ 1 ] = (short ) (getPositionX(arrIndex) + movement. getOffsetX());
82+ bodyY[arrIndex+ 1 ] = (short ) (getPositionY(arrIndex) + movement. getOffsetY());
83+ movements[arrIndex+ 1 ] = movement;
7584 arrIndex++ ;
76- bodyX[arrIndex] = (short )(lastPosition[0 ] + offset[0 ]);
77- bodyY[arrIndex] = (short )(lastPosition[1 ] + offset[1 ]);
78- movements[arrIndex] = movement;
7985 }
8086
87+ /**
88+ * Get the size of array
89+ */
8190 public short getSize () {
8291 return (short ) (arrIndex + 1 );
8392 }
8493
85-
8694 /**
8795 * Updates the movement of the snake and checks for collisions with food, borders and body.
8896 * If the snake collides with food, a new food is created and the score is updated.
@@ -109,32 +117,31 @@ public class Snake {
109117
110118 /**
111119 * Starts the snake at the specified position.
112- * @param position The position to start the snake.
120+ * @param positionX The position X to start the snake.
121+ * @param positionY The position Y to start the snake.
113122 */
114- public void startSnake (short [] position ) {
115- bodyX[0 ] = position[ 0 ] ;
116- bodyY[0 ] = position[ 1 ] ;
123+ public void startSnake (short positionX , short positionY ) {
124+ bodyX[0 ] = positionX ;
125+ bodyY[0 ] = positionY ;
117126 movements[0 ] = SnakeMove . UP ;
118127 }
119128
120129 /**
121- * Updates the position of all snake pieces of body.
130+ * Updates the position of snake head body
122131 */
123132 private void updateHeadPosition () {
124133 doMovement(movements[0 ], SnakeGame . VELOCITY );
125134 }
126135
127-
128136 /**
129137 * Checks if the snake's head collides with its body.
130138 * @return true if there is a collision, false otherwise.
131139 */
132140 private boolean checkCollisionWithBody () {
133141 var result = false ;
134142 for (int index = 1 ; index < getSize(); index++ ) {
135- short [] body = {bodyX[index], bodyY[index]};
136- if (Math . abs(body[0 ] - bodyX[0 ]) <= BODY_COLISION_TOLERANCE &&
137- Math . abs(body[1 ] - bodyY[0 ]) <= BODY_COLISION_TOLERANCE ) {
143+ if (Math . abs(bodyX[index] - bodyX[0 ]) <= BODY_COLISION_TOLERANCE &&
144+ Math . abs(bodyY[index] - bodyY[0 ]) <= BODY_COLISION_TOLERANCE ) {
138145 result = true ;
139146 break ;
140147 }
@@ -160,23 +167,38 @@ public class Snake {
160167 bodyY[0 ] < SnakeGame . BORDER || bodyY[0 ] > SnakeGame . H - SnakeGame . BORDER - SnakeGame . SNAKE_SIZE_H );
161168 }
162169
170+ private void fixBodyYGrid () {
171+ gap = (byte )(bodyY[0 ] % SnakeGame . GRID_SIZE );
172+ bodyY[0 ] = (short ) (gap < 5 ? bodyY[0 ] - gap : bodyY[0 ] + gap);
173+ }
174+
175+ private void fixBodyXGrid () {
176+ gap = (byte )(bodyY[0 ] % SnakeGame . GRID_SIZE );
177+ bodyX[0 ] = (short ) (gap < 5 ? bodyX[0 ] - gap : bodyX[0 ] + gap);
178+ }
179+
163180 /**
164181 * Do a movement in the snake body with a velocity value
182+ * @param movement actual snakeMove
165183 * @param velocity short value of velocity
166184 */
167185 public void doMovement (SnakeMove movement , short velocity ) {
168186 switch (movement) {
169187 case UP :
170188 bodyY[0 ] -= velocity;
189+ fixBodyYGrid();
171190 break ;
172191 case DOWN :
173192 bodyY[0 ] += velocity;
193+ fixBodyYGrid();
174194 break ;
175195 case LEFT :
176196 bodyX[0 ] -= velocity;
197+ fixBodyXGrid();
177198 break ;
178199 case RIGHT :
179200 bodyX[0 ] += velocity;
201+ fixBodyXGrid();
180202 break ;
181203 }
182204 }
0 commit comments