我正在打印数字1,2,3,4,5,6,7,8(10/10)的简单地图,每次我打左或右它
不说
如果我从地图上掉下来,但他问我下一个YX是大于9(从底部和右侧碰撞)还是小于0(从顶部和右侧碰撞),但他问我下一个单元格是什么(是1,2,3,4,5,6,7,8)
我该怎么检查他是否从地图上掉下来?
救命啊!!!!!!!
import java.util.Scanner;
public class Game {
static String dir = "";
static int player;
static int chest;
static int monster;
static int key;
static int tile;
static int wall;
static int keysFound = 0;
static int chestsOpened = 0;
static int chestsLeft = 3;
static int sword = 0;
static int angel;
static int pickaxe = 0;
static Scanner scan = new Scanner(System.in);
static int[][] map = new int[10][10];
static int x = map[0][0];
static int y = map[0][0];
static int playerPos = map[x][y];
static int life = 3;
static boolean isInGame;
static String NoTileFound = "trying to knock this wall is pointless try some other move";
static String FreeTile = "you move to a free tile";
public static void main(String[] args) {
MapCreator mapcreate = new MapCreator();
map = mapcreate.getMapcreated();
int isPlaying;
System.out.println(" Welcome to the game, kill monsters and open three chests to win");
System.out.println(" Gather a key to open the chest, collect 3 of theese to win! that's it!");
System.out.println(" Press 1 to play..anything else to exit!");
isPlaying = scan.nextInt();
if (isPlaying == 1) {
isInGame = true;
enterGame();
} else {
System.out.println("You missed out on a good game...");
System.exit(1);
}
}
public static void enterGame() {
System.out.println(
"Type, up, down, left, or right, to move. equipment to see what you have and exit to exit game");
while (isInGame) {
move();
}
}
public static void move() {
dir = scan.nextLine();
switch (dir) {
case "up":
entertileup();
break;
case "down":
entertiledown();
break;
case "left":
entertileright();
break;
case "right":
entertiledown();
break;
case "equipement":
System.out.println("you have " + life + " lifes, " + key + " keys and " + chestsOpened
+ " chests opened, keep searching solider!");
move();
break;
case "exit":
System.out.println("You missed out on a good game...");
System.exit(1);
}
ShowMap mapprint = new ShowMap();
mapprint.print(map);
}
public static void map() {
wall = 1;
tile = 2;
monster = 3;
chest = 4;
key = 5;
sword = 6;
player = 0;
angel = 7;
pickaxe = 8;
if (playerPos == tile) {
System.out.println(FreeTile);
}
if (playerPos == monster) {
monsterEncounter();
}
if (playerPos == chest) {
openChest();
}
if (playerPos == key) {
findKey();
}
if (playerPos == sword) {
findsword();
if (playerPos == angel) {
findangel();
}
if (playerPos == pickaxe) {
findpickaxe();
}
}
}
public static void entertileup() {
if (x + 1 > 9) {
System.out.println(NoTileFound);
move();
} else if (map[x + 1][y] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
x = x + 1;
playerPos = map[x][y];
map();
}
} else if (map[x + 1][y] == 2)
x = x + 1;
playerPos = map[x][y];
map();
}
public static void entertiledown() {
if (x - 1 < 0) {
System.out.println(NoTileFound);
move();
} else if (map[x - +1][y] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
x = x - 1;
playerPos = map[x][y];
map();
}
} else if (map[x - 1][y] == 2)
x = x - 1;
playerPos = map[x][y];
map();
}
public static void entertileright() {
if (y - 1 < 0) {
System.out.println(NoTileFound);
move();
} else if (map[x][y - 1] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
y = y - 1;
playerPos = map[x][y];
map();
}
} else if (map[x][y - 1] == 2)
y = y - 1;
playerPos = map[x][y];
map();
}
public static void entertileleft() {
if (y + 1 > 9) {
System.out.println(NoTileFound);
move();
} else if (map[x][y + 1] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
y = y + 1;
playerPos = map[x][y];
map();
}
} else if (map[x][y + 1] == 2)
y = y + 1;
playerPos = map[x][y];
map();
}
public static void findsword() {
System.out.println("You found a sword, monsters can't kill you! ");
sword = sword + 1;
map[x][y] = 0;
}
public static void findangel() {
System.out.println("You have found angel! what a mighty creature! He gives you a life");
life = life + 1;
map[x][y] = 0;
}
public static void findpickaxe() {
System.out.println("You have found a pickaxe Niceeee! you can destroy walls now! just simply walk over it");
pickaxe = pickaxe + 1;
map[x][y] = 0;
}
public static void monsterEncounter() {
if (sword == 0) {
if (life > 0) {
System.out.println("WOAH! A MONSTER! You kill him but you lose a life!");
life = life - 1;
System.out.println("You have: " + life + " life(s) left! Don't die out there!");
map[x][y] = 0;
} else {
System.out.println("You have: " + life + " , You encountered too many monsters. GAME OVER.");
System.exit(1);
}
} else {
System.out.println(" You killed a monster with mighty sword, no life loss, nice! ");
map[x][y] = 0;
}
}
public static void findKey() {
System.out.println("You found a key, nice!");
keysFound += 1;
map[x][y] = 2;
}
public static void openChest() {
System.out.println("Chest, nice!!");
if (keysFound >= 1) {
chestsOpened += 1;
int totalChestsFound = chestsLeft - chestsOpened;
chestsLeft -= 1;
System.out.println("You have opened a chest! You have " + totalChestsFound + " chests to go!");
map[x][y] = 2;
keysFound -= 1;
}
if (chestsLeft == 0) {
System.out.println("YOU WON! GOOD GAME SIR, GOOD GAME.");
System.exit(1);
} else {
System.out.println("No keys, no chests. Keep searching soldier.");
}
}
}