gpt4 book ai didi

java - randomWalk.java 我缺少什么(初学者)

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:17 24 4
gpt4 key购买 nike

我已经编写了大部分代码,只是在完成时遇到一些困难。我应该创建一些代码,让一个正方形在绘图板上随机走动,直到它离开屏幕。我可以让它发挥一点作用,但我感觉它不正确。任何帮助将不胜感激。

这是一个它应该是什么样子的例子 RandomWalking

这是我的代码,到目前为止我最关心的问题是 walkRandom 方法,它旁边有星星

public class RandomWalk {

// DrawingPanel will have dimensions DIM by DIM
public static final int DIM = 400;

// Start the random walk in the center of the screen
public static final int CENTER = DIM / 2;

// how big should the cursor appear?
public static final int CURSOR_DIM = 10;

// how long should the cursor saty in one place?
public static final int SLEEP_TIME = 5; // milliseconds

public static void main( String[] args ) {
DrawingPanel panel = new DrawingPanel( DIM, DIM );

Random rand = new Random();

walkRandomly( panel, rand );
}

/**
* Draw a random walk on the panel.
* Stop as soon as you walk off the panel.
* Each random step should go only in one
* of these directions: left, right, up, down.
* @param panel a DrawingPanel to draw the
* random walk on
* @param rand a Java Random object to be
* used to generate random steps
*/
public static void walkRandomly( DrawingPanel panel, Random rand ) {
Graphics g = panel.getGraphics();

// start in center of panel
int x = CENTER;
int y = CENTER;

// Randomly step left, right, up, or down
// until cursor goes off screen.
while ( onScreen( x, y ) ) {

// Draw the cursor in BLACK
g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
// Wait a bit.
panel.sleep( SLEEP_TIME );
// Show a shadow version of the cursor
g.setColor(Color.GRAY);
g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);


// Choose a new location for the cursor
//*********************************
if (rand.nextInt() < DIM - 300){
x--;
} else if (rand.nextInt() < DIM / 2){
x++;
} else if (rand.nextInt() < DIM - 100){
y--;
} else if (rand.nextInt() < DIM) {
y++;
}
//*********************************


// draw the cursor at its new location
g.setColor(Color.BLACK);
g.fillRect(x, y, CURSOR_DIM, CURSOR_DIM);
}
}

/**
* determine whether (x, y) is a point on the panel.
* @param x the x-coord of the cursor
* @param y the y-coord of the cursor
* @return true if (x,y) is on the screen,
* false if (x,y) is off the screen
*/
public static boolean onScreen( int x, int y ) {
// x/y high = 400, x/y low = 0
//*********************************
return (x <= DIM && x >= 0 || y <= DIM && y >= 0);

}
}

最佳答案

您的代码存在一些问题。您的问题没有完全澄清,但我认为:

  • 所有方向发生的概率应该相同
  • 光标只能移动“完整步”,即它必须移动 CURSOR_DIM一次不少于(这就是您的示例中似乎发生的情况)

问题主要出在方法walkRandomly上,我建议你选择下一步的方向如下:

// Choose a new location for the cursor
int direction = rand.nextInt(4); //0=left, 1=right, 2=up, 3=down
if (direction == 0){
x -= CURSOR_DIM; //The step should be equal to the cursor width, not 1
} else if (direction == 1){
x += CURSOR_DIM;
} else if (direction == 2){
//you usually subtract to go up, the origin being the upper-left corner
y -= CURSOR_DIM;
} else if (direction == 3) {
y += CURSOR_DIM;
}

还有另外两个问题:

  1. 您在循环的开始和结束处将光标绘制为黑色,这是多余的。您可以删除其中之一并获得相同的结果。

  2. onScreen方法,xy值等于 DIM应该返回 false,因为通常图像/数组的边界是 0 到 DIM 独家。您还应该替换 ||通过 AND,因为光标应该在 x AND y 范围内,而不是 OR。
    return (x < DIM && x >= 0 && y < DIM && y >= 0);

希望对你有帮助

关于java - randomWalk.java 我缺少什么(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com