View Javadoc

1   /**
2    * This program is free software; you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License version 2 as 
4    * published by the Free Software Foundation;
5    */
6   
7   package net.curre.prefcount.gui.type;
8   
9   /**
10   * This enum represents player's place.
11   * <p/>
12   * Created date: May 28, 2008
13   *
14   * @author Yevgeny Nyden
15   */
16  public enum Place {
17  
18    /** East side player. */
19    EAST(0, "pref.scoreboard.eastLetter", "pref.dialog.east"),
20  
21    /** South side player. */
22    SOUTH(1, "pref.scoreboard.southLetter", "pref.dialog.south"),
23  
24    /** West side player. */
25    WEST(2, "pref.scoreboard.westLetter", "pref.dialog.west"),
26  
27    /** North side player. */
28    NORTH(3, "pref.scoreboard.northLetter", "pref.dialog.north");
29  
30    /** Place's short string resource key. */
31    public final String shortKey;
32  
33    /** Place's long string resource key. */
34    public final String longKey;
35  
36    /** Index of this place relative to other places. */
37    public final int index;
38  
39    /** Three player game places. */
40    public static final Place[] THREE_PLAYERS = {EAST, SOUTH, WEST};
41  
42    /** Four player game places. */
43    public static final Place[] FOUR_PLAYERS = {NORTH, EAST, SOUTH, WEST};
44  
45    /**
46     * Constructor.
47     *
48     * @param index    index of this place relative to other places.
49     * @param shortKey place's short string resource key.
50     * @param longKey  place's long string resource key.
51     */
52    private Place(int index, String shortKey, String longKey) {
53      this.shortKey = shortKey;
54      this.index = index;
55      this.longKey = longKey;
56    }
57  
58    /**
59     * Given number of player, returns array of places
60     * for this game.
61     *
62     * @param playerNum number of player in the game.
63     * @return array of places.
64     */
65    public static Place[] getPlaces(int playerNum) {
66      if (playerNum == 3) {
67        return THREE_PLAYERS;
68      } else if (playerNum == 4) {
69        return FOUR_PLAYERS;
70      } else {
71        throw new IllegalArgumentException("Only 3 and 4 players supported!");
72      }
73    }
74  
75    /**
76     * Fetches a place for the given index.
77     *
78     * @param index place's index.
79     * @return place for the given index.
80     */
81    public static Place getPlaceForIndex(int index) {
82      switch (index) {
83        case 0:
84          return EAST;
85  
86        case 1:
87          return SOUTH;
88  
89        case 2:
90          return WEST;
91  
92        case 3:
93          return NORTH;
94  
95        default:
96          throw new IllegalArgumentException("Unable to determine place for index " + index + "!");
97      }
98    }
99  
100   /**
101    * Gets all whist places that the given player.
102    *
103    * @param place      player's place.
104    * @param numPlayers total number of player in the game.
105    * @return whist whist places for all opponents of the given player.
106    */
107   public static Place[] getOtherPlayersWhistPlaces(Place place, int numPlayers) {
108     Place[] places = new Place[numPlayers - 1];
109     switch (place) {
110       case EAST:
111         places[0] = SOUTH;
112         places[1] = WEST;
113         break;
114 
115       case SOUTH:
116         places[0] = EAST;
117         places[1] = WEST;
118         break;
119 
120       case WEST:
121         places[0] = EAST;
122         places[1] = SOUTH;
123         break;
124 
125       case NORTH:
126         places[0] = EAST;
127         places[1] = SOUTH;
128         places[2] = WEST;
129         break;
130 
131       default:
132         throw new IllegalArgumentException("Illegal place " + place + "!");
133     }
134     if (numPlayers == 4 && place != NORTH) {
135       places[2] = NORTH;
136     }
137 
138     return places;
139   }
140 
141 }