前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java代写:CS455 Random Walk

Java代写:CS455 Random Walk

原创
作者头像
拓端
发布2022-10-24 21:51:57
3030
发布2022-10-24 21:51:57
举报
文章被收录于专栏:拓端tecdat拓端tecdat

全文链接:tecdat.cn/?p=29602

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

从测试函数入口入手

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

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

代码语言:javascript
复制
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);
  }
  ...
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全文链接:tecdat.cn/?p=29602
    • Requirement
      • Analysis
        • Tips
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com