# Thread: Positions and values, getters, setters

1. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

## 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.

Last edited by Malus; 01-23-2010 at 02:12 PM. Reason: spelling mistake

2. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

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.

3. 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.

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.

4. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

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?

5. 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?

6. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

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

7. 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.

8. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

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

9. Member
Join Date
Jan 2010
Posts
6
Rep Power
0

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?

10. Member
Join Date
Aug 2009
Posts
76
Rep Power
0
Originally Posted by 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?
Make a method that iterates over the map and store the keys in a set that you end up returning
Last edited by senorbum; 01-23-2010 at 06:56 PM. Reason: Typo

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•