F:\java\knim-game\sources\ru\ifmo\knim\main\GameInfo.java

1    /** 
2     * @(#)GameInfo.java 
3     *  
4     *  Copyright Anthony Yakovlev <yakovlev@rain.ifmo.ru> and Michail Lukin <michail@users.msn.com> 
5     */ 
6     
7    package ru.ifmo.knim.main; 
8     
9    /** 
10    * Represents game info 
11    * @author Anthony Yakovlev 
12    */ 
13   public class GameInfo { 
14       /** 
15        * Current state of game 
16        */ 
17       private int lines []; 
18        
19       /** 
20        * Limit values for all lines 
21        */ 
22       private int rlines []; 
23        
24       /** 
25        * Amount of lines of stones in current game 
26        */ 
27       private int nLines; 
28        
29       /** 
30        * The value of difficulty 
31        */ 
32       private int difficulty; 
33        
34       /** 
35        * The amount of phora stones left  
36        */ 
37       private int badMovesLeft; 
38        
39       /** 
40        * "Easy" level of difficulty 
41        */ 
42       public static final int DIFFICULTY_EASY = 0; 
43        
44       /** 
45        * "Medium" level of difficulty 
46        */ 
47       public static final int DIFFICULTY_MEDIUM = 1; 
48        
49       /** 
50        * "Hard" level of difficulty 
51        */ 
52       public static final int DIFFICULTY_HARD = 2; 
53        
54       /** 
55        * No difficulty at all 
56        */ 
57       public static final int DIFFICULTY_NONE = -239; 
58        
59       MoveResult  latestMove; 
60        
61       /** 
62        * Creates a gameinfo object 
63        * @param nLines - amount of lines in a game 
64        */ 
65       public GameInfo (int nLines) { 
66           lines = new int [nLines]; 
67           this.nLines = nLines; 
68           restartGame(); 
69           latestMove = new MoveResult(); 
70           difficulty = DIFFICULTY_EASY; 
71       } 
72        
73       /** 
74        * non-default contructor 
75        * @param stones 
76        */ 
77       public GameInfo (int [] stones) { 
78           setStones(stones); 
79           latestMove = new MoveResult(); 
80           difficulty = DIFFICULTY_EASY; 
81       } 
82        
83       /** 
84        * Gets array of fish lines 
85        * @return 
86        */ 
87       public final int [] getLines () { 
88           return (int [])lines.clone();        
89       } 
90        
91       /** 
92        * Set array of stones as rules for new game 
93        * @param stones 
94        */ 
95       public void setStones(int [] stones) { 
96           nLines = stones.length; 
97           lines = new int [nLines]; 
98           rlines = new int [nLines]; 
99           System.arraycopy(stones, 0, lines, 0, nLines); 
100          System.arraycopy(stones, 0, rlines, 0, nLines); 
101      } 
102       
103      /** 
104       * Restarts game. 
105       */ 
106      public synchronized void restartGame () { 
107          badMovesLeft = 2 - difficulty; 
108          System.arraycopy(rlines, 0, lines, 0, nLines); 
109      } 
110       
111      /** 
112       * Very cool function -- analog of compareAndSet primitive 
113       * @param difficulty 
114       * @return 
115       */ 
116      public synchronized final int setGetDifficulty (int difficulty) { 
117          if (difficulty < DIFFICULTY_EASY || difficulty > DIFFICULTY_HARD ) {         
118              return this.difficulty; 
119          } else { 
120              this.difficulty = difficulty; 
121              return difficulty; 
122          } 
123      } 
124       
125      /** 
126       * Returns the amount of lines with stones 
127       * @return value from description 
128       */ 
129      public synchronized int getLineAmount () { 
130          return nLines;       
131      } 
132       
133      /** 
134       * Returns amount of fish in a line 
135       * @param lineIdx - the line idx 
136       * @return amount of stones 
137       */ 
138      public synchronized int getFishInLine(int lineIdx) { 
139          return lines [lineIdx];              
140      } 
141       
142      /** 
143       * Takes the given amount of stones from the given line 
144       * @param lineIdx - the line idx 
145       * @param amount - amount of stones to take 
146       */ 
147      public synchronized void takeStones (int lineIdx, int amount) { 
148          lines [lineIdx] -= amount;       
149      } 
150       
151      /** 
152       * Returns the amount of phora moves left 
153       * @return the amount of phora moves 
154       */ 
155      public int getBadMoveLeft() { 
156          return badMovesLeft;         
157      } 
158       
159      /** 
160       * Sets the amount of phora moves 
161       * @param badMovesLeft the amount of phora moves 
162       */ 
163      public void setBadMovesLeft(int badMovesLeft) { 
164          this.badMovesLeft = badMovesLeft; 
165      } 
166       
167      /** 
168       * Detects whether the game is over or not 
169       * @return true if the game is over 
170       */ 
171      public boolean isGameover() { 
172          for (int i = 0; i < getLineAmount(); i++) { 
173              if (getFishInLine(i) > 0) { 
174                  return false; 
175              } 
176          } 
177          return true; 
178      } 
179  } 
180