给出一个java版本贪吃蛇,重构前和重构后的代码,要求代码大于300行
时间: 2024-03-12 07:49:41 浏览: 80
java贪食蛇源代码(300多行)
5星 · 资源好评率100%
好的,下面给出一个Java版本的贪吃蛇游戏的重构前和重构后的代码。这里使用了工厂模式和观察者模式来进行重构,以提高代码的可维护性和可扩展性。
重构前:
```
// SnakeGame.java
public class SnakeGame {
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private int score = 0;
private boolean gameOver = false;
private Snake snake;
private Apple apple;
public SnakeGame() {
snake = new Snake();
apple = new Apple();
}
public void run() {
while (!gameOver) {
moveSnake();
checkCollision();
generateApple();
}
System.out.println("Game Over! Your score is " + score);
}
private void moveSnake() {
snake.move();
}
private void checkCollision() {
if (snake.collidesWith(apple)) {
snake.grow();
score++;
} else if (snake.collidesWithSelf() || snake.hitsWall(WIDTH, HEIGHT)) {
gameOver = true;
}
}
private void generateApple() {
if (apple.isEaten()) {
apple = new Apple();
}
}
}
// Snake.java
public class Snake {
private List<Point> body;
private Point direction;
public Snake() {
body = new ArrayList<>();
body.add(new Point(10, 10));
direction = new Point(1, 0);
}
public void move() {
Point head = body.get(0);
Point newHead = new Point(head.x + direction.x, head.y + direction.y);
body.add(0, newHead);
body.remove(body.size() - 1);
}
public boolean collidesWith(Point point) {
return body.contains(point);
}
public boolean collidesWithSelf() {
Point head = body.get(0);
for (int i = 1; i < body.size(); i++) {
if (head.equals(body.get(i))) {
return true;
}
}
return false;
}
public boolean hitsWall(int width, int height) {
Point head = body.get(0);
return head.x < 0 || head.x >= width || head.y < 0 || head.y >= height;
}
public void grow() {
Point head = body.get(0);
Point newHead = new Point(head.x + direction.x, head.y + direction.y);
body.add(0, newHead);
}
}
// Apple.java
public class Apple {
private Point position;
private boolean isEaten;
public Apple() {
position = new Point((int) (Math.random() * 20), (int) (Math.random() * 20));
isEaten = false;
}
public boolean isEaten() {
return isEaten;
}
public void setEaten(boolean eaten) {
isEaten = eaten;
}
public Point getPosition() {
return position;
}
public void setPosition(Point position) {
this.position = position;
}
}
```
重构后:
```
// SnakeGame.java
public class SnakeGame {
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private int score = 0;
private boolean gameOver = false;
private Snake snake;
private Apple apple;
public SnakeGame() {
SnakeFactory snakeFactory = new SnakeFactory();
snake = snakeFactory.createSnake();
AppleFactory appleFactory = new AppleFactory();
apple = appleFactory.createApple();
snake.addObserver(apple);
}
public void run() {
while (!gameOver) {
moveSnake();
checkCollision();
generateApple();
}
System.out.println("Game Over! Your score is " + score);
}
private void moveSnake() {
snake.move();
}
private void checkCollision() {
if (snake.collidesWith(apple)) {
snake.grow();
score++;
apple.setEaten(true);
} else if (snake.collidesWithSelf() || snake.hitsWall(WIDTH, HEIGHT)) {
gameOver = true;
}
}
private void generateApple() {
if (apple.isEaten()) {
AppleFactory appleFactory = new AppleFactory();
apple = appleFactory.createApple();
snake.addObserver(apple);
}
}
}
// Snake.java
public class Snake extends Observable {
private List<Point> body;
private Point direction;
public Snake() {
body = new ArrayList<>();
body.add(new Point(10, 10));
direction = new Point(1, 0);
}
public void move() {
Point head = body.get(0);
Point newHead = new Point(head.x + direction.x, head.y + direction.y);
body.add(0, newHead);
body.remove(body.size() - 1);
setChanged();
notifyObservers();
}
public boolean collidesWith(Point point) {
return body.contains(point);
}
public boolean collidesWithSelf() {
Point head = body.get(0);
for (int i = 1; i < body.size(); i++) {
if (head.equals(body.get(i))) {
return true;
}
}
return false;
}
public boolean hitsWall(int width, int height) {
Point head = body.get(0);
return head.x < 0 || head.x >= width || head.y < 0 || head.y >= height;
}
public void grow() {
Point head = body.get(0);
Point newHead = new Point(head.x + direction.x, head.y + direction.y);
body.add(0, newHead);
setChanged();
notifyObservers();
}
}
// Apple.java
public class Apple implements Observer {
private Point position;
private boolean isEaten;
public Apple() {
position = new Point((int) (Math.random() * 20), (int) (Math.random() * 20));
isEaten = false;
}
public boolean isEaten() {
return isEaten;
}
public void setEaten(boolean eaten) {
isEaten = eaten;
}
public Point getPosition() {
return position;
}
public void setPosition(Point position) {
this.position = position;
}
@Override
public void update(Observable o, Object arg) {
Snake snake = (Snake) o;
setPosition(getRandomPosition());
setEaten(false);
}
private Point getRandomPosition() {
return new Point((int) (Math.random() * 20), (int) (Math.random() * 20));
}
}
// SnakeFactory.java
public class SnakeFactory {
public Snake createSnake() {
return new Snake();
}
}
// AppleFactory.java
public class AppleFactory {
public Apple createApple() {
return new Apple();
}
}
```
重构后的代码将蛇和苹果分别封装成了类,使用工厂模式进行了创建,并使用观察者模式将蛇和苹果对象解耦,以便于扩展和维护。同时,新增了一些方法,如Snake类的Observable接口的方法和Apple类的update方法,来实现观察者模式。经过重构后,代码变得更加清晰、可维护、可扩展。
阅读全文