import java.util.Scanner;
import java.util.Random;
class 贪吃蛇{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int map[][]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int arr1[]=new int [1000];
int arr2[]=new int [1000];
int l=3;
int x=4,y=2;
for (int i=0;i<l ;i++ ) {
arr1[i]=i+x;
arr2[i]=y;
map[arr1[i]][arr2[i]]=100;
}
map[arr1[0]][arr2[0]]=3;
showMap(map);
while (true) {
System.out.println("w,a,s,d,f");
String a=sc.next();
if (a.equals("w")) {
if (map[arr1[0]-1][arr2[0]]==1) {
map[arr1[l-1]][arr2[l-1]]=1;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr1[0]-=1;
}else if (map[arr1[0]-1][arr2[0]]==2) {
l++;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr1[0]-=1;
point(map);
}else {
break;
}
}else if (a.equals("s")) {
if (map[arr1[0]+1][arr2[0]]==1) {
map[arr1[l-1]][arr2[l-1]]=1;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr1[0]+=1;
}else if (map[arr1[0]+1][arr2[0]]==2) {
l++;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr1[0]+=1;
point(map);
}else {
break;
}
}else if (a.equals("a")) {
if (map[arr1[0]][arr2[0]-1]==1) {
map[arr1[l-1]][arr2[l-1]]=1;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr2[0]-=1;
}else if (map[arr1[0]][arr2[0]-1]==2) {
l++;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr2[0]-=1;
point(map);
}else {
break;
}
}else if (a.equals("d")) {
if (map[arr1[0]][arr2[0]+1]==1) {
map[arr1[l-1]][arr2[l-1]]=1;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr2[0]+=1;
}else if (map[arr1[0]][arr2[0]+1]==2) {
l++;
for (int i=l-1;i>0 ;i-- ) {
arr1[i]=arr1[i-1];
arr2[i]=arr2[i-1];
}
arr2[0]+=1;
point(map);
}else {
break;
}
}
for (int i=1;i<l-1 ;i++ ) {
map[arr1[i]][arr2[i]]=100;
}
map[arr1[0]][arr2[0]]=3;
showMap(map);
}
}
public static void showMap(int [][]map){
for (int i=0;i<map.length ;i++ ) {
for (int j=0;j<map[i].length ;j++ ) {
if (map[i][j]==0) {
System.out.print("■");
}else if (map[i][j]==1) {
System.out.print(" ");
}else if (map[i][j]==100) {
System.out.print("○");
}else if (map[i][j]==2) {
System.out.print("☆");
}else if (map[i][j]==3) {
System.out.print("●");
}
}
System.out.println("");
}
}
public static void point(int map[][]){
Random sj=new Random();
while (true) {
int a=sj.nextInt(10)+1;
int b=sj.nextInt(20)+1;
if (map[a][b]==1) {
map[a][b]=2;
break;
}
}
}
} |
|