# Positions and values, getters, setters

• 01-23-2010, 02:11 PM
Malus
Positions and values, getters, setters
Hey I'm programming a game that is divided into squares, like a grid.
Therefore I have positions such as (x,y) in that grid.
Now I want to give some of these positions a certain int value, so I created a new class called Field, that had both the position arguments and my int value:
(x, y, value) - that part works well.
But now I want to get that value (if there is one, otherwise return null), by only typing in the position type arguments.

I thought something like, if an object of type Field with those two (x,y) coordinates existed, it should return it's int value, otherwise return null. But I can't get it to work.

just to simplify even more:
I want to return the value of a current position, if it has one, by only calling the coordinates of type Position.

• 01-23-2010, 02:31 PM
Fubarable
Code would help others to figure out just what is wrong.
• 01-23-2010, 02:40 PM
Malus
I don't have a code, I only know what I would like to do, but not how to..
I thought it would be some general problem, when you assign a value to an object (a position in my case) and then you want to retrieve that value, simply by typing in the position.
• 01-23-2010, 03:00 PM
Fubarable
Quote:

Originally Posted by Malus
I don't have a code, I only know what I would like to do, but not how to..

So dive in and try. You'll learn a whole lot more by doing this.

Quote:

I thought it would be some general problem, when you assign a value to an object (a position in my case) and then you want to retrieve that value, simply by typing in the position.
This smells of a 2-dimensional array.
• 01-23-2010, 04:08 PM
Malus
If I create a 2-dimensional i,j-array list, would I then be able to access the (1,2)'th value by simply typing in the (2,2)'th value?

Pictured:
+-------------+
| a | b | c | d |
+-------------+
| 1 | 2 | 3 | 1 |
+-------------+

Can I somehow get the 1-value, by calling the array list with only the argument a? And the other way around, can I get all the corrosponding letters (a, d), if I call the array list with the value 1?
• 01-23-2010, 04:32 PM
Fubarable
I have to wonder what your overall goal with this code is. Are you programming Conway's Game of Life or some other grid game/simulation?
• 01-23-2010, 04:36 PM
Malus
Ah no, I'm programming a maze game.
I need to add values to the positions in order to follow a specific algorithm:
Maze Router Demonstration Applet - John A. Nestor
• 01-23-2010, 04:41 PM
Fubarable
Sounds like you are programming a Graph. I am no pro at this, but if you search this forum and others, you'll find examples of this type of problem as it is not uncommon here.
• 01-23-2010, 06:05 PM
Malus
Ahh seems like I solved my problem! :D
I simply used a key-value table (a map) to store my values with Position coordinates as keys. Pretty smart solution if that's not too bold :D
• 01-23-2010, 06:38 PM
Malus
How can I return a Collection of all the keys in a key-value table, that refer to the same value, doesn't matter if it's a list of a set to me, as long as it's a Collection.

Again to simplify...
if I have the following keys and values:
keys:...................values:
a...........................1
b...........................2
c...........................3
d...........................1

I want to return a list (a,d) (or set), by calling this method with the value 1. How can I do this?
• 01-23-2010, 06:55 PM
senorbum
Quote:

Originally Posted by Malus