Skip to content

Commit

Permalink
Add Snake game
Browse files Browse the repository at this point in the history
  • Loading branch information
peterarsentev committed Mar 27, 2024
1 parent 7351f30 commit 54b0d02
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 143 deletions.
44 changes: 1 addition & 43 deletions snake/src/main/java/ru/job4j/snake/Cell.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
package ru.job4j.snake;

import java.util.Objects;

public class Cell {
private final int x;
private final int y;

public Cell(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public String toString() {
return "Cell[" + x + ", " + y + ']';
}

public int getX() {
return x;
}

public int getY() {
return y;
}



@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Cell cell = (Cell) o;
return x == cell.x
&& y == cell.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
public record Cell(int x, int y) {
}
5 changes: 2 additions & 3 deletions snake/src/main/java/ru/job4j/snake/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
Expand Down Expand Up @@ -37,8 +36,8 @@ private Rectangle buildRectangle(int x, int y, int size) {

private Rectangle block(Cell cell, Color color) {
Rectangle rect = new Rectangle();
rect.setX(cell.getX() * 40 + 5);
rect.setY(cell.getY() * 40 + 5);
rect.setX(cell.x() * 40 + 5);
rect.setY(cell.y() * 40 + 5);
rect.setHeight(30);
rect.setWidth(30);
rect.setFill(color);
Expand Down
16 changes: 8 additions & 8 deletions snake/src/main/java/ru/job4j/snake/Snake.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.job4j.snake;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Snake {
private List<Cell> body = new ArrayList<>();
private Cell apple;
private Direction direction = Direction.RIGHT;
private final List<Cell> body = new CopyOnWriteArrayList<>();
private volatile Cell apple;
private volatile Direction direction = Direction.RIGHT;

public Snake(List<Cell> cells) {
body.addAll(cells);
Expand All @@ -23,10 +23,10 @@ void directionTo(Direction direction) {
boolean step() {
var tail = body.get(body.size() - 1);
var next = switch (direction) {
case RIGHT -> new Cell(tail.getX() + 1, tail.getY());
case LEFT -> new Cell(tail.getX() - 1, tail.getY());
case UP -> new Cell(tail.getX(), tail.getY() - 1);
case DOWN -> new Cell(tail.getX(), tail.getY() + 1);
case RIGHT -> new Cell(tail.x() + 1, tail.y());
case LEFT -> new Cell(tail.x() - 1, tail.y());
case UP -> new Cell(tail.x(), tail.y() - 1);
case DOWN -> new Cell(tail.x(), tail.y() + 1);
};
var eat = next.equals(apple);
if (!eat) {
Expand Down
89 changes: 0 additions & 89 deletions snake/src/test/java/ru/job4j/snake/Logic3TTest.java

This file was deleted.

0 comments on commit 54b0d02

Please sign in to comment.