未完成的贪吃蛇
个人第一次写博客有点紧张,很久以前就想写博客,今天才写,感觉有点晚,但是还是鼓起勇气写起来吧。万事开头难,写了一次之后,就不会那么紧张了。
废话不多说,进入今天要写的东西吧。
今天我上课,老师说要我们写一个贪吃蛇,所以各位教室里的同学纷纷动起了手脚来,其实我心里是慌慌的,下面是题目:
![这里写图片描述](https://img-blog.csdn.net/20160713191754219)
其实思路在那里,所以动起来,都是自己写的,所以我没用上那个wrom里面的方法,我就空实现了,网上是有实现了的,当然网上的好,下面上自己的思路吧
1.先找出类
———-面板类 WormPanel.class 蛇类 Worm.class
———-坐标类 Node.class 蛋类 Egg.class
package com.zz;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
/**
* 1.创建WormPanel类
* 2.创建棋盘
* 3.初始化蛇的位置
* 4.增加蛋
*/
public class WormPanel {
//布置面板 原来自己用布尔值的,后面发现不能有位置存蛋(只有两种情况,所以无法判断)
//存 字符 'z'表示可以走动 'y' 表示是蛇占用了
private char[][] panelArr = new char[10][32];
LinkedList<Node> wormCoor;
Worm worm ;
HashSet<Egg> eggSet;
WormPanel(){
//画板初始化
for (int i = 0; i < panelArr.length; i++) {
for (int j = 0; j < panelArr[i].length; j++) {
panelArr[i][j]='z';
}
}
//当创建面板的时候 把蛇放上去
worm = new Worm();
//同时放蛋
eggSet = new HashSet<>();
int i=(int)(Math.random()*2)+2;
while(eggSet.size()<i){
if(produceEgg()!=null){
eggSet.add(new Egg(produceEgg()));
}
}
}
//产蛋
public Node produceEgg(){
int a = (int)(Math.random()*8)+1;
int b = (int)(Math.random()*30)+1;
if(panelArr[a][b]=='z'){
panelArr[a][b]='0';
Node node = new Node(a,b);
return node;
}
return null;
}
/*
* 1.蛇内部类,
* 得到蛇肯定要初始化蛇,蛇就在那个位置上
*/
class Worm{
Worm(){
Node node1 =new Node(3,9);
Node node2 =new Node(4,9);
Node node3 =new Node(5,9);
Node node4 =new Node(5,10);
Node node5 =new Node(5,11);
Node node6 =new Node(6,11);
Node node7 =new Node(7,11);
wormCoor = new LinkedList<>();
wormCoor.add(node1);
wormCoor.add(node2);
wormCoor.add(node3);
wormCoor.add(node4);
wormCoor.add(node5);
wormCoor.add(node6);
wormCoor.add(node7);
for (Node n : wormCoor) {
int i = n.i;
int j = n.j;
panelArr[i][j] ='y';
}
}
}
//蛇吃蛋
public boolean isEggSnake(int i,int j){
// if(panelArr[i][j]=='0'){
// panelArr[i][j]='y';
// return true;
// }
// return false;
Iterator<Egg> iter =eggSet.iterator();
while(iter.hasNext()){
Egg e = iter.next();
if(e.node.i==i&&e.node.j==j){
panelArr[i][j]='y';
return true;
}
}
return false;
}
//蛇移动
public void mobileSnake(char dir){
//方向上
if(dir=='w'||dir=='W'){
//得到蛇的头坐标
Node node1 = wormCoor.get(0);
int i = node1.i;
int j = node1.j;
panelArr[i-1][j]='y';
wormCoor.addFirst(new Node(i-1,j));
Node node2 = wormCoor.removeLast();
int i2 =node2.i;
int j2 =node2.j;
//判断头的移动,是否是吃到蛋
if(isEggSnake(i-1,j)){
panelArr[i2][j2]='y';
wormCoor.addLast(node2);
}else{
panelArr[i2][j2]='z';
}
}
//方向下
if(dir=='s'||dir=='S'){
//得到蛇的头坐标
Node node1 = wormCoor.get(0);
int i = node1.i;
int j = node1.j;
wormCoor.addFirst(new Node(i+1,j));
panelArr[i+1][j]='y';
Node node2 = wormCoor.removeLast();
int i2 =node2.i;
int j2 =node2.j;
//判断头的移动,是否是吃到蛋
if(isEggSnake(i+1,j)){
panelArr[i2][j2]='y';
wormCoor.addLast(node2);
}else{
panelArr[i2][j2]='z';
}
}
//方向左
if(dir=='a'||dir=='a'){
//得到蛇的头坐标
Node node1 = wormCoor.get(0);
int i = node1.i;
int j = node1.j;
wormCoor.addFirst(new Node(i,j-1));
panelArr[i][j-1]='y';
Node node2 = wormCoor.removeLast();
int i2 =node2.i;
int j2 =node2.j;
//判断头的移动,是否是吃到蛋
if(isEggSnake(i,j-1)){
panelArr[i2][j2]='y';
wormCoor.addLast(node2);
}else{
panelArr[i2][j2]='z';
}
}
//方向右
if(dir=='d'||dir=='D'){
//得到蛇的头坐标
Node node1 = wormCoor.get(0);
int i = node1.i;
int j = node1.j;
wormCoor.addFirst(new Node(i,j+1));
panelArr[i][j+1]='y';
Node node2 = wormCoor.removeLast();
int i2 =node2.i;
int j2 =node2.j;
//判断头的移动,是否是吃到蛋
if(isEggSnake(i,j+1)){
panelArr[i2][j2]='y';
wormCoor.addLast(node2);
}else{
panelArr[i2][j2]='z';
}
}
}
public static void main(String[] args) {
WormPanel wp = new WormPanel();
//panelPrint 得到每次蛇移动的数组
char[][] panelPrint = wp.panelArr;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < panelPrint.length; i++) {
for (int j = 0; j < panelPrint[i].length; j++) {
if(i==0||i==panelPrint.length-1){
System.out.print("-");
}else if(j==0||j==panelPrint[i].length-1){
System.out.print("|");
}else if(panelPrint[i][j]=='0'){
System.out.print('0');
}else
if(panelPrint[i][j]=='y'){
System.out.print("#");
}else{
System.out.print(" ");
}
// System.out.print("("+i+","+j+")");
}
System.out.println();
}
while(true){
String str =sc.next();
char[] ch = str.toCharArray();
wp.mobileSnake(ch[0]);
for (int i = 0; i < panelPrint.length; i++) {
for (int j = 0; j < panelPrint[i].length; j++) {
if(i==0||i==panelPrint.length-1){
System.out.print("-");
}else if(j==0||j==panelPrint[i].length-1){
System.out.print("|");
}else if(panelPrint[i][j]=='0'){
System.out.print('0');
}else if(panelPrint[i][j]=='y'){
System.out.print("#");
}else{
System.out.print(" ");
}
// System.out.print("("+i+","+j+")");
}
System.out.println();
}
}
}
}