控制台版贪吃蛇java

package linked_list;


import java.util.Scanner;
/**测试类
 *1,创建 蛇的面板类,
 *   创建蛇类
 *2, 将蛇打印到控制台(使用默认位置)
 *3, 蛇移动
 *     通过用户操作实现蛇的移动
 *     u: 表示向上  d:表示向下  l,表示向左,r,表示向右
 *
 */
public class WormTest {
public static void main(String[] args) {
//1.创建一个蛇的面板
WormPanel panel = new WormPanel();
//2.获取一个 蛇 对象
Worm worm = panel.getWorm();
//打印构建的这条蛇的所有节点的坐标
System.out.println("worm: "+worm);
/*for(int i = 1; i <= 15;i++){
panel.print();
worm.step();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}*/
//让用户通过键盘输入控制蛇移动的方向
// u上  d下  l左  r右
Scanner  s= new Scanner(System.in);
while(true){
panel.print();
String dir = s.nextLine();
if(dir.equalsIgnoreCase("w")){
worm.step(Worm.UP);


}else if(dir.equalsIgnoreCase("s")){
worm.step(Worm.DOWN);

}else if(dir.equalsIgnoreCase("a")){
worm.step(Worm.LEFT);

}else if(dir.equalsIgnoreCase("d")){
worm.step(Worm.RIGHT);

}else if(dir.equalsIgnoreCase("q")){
System.out.println("bye-bye ^-^!");
break;
}else{
worm.step();
}
}

}
}

package linked_list;
/**
 * 节点类:Node
 * 通过Node,构建多个 节点,然后将
 * 多个节点构建为一个 蛇 
 *
 */
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;
}
public String toString(){
return "["+i+","+j+"]";
}
}

package linked_list;


import java.util.LinkedList;
/**
 * 蛇类:Worm
 * 1,声明属性常量,用来表示蛇移动的方向
 * 2,创建 LinkedList对象,
 * 3,通过创建多个 Node 节点,组成一条蛇
 * 4,通过 LinkedList提供的首尾方法对
 *    蛇的移动进行简单设置
 *
 */
public class Worm {
private LinkedList<Node> nodes = new LinkedList<Node>();
public static final int UP = - 10;
public static final int DOWN = 10;
public static final int LEFT = -1;
public static final int RIGHT = 1;
int dir ;//当前方向
//创建多个Node节点,构建一个蛇
public Worm(){
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));
dir = RIGHT;//默认当前方向往右
}
/*蛇移动一步*/
public void step(){
/*去尾巴加头,加头加在什么位置
* 现在的头的位置i,j
* 往右 i不变  j+1
* 往左 i不遍 j-1
* 往上 i-1  j不遍
* 往下 i+1  j不遍
*/
Node head = nodes.getFirst();
int i = head.getI();
int j = head.getJ();
i = i + dir / 10;
j = j + dir % 10;
nodes.addFirst(new Node(i,j));
nodes.removeLast();
}
public void step(int dir){
//控制蛇移动的范围只用是在当前蛇面板中
if(this.dir+dir==0)
throw new RuntimeException("不能掉头走!");
this.dir = dir;
step();
}
public boolean contains(int i,int j){
return nodes.contains(new Node(i,j));
}
public String toString(){
return nodes.toString();
}
}

package linked_list;
/**
 * 蛇面板类:WormPanel
 * 1,构建蛇所在的虚拟面板,
 * 2,编写相关方法,用于
 *    控制台输出蛇所在的面板及蛇对象
 */
public class WormPanel {
private Worm worm;
int rows = 10;
int cols = 32;
public WormPanel(){
worm = new Worm();
}
public Worm getWorm(){
return worm;
}
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(worm.contains(i, j)){
System.out.print("#");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值