黑马程序员技术交流社区
标题:
DOS模式下的贪吃蛇
[打印本页]
作者:
yu244934256
时间:
2016-9-21 01:39
标题:
DOS模式下的贪吃蛇
public class GluttonousSnake {
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[100];
int arr2[] = new int[100];
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;
}
}
}
}
作者:
l123456
时间:
2016-9-21 09:34
可以没毛病、、。。。。。
作者:
litianji2016
时间:
2016-9-21 10:59
佩服的五体投地
作者:
bopeia
时间:
2016-9-21 12:11
玩玩看。
作者:
松松2016
时间:
2016-9-21 12:20
棒棒哒我也要学会
作者:
Young66329
时间:
2016-9-21 12:37
6666666666
作者:
lixuelin
时间:
2016-9-21 15:03
学来学期待学完基础班的我是个啥样子了
作者:
dmyz3214382
时间:
2016-9-21 15:13
听不错的!!!!!!
作者:
灵动
时间:
2016-9-21 21:56
666666;樓主+1
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2