返回

Java代码编写:CS455 Random Walk

发布时间:2022-11-08 23:09:25 250

Requirement

In this assignment you will write a graphics-based program to do a ​​random walk​​, sometimes also known as a drunkard’s walk. This random walk simulates the wandering of an intoxicated person on a square street grid. The drunkard will start out in the middle of the grid and will randomly pick one of the four compass directions, and take a step in that direction, then another step from that new location in a random direction, etc.

This assignment will give you practice with creating classes, using loops, using the java library for random number generation, doing console-based IO, and drawing to a graphics window. Also you’ll get practice in general program development.

Analysis

Randow Walk,即​​随机游走​​问题,类似布朗运动,物体在下一刻走动的方向完全是随机的。本题利用随机数生成器来模拟下一刻的方位。框架使用java的​​awt​​包实现了UI部分,我们只需要将随机算法加入到提供的框架中。
解题的关键是了解随机数生成器的用法,根据提供的框架,找到对应函数入口,根据给定的测试集进行调试即可。

Tips

从测试函数入口入手

private void testTranslate(Impoint loc, int deltaX, int deltaY) {
...
ImPoint p2 = loc.translate(deltaX, deltaY);
...
}
复制代码

因此我们需要实现ImPoint类,以及translate方法,核心代码如下

class ImPoint {
private int x;
private int y;
...
public Impoint translate(int deltaX, int deltaY) {
Random r = new Random();
int x = deltaX + nextInt(2) - 1;
int y = deltaY + nextInt(2) - 1;
return new Impoint(x, y);
}
...
}

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线
下一篇
Python代码编写:CSC108H Tic-Tac-Toe 2022-11-08 22:35:20