这是一个游戏,感觉不错,加上有固定的套路去寻找答案,所以就决定用程序实现。

这个小游戏的网址:http://www.hacker.org/runaway/index.php

这个游戏是一个机器人在布满炸弹的地图(bigLayout)中寻找出路,出路是在雷区的边界(绿色区域)。但机器人(robot)记住的路途太少不可以记住从开始到安全区域的所有路径。robot会记住最小(min)的路径步数和最大(max)的路劲步数,但走完了记忆步骤,就开始循环记忆步骤。robot只会两种走法(向右和向下),开始时的图形为下图:


java Robot模拟按下键盘v java robot 游戏g_算法



下面说一下算法:

游戏<Runaway Robot>算法:

int min  //最小步数

int max  //最大步数

String info //布局

Output:int path[][] //可行路径

1 do getBigLayout

2 for min to max:

for-each Dot,Dot为固定步骤一次循环到达的位置

if dot可用:

do getLayout

do getLayoutUse

do getLayoutCut

3 return:Layout

getBigLayout1 把info代表的信息转化为地图

getLayout:

2  依据bigLayout,丰满layout布局 ,即把到到达dot位置的步骤循环走完bigLayout,经过区域的炸弹全部映射到layout中,其中炸弹区为   标记为red,null标记为null

1 初始化layout起点为green

2 for-each layout.dot,由上至下,由左到右遍历layout

if dot为green

感染dot的右一或下一位置为null的为green

3 return layout

getLayoutCut:

2  for-each layout.dot,由下至上,由右到左遍历layout

if dot为robot

感染dot的左一或上一位置 为green 为 robot

java算法实现: 注意:0,green;1,red;2,null;3,robot;

1. package runaway;  
2. public class Robot {  
3.       
4. //主要算法实现  
5. public int[][] go(String info,int min,int max){  
6. int bigLayout[][]=getBigLayout(info);  
7. int layout[][]=null;  
8. for(int i=min;i<=max;i++){  
9. for(int j=0;j<=i;j++){  
10. int I=i-j+1;  
11. int J=j+1;  
12.                 layout=getLayout(bigLayout,I,J);  
13.                 layout=layoutUse(layout);  
14.                 layout=layoutCut(layout);  
15. if(layout!=null)  
16. return layout;  
17.             }  
18.         }  
19. return null;  
20.     }  
21.   
22. //获取bigLayout  
23. private int[][] getBigLayout(String info){  
24. int n=(int)Math.sqrt((double)info.length());  
25. int bigLayout[][] =new int[n+1][n+1];  
26. for(int i=0;i<bigLayout.length;i++){  
27. for(int j=0;j<bigLayout[0].length;j++){  
28. 2;  
29.             }  
30.         }  
31. int sum=0;  
32. for(int i=0;i<n;i++){  
33. for(int j=0;j<n;j++){  
34. if(info.charAt(sum)=='X'){  
35. 1;  
36.                     sum++;  
37. else if(info.charAt(sum)=='.'){  
38. 2;  
39.                     sum++;  
40. else{  
41. "error:getBigLayout");  
42. 0);  
43.                 }  
44.             }  
45.         }  
46. return bigLayout;  
47.     }  
48.       
49. //获取layout  
50. private int[][] getLayout(int[][] bigLayout, int i, int j) {  
51. int layout[][]=new int[i][j];  
52. for(int x=0;x<layout.length;x++){  
53. for(int y=0;y<layout[0].length;y++){  
54. 2;  
55.             }  
56.         }  
57. for(int m=0;m<bigLayout.length;m++){  
58. for(int n=0;n<bigLayout[0].length;n++){  
59. int M=m;  
60. int N=n;  
61. while(M>=(i-1) && N>=(j-1)){  
62. 1;  
63. 1;  
64.                 }  
65. if(M>=0 && M<i){  
66. if(N>=0 && N<j){  
67. if(bigLayout[m][n]==1){  
68. 1;  
69. else if(bigLayout[m][n]!=2){  
70. "error:getLayout!!");  
71. 0);  
72.                         }  
73.                     }  
74.                 }  
75.             }  
76.         }  
77. return layout;  
78.     }  
79.   
80. //获取layout,其中包括可行路径,可为null  
81. private int[][] layoutUse(int[][] layout) {  
82. if(layout[layout.length-1][layout[0].length-1]==1 || layout[0][0]==1)  
83. return null;  
84. if(layout[0][0]==2)  
85. 0][0]=0;  
86. else{  
87. "error:layoutUse01");  
88. 0);  
89.         }  
90. for(int i=0;i<layout.length;i++){  
91. for(int j=0;j<layout[0].length;j++){  
92. if(layout[i][j]==0){  
93. if((i+1)<layout.length && layout[i+1][j]==2){  
94. 1][j]=0;  
95.                     }  
96. if((j+1)<layout[0].length && layout[i][j+1]==2){  
97. 1]=0;  
98.                     }  
99.                 }  
100.             }  
101.         }  
102.           
103. return layout;  
104.     }  
105.   
106. //获取layout,其中包括安全路径,可为null  
107. private int[][] layoutCut(int[][] layout) {  
108. if(layout==null)  
109. return null;  
110. if(layout[layout.length-1][layout[0].length-1]==0){  
111. 1][layout[0].length-1]=3;  
112. else{  
113. return null;  
114.         }  
115. for(int i=layout.length-1;i>=0;i--){  
116. for(int j=layout[0].length-1;j>=0;j--){  
117. if(layout[i][j]==3){  
118. if((i-1)>=0 && layout[i-1][j]==0){  
119. 1][j]=3;  
120.                     }  
121. if ((j-1)>=0 && layout[i][j-1]==0){  
122. 1]=3;  
123.                     }  
124.                 }  
125.             }  
126.         }  
127. return layout;  
128.     }  
129. }

