i j
j UP -1 0
01234567890123456789012345678901 DOWN 1 0
i 0-------------------------------- LEFT 0-1
1| O | RIGHT 0 1
2| ^ | 2,9
3| <-#-> |3,8 3,9 3,10
4| # | 4,9
5| ### | 5,9 5,10 5,11
6| # O O | 6,11
7| # | 7,11
8| | nodes.remove(nodes.size()-1)
9--------------------------------
class Node(i,j) -----坐标类
class Worm—蛇类(面板的内部类)(List nodes) 食物用 Set集合存储
step() 蛇的坐标用LinkedList存储
step(int dir)
class WormPanel----面板
代码
public class Node {
private int i;
private int 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 String toString() {
return “[”+i+","+j+"]";
}
@Override
public boolean equals(Object obj) { //去重 没有其他作用
if(obj==null)
return false;
if(this == obj)
return true;
if(obj instanceof Node) {
Node o = (Node) obj;
return this.i == o.i && this.j == o.j;
}
return false;
}
@Override
public int hashCode() { //不太理解
return (i<<16)|j; //类似于name 和 age 为了区分
}
}
public class WormDemo {
public static void main(String[] args) {
final WormPane pane = new WormPane();
final WormPane.Worm worm = pane.getWorm();/.getWorm();/
Scanner s = new Scanner(System.in);
while(true){
pane.print();
System.out.println(worm);
String dir = s.nextLine();
if(dir.equalsIgnoreCase("u")){
worm.step(WormPane.Worm.UP);
}else if(dir.equalsIgnoreCase("d")){
worm.step(WormPane.Worm.DOWN);
}else if(dir.equalsIgnoreCase("l")){
worm.step(WormPane.Worm.LEFT);
}else if(dir.equalsIgnoreCase("r")){
worm.step(WormPane.Worm.RIGHT);
}else if(dir.equalsIgnoreCase("q")){
System.out.println("Bye ^_-");
break;
}else{
worm.step();
}
}
}
}
public class WormPane {
private Worm worm;
/** 行数 /
private int rows = 10;
/* 列数 */
private int cols = 32;
/** 食物 */
private Set foods = new HashSet();
public WormPane() {
worm = new Worm();
initFoods(5);
}
public void initFoods(int n){
Random r = new Random();
while(true){
int i = r.nextInt(rows-2)+1;
int j = r.nextInt(cols-2)+1;
if(worm.contains(i, j)){ //
continue;
}
Node food = new Node(i,j);
//if(foods.contains(food)){
// continue;
//}
foods.add(food);//不会重复添加
if(foods.size()==n){
break;
}
}
}
public Worm getWorm() {
return worm;
}
/** 画出当前面板 */
public void print(){
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(i0||irows-1){
System.out.print("-");//不能输出回车
}else if(j0||jcols-1){
System.out.print("|");
}else if(worm.contains(i, j)){
System.out.print("#");
}else if(foods.contains(new Node(i, j))){
System.out.print(“0”);
}else{
System.out.print(" ");
}
}
System.out.println(); //一行结束以后画回车
}
}
//蛇类
public class Worm {
// 约束了集合中元素的类型, nodes中只能放置Node实例
private LinkedList nodes =
new LinkedList();
//当前默认的行走方向
private int dir;
public static final int UP = -10;
public static final int DOWN = 10;
public static final int LEFT = -1;
public static final int RIGHT = 1;
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 Worm(LinkedList<Node> nodes) {
this.nodes.clear(); //为什么先清除
this.nodes.addAll(nodes);//复制了集合的内容
//this.nodes=nodes;//使用同一个集合对象
}
/** 走一步 */
public void step(){
//找到头节点
Node head = nodes.getFirst();//相当于:get(0)
//根据当前方向计算新节点
int i = head.getI() + dir/10;
int j = head.getJ() + dir%10;//能不能/1
head = new Node(i,j);
//插入新节点到头部
nodes.addFirst(head);//相当于:add(0, head)
if(foods.remove(head)){
//如果删除成功返回true,表示吃掉了一个食物
return;
}
//删除末尾节点
nodes.removeLast();//相当于:remove(nodes.size()-1)
}
/** 换个方向走一步 */
public void step(int dir){
if(this.dir+dir==0){
throw new RuntimeException("不能掉头行驶!");
}
this.dir = dir;
step();
}
public boolean contains(int i, int j) {
// for(int k=0; k<nodes.size(); k++){
// Node n = nodes.get(i);
// if(n.getI()==i && n.getJ()==j){
// return true;
// }
// }
// return false;
return nodes.contains(new Node(i,j));
}
@Override
public String toString() {
return nodes.toString();
}
}
}
public class Test {
public static void main(String[] args) {
Person p=new Person(“张三”,4);
System.out.println(p);
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “name=”+name+“age=”+age;
}
}