mirror of
https://github.com/Xevion/contest.git
synced 2025-12-06 01:14:36 -06:00
Snake2 fiixed Coordinate equals method, fixed pre-game snake body not being removed, fixed tail being checked for snake collision, and fixed game over snake rendering
This commit is contained in:
@@ -26,9 +26,12 @@ class Coordinate
|
|||||||
return new Coordinate(this.x + other.x, this.y + other.y);
|
return new Coordinate(this.x + other.x, this.y + other.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Coordinate other)
|
@Override
|
||||||
|
public boolean equals(Object obj)
|
||||||
{
|
{
|
||||||
return this.x == other.x && this.y == other.y;
|
if (obj instanceof Coordinate)
|
||||||
|
return this.x == ((Coordinate) obj).x && this.y == ((Coordinate) obj).y;
|
||||||
|
return super.equals(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,7 +53,7 @@ public class Snake2
|
|||||||
{
|
{
|
||||||
// Read in board input
|
// Read in board input
|
||||||
char[][] board = new char[15][15];
|
char[][] board = new char[15][15];
|
||||||
Scanner input = new Scanner(new File("./src/snake2.dat"));
|
Scanner input = new Scanner(new File("snake2.dat"));
|
||||||
for (int y = 0; y < 15; y++) {
|
for (int y = 0; y < 15; y++) {
|
||||||
String line = input.nextLine();
|
String line = input.nextLine();
|
||||||
for (int x = 0; x < 15; x++)
|
for (int x = 0; x < 15; x++)
|
||||||
@@ -79,13 +82,11 @@ public class Snake2
|
|||||||
for (int y = 14; y >= 0; y--) {
|
for (int y = 14; y >= 0; y--) {
|
||||||
for (int x = 14; x >= 0; x--) {
|
for (int x = 14; x >= 0; x--) {
|
||||||
if (board[y][x] == 'X') {
|
if (board[y][x] == 'X') {
|
||||||
snakeHead = new Coordinate(x, y);
|
|
||||||
board[y][x] = ' ';
|
board[y][x] = ' ';
|
||||||
break;
|
if (snakeHead == null)
|
||||||
|
snakeHead = new Coordinate(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (snakeHead != null)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize simulation variables
|
// Initialize simulation variables
|
||||||
@@ -124,11 +125,18 @@ public class Snake2
|
|||||||
Coordinate newPosition = snake.get(0).plus(currentMove);
|
Coordinate newPosition = snake.get(0).plus(currentMove);
|
||||||
|
|
||||||
// Test if new move is outside boundaries or if the snake contains the new position
|
// Test if new move is outside boundaries or if the snake contains the new position
|
||||||
if (newPosition.x < 0 || newPosition.y < 0 || newPosition.x >= 15 || newPosition.y >= 15 || snake.contains(newPosition)) {
|
if (newPosition.x < 0 || newPosition.y < 0 || newPosition.x >= 15 || newPosition.y >= 15) {
|
||||||
gameover = true;
|
gameover = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test if the snake contains the new position (hitting itself), minus the tail.
|
||||||
|
for (int i = 0; i < snake.size() - 2; i++)
|
||||||
|
if (newPosition.equals(snake.get(i))) {
|
||||||
|
gameover = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// 'Move' the snake by adding new position, popping tail once
|
// 'Move' the snake by adding new position, popping tail once
|
||||||
snake.add(0, newPosition);
|
snake.add(0, newPosition);
|
||||||
|
|
||||||
@@ -142,8 +150,10 @@ public class Snake2
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print game's end status
|
// Print game's end status
|
||||||
if (gameover)
|
if (gameover) {
|
||||||
|
snake.remove(snake.size() - 1); // Remove tail to simulate out of bounds/into snake movement
|
||||||
out.println("GAME OVER");
|
out.println("GAME OVER");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
out.println(String.format("%d pellets", pellets));
|
out.println(String.format("%d pellets", pellets));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user