java 推箱子 兩個箱子 兩個位置 ,練習java二維陣列
import java.util.Scanner;
public class Homework_TuiXiangZi1 {
@SuppressWarnings("resource")
public static void main(String[] args) {
/*
* 首先, 我們創建出來小人可以移動的地圖--->這個地圖是一個二維陣列
**/
int[][] map = new int[5][5];
/*
* 接下來,我希望從輸入確定我的小人的初始位置
*/
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入小人初始位置的行座標從 0 到 " + (map.length - 1));
int m = scanner.nextInt(); // 小人的行座標
if (m > map.length - 1 || m < 0) {
System.out.println("您輸入的行數有誤,小人座標脫離了地圖!!");
return;
}
System.out.println("請輸入小人初始位置的列座標從 0 到 " + (map[m].length - 1));
int n = scanner.nextInt(); // 小人的列座標!!
if (n > map[m].length - 1 || n < 0) {
System.out.println("您輸入的列數有誤,小人座標脫離了地圖");
return;
}
map[m][n] = 1; // 給小人賦值
/*
* 關於箱子1的邏輯判斷
*/
System.out.println("請輸入箱子1的行座標,從0 到" + (map.length - 1));
int boxM = scanner.nextInt();
if (boxM >= map.length || boxM < 0) {
System.out.println("箱子1已經脫離地圖");
return;
}
System.out.println("請輸入箱子1的列座標,從0 到 " + (map[boxM].length - 1));
int boxN = scanner.nextInt();
if (boxN >= map[boxM].length || boxN < 0) {
System.out.println("箱子1已經脫離地圖了");
return;
}
if (boxM == m && boxN == n) {
System.out.println("此處小人已經佔了,請另尋他處!");
return;
}
map[boxM][boxN] = 2;// 給箱子所在的位置賦值
/*
* 關於箱子二的邏輯判斷
*/
System.out.println("請輸入箱子2的行座標,從0 到" + (map.length - 1));
int boxM1 = scanner.nextInt();
if (boxM1 >= map.length || boxM1 < 0) {
System.out.println("箱子1已經脫離地圖");
return;
}
System.out.println("請輸入箱子1的列座標,從0 到 " + (map[boxM].length - 1));
int boxN1 = scanner.nextInt();
if (boxN1 >= map[boxM1].length || boxN1 < 0) {
System.out.println("箱子1已經脫離地圖了");
return;
}
if (boxM1 == m && boxN1 == n) {
System.out.println("此處小人已經佔了,請另尋他處!");
return;
} else if (boxM1 == boxM && boxN1 == boxN) {
System.out.println("此處箱子已經佔了!");
}
map[boxM1][boxN1] = 2;// 給箱子所在的位置賦值
/*
* 關於箱子目的地1的邏輯判斷
*
*/
System.out.println("請輸入箱子空的行座標,從0 到" + (map.length - 1));
int boxK = scanner.nextInt();
if (boxK >= map.length || boxK < 0) {
System.out.println("箱子空 已經脫離地圖了");
return;
}
System.out.println("請輸入箱子空的列座標,從0 到" + (map[boxK].length - 1));
int boxL = scanner.nextInt();
if (boxL >= map[boxK].length || boxL < 0) {
System.out.println("箱子空 已經脫離地圖了");
}
map[boxK][boxL] = 3;// 給箱子空(相當於給箱子所要推送的位置)賦值
if (boxK == m && boxL == n) {
map[m][n] = 4;
} else if ((boxK == boxM && boxL == boxM) || (boxK == boxM1 && boxL == boxN1)) {
map[boxK][boxK] = 5;
}
/*
* 關於箱子目的地2的邏輯判斷
*
*/
System.out.println("請輸入箱子空的行座標,從0 到" + (map.length - 1));
int boxK1 = scanner.nextInt();
if (boxK1 >= map.length || boxK1 < 0) {
System.out.println("箱子空 已經脫離地圖了");
return;
}
System.out.println("請輸入箱子空的列座標,從0 到" + (map[boxK1].length - 1));
int boxL1 = scanner.nextInt();
if (boxL1 >= map[boxK1].length || boxL1 < 0) {
System.out.println("箱子空 已經脫離地圖了");
}
map[boxK1][boxL1] = 3;// 給箱子空(相當於給箱子所要推送的位置)賦值
if (boxK1 == m && boxL1 == n) {
map[m][n] = 4;
} else if ((boxK1 == boxM && boxL1 == boxM) || (boxK1 == boxM1 && boxL1 == boxN1)) {
map[boxK1][boxL1] = 5;
}
/*-----------------建立地圖並賦值結束--------------------*/
/*
* 檢查地圖是否有誤
*/
if (map[boxK][boxL] == 5 && map[boxK1][boxL1] == 5) {
System.out.println("您設定的地圖有誤請重新設定!!!");
return;
}
/*
* 我們檢查一下目前的地圖和小人
*/
System.out.println("小地圖目前狀態如下:");
System.out.println("-------------------------------");
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + "\t");
}
System.out.println();
}
/*-------------------檢查地圖結束----------------------*/
/*
* 小人行走的邏輯
*/
while (true) {
System.out.println("請輸入小人行走的方向: W 上 , A:左 , S: 下 , D:右");
String commend = scanner.next();
commend.toUpperCase();
switch (commend) {
case "W":
// 這裡讓小人向上移動
if (m == 0) {
System.out.println("小人已經到最上面了不能再走了!");
} else {
if (map[m][n] == 4) {
if (map[m - 1][n] == 2) {
if (m - 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m - 2][n] == 2) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m - 2][n] == 5) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m - 2][n] == 3) {
map[m - 2][n] = 5;
map[m][n] = 3;
m = m - 1;
map[m][n] = 1;
} else {
map[m - 2][n] = 2;
map[m][n] = 3;
m = m - 1;
map[m][n] = 1;
}
}
} else if (map[m - 1][n] == 5) {
if (m - 1 == 0) {
System.out.println("已經到最上面了!");
} else {
map[m - 1][n] = 3;
if (map[m - 2][n] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m - 2][n] == 3) {
map[m - 2][n] = 5;
map[m][n] = 3;
m = m - 1;
map[m][n] = 4;
} else {
map[m - 2][n] = 2;
map[m][n] = 3;
m = m - 1;
map[m][n] = 4;
}
}
} else if (map[m - 1][n] == 3) {
map[m][n] = 3;
m = m - 1;
map[m][n] = 4;
} else {
map[m][n] = 3;
m = m - 1;
map[m][n] = 1;
}
} else {
if (map[m - 1][n] == 2) {
if (m - 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m - 2][n] == 2) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m - 2][n] == 5) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m - 2][n] == 3) {
map[m - 2][n] = 5;
map[m][n] = 0;
m = m - 1;
map[m][n] = 1;
} else {
map[m - 2][n] = 2;
map[m][n] = 0;
m = m - 1;
map[m][n] = 1;
}
}
} else if (map[m - 1][n] == 5) {
if (m - 1 == 0) {
System.out.println("已經到最上面了!");
} else {
if (map[m - 2][n] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m - 2][n] == 3) {
map[m - 2][n] = 5;
map[m][n] = 0;
m = m - 1;
map[m][n] = 4;
} else {
map[m - 2][n] = 2;
map[m][n] = 0;
m = m - 1;
map[m][n] = 4;
}
}
} else if (map[m - 1][n] == 3) {
map[m][n] = 0;
m = m - 1;
map[m][n] = 4;
} else {
map[m][n] = 0;
m = m - 1;
map[m][n] = 1;
}
}
}
break;
case "A":
if (n == 0) {
System.out.println("小人已經到最左面了不能再走了!");
} else {
if (map[m][n] == 4) {
if (map[m][n - 1] == 2) {
if (n - 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m][n - 2] == 2) {
System.out.println("左面還有一個箱子,請重新選擇方向!");
} else if (map[m][n - 2] == 5) {
System.out.println("左面還有一個箱子,請重新選擇方向!");
} else if (map[m][n - 2] == 3) {
map[m][n - 2] = 5;
map[m][n] = 3;
n = n - 1;
map[m][n] = 1;
} else {
map[m][n - 2] = 2;
map[m][n] = 3;
n = n - 1;
map[m][n] = 1;
}
}
} else if (map[m][n - 1] == 5) {
if (n - 1 == 0) {
System.out.println("已經到最上面了!");
} else {
map[m][n - 1] = 3;
if (map[m][n - 2] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m][n - 2] == 3) {
map[m][n - 2] = 5;
map[m][n] = 3;
n = n - 1;
map[m][n] = 4;
} else {
map[m][n - 2] = 2;
map[m][n] = 3;
n = n - 1;
map[m][n] = 4;
}
}
} else if (map[m][n - 1] == 3) {
map[m][n] = 3;
n = n - 1;
map[m][n] = 4;
} else {
map[m][n] = 3;
n = n - 1;
map[m][n] = 1;
}
} else {
if (map[m][n - 1] == 2) {
if (n - 1 == 0) {
System.out.println("箱子已經到左邊了,請重新選擇方向!");
} else {
if (map[m][n - 2] == 2) {
System.out.println("左面還有一個箱子,請重新選擇方向!");
} else if (map[m][n - 2] == 5) {
System.out.println("左面還有一個箱子,請重新選擇方向!");
} else if (map[m][n - 2] == 3) {
map[m][n - 2] = 5;
map[m][n] = 0;
n = n - 1;
map[m][n] = 1;
} else {
map[m][n - 2] = 2;
map[m][n] = 0;
n = n - 1;
map[m][n] = 1;
}
}
} else if (map[m][n - 1] == 5) {
if (n - 1 == 0) {
System.out.println("已經到最上面了!");
} else {
if (map[m][n - 2] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m][n - 2] == 3) {
map[m][n - 2] = 5;
map[m][n] = 0;
n = n - 1;
map[m][n] = 4;
} else {
map[m][n - 2] = 2;
map[m][n] = 0;
n = n - 1;
map[m][n] = 4;
}
}
} else if (map[m][n - 1] == 3) {
map[m][n] = 0;
n = n - 1;
map[m][n] = 4;
} else {
map[m][n] = 0;
n = n - 1;
map[m][n] = 1;
}
}
}
break;
case "S":
if (m == map.length) {
System.out.println("小人已經到最上面了不能再走了!");
} else {
if (map[m][n] == 4) {
if (map[m + 1][n] == 2) {
if (m + 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m + 2][n] == 2) {
System.out.println("下面還有一個箱子,請重新選擇方向!");
} else if (map[m + 2][n] == 5) {
System.out.println("下面還有一個箱子,請重新選擇方向!");
} else if (map[m + 2][n] == 3) {
map[m + 2][n] = 5;
map[m][n] = 3;
m = m + 1;
map[m][n] = 1;
} else {
map[m + 2][n] = 2;
map[m][n] = 3;
m = m + 1;
map[m][n] = 1;
}
}
} else if (map[m + 1][n] == 5) {
if (m + 1 == map.length) {
System.out.println("已經到最下面了!");
} else {
map[m + 1][n] = 3;
if (map[m + 2][n] == 2) {
System.out.println("下面有一個箱子,請重新選擇方向");
break;
} else if (map[m + 2][n] == 3) {
map[m + 2][n] = 5;
map[m][n] = 3;
m = m + 1;
map[m][n] = 4;
} else {
map[m + 2][n] = 2;
map[m][n] = 3;
m = m + 1;
map[m][n] = 4;
}
}
} else if (map[m + 1][n] == 3) {
map[m][n] = 3;
m = m + 1;
map[m][n] = 4;
} else {
map[m][n] = 3;
m = m + 1;
map[m][n] = 1;
}
} else {
if (map[m + 1][n] == 2) {
if (m + 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m + 2][n] == 2) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m + 2][n] == 5) {
System.out.println("上面還有一個箱子,請重新選擇方向!");
} else if (map[m + 2][n] == 3) {
map[m + 2][n] = 5;
map[m][n] = 0;
m = m + 1;
map[m][n] = 1;
} else {
map[m + 2][n] = 2;
map[m][n] = 0;
m = m + 1;
map[m][n] = 1;
}
}
} else if (map[m + 1][n] == 5) {
if (m + 1 == 0) {
System.out.println("已經到最上面了!");
} else {
if (map[m + 2][n] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m + 2][n] == 3) {
map[m + 2][n] = 5;
map[m][n] = 0;
m = m + 1;
map[m][n] = 4;
} else {
map[m + 2][n] = 2;
map[m][n] = 0;
m = m + 1;
map[m][n] = 4;
}
}
} else if (map[m + 1][n] == 3) {
map[m][n] = 0;
m = m + 1;
map[m][n] = 4;
} else {
map[m][n] = 0;
m = m + 1;
map[m][n] = 1;
}
}
}
break;
case "D":
if (n == map.length) {
System.out.println("小人已經到最右面了不能再走了!");
} else {
if (map[m][n] == 4) {
if (map[m][n + 1] == 2) {
if (n + 1 == 0) {
System.out.println("箱子已經到頂了,請重新選擇方向!");
} else {
if (map[m][n + 2] == 2) {
System.out.println("右面還有一個箱子,請重新選擇方向!");
} else if (map[m][n + 2] == 5) {
System.out.println("右面還有一個箱子,請重新選擇方向!");
} else if (map[m][n + 2] == 3) {
map[m][n + 2] = 5;
map[m][n] = 3;
n = n + 1;
map[m][n] = 1;
} else {
map[m][n + 2] = 2;
map[m][n] = 3;
n = n + 1;
map[m][n] = 1;
}
}
} else if (map[m][n + 1] == 5) {
if (n + 1 == 0) {
System.out.println("已經到最上面了!");
} else {
map[m][n + 1] = 3;
if (map[m][n + 2] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m][n + 2] == 3) {
map[m][n + 2] = 5;
map[m][n] = 3;
n = n + 1;
map[m][n] = 4;
} else {
map[m][n + 2] = 2;
map[m][n] = 3;
n = n + 1;
map[m][n] = 4;
}
}
} else if (map[m][n + 1] == 3) {
map[m][n] = 3;
n = n + 1;
map[m][n] = 4;
} else {
map[m][n] = 3;
n = n + 1;
map[m][n] = 1;
}
} else {
if (map[m][n + 1] == 2) {
if (n + 1 == map.length) {
System.out.println("箱子已經到右邊了,請重新選擇方向!");
} else {
if (map[m][n + 2] == 2) {
System.out.println("右面還有一個箱子,請重新選擇方向!");
} else if (map[m][n + 2] == 5) {
System.out.println("右面還有一個箱子,請重新選擇方向!");
} else if (map[m][n + 2] == 3) {
map[m][n + 2] = 5;
map[m][n] = 0;
n = n + 1;
map[m][n] = 1;
} else {
map[m][n + 2] = 2;
map[m][n] = 0;
n = n + 1;
map[m][n] = 1;
}
}
} else if (map[m][n + 1] == 5) {
if (n + 1 == 0) {
System.out.println("已經到最上面了!");
} else {
if (map[m][n + 2] == 2) {
System.out.println("上面有一個箱子,請重新選擇方向");
break;
} else if (map[m][n + 2] == 3) {
map[m][n + 2] = 5;
map[m][n] = 0;
n = n + 1;
map[m][n] = 4;
} else {
map[m][n + 2] = 2;
map[m][n] = 0;
n = n + 1;
map[m][n] = 4;
}
}
} else if (map[m][n + 1] == 3) {
map[m][n] = 0;
n = n + 1;
map[m][n] = 4;
} else {
map[m][n] = 0;
n = n + 1;
map[m][n] = 1;
}
}
}
break;
default:
System.out.println("您主動關閉程式了!");
return;
}
System.out.println("小地圖目前狀態如下:");
System.out.println("-------------------------------");
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + "\t");
}
System.out.println();
}
if (map[boxK][boxL] == 5 && map[boxK1][boxL1] == 5) {
System.out.println("您已經成功了!");
return;
}
}
}
}