java習題2 貪喫蛇

                                       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;
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章