Results 1 to 11 of 11
  1. #1
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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 02:12 PM. Reason: spelling mistake

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Malus View Post
    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.

  5. #5
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  7. #7
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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

  10. #10
    Malus is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default 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?

  11. #11
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by Malus View Post
    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?
    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

Similar Threads

  1. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 02:47 AM
  2. Replies: 1
    Last Post: 02-19-2009, 04:50 AM
  3. Test Lead Java positions
    By aravindln in forum Jobs Offered
    Replies: 0
    Last Post: 09-19-2008, 01:55 PM
  4. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 PM
  5. How To Add Fields And Generating Getter-Setters
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-31-2007, 12:13 AM

Posting Permissions

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