Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Enum Initialization Error  (Read 2526 times)
0 Members and 1 Guest are viewing this topic.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Posted 2012-09-13 15:15:27 »

Fun... I am asking a noob question in java :O Smiley

It's about Enums, I started using them recently. I have a problem with this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   protected static enum Side {
      LEFT("BOTTOM", "TOP"),
      TOP("LEFT", "RIGHT"),
      RIGHT("TOP", "BOTTOM"),
      BOTTOM("RIGHT", "LEFT");

      public final Side toLeft;
      public final Side toRight;

      Side(String l, String r) {
         toLeft = Side.valueOf(l);
         toRight = Side.valueOf(r);
      }
   }
(Inner-Class-Enum)

I want to store the references to the sides of each side. So the neighbour of top is left to the left and right to the right...

The sides look like this:

......TOP.......
.L............R.
.E............I.
.F............G.
.T............H.
..............T.
.....BOTTOM.....

How would you solve this problem?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline UprightPath
« Reply #1 - Posted 2012-09-13 15:43:36 »

You can't do what you just tried. There are several options though as to how. None of them will probably be what you want, but it's the way that they work. Especially if you want to include the 'final' functionality.

Best way around it? Create an initializer method that you call where-ever other things get initialized to set up these relations. Then get them through a method (Which causes these variables to act as final for this purpose, since an enum cannot be extended.)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
protected static enum Side {
    LEFT, TOP, RIGHT, BOTTOM;
    public static void initialize() {
        LEFT.toLeft = BOTTOM;
        LEFT.toRight = TOP;
        // Etc.
    }
    private Side toLeft;
    private Side toRight;

    public Side getToLeft() {
        return toLeft;
    }

    public Side getToRight() {
        return toRight;
    }
}

Offline Best Username Ever

Junior Duke





« Reply #2 - Posted 2012-09-13 15:46:22 »

I am guessing that the problem comes from the fact that the method you are calling assumes to have a reference to all the other enum constants. Since it's used in the constructor, the references it needs do not exist yet.

Here is one alternative. You could also use a switch statement with enum consant values or an enum map.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
  public enum Side
  {
    Left { public Side cw() { return Top; } public Side ccw() { return Bottom; } },
    Top { public Side cw() { return Right; } public Side ccw() { return Left; } },
    Right { public Side cw() { return Bottom; } public Side ccw() { return Top; } },
    Bottom { public Side cw() { return Left; } public Side ccw() { return Right; } };

    public abstract Side cw();
    public abstract Side ccw();
  }


Edit: UprightPath submitted something while I was still writing, but the only change I made was to correct a spelling error.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2012-09-13 16:01:16 »

and another possibility

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public enum Side
{
  TOP, RIGHT, BOTTOM, LEFT;//Note that the sides are in clockwise order
 
  public Side getClockWise()
  {
    return values()[(ordinal()+1)%4];
  }
 
  public Side getCounterClockWise()
  {
    return values()[Math.abs((ordinal()-1)%4)];
  }
}


ps: one could ofcourse also hardcode the to idices per side
Offline actual

JGO Coder


Medals: 23



« Reply #4 - Posted 2012-09-13 16:03:38 »

You can use a static initializer to keep the entire definition within the enum and retrieve without having to recalculate anything each time. I made toLeft and toRight private and added accessor methods. They are no longer final but you can't change the values any how.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
enum Side {
   
   
    LEFT,
    TOP,
    RIGHT,
    BOTTOM;

    private  Side toLeft;
    private  Side toRight;
   
    private void setSides(Side leftSide, Side rightSide) {
       toLeft = leftSide;
       toRight = rightSide;
    }
   
    public Side left() {
       return toLeft;
    }
   
    public Side right() {
       return toRight;
    }


    static {
       
       LEFT.setSides(BOTTOM,TOP);
       RIGHT.setSides(TOP, BOTTOM);
       TOP.setSides(LEFT,RIGHT);
       BOTTOM.setSides(RIGHT,LEFT);
    }
}


then

1  
2  
      System.out.println(Side.LEFT.left() == Side.RIGHT);  // prints false
      System.out.println(Side.LEFT.left() == Side.BOTTOM);  // prints true



Eclipse complained when I tried making the enum static and protected.





