實現貪吃蛇遊戲的思路:
class Node(i,j) //表示座標
class Worm(List<Node> nodes) // 行數:10行//用LinkedList存儲蛇的座標點。當前進是用addFirst(Node node)方法添加一個一個座標點(可以通過getFirst()得到上一個座標-1得出);然後再刪除尾節點。如果碰到食物則不刪除尾節點。
// 列數:32列
step()//往前走一步
step(int dir)//根據方向往前走一步
class WormPanel //畫板
---HashSet存儲食物 //當隨機獲得食物座標點的位置時判斷它的座標點是否與蛇身的座標點重複(如果重複則繼續獲取隨機數,知道不重複位置)。
內部類--Worm(LinkedList)
i j
j UP -1 001234567890123456789012345678901 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--------------------------------
import java.util.Scanner;
class WormDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
WormPanel wormPanel = new WormPanel();
WormPanel.Worm worm =wormPanel.getWorm();
while(true)
{
wormPanel.print();
String dir = sc.nextLine();//u、d、l、r代表上下左右方向
if("u".equalsIgnoreCase(dir))
worm.step(WormPanel.Worm.UP);
else if("d".equalsIgnoreCase(dir))
worm.step(WormPanel.Worm.DOWN);
else if("l".equalsIgnoreCase(dir))
worm.step(WormPanel.Worm.LEFT);
else if("r".equalsIgnoreCase(dir))
worm.step(WormPanel.Worm.RIGHT);
else if("q".equalsIgnoreCase(dir))
{
System.out.println("bye ^_^");
break;
}
else
worm.step();
}
}
<span style="font-family: Arial, Helvetica, sans-serif;">}</span>
<pre name="code" class="java">package snakeGame;
import java.util.*;
class WormPanel
{
//蛇
private Worm worm;
//行數
private int rows = 10;
//列數
private int cols = 32;
//食物
private HashSet<Node> foods = new HashSet<Node>();
public WormPanel(){
worm = new Worm();
initFoods(6);
}
public Worm getWorm()
{
return this.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 if(foods.contains(new Node(i,j)))
System.out.print("0");
else
System.out.print(" ");
}
System.out.println();
}
}
//隨機生成食物
public void initFoods(int num)
{
Random random = new Random();
while(true)
{
int i = random.nextInt(rows-2)+1;
int j = random.nextInt(cols-2)+1;
//先判斷是否是構成蛇的座標
if(worm.contains(i,j))
{
continue;
}
foods.add(new Node(i,j));//不會添加重複座標
if(foods.size()==num)
break;
}
}
public class Worm
{
public static final int UP = -10;
public static final int DOWN = 10;
public static final int LEFT = -1;
public static final int RIGHT = 1;
private LinkedList<Node> nodes = new LinkedList<Node>();
private int direction;
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));
this.direction = RIGHT;
}
public void step()
{
//得到第一個#號對應的座標
Node head = nodes.getFirst();
//計算新的節點的座標
int i = head.getI()+direction/10;
int j = head.getJ()+direction%10;
head = new Node(i,j);
//在蛇的頭部加入一個節點
nodes.addFirst(head);
//判斷是否遇到一個食物
if(foods.remove(head))
{
return;
}
//刪除尾部節點
nodes.removeLast();
}
public void step(int direction)
{
if(this.direction+direction==0)
throw new RuntimeException("不允許掉頭");
this.direction = direction;
step();
}
//判斷是否包含某個座標的方法
public boolean contains(int i,int j)
{
return nodes.contains(new Node(i,j));
}
}
}
package snakeGame;
class Node
{
private int i;
private int j;
public Node(){}
public Node(int i,int j)
{
this.i = i;
this.j = j;
}
public void setI(int i)
{
this.i = i;
}
public void setJ(int j)
{
this.j = j;
}
public int getI()
{
return this.i;
}
public int getJ()
{
return this.j;
}
public String toString()
{
return i+","+j;
}
public int hashCode()
{
return (i<<16)|j;
}
public boolean equals(Object obj)
{
if(obj==null)
return false;
if(this==obj)
return true;
if(obj instanceof Node)
{
Node node = (Node)obj;
return this.i==node.i && this.j==node.j;
}
return false;
}
}
註釋:
public void step()
{
//得到第一個#號對應的座標
Node head = nodes.getFirst();
//計算新的節點的座標
int i = head.getI()+direction/10;
int j = head.getJ()+direction%10;
head = new Node(i,j);
//在蛇的頭部加入一個節點
nodes.addFirst(head);
//判斷是否遇到一個食物
if(foods.remove(head))
{
return;
}
//刪除尾部節點
nodes.removeLast();
}
//隨機生成食物
public void initFoods(int num)
{
Random random = new Random();
while(true)
{
int i = random.nextInt(rows-2)+1;
int j = random.nextInt(cols-2)+1;
//先判斷是否是構成蛇的座標
if(worm.contains(i,j))
{
continue;
}
foods.add(new Node(i,j));//不會添加重複座標
if(foods.size()==num)
break;
}
}
以上兩段代碼中的foods.remove()方法需要重寫hashCode()和equals()方法。比較的是他們的座標值。將他們相同的座標刪除。