Results 1 to 11 of 11
- 01-23-2010, 01:11 PM #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.
I can't seem to solve this problem, and I'm really in a hurry, so please help.Last edited by Malus; 01-23-2010 at 01:12 PM. Reason: spelling mistake
-
Code would help others to figure out just what is wrong.
- 01-23-2010, 01:40 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Reply
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.
-
So dive in and try. You'll learn a whole lot more by doing this.
This smells of a 2-dimensional array.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:08 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Reply
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?
-
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, 03:36 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Reply
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
-
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, 05:05 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Reply
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, 05:38 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
Reply
Okay follow up question:
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, 05:55 PM #11
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
[SOLVED] Problem with program(getters/setters,arrayList).
By Pierced1 in forum New To JavaReplies: 1Last Post: 02-19-2009, 03:50 AM -
Test Lead Java positions
By aravindln in forum Jobs OfferedReplies: 0Last Post: 09-19-2008, 12:55 PM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM -
How To Add Fields And Generating Getter-Setters
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks