控制檯版貪吃蛇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();
}
}
}