Results 1 to 14 of 14
  1. #1
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Give variable an alias

    Is it possible to give a variable, like, for example, a string in a string array a secondary label that can be use to call upon it?

    If I had a string array of 64 strings that are meant for form a 8x8 grid, and the array held an "object name" string, can I call it by another name? I want to give String[0] to be called something like 11 or 1-1, String[1] 21 or 2-1, String[2] 31 or 3-1, String[8] 12 or 1-2.

    Is that possible?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'm not sure what you are trying to achieve but perhaps a Map or using Objects is what you are looking for.

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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    NOOOOOoooooooooooooooooooooooooo!

    Actually if all you want to do is take a 2 digit value (34) to insert a hyphen between the digits (3-4) then just write a method that takes a String and returns a formatted String.

  5. #5
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    @Fubarable

    Does that mean i'll have two distinct values associated with one variable? If so, how to I get the value from just one and not the other?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    No you cannot associate 2 distinct values with the one variable.

    How about you explain what you are really trying to do not what your attempted solution is (which is what you have asked about here).

  7. #7
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    For fun I am setting up a chess game. I want a square to have two values, a location on the grid in either xx or x-x format, and either a null or a piece name.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Then try one of my first suggestions. Use a class to represent a Square. Each Square object can have a co-ord in xx format, a co-ord in x-x format, a piece, a colour etc.

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

    Default

    Yeah, you definitely want a two dimensional array of something, perhaps of a ChessSquare object (a class that you create as Junky suggests, that can be empty or hold a ChessPiece object), with one index to represent the rank and the other to represent the file. This will be part of your non-GUI model. Then when you want to hook this up to your chess GUI, you'll likely have a two-dimensional array of JLabel or something similar to give the visual representation of your board.
    Last edited by Fubarable; 07-21-2011 at 06:13 AM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As Fubarable says, use of 2-dimensional array make your life easy. Specially in future if you want to do any enhancement on your code.

  11. #11
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    After looking up 2 dimensional arrays, it feels like that may be the easiest solution for my current understanding of Java.

    So maybe String[8][8]? Would that create a row/column array? String[1][1] would be a spot on the grid, and it's value would be Black Rook. String[2][1] would be Black Knight, String[1][2] would be Black Pawn.

  12. #12
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default

    If 2 dimensional arrays work like I described above, I think i'll use the string[] to define only what exists in the square, and a separate method for checking validity of the move. The gui will grab the value out of the array to place a picture in a given box.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,661
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by SiX View Post
    After looking up 2 dimensional arrays, it feels like that may be the easiest solution for my current understanding of Java.

    So maybe String[8][8]? Would that create a row/column array? String[1][1] would be a spot on the grid, and it's value would be Black Rook. String[2][1] would be Black Knight, String[1][2] would be Black Pawn.
    Beware that arrays start their index values at zero; so an array with eight elements has the index values 0, 1, 2, 3, 4, 5, 6 and 7.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    If you want to practice OO design, write a Board class with a method getSquare(int rank, char file) that returns a Square. Internally, you might do as Fubarable suggested and use a Square[][]. The Square class could have methods like containsPiece() and getPiece() that returns a Piece. Piece could have methods like getColor() and getType(). You could even subclass Piece and write the rules of their movement into each subclass with a method like canMoveTo(int rank, char file).
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  2. Why does this give me an error?
    By AlleDingeAndern in forum New To Java
    Replies: 4
    Last Post: 02-03-2011, 11:51 PM
  3. import alias
    By Pacerier in forum New To Java
    Replies: 5
    Last Post: 11-23-2010, 07:27 PM
  4. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  5. Give me some Assigment please
    By mindblaster in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 09:05 PM

Posting Permissions

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