没有合适的资源?快使用搜索试试~ 我知道了~
Part 3:GridWorld Classes and Interfaces - 2015年软件工程实训 - SoYa Wik
试读
15页
需积分: 0 0 下载量 193 浏览量
更新于2022-08-03
收藏 611KB PDF 举报
在GridWorld中,我们主要关注的是几个核心的类和接口,它们构成了这个模拟环境的基础。GridWorld是一个用于教学目的的编程环境,它允许用户创建和交互复杂的网格状系统,其中包含各种活动对象,如Actor。在Part 3:GridWorld Classes and Interfaces中,我们将深入探讨这些关键组件。
`Actor`类是所有GridWorld中演员对象的基类。用户通常会创建继承自`Actor`的自定义类来表示他们想要的行为和外观。每个`Actor`都有一个位置和一个方向,这两个属性是通过`Location`类和`Grid`接口来管理的。
`Location`类封装了网格中演员位置的坐标。它不仅存储了行(row)和列(column)值,还提供了判断不同位置间关系的方法,例如判断两个位置是否相邻或者在特定的 compass 方向上。`Location`类还定义了8个常量来表示指南针上的方向,例如`NORTH`、`EAST`、`SOUTH`和`WEST`,以及45度角的偏转值,如`NORTHEAST`、`SOUTHEAST`等。此外,还有表示常见转角的常量,如`HALF_RIGHT`,这使得可以方便地计算演员转向的角度。
`Grid`接口是表示网格结构的核心,有两个实现类:`BoundedGrid`和`UnboundedGrid`。`BoundedGrid`用于表示有边界的网格,即演员不能超出网格边界;而`UnboundedGrid`则允许演员在无限大的网格中移动。这两个类都提供了添加、移除和获取网格中演员的方法,以及检查位置是否有效、移动演员等操作。
在GridWorld中,演员不仅知道自己的位置,也知道自己所在的网格。这意味着每个`Actor`对象都有指向其所在`Grid`的引用,以及表示其方向的整数值。演员的方向可以通过加减上述的转角常量来改变,例如,要使演员向右转90度,只需将当前方向加上`EAST`的值即可。
在实际编程中,用户可能会创建自己的`Actor`子类,并重写`act()`方法来定义演员的行为。例如,可以编写一个`Bee`类,使其在每次`act()`时随机移动或按照某种模式移动。`Actor`还可以有自己的图形表示,通过`DisplayInfo`类来定义显示的图像和颜色。
GridWorld提供了一个结构化的框架,用于学习面向对象编程、多线程以及复杂系统中的互动。通过理解`Actor`、`Location`、`Grid`以及它们之间的关系,开发者能够构建出各种有趣且动态的网格世界。在实训中,学生可以结合这些基础知识,编写自己的GridWorld程序,提高编程技能并体验到游戏化学习的乐趣。
15/8/26 下午5:06Part 3:GridWorld Classes and Interfaces - 2015年软件程实训 - SoYa Wiki
第 1 (共 15 )http://my.ss.sysu.edu.cn/wiki/pages/viewpage.action?pageId=443613209
等已等已Aug 15, 2015
Part 3GridWorld Classes and Interfaces
Part 3: GridWorld Classes and Interfaces
In our example programs, a grid contains actors that are instances of classes that extend the Actor class. There are two classes that
implement the Grid interface: BoundedGrid and UnboundedGrid. Locations in a grid are represented by objects of the Location class. An
actor knows the grid in which it is located as well as its current location in the grid. The relationships among these classes are shown in the
following figure.
The Location Class
Every actor that appears has a location in the grid. The Location class encapsulates the coordinates for an actor's position in a grid. It also
provides methods that determine relationships between locations and compass directions.
Every actor in the grid also has a direction. Directions are represented by compass directions measured in degrees: 0 degrees is north, 45
degrees is northeast, 90 degrees is east, etc. The Location class provides eight constants that specify the compass directions.
public static final int NORTH = 0;
public static final int EAST = 90;
public static final int SOUTH = 180;
public static final int WEST = 270;
public static final int NORTHEAST = 45;
public static final int SOUTHEAST = 135;
public static final int SOUTHWEST = 225;
public static final int NORTHWEST = 315;
15/8/26 下午5:06Part 3:GridWorld Classes and Interfaces - 2015年软件程实训 - SoYa Wiki
第 2 (共 15 )http://my.ss.sysu.edu.cn/wiki/pages/viewpage.action?pageId=443613209
In addition, the Location class specifies constants for commonly used turn angles. For example, Location.HALF_RIGHT denotes a turn by
45 degrees. Here are the constants for the turn angles.
To make an actor turn by a given angle, set its direction to the sum of the current direction and the turn angle. For example,
the turn method of the Bug class makes this call.
A location in a grid has a row and a column. These values are parameters of the Location constructor.
Two accessor methods are provided to return the row and column for a location.
Two other Location methods give information about the relationships between locations and directions.
public Location getAdjacentLocation(int direction)
returns the adjacent location in the compass direction that is closest to direction
public int getDirectionToward(Location target)
returns the closest compass direction from this location toward target
For example, assume you have the statement below.
The following statements will result in the values indicated by the comments.
Note that the row values increase as you go south (down the screen) and the column values increase as you go east (to the right on the
screen).
The Location class defines the equals method so that loc.equals(other) returns true if other is a Location object with the same row and
column values as loc and returns false otherwise.
The Location class implements the Comparable interface. The compareTo method compares two Location objects. The method
call loc.compareTo(other) returns a negative integer if loc has a smaller row coordinate than other, or if they have the same row
and loc has a smaller column coordinate than other. The call returns 0 if loc and other have the same row and column values. Otherwise,
the call returns a positive integer.
public static final int LEFT = -90;
public static final int RIGHT = 90;
public static final int HALF_LEFT = -45;
public static final int HALF_RIGHT = 45;
public static final int FULL_CIRCLE = 360;
public static final int HALF_CIRCLE = 180;
public static final int AHEAD = 0;
setDirection(getDirection() + Location.HALF_RIGHT);
public Location(int r, int c)
public int getRow()
public int getCol()
Location loc1 = new Location(5, 7);
Location loc2 = loc1.getAdjacentLocation(Location.WEST);
// loc2 has row 5, column 6
Location loc3 = loc1.getAdjacentLocation(Location.NORTHEAST);
// loc3 has row 4, column 8
int dir = loc1.getDirectionToward(new Location(6, 8));
// dir has value 135 (degrees)
15/8/26 下午5:06Part 3:GridWorld Classes and Interfaces - 2015年软件程实训 - SoYa Wiki
第 3 (共 15 )http://my.ss.sysu.edu.cn/wiki/pages/viewpage.action?pageId=443613209
Do You Know?
Set 3
Assume the following statements when answering the following questions.
1. How would you access the row value for loc1?
2. What is the value of b after the following statement is executed?
3. What is the value of loc3 after the following statement is executed?
4. What is the value of dir after the following statement is executed?
5. How does the getAdjacentLocation method know which adjacent location to return?
The Grid Interface
The interface Grid<E> specifies the methods for any grid that contains objects of the type E. Two classes, BoundedGrid<E> and
UnboundedGrid<E> implement the interface.
You can check whether a given location is within a grid with this method.
boolean isValid(Location loc)
returns true if loc is valid in this grid, false otherwise
Precondition: loc is not null
All methods in this case study have the implied precondition that their parameters are not null. The precondition is emphasized in this
method to eliminate any doubt whether null is a valid or invalid location. The null reference does not refer to any location, and you must not
pass null to the isValid method.
The following three methods allow us to put objects into a grid, remove objects from a grid, and get a reference to an object in a grid.
E put(Location loc, E obj)
puts obj at location loc in this grid and returns the object previously at that location (or null if the location was previously unoccupied)
Precondition: (1) loc is valid in this grid (2) obj is not null
E remove(Location loc)
removes the object at location loc and returns it (or null if the location is unoccupied)
Precondition: loc is valid in this grid
E get(Location loc)
returns the object at location loc (or null if the location is unoccupied)
Precondition: loc is valid in this grid
An additional method returns all occupied locations in a grid.
Location loc1 = new Location(4, 3);
Location loc2 = new Location(3, 4);
boolean b = loc1.equals(loc2);
Location loc3 = loc2.getAdjacentLocation(Location.SOUTH);
int dir = loc1.getDirectionToward(new Location(6, 5));
剩余14页未读,继续阅读
资源推荐
资源评论
2022-08-03 上传
2022-08-03 上传
2022-08-03 上传
2022-08-03 上传
147 浏览量
125 浏览量
200 浏览量
155 浏览量
130 浏览量
199 浏览量
169 浏览量
2022-08-03 上传
134 浏览量
2019-05-16 上传
2009-12-28 上传
2020-02-12 上传
104 浏览量
129 浏览量
2023-07-29 上传
2023-07-20 上传
5星 · 资源好评率100%
160 浏览量
2019-07-17 上传
145 浏览量
2019-07-17 上传
141 浏览量
资源评论
地图帝
- 粉丝: 25
- 资源: 297
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功