public class Test {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
public class Node {
private int x ;
private int y ;
public Node(){}
public Node(int x , int y){
this.x = x ;
this.y = y ;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public int hashCode() {
/*final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;*/
return x;
}
@Override
public boolean equals(Object obj) {
/* if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;*/
if (null == obj) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Node) {
Node n = (Node) obj;
if (this.x == n.x && this.y == n.y) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "Node [x=" + x + ", y=" + y + "]";
}
}
import java.util.LinkedList;
public class Snake {
//存放节点的链表, 也就是蛇身
private LinkedList<Node> nodes = new LinkedList<Node>();
//申明一个foods(食物)对象
InitPanel food;
//定义方向
public static final int UP = -10;
public static final int DOWN = 10;
public static final int LEFT = -1;
public static final int RIGHT =1;
//定义蛇初始的方向
private int direction;
/**
*创建Snake 初始化时的对象
*/
public Snake(){
nodes.add(new Node(3,9));
nodes.add(new Node(4,9));
nodes.add(new Node(4,10));
nodes.add(new Node(5,10));
//蛇头的方向
this.direction = UP;
}
public Snake(LinkedList<Node> nodes){
//清空链表中的所有节点
this.nodes.clear();
//添加所有的节点
this.nodes.addAll(nodes);
}
/**
*移动
*/
private void step(){
//先获取头节点, 也就是蛇头
Node top = this.nodes.getFirst();
//移动后的坐标
int x =top.getX()+ this.direction/10;
int y =top.getY()+this.direction%10;
//新蛇头的坐标
Node t = new Node(x,y);
//添加蛇头
nodes.addFirst(t);
//删除蛇尾巴
if(food.getFood().contains(nodes.getFirst())){
food.Meet();
}else{
nodes.removeLast();
}
}
/**
*移动
*/
public void step(int direction,InitPanel sp){
if(this.direction + direction == 0){
throw new RuntimeException("方向走反");
}
this.direction = direction;
this.food = sp ;
step();
}
/**
*判断节点是否都在蛇身上
*/
public boolean containNode(Node node){
return nodes.contains(node);
}
/**
*得到所有的节点对象
*/
public LinkedList<Node> getNodes(){
return nodes;
}
}
import java.util.HashSet;
import java.util.Random;
public class InitPanel {
public int rows = 10;
public int cols = 30;
private Snake snake;
private HashSet<Node> foods = new HashSet<Node>();
Random random;
/**
*界面构造器
*/
public InitPanel(){
//创建出蛇对象
snake = new Snake();
//创建出食物对象
this.initFoods(5);
}
//返回蛇身
public Snake getSnake(){
return snake;
}
/**
* 实例化食物对象
*/
public void initFoods(int i){
random = new Random();
while(true){
//食物不能出现的位置,不能在边界上 ,也不能在边界外
int x = random.nextInt(rows-2)+1;
int y = random.nextInt(cols-2)+1;
Node food = new Node(x,y);
//食物还不能在蛇身上
if(snake.containNode(food)){
continue;
}
//食物与食物之间出现在界面上的时候还不能再同一个位置
if(foods.contains(food)){
continue;
}
//添加食物在食物链表中
foods.add(food);
//如果生成的食物数量达到i,则退出循环
if(foods.size()==i){
break;
}
}
}
//产生界面
public void print(){
for(int i = 0 ; i < rows ; i ++){
for(int j = 0 ; j < cols ; j ++){
if(i == 0 || i == rows - 1){
System.out.print("-");
}else if(j == 0 || j == cols - 1 ){
System.out.print("|");
}else if(snake.containNode(new Node(i,j))){
System.out.print("*");
}else if(foods.contains(new Node(i,j))){
System.out.print("o");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
//返回食物的所有节点
public HashSet<Node> getFood(){
return foods;
}
//当蛇吃到食物的时候,该食物就应该消失
public HashSet<Node> Meet(){
foods.remove(snake.getNodes().getFirst());
return foods;
}
}
import java.util.Scanner;
public class Game {
//游戏开始
public void start(){
InitPanel sp = new InitPanel();
Snake snake = sp.getSnake();
Scanner input = new Scanner(System.in);
while(true){
sp.print();
System.out.println("dir:");
String str =input.nextLine();
switch (str) {
case "w":
snake.step(Snake.UP,sp);
break;
case "s":
snake.step(Snake.DOWN,sp);
break;
case "a":
snake.step(Snake.LEFT,sp);
break;
case "d":
snake.step(Snake.RIGHT,sp);
break;
case "q":
System.out.println("结束了游戏 拜拜");
System.exit(0);
break;
default:
break;
}
}
}
}
贪吃蛇其实可以看成由一节节的节点组成的一个链表的集合