package com.myproj.snake;

 public class Node {
private int i,j;
public Node(){}
public Node(int i, int j) {
super();
this.i = i;
this.j = j;
} public int getI() {
return i;
} public void setI(int i) {
this.i = i;
} public int getJ() {
return j;
} public void setJ(int j) {
this.j = j;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
result = prime * result + j;
return result;
}
@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 (i != other.i)
return false;
if (j != other.j)
return false;
return true;
} }

Node.java

 package com.myproj.snake;

 import java.util.LinkedList;

 public class Snake {
private LinkedList<Node> nodes=new LinkedList<Node>();
public static final int UP=-5;
public static final int DOWN=5;
public static final int LEFT=-1;
public static final int RIGHT=1;
private int dir;//当前方向;
public Snake(){
nodes.add(new Node(3,9));
nodes.add(new Node(4,9));
nodes.add(new Node(5,9));
nodes.add(new Node(5,10));
nodes.add(new Node(5,11));
nodes.add(new Node(6,11));
nodes.add(new Node(7,11));
this.dir=RIGHT;
}
public boolean contains(int i,int j){
return nodes.contains(new Node(i,j));
}
public void move(){
nodes.removeLast();
Node head=nodes.getFirst();
int i=head.getI()+dir/5;
int j=head.getJ()+dir%5;
nodes.addFirst(new Node(i,j));
}
public void move(int dir){
if(this.dir+dir==0){
System.out.println("不能逆向行驶!");
System.out.println("游戏终止!");
System.exit(0);
}
this.dir=dir;
move();
}
}

Snake.java

 package com.myproj.snake;

 public class SnakePan {
private Snake snake=new Snake();
public Snake getSnake() {
return snake;
}
public void setSnake(Snake snake) {
this.snake = snake;
}
public void paint(){
for(int i=0;i<=15;i++){
for(int j=0;j<=40;j++){
if(i==0||i==15){
System.out.print("-");
}else if(j==0||j==40){
System.out.print("|");
}else if(snake.contains(i,j)){
System.out.print("■");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

SnakePan

 package com.myproj.snake;

 import java.util.Scanner;

 public class SnakeTest {
public static void main(String[] args){
SnakePan snakePan=new SnakePan();
Snake snake=snakePan.getSnake();
snakePan.paint();
/*for(int i=0;i<10;i++){
snakePan.paint();
snake.move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/ Scanner scanner=new Scanner(System.in);
System.out.println("请输入方向【上(U)、下(D)、左(L)、右(R)】:");
while(true){
String dir=scanner.nextLine();
if(dir.equalsIgnoreCase("U")){
snake.move(snake.UP);
}else if(dir.equalsIgnoreCase("D")){
snake.move(snake.DOWN);
}else if(dir.equalsIgnoreCase("L")){
snake.move(snake.LEFT);
}else if(dir.equalsIgnoreCase("R")){
snake.move(snake.RIGHT);
}else{
System.exit(0);
}
snakePan.paint();
}
}
}

SnakeTest.java

最新文章

  1. OC 观察者模式(通知中心,KVO)
  2. VS2010 下 将 EntityFramework 的版本从 4.0 升级到 5.0
  3. fir.im Weekly - 17 个提升 iOS 开发效率的必备工具
  4. [2013 eoe移动开发者大会]靳岩:从码农到极客的升级之路
  5. hdu-acm steps Common Subsequence
  6. Educational Codeforces Round 13 D:Iterated Linear Function(数论)
  7. matio使用
  8. poj3265
  9. Android with Eclipse - Waiting for HOME (&#39;android.process.acore&#39;) to be launched?
  10. 如何调教Android Studio-Windows安装AS后的必备工作
  11. 2. SpringMVC 上传文件操作
  12. GC垃圾回收算法
  13. scrapy 爬虫返回json格式内容unicode编码转换为中文的问题解决
  14. 回顾django内容
  15. Linux第五章笔记
  16. Bugzilla使用规范
  17. Spring-boot+Mybatis+Maven+MySql搭建实例
  18. C#高级编程9-第2章 核心C#
  19. iOS-代码修改Info.plist文件
  20. Android判断Service是否运行

热门文章

  1. VueJS使用笔记
  2. 【技术翻译】支持向量机简明教程及其在python和R下的调参
  3. 为什么ERP行业发展缓慢规模难扩大?
  4. Git详解及 github与gitlab使用
  5. Servlet与Jsp的结合使用实现信息管理系统二
  6. 分享一小坑(与swagger有关),以后碰到了可以快速规避
  7. Mysql 的 IF 判断
  8. tensorflow 安装升级
  9. .Net Core实现将文件上传到七牛云存储
  10. SpringCloud Feign使用详解