此后,我们要在可行路径中选一条输出。具体实现代码入下:

1. package test;  
2. public class Look {  
3. public void look(int matrix[][]){  
4. int m=matrix.length;  
5. int n=matrix[0].length;  
6. int i=0;  
7. int j=0;  
8. while(j<=n&i<m){  
9. if((j+1)<n && matrix[i][j+1]==3){  
10. "右 ");  
11.                 j++;  
12. else{  
13.                 i++;  
14. if(i<m){  
15. "下 ");  
16.                 }     
17.             }  
18.         }  
19.     }  
20. }



java实现后,就是运行了。

首先是获取info信息,获取方法和内容如下:


java Robot模拟按下键盘v java robot 游戏g_java Robot模拟按下键盘v_02


其中灰色的value值后的值(本例为FVterrainString=.....X..X...XX.XX...... .......X....XX. ..X.X.X.....X....X.X....... X

...X.      . 
     . 
     . 
     ..X.....XXX.XX..X.........XXX......X....XX.....X.....X..X..X..X....X..X..XX.X.X..X.......X...XXX.XX...X...X.X.XX.XXXX.XX.X....X.X..XX..X...XX.............X.....X..XXX.XXX...XX..X..............X.X.....X.X....X.X.XXX.....X...........XX.X......X......XX..X.X..X..X....XX..X.X.XX...X...X.X...X.X.XX.XX..X..X.X..X..X......X..X..X....XX...X.X..............X...............X..X....XX..X..........X..X...X...X.X....XX...X......XX.....X...........X.XXXXX.......X..X..XXXX..X..........................X.X..X..X........X.....X..XX.X.X..X.XX..........XX.X..X.X......X.XX..XXX.XX.X.X.......X.X..........X........X.XX...XX....XX.X.XX..........XXXX....X...XXX.X.........X.X......X....X...............X.X.XXX..X..X.X..X.X...XX..XX.............X...........X......X.X.XX.....XX..X.....XXX....X.XX...XX...X.X.........X...............XX.....X......X...X.XXX.....XX.X.X.....X.....X....X............X.X....X....X......X..X.....XX..XX...................X....X......XX.........X.....X......X.X..XXXX.....XXX..........X.X.......X...........X.X.....X...........X.....X........X.........XX..XX.X...XX........X...X...XX.......X.XX....X...X....X........XX...XX............X.XX.X.....XX............XX..X......X.X..X......X.....X.X....X....X...XXXX.........X.XX.XXX.X......X.X.X...X........X.X.X........X...X.X..XX.X..X......X....X....X...X.......X..............X................X........X.......X.XX.XX...XXXX.X..X.XX.X..XX..X.X.........X...X.X...X...X.X..XX...XX........X.....XX...X..X..X.X....X...XX....XX....XX.X.X..XX...............X.....X...X....X.......X.......X..XX.X........XX...X..X..X..XX............X.X...X.XX...XX.....X..XX...X...............XX..XX.XX..X...X...X.......X........X.X.X...........X......X.....X.X......X....X.X..........X....X.......X............X.X......X.....X............XX..XX...X.X...XX......X.X...........X...X.X....X...X......XX....XX.X.XX.XXXX..X...........X...X.X......XX...XX.X...X.X....X.....X.X.XXX...XX..XXX....X...X...X.X.XX..XX.....X..XXXXXX.....X.XX...X.XX....XX.....X..X...XXX.....X.X.X.X..XX..X...X.XX.....X.............XX....X....X....X.X.......X..X.........X..XX......XXXXXX...X.........X...X....X...X.X.X..XX.XXX...XX.X.X.............X...XXX.........X.XXX.X........XX......X...XX..X.............XXX...XX.......X...........X.X..XX..X.....X...X.X...X........XX.XX...XXX..X.......X.X.X.....X....XXX.X.XX....X..XX.X.....X...X.....X.X...X.XX.X..X...XX.X....X.......X...XX...X......XX..X...X.....X......XXXX.X.....XX..X...X.........X.....X..X....XXX.....X........X..X..XX.XXX.....XX...........X........X...X.X..X..X.&FVinsMax=30&FVinsMin=18&FVboardX=51&FVboardY=51&FVlevel=96),“.”代表安全,"X"代表炸弹。其中min为 
     FVinsMin,max为FVinsMax。这些就可以在运行软件中得知。


现在编写运行代码如下:


import java.util.Scanner;
import runaway.Robot;
import test.Look;


public class Test {

	public static void main(String[] args) {
		String info;
		int min;
		int max;
		while(true){
			System.out.println("input::");
			Scanner sc=new Scanner(System.in);
			String value=sc.nextLine();
			info=value.substring(value.indexOf("FVterrainString=")+16, value.indexOf("&FVinsMax="));
			String Max=value.substring(value.indexOf("&FVinsMax=")+10, value.indexOf("&FVinsMin="));
			String Min=value.substring(value.indexOf("&FVinsMin=")+10, value.indexOf("&FVboardX="));
			min=Integer.valueOf(Min);
			max=Integer.valueOf(Max);
			Robot bo=new Robot();
			int path[][]=bo.go(info,min,max);
			if(path==null){
				System.out.println("game over:null");
			}
			for(int [] x:path){
				for(int y:x){
					System.out.print(y+"  ");
				}
				System.out.println("");
			}
			new Look().look(path);
			System.out.println();
		}
		

	}

}




运行上述代码,在input::后面输入在网页源码中获取的信息,并回车确认。这样既可以得到运行结果。如下图:


java Robot模拟按下键盘v java robot 游戏g_游戏过关_03


根据运行结果,输入步骤的选择,最后就可以运行成功。输入并闯关成功的图片所示如下:

java Robot模拟按下键盘v java robot 游戏g_java实现_04


game over!!!


——fxleyu

20130115写于QQ空间

20130124在原有基础上增加Look类,实现结果的人性化显示。