使用说明
使
用
说
明
1.项目结构
part5—-problemone
|____problemtwo
|____problemthree
注意: 运行之前导入gridworld.jar
2.三种网格
problem1-sparseBoundedGrid
继承了AbstractGrid
public class SparseBoundedGrid extends AbstractGrid<Actor>{
protected int row;
protected int col;
protected ArrayList<SparseGridNode> occupantsArrayList;
}
对get put remove 等进行了重写
//get
@Override
public Actor get(Location loc) {
if (!isValid(loc)){
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
}
/*获取行
*/
SparseGridNode itemGridNode=occupantsArrayList.get(loc.getRow());
if(itemGridNode == null){
return null;
}
/*获取列
*/
while(itemGridNode!=null){
if(itemGridNode.col==loc.getCol()){
return itemGridNode.occupant;
}
itemGridNode=itemGridNode.next;
}
return null;
}
//put
@Override
public Actor put(Location loc, Actor obj) {
if (!isValid(loc)){
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
}
if (obj == null)
throw new NullPointerException("obj = null");
// Add the object to the grid.
Actor oldOccupant = get(loc);
/*获取行
*/
SparseGridNode itemGridNode=occupantsArrayList.get(loc.getRow());
/*行内无元素
*/
if(itemGridNode == null){
occupantsArrayList.set(loc.getRow(), new SparseGridNode(obj, loc.getCol()));
return oldOccupant;
/*当loc的列小于行内第一个元素的列
*/
if(itemGridNode.col > loc.getCol()){
SparseGridNode tempGridNode = new SparseGridNode(obj,loc.getCol());
tempGridNode.next = itemGridNode;
occupantsArrayList.set(loc.getRow(), tempGridNode);
return oldOccupant;
}
/*遍历行内列
*/
while(itemGridNode != null){
/*如果存在对应列 , 替换位置上的元素;
*/
if(itemGridNode.col==loc.getCol()){
itemGridNode.occupant = obj;
return oldOccupant;
}
else{
if(itemGridNode.next!= null){
/*如果列不存在,并且找到适当插入位置
*/
if(itemGridNode.col<loc.getCol()&&itemGridNode.next.col>loc.getCol()){
/**
* insert;
*/
SparseGridNode tempGridNode = new SparseGridNode(obj,loc.getCol());
tempGridNode.next=itemGridNode.next;
itemGridNode.next = tempGridNode;
return oldOccupant;
}
/*最后一个元素时,插入尾巴
*/
else{
itemGridNode.next = new SparseGridNode(obj, loc.getCol());
return oldOccupant;
}
}
/**迭代
*/
itemGridNode = itemGridNode.next;
}
return oldOccupant;
}
//remove
public Actor remove(Location loc) {
if (!isValid(loc)){
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
}
// Remove the object from the grid.
Actor r = get(loc);
/*获取行
*/
SparseGridNode item = occupantsArrayList.get(loc.getRow());
if(item == null){
return r;
}
/*parent 是 item的上一节点 方便节点的链接
*/
SparseGridNode parent = null;
while(item!=null){
/*找到对应列
*/
if(item.col == loc.getCol()){
/*如果找到的列第一个元素的列,将item的下一个node作为该行的首元素
*/
if(parent == null){
occupantsArrayList.set(loc.getRow(), item.next);
return r;
}
/*如果 不是第一个元素的列,将item的上一个节点和item的下一个节点链接起来;
*/
parent.next = item.next;
item.next = null;
return r;
}
/*迭代
*/
parent = item;
item=item.next;
}
return r;
}
- problem2-sparseBoundedGrid1
这里直接继承了 UnboundedGrid
并对 isvalid等函数进行重写
public class SparseBoundedGrid1 extends info.gridworld.grid.UnboundedGrid<Actor>{
private int row;
private int col;
public SparseBoundedGrid1(int row,int col){
this.row=row;
this.col=col;
}
@override
public int getNumRows()
{
return row;
}
@override
public int getNumCols()
{
return col;
}
@override
public boolean isValid(Location loc)
{
return loc.getRow()>=0&&loc.getRow()<getNumRows()&&loc.getCol()>=0&&loc.getCol()<getNumCols();
}
}
- problem3-UnboundedGrid
继承了1.中的 SparseArrayGrid
通过重写 getNumRows和getNumCols将格子拓展为无界的格子
@override
public int getNumRows()
{
return -1;
}
@Override
public int getNumCols()
{
return -1;
}
相应的,为了符合无界网格,isValid也需要重写一下
@Override
public boolean isValid(Location loc) {
return loc.getCol()>=0&&loc.getRow()>=0;
}
相应的 get 方法 略微调整
public Actor get(Location loc) {
if (!isValid(loc)){
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
}
/**这里调整 返回null,表示得到的位置有效但没有东西
*/
if(loc.getRow()>=row||loc.getCol()>=col||loc.getRow()<0||loc.getCol()<0){
return null;
}
......
}
put函数
public Actor put(Location loc, Actor obj) {
if(!isValid(loc)){
throw new IllegalArgumentException("Location " + loc
+ " is not valid");
}
/*需要扩展的时候,网格扩展
*/
while(loc.getRow()>=row||loc.getCol()>=col||loc.getRow()<0||loc.getCol()<0){
extendGrid(loc);
}
return super.put(loc, obj);
}
关键之处的扩展
for(int i = row; i< 2*row;i++){
occupantsArrayList.add(null);
}
row=2*row;
col=2*col;