@@ -46,7 +46,7 @@ public class Snake {
4646 * @return SnakeBody the snake body at the specified index
4747 */
4848 public void updatePosition (SnakeMove movement ) {
49- snakeBody . get( 0 ). setMovement(movement);
49+ getSnakeHead( ). setMovement(movement);
5050 }
5151
5252 /**
@@ -61,8 +61,8 @@ public class Snake {
6161 * Gets the first position of the snake.
6262 * @return Position The first position of the snake.
6363 */
64- public Position getFirstPosition () {
65- return snakeBody . get( 0 ) . position ;
64+ public int [] getFirstPosition () {
65+ return getSnakeHead() . getPosition() ;
6666 }
6767
6868 /**
@@ -83,7 +83,8 @@ public class Snake {
8383 var movement = lastBody. getMovement();
8484 var offset = movement. getOffset();
8585 var body = new SnakeBody (movement, positions);
86- body. position. sum(offset[0 ], offset[1 ]);
86+ body. setX(body. getX() + offset[0 ]);
87+ body. setY(body. getY() + offset[1 ]);
8788 addSnakeBody(body);
8889 }
8990
@@ -97,8 +98,8 @@ public class Snake {
9798 for (int index = snakeBody. size() - 1 ; index > 0 ; index-- ) {
9899 var currentBody = snakeBody. get(index);
99100 var previousBody = snakeBody. get(index - 1 );
100- currentBody. getPosition() . setX(previousBody. getPosition() . getX());
101- currentBody. getPosition() . setY(previousBody. getPosition() . getY());
101+ currentBody. setX(previousBody. getX());
102+ currentBody. setY(previousBody. getY());
102103 currentBody. setMovement(previousBody. getMovement());
103104 }
104105 }
@@ -117,15 +118,15 @@ public class Snake {
117118 * Starts the snake at the specified position.
118119 * @param position The position to start the snake.
119120 */
120- public void startSnake (Integer [] position ) {
121- addSnakeBody(new SnakeBody (SnakeMove . UP , new Position ( position) ));
121+ public void startSnake (int [] position ) {
122+ addSnakeBody(new SnakeBody (SnakeMove . UP , position));
122123 }
123124
124125 /**
125126 * Updates the position of all snake pieces of body.
126127 */
127128 private void updateHeadPosition () {
128- snakeBody . get( 0 ). doMovement(SnakeGame . VELOCITY );
129+ getSnakeHead( ). doMovement(SnakeGame . VELOCITY );
129130 }
130131
131132
@@ -134,38 +135,44 @@ public class Snake {
134135 * @return true if there is a collision, false otherwise.
135136 */
136137 private boolean checkCollisionWithBody () {
137- var firstPosition = getFirstPosition ();
138+ var firstBody = getSnakeHead ();
138139 var result = false ;
139140 for (int index = 1 ; index < snakeBody. size(); index++ ) {
140- var bodyPos = snakeBody. get(index) . getPosition( );
141- if (Math . abs(bodyPos . getX() - firstPosition . getX()) <= BODY_COLISION_TOLERANCE &&
142- Math . abs(bodyPos . getY() - firstPosition . getY()) <= BODY_COLISION_TOLERANCE ) {
141+ var body = snakeBody. get(index);
142+ if (Math . abs(body . getX() - firstBody . getX()) <= BODY_COLISION_TOLERANCE &&
143+ Math . abs(body . getY() - firstBody . getY()) <= BODY_COLISION_TOLERANCE ) {
143144 result = true ;
144145 break ;
145146 }
146147 }
147148 return result;
148149 }
149150
151+ /**
152+ * Gets the snake's head.
153+ * @return SnakeBody The snake's head.
154+ */
155+ public SnakeBody getSnakeHead () {
156+ return snakeBody. get(0 );
157+ }
150158
151159 /**
152160 * Checks if the snake collides with the food.
153161 * @return true if the snake collides with the food, false otherwise.
154162 */
155163 private boolean checkCollisionWithFood () {
156- var firstPosition = getFirstPosition();
157- var foodPosition = SnakeGame . food. getPosition();
158- return (Math . abs(foodPosition. getX() - firstPosition. getX()) <= FOOD_COLISION_TOLERANCE &&
159- Math . abs(foodPosition. getY() - firstPosition. getY()) <= FOOD_COLISION_TOLERANCE );
164+ var bodyPos = getSnakeHead();
165+ return (Math . abs(SnakeGame . FOOD_X - bodyPos. getX()) <= FOOD_COLISION_TOLERANCE &&
166+ Math . abs(SnakeGame . FOOD_Y - bodyPos. getY()) <= FOOD_COLISION_TOLERANCE );
160167 }
161168
162169 /**
163170 * Checks if the snake has collided with the game borders.
164171 * @return true if the snake has collided with the borders, false otherwise.
165172 */
166173 private boolean checkCollisionWithBorders () {
167- var position = getFirstPosition ();
168- return (position . getX() < SnakeGame . BORDER || position . getX() > SnakeGame . W - SnakeGame . BORDER - SnakeGame . SNAKE_SIZE_W ||
169- position . getY() < SnakeGame . BORDER || position . getY() > SnakeGame . H - SnakeGame . BORDER - SnakeGame . SNAKE_SIZE_H );
174+ var body = getSnakeHead ();
175+ return (body . getX() < SnakeGame . BORDER || body . getX() > SnakeGame . W - SnakeGame . BORDER - SnakeGame . SNAKE_SIZE_W ||
176+ body . getY() < SnakeGame . BORDER || body . getY() > SnakeGame . H - SnakeGame . BORDER - SnakeGame . SNAKE_SIZE_H );
170177 }
171178}
0 commit comments