说明:刚入门的时候面试,有个老师傅说,你们喜欢打游戏,让你们写个简单的推箱子,能写出来就过
我说这多简单
结果说要用枚举类,数组来写
写得一踏糊涂,最后没通过
如今工作两年了,忽然想起来了这个事情,花费一点时间写完,发现确实很有收获,希望大家也能有所收获,坚持不懈在这条道路上越走越远
import java.util.Arrays;
import java.util.Scanner;
public class BoxStart {
private static int mapX = 9;
private static int mapY = 9;
private static String[][] map = new String[mapX][mapY];
public static void initMap(){
for (int i = 0; i < map.length; i++) {
Arrays.fill(map[i], Shape.road.getName());
}
}
public static void printMap(){
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println("");
}
}
public static void changeMap(int x,int y,String shape){
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(i==x && y==j){
map[i][j] = shape;
}
}
}
}
public static Boolean finishStatus(){
boolean status = true;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j].equals(Shape.WhiteBox.getName())) {
status = false;
break;
}
}
}
return !status;
}
public static void getRemoveAfter(String[] arr,int x , int y,boolean changeX,boolean changeY){
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(changeX && i == x){
map[i][j] = arr[j];
}
if(changeY && j == y){
map[i][j] = arr[i];
}
}
}
}
public static String[] recursionArr(String[] arr,int peopleX,int x){
for (int i = x; i < arr.length; i++) {
if(i == x){
if(i == arr.length-1){
return arr;
}else {
String next = arr[i+1];
if(next.equals(Shape.road.getName())){
for (int m = x; m >=peopleX; m--){
arr[m+1] = arr[m];
if(m == peopleX){
arr[peopleX] = Shape.road.getName();
}
}
return arr;
}else if(next.equals(Shape.BlackBox.getName())){
if(x>peopleX) {
for (int m = x; m >= peopleX; m--) {
arr[m + 1] = arr[m];
if (m == peopleX) {
arr[peopleX] = Shape.road.getName();
arr[i + 1] = Shape.BlackBox.getName();
}
}
}
// System.out.println(arr);
return arr;
}else if(next.equals(Shape.WhiteBox.getName())){
return recursionArr(arr,peopleX,i+1);
}
}
}
}
return arr;
}
public static int[] getPeoplePosition(){
int[] people = new int[2];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(map[i][j].equals(Shape.People.getName())){
people[0] = i;
people[1] = j;
}
}
}
return people;
}
public static void reverseArray(String[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
// 交换元素
String temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
// 移动指针
left++;
right--;
}
}
public static void upMove(){
int[] position = getPeoplePosition();
int peopleX = position[0];
int peopleY = position[1];
String [] arr = new String[mapX];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(j == peopleY){
arr[mapX-i-1] = map[i][j];
}
}
}
String[] res = recursionArr(arr,mapX-peopleX-1,mapX-peopleX-1);
reverseArray(res);
getRemoveAfter(res,peopleX,peopleY,false,true);
}
public static void downMove(){
int[] position = getPeoplePosition();
int peopleX = position[0];
int peopleY = position[1];
String [] arr = new String[mapX];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(j == peopleY){
arr[i] = map[i][j];
}
}
}
String[] res = recursionArr(arr,peopleX,peopleX);
// reverseArray(res);
getRemoveAfter(res,peopleX,peopleY,false,true);
}
public static void leftMove(){
int[] position = getPeoplePosition();
int peopleX = position[0];
int peopleY = position[1];
String [] arr = new String[mapY];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(i == peopleX){
arr[mapY-j-1] = map[i][j];
}
}
}
String[] res = recursionArr(arr,mapY-peopleY-1,mapY-peopleY-1);
reverseArray(res);
getRemoveAfter(res,peopleX,peopleY,true,false);
}
public static void rightMove(){
int[] position = getPeoplePosition();
int peopleX = position[0];
int peopleY = position[1];
String [] arr = new String[mapY];
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if(i == peopleX){
arr[j] = map[i][j];
}
}
}
String[] res = recursionArr(arr,peopleY,peopleY);
// reverseArray(res);
getRemoveAfter(res,peopleX,peopleY,true,false);
}
public static void main(String[] args) {
initMap();
changeMap(5,5,Shape.People.getName());
changeMap(7,1,Shape.WhiteBox.getName());
changeMap(1,5,Shape.BlackBox.getName());
printMap();
while (finishStatus()){
System.out.println("---------input w/a/s/d----------");
Scanner scanner = new Scanner(System.in);
String controls = scanner.next();
if("w".equals(controls)){
upMove();
}
if("a".equals(controls)){
leftMove();
}
if("s".equals(controls)){
downMove();
}
if("d".equals(controls)){
rightMove();
}
printMap();
}
System.out.println("--------------------------");
System.out.println("---------success----------");
System.out.println("--------------------------");
}
public enum Shape {
road(1, "*"),
WhiteBox(2, "□"),
BlackBox(3, "■"),
People(4, "Ꙫ");
private int code;
private String name;
Shape(int code, String name) {
this.code = code;
this.name = name;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}