Offline Riven
« League of Dukes »

JGO Overlord


Medals: 817
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-09-13 16:04:01 »

and another possibility

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public enum Side
{
  TOP, RIGHT, BOTTOM, LEFT;//Note that the sides are in clockwise order
 
  public Side getClockWise()
  {
    return values()[(ordinal()+1)%4];
  }
 
  public Side getCounterClockWise()
  {
-    return values()[Math.abs((ordinal()-1)%4)]; // bug!
+    return values()[(ordinal()+3)%4];
  }
}


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Danny02
« Reply #6 - Posted 2012-09-13 16:06:35 »

ah yes sry^^
quite interesting how many different implementations are possible for this simple problem Smiley
Offline actual

JGO Coder


Medals: 23



« Reply #7 - Posted 2012-09-13 16:13:09 »

quite interesting how many different implementations are possible for this simple problem Smiley

Yes it is. I would also be tempted to add something like :
1  
2  
3  
4  
5  
6  
7  
    public static Side leftOf(Side side) {
       return side.toLeft;
    }
   
    public static Side rightOf(Side side) {
       return side.toRight;
    }


So I could do things like:

1  
Side neighbor = Side.leftOf(enemy.facingDirection());


which reads more naturally to me (although that's just preference).
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-09-13 16:21:30 »

quite interesting how many different implementations are possible for this simple problem Smiley
Yes... I have many to choose from Grin
I think I'll choose yours, because it's the most beautiful in my eyes Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #9 - Posted 2012-09-13 16:25:03 »

Eclipse complained when I tried making the enum static and protected.
It's nested Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #10 - Posted 2012-09-13 17:01:29 »

You like it? then another one, perhaps even more elegant Smiley

UPDATED:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public enum Direction
{
  Front, Right, Back, Left;

  public Direction get(Direction d)
  {
    return values()[(d.ordinal() + ordinal()) % 4];
  }
}


So you can write entityDirection.get(Back) to get the Direction which is in the opposit direction of the entity or entDir.get(Left) the dir which is left ...
Offline UprightPath
« Reply #11 - Posted 2012-09-13 17:48:03 »

I've never been a fan of the static code chunk, if only because it's hard to tell when it gets executed. For this, I figure it's not too much of a problem, but I tend to not suggest it. xD

Offline actual

JGO Coder


Medals: 23



« Reply #12 - Posted 2012-09-13 18:06:02 »

Matheus, Danny,

While the code is nice and compact, in this specific case, I think doing something more like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
enum Side {
   
    LEFT, TOP,  RIGHT,  BOTTOM;

    public Side left() {
       return toLeft;
    }
   
    public Side right() {
       return toRight;
    }

    public Side opposite() {
      return oppositeOf;
   }

    private Side toLeft, toRight, oppositeOf;
   
    private void setSides(Side leftSide, Side rightSide, Side oppositeSide) {
       toLeft = leftSide;
       toRight = rightSide;
       oppositeOf = oppositeSide;
    }

    static {
       LEFT.setSides(BOTTOM,TOP,RIGHT);
       RIGHT.setSides(TOP, BOTTOM,LEFT);
       TOP.setSides(LEFT,RIGHT,BOTTOM);
       BOTTOM.setSides(RIGHT,LEFT,TOP);
    }
}


is simpler in that you don't have to care about the order the Enum values are defined in and you don't have to do calculations, array lookups, or retrieving ordinal positions to get what can be defined simply once and for all. The downside is storing 3 enum values for each enum which in this case is nothing.  If the Enum had a lot more values (N,NNE,NE,ENE,E, etc..) or had a lot more relationships to track then the more compact code would look more attractive.

Quote
I've never been a fan of the static code chunk, if only because it's hard to tell when it gets executed. For this, I figure it's not too much of a problem, but I tend to not suggest it. xD
I got the static initializer idea from a stack overflow post. I was originally thinking along your lines but what I like about this is that the definition is complete within itself. You don't have to remember to call a method from some where else. Then again, I tend to use statics more often than other Java programmers I've worked with.

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2012-09-13 19:21:15 »

You guys are VERY fun Smiley I like this community Cheesy

Even if It's not about entities (It's about planet "sides"), this has helped me a lot Smiley thank you!

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!