我需要为变量agentPosX
和agentPosY
使用随机值,并且我需要打印它们以查看它们获得的值。
代码很长,但是我需要帮助的行是52、53和322。
我用了static
,但我认为它是不对的。
import java.util.*;
import java.io.*;
public class WumpusWorld {
public static final int PIT_FLAG = 1;
public static final int WUMPUS_FLAG = 2;
public static final int GOLD_FLAG = 4;
public static final int BREEZE_FLAG = 8;
public static final int STENCH_FLAG = 16;
public static final int GLITTER_FLAG = 32;
public static final int BUMP_FLAG = 64;
public static final int SCREAM_FLAG = 128;
private static final int FACING_NORT = 1;
private static final int FACING_EAST = 2;
private static final int FACING_WEST = 3;
private static final int FACING_SOUTH = 4;
/*
* Actions
*/
public static final int GO_FORWARD = 1;
public static final int TURN_LEFT = 2;
public static final int TURN_RIGHT = 3;
public static final int GRAB = 4;
public static final int SHOOT = 5;
public static final int CLIMB = 6;
public static final String STR_GO_FORWARD = "Avanzar";
public static final String STR_TURN_LEFT = "Izquierda";
public static final String STR_TURN_RIGHT = "Derecha";
public static final String STR_GRAB = "Agarrar";
public static final String STR_SHOOT = "Disparar";
public static final String STR_CLIMB = "Escalar";
//matriz random
/*
* Perceptions
*/
private int perceptions = 0;
public double actionCost = 1.0;
public double outWithGold = 1000.0;
public double killCost = 10000.0;
public int worldDimensionX;
public int worldDimensionY;
public double pitProbability;
public static int agentPosX = (int)Math.random()*10;
public static int agentPosY = (int)Math.random()*10;
private int agentDir = FACING_EAST;
private boolean agentSafe = true;
private double agentScore = 0.0;
private boolean agentArrow = true;
private boolean agentGold = false;
private boolean agentOut = false;
private int world[];
PrintStream out = System.out;
Scanner reader;
public static void main(String args[]) {
//WumpusWorld prin = new WumpusWorld(worldDimensionX,worldDimensionY, pitProbability, actionCost, outWithGold, killCost);
int n = 100;
if ( args.length == 1 ) {
try {
n = Integer.parseInt(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("SIMULATION_STARTED");
System.out.println("gato: " + agentPosX + " gatoY: " + agentPosY);
for (int i = 0; i < n; i++) {
WumpusWorld world = new WumpusWorld(10,
10,
0.15,
1,
1000,
1000);
System.out.println("EPISODE_STARTED");
while ( world.isAgentOk() & !world.isAgentOut() ) {
System.out.println( world.getPerceptions() );
world.readAction();
}
System.out.println("EPISODE_ENDED, AGENT_SCORE=" + world.agentScore);
String line = world.readLine();
if ( !"START_SIMULATION_REQUEST".equals(line) ) break;
}
}
}
这一行基本上就是你代码中的内容。
System.out.println( (int)Math.random()*10 ); // prints zero
上面的代码将始终打印零。为什么?因为您正在四舍五入的小数值是<1的整数次方。它总是零。要更清楚地了解这一点,请考虑以下示例:
double v = Math.random();
System.out.println( v ); // prints a decimal < 1
System.out.println( (int) (v*10) ); // prints a value between 1 and 9
System.out.println( (int) v*10 ); // prints 0
像这样转换Math.random()
的结果太早了,您希望先进行乘法运算-
public static int agentPosX = (int) (Math.random()*10);
public static int agentPosY = (int) (Math.random()*10);
我建议你使用Random
,
private static Random rand = new Random();
public static int agentPosX = rand.nextInt(10) + 1;
public static int agentPosY = rand.nextInt(10) + 1;