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

1    /** 
2     * @(#)MoveResult.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    * Class represents move result 
11    * @author Anthony Yakovlev 
12    */ 
13   public class MoveResult { 
14       /** 
15        * Is the game finished 
16        */ 
17       public boolean isGameOver = false; 
18        
19       /** 
20        * Winner index 
21        */ 
22       public int     winner = 0; 
23        
24       /** 
25        * Line from which the stones were taken 
26        */ 
27       public int     takenFrom = 0; 
28        
29       /** 
30        * The amount of stones that were taken 
31        */ 
32       public int     nTaken = 0; 
33        
34       /** 
35        * Nobody is the winner on current move 
36        */ 
37       public static final int WINNER_NOBODY = 0; 
38        
39       /** 
40        * Computer is the winner 
41        */ 
42       public static final int WINNER_COMPUTER = 1; 
43        
44       /** 
45        * Player is the winner 
46        */ 
47       public static final int WINNER_PLAYER = 2; 
48        
49       /** 
50        * Default constructor of class 
51        */ 
52       public MoveResult () { 
53           isGameOver = false; 
54           winner = WINNER_NOBODY; 
55       } 
56        
57       /** 
58        * Say that the game is over or not  
59        * @param b - flag 
60        */ 
61       public void setGameOver(boolean b) { 
62           this.isGameOver = b;         
63       } 
64        
65       /** 
66        * Set id of a winner 
67        * @param id - winner 
68        */ 
69       public void setWinner (int id) { 
70           winner = id; 
71       } 
72        
73       /** 
74        * Take stones 
75        * @param takeFrom - where to take 
76        * @param nTaken - how much to take 
77        */ 
78       public void take (int takeFrom, int nTaken) { 
79           this.takenFrom = takeFrom; 
80           this.nTaken = nTaken; 
81       } 
82        
83       /** 
84        * Performs array to structure result conversion 
85        * @param move the array of amount of stones in lines 
86        * @return the parsed structure of null if error 
87        */ 
88       public static MoveResult getMoveResult (int [] move) { 
89           // check correctness 
90           int posMove = -1; 
91           for (int i = 0; i < move.length; i++) { 
92               if (move [i] > 0) { 
93                   if (posMove != -1) { 
94                       return null;                     
95                   } 
96                   posMove = i; 
97               } else if (move [i] < 0){ 
98                   return null; 
99               } 
100          } 
101           
102          // parse array in struct 
103          if (posMove == -1) { 
104              return null; 
105          } else { 
106              MoveResult result = new MoveResult(); 
107              result.setWinner(WINNER_NOBODY); 
108              result.setGameOver(false); 
109              result.take(posMove, move [posMove]); 
110              return result; 
111          } 
112      } 
113  } 
114