GridWorld Problem

SparseGrid

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 13296 Views - Last Post: 01 May 2012 - 09:22 AM Rate Topic: -----

#1 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

GridWorld Problem

Post icon  Posted 12 March 2009 - 10:40 AM

Problem: Code compiles and runs but actors cannot be added to grid. Any insight will be appreciated.

SparseGrid Class:
import java.util.*;
import info.gridworld.grid.*;
import info.gridworld.actor.*;

public class SparseGrid<E> extends AbstractGrid<E>
{
  SparseGridNode[] arr;
  int row, col;
  
  public SparseGrid(int r, int c)
  {
	row = r;
	col = c;
	arr = new SparseGridNode[row];
	
	
	  
  }
  
  public boolean isValid(Location loc)
  {
	if ((loc.getRow() <= row && loc.getRow() >= 0) && (loc.getCol() <= col && loc.getCol() >= 0))
	  return true;
	else
	  return false;
  }
  
  public int getNumRows()
  {
	return row;
  }
  
  public int getNumCols()
  {
	return col;
  }
  
  public E put(Location loc, E obj)
  {
	if (!isValid(loc))
	  throw new IllegalArgumentException("Location " + loc + " is evil");
	if (obj == null)
	  throw new NullPointerException("obj is evil");
	
	if (arr[loc.getRow()] != null)
	{
	  SparseGridNode temp = arr[loc.getRow()];
	  
	  while (temp.getColIndex() != loc.getCol())
	  {
		temp = temp.getNext();
	  }
	  
	  temp.setOccupant(obj);
	}
	
	return obj;
  }
  
  
  public ArrayList<Location> getOccupiedLocations()
  {
	ArrayList<Location> temp = new ArrayList<Location>();
	
	for (int i = 0; i < row; i++)
	{
	  SparseGridNode node = arr[i];
	  
	  while(node != null)
	  {
		temp.add(new Location (i, node.getColIndex()));
		node = node.getNext();
	  }
	}
	return temp;
	
  }
  
  public E remove(Location loc)
  {
	if (!isValid(loc))
	  throw new IllegalArgumentException("Location " + loc + " is evil");
	
	SparseGridNode node = arr[loc.getRow()];
	SparseGridNode prev;
	
	if (node.getColIndex() == loc.getCol())
	{
	  E temp = (E) node.getOccupant();
	  arr[loc.getRow()] = node.getNext();
	  return  temp;
	}
	else
	{
	prev = node;
	node = node.getNext();
	while(node != null)
	{ 
	  if (node.getColIndex() == loc.getCol())
	  {
		prev.setNext(node.getNext());
		E temp = get(loc);
		return  temp;
	  }
	  prev = node;
	  node = node.getNext();
	}
	}
	
	return null;
  }
  
  public E get(Location loc)
  {
	if (!isValid(loc))
	  throw new IllegalArgumentException("Location " + loc + " is evil");
	
	SparseGridNode node = arr[loc.getRow()];
	
	while (node != null)
	{
	  if (loc.getCol() == node.getColIndex())
	  {
		E temp = (E)node.getOccupant();
		return  temp;
	  }
	  node = node.getNext();
	}
	return null;
  }
  //
}



SparseGridNode Class:
public class SparseGridNode
  {
	int colIndex;
	SparseGridNode next;
	Object occupant;
	
	public SparseGridNode(int c, Object o)
	{
	  colIndex = c;
	  occupant = o;
	}
	
	public int getColIndex()
	{
	  return colIndex;
	}
	
	public Object  getOccupant()
	{
	  return occupant;
	}
	
	public void setOccupant(Object e)
	{
	  occupant = e;
	}
	
	public void setNext(SparseGridNode node)
	{
	  next = node;
	}
	
	public SparseGridNode getNext()
	{
	  return next;
	}
	
	
  }



Tester:
import java.util.*;
import info.gridworld.grid.*;
import info.gridworld.actor.*;
public class SparseGridTester
{
  public static void main (String[] args)
  {
	ActorWorld world = new ActorWorld();
	world.addGridClass("SparseGrid");
	world.add(new Location(0, 5), new Critter());
	world.show();
	
	
  }
}


This post has been edited by Rekmesh: 12 March 2009 - 08:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: GridWorld Problem

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: GridWorld Problem

Posted 12 March 2009 - 11:21 AM

And what is GridWorld?
Was This Post Helpful? 0
  • +
  • -

#3 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 11:56 AM

GridWorld is a computer program case study written in Java for the AP computer sci program.
Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: GridWorld Problem

Posted 12 March 2009 - 02:42 PM

Well in order to help you you need to post the code for the GridWorld classes or a link to the Java Documentation in order to know how these classes work.
Was This Post Helpful? 0
  • +
  • -

#5 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 05:33 PM

Here:

http://www.horstmann...dworld/javadoc/
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 07:27 PM

Could you add some Java Docs or a brief about what your trying to do.
Like make critter face direction, move to that direction then change color of its closest actor. What is your intention to how the Critter is supposed to act and could you tell me why you overrided EVERY method?
Was This Post Helpful? 0
  • +
  • -

#7 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 07:47 PM

So sorry, I should have done that in the first post :(

The assignment is to basically implement a custom grid that uses an array of nodes and to be able to add the grid to the list of grid options. Critters behave normally.

Quote

Could you tell me why you overrided every method?


AbstractGrid implements the grid interface but only defines getNeighbors, getValidAdjacentLocations, getEmptyAdjacentLocations and getOccupiedAdjacentLocations because AbstractGrid is an abstract class. Since SparseGrid extends AbstractGrid, I must define all the methods AbstractGrid did not define. Or am I wrong? :blink:

This post has been edited by Rekmesh: 12 March 2009 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#8 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 07:53 PM

Haven't done abstract grids yet in class but yeah that makes sense.
Hmm... are any of the methods themselves supposed to add an actor into gridworld? If not then problem is in the tester.
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 08:00 PM

Um, Did you extend the Critter class anywhere? >_>
Can't put an actor in that doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#10 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 08:34 PM

The tester is fine and adds actors to all other grids except my sparseGrid. I have tried the setGrid method but it doesn't work either.
Was This Post Helpful? 0
  • +
  • -

#11 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 08:38 PM

Well it cant add it because you havent extended Critter in your grid. Your grid is just a grid, cant add an actor of a grid. So you have to extends Critter in your class
Was This Post Helpful? 0
  • +
  • -

#12 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 08:44 PM

Java does not support multiple inheritance. In my SparseGrid Class, I have extended AbstractGrid. Or am I not understanding what you just said?

This post has been edited by Rekmesh: 12 March 2009 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 08:48 PM

Clarification of what I was saying:
public class SparseGrid<E> extends AbstractGrid<E>, Critter


Was saying that is how it should be, have to extend Critter.
Well then Sorry idk what to tell you. It cant use Critter as an object in your grid if your class can't even tell that it is recieving Critter Class. Have to extend the class to be able to use the object of the class. You'll have to ask your Teacher what the problem is. I have found that many of the more knowledgeable Programmers here aren't familiar with Gridworld, its a learning thing, so people who already know java don't use it :P Goodluck with your Coding, Sorry again!

Edit- have you tried to implements Grid<E>? Check this site out, might help.

This post has been edited by Fuzzyness: 12 March 2009 - 09:02 PM

Was This Post Helpful? 0
  • +
  • -

#14 Rekmesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 15-September 08

Re: GridWorld Problem

Posted 12 March 2009 - 09:11 PM

Unfortunantly, these statements:
public class SparseGrid<E> extends AbstractGrid<E>, Critter



public class SparseGrid<E> implements Grid<E>



will not work. Thanks for the help though. I've already asked my teacher but the whole class is still stumped on this one problem! :angry:

Can anyone else shed some light about the problem?

This post has been edited by Rekmesh: 12 March 2009 - 09:12 PM

Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: GridWorld Problem

Posted 12 March 2009 - 09:16 PM

A little Heirarchy here for you
Grid<E>
**Implements**
AbstractGrid<E>
**Extends*
Un/Bounded Grid

This post has been edited by Fuzzyness: 12 March 2009 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2