package com.demo.hwj.javademo.kotlin;import java.util.Arrays;//要使用工具类;public class Testing { static int[] mInput = {1, 2, 3, 0, 9, 10, 11};//这是之前的顺序;这里静态的作用便是方便之后复制; static int[] mStep = new int[100];//步数 static int[] mResult = {9, 10, 11, 0, 1, 2, 3};//这是之后的顺序 static int mBridgeIndex = 0;//中间的坐标 static int[] mLeft = null;//左边操作的数组 static int[] mRight = null;//右边操作的数组 static int stepPosition = 0;//当前步的坐标 public static void main(String[] args) { init();//初始化 start();//开始 } private static void start() { do { switch (mStep[stepPosition]) { case 1: if (move(1, 1)) { break; } case 2: if (move(1, 2)) { break; } case 3: if (move(2, 1)) { break; } case 4: if (!move(2, 2)) { //reset mInput ,rollback mStep rollback(); continue; } } stepPosition++; } while (!Arrays.equals(mResult, mInput)); outputStep(); } //这个方法就是输出运动的实况吧; private static void outputStep() { int[] finallyStep = Arrays.copyOf(mStep, stepPosition); reset();// for (int i = 0; i < finallyStep.length; i++) { switch (finallyStep[i]) { case 1: moveLeft(1); System.out.printf(i + ": Left walking =>" + Arrays.toString(mInput) + "\n"); break; case 2: moveLeft(2); System.out.printf(i + ": Left jumping =>" + Arrays.toString(mInput) + "\n"); break; case 3: moveRight(1); System.out.printf(i + ": Right walking =>" + Arrays.toString(mInput) + "\n"); break; case 4: moveRight(2); System.out.printf(i + ": Right jumping =>" + Arrays.toString(mInput) + "\n"); break; } } }//这是初始化,初始化游戏的; private static void init() { mBridgeIndex = mInput.length / 2;// get middle bridge's position,用来获取中间的坐标; mLeft = Arrays.copyOfRange(mInput, 0, mBridgeIndex);//将左边的数组用Arrays工具类的copy of range()方法将从0到中间复制到新数组中; mRight = Arrays.copyOfRange(mInput, mBridgeIndex + 1, mInput.length);//同左; Arrays.fill(mStep, 1);//将Setp数组用1填充; } /** * move input child element .include all the way for move * * @param direction the direction for move. 1 means left,2 means right * @param stepInterval the way for move. 1 means walking,2 means jumping */ //这是运动的总类,包含了所有的移动方式; private static boolean move(final int direction, final int stepInterval) { //这里主要是发现是否有异常; if (stepInterval != 1 && stepInterval != 2) new IllegalArgumentException(" stepInterval must equal 1 or 2. -----> line 65"); if (direction != 1 && direction != 2) new IllegalArgumentException(" direction must equal 1 or 2.-----> line 65"); boolean isWalking; //这里要判断移动的方式,有跳和走,有左有右 if (direction == 1) { isWalking = moveLeft(stepInterval); } else { isWalking = moveRight(stepInterval); } //当被指向时候进行判断; if (isWalking) { //record down step record(direction, stepInterval); } return isWalking; } private static void record(int direction, int step) { switch (direction) { case 1: mStep[stepPosition] = direction * step; break; case 2: mStep[stepPosition] = direction + step; break; } } private static void rollback() { if (mStep[stepPosition] >= 4) { mStep[stepPosition] = 1; stepPosition--; } if (stepPosition < 0) stepPosition = 0; if (mStep[stepPosition] < 4) { mStep[stepPosition]++; reset(); } } /** * move input child element .include the way for move is Left * * @param stepInterval the way for move. 1 means walking,2 means jumping * @return if return false that means cannot moving */ //左向的方法 private static boolean moveLeft(final int stepInterval) { for (int i = 0; i < mInput.length; i++) { if (containOf(mLeft, mInput[i])) {// search item whether inside for mInput if (i < mInput.length - stepInterval && mInput[i + stepInterval] == 0) { //change item int a = mInput[i]; mInput[i + stepInterval] = a; mInput[i] = 0; return true; } } } return false; } //右向的方法 private static boolean moveRight(final int stepInterval) { for (int i = mInput.length - 1; i >= 0; i--) { if (containOf(mRight, mInput[i])) {// search item whether inside for mInput if (i >= 0 + stepInterval && mInput[i - stepInterval] == 0) { //change item mInput[i - stepInterval] = mInput[i]; mInput[i] = 0; return true; } } } return false; } private static boolean containOf(int[] arr, int child) { for (int i : arr) { if (i == child) return true; } return false; } private static void reset() { mInput[0] = 1; mInput[1] = 2; mInput[2] = 3; mInput[3] = 0; mInput[4] = 4; mInput[5] = 5; mInput[6] = 6; stepPosition = 0; }}
添加回答
举报
0/150
提交
取消