View Javadoc
1   /******************************************************************************
2    * GameEventMove.java - Define one player turn of moves
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2011 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.2  2011/06/15 03:39:12  dick
10   * Bearing off message needs to stuff You into the playerName.
11   *
12   * Revision 1.1  2011/05/22 05:08:59  dick
13   * All GameEvent objects are named starting with GameEvent.
14   *
15   * Revision 1.6  2011/05/21 22:50:29  dick
16   * Fix parsing for different move types.
17   * Last move of game doesn't have a period.
18   *
19   * Revision 1.5  2011/05/21 05:11:59  dick
20   * Move is a Life.Persistent object.
21   *
22   * Revision 1.4  2011/05/17 23:15:23  dick
23   * We need to set and getDirection to know which direction this move is going.
24   * If it's a Bar or Home, we need to know which Bar or Home to use.
25   *
26   * Revision 1.3  2011/05/16 21:20:25  dick
27   * toString() gives some helpful debug info.
28   *
29   * Revision 1.2  2011/05/16 11:35:11  dick
30   * GameEvent is an abstract class, not an interface, so we can have some common methods.
31   *
32   * Revision 1.1  2011/05/15 02:16:39  dick
33   * Move the GameEvent objects to c.b.f.domain.gameEvent.
34   *
35   * Revision 1.3  2011/05/14 04:43:01  dick
36   * I needed to deal with some lightweight Boards.
37   * So finally make the primary document Document
38   * and demote just the Board handling to a domain object.
39   *
40   * Revision 1.2  2011/05/13 14:23:10  dick
41   * Manage one turn's worth of moves.  "You move 6-2 3-1".
42   *
43   */
44  
45  /* 
46   * This program is free software: you can redistribute it and/or modify
47   * it under the terms of the GNU General Public License as published by
48   * the Free Software Foundation, either version 3 of the License, or
49   * (at your option) any later version.
50   *
51   * This program is distributed in the hope that it will be useful,
52   * but WITHOUT ANY WARRANTY; without even the implied warranty of
53   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54   * GNU General Public License for more details.
55   *
56   * You should have received a copy of the GNU General Public License
57   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
58   *
59   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
60   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
61   * 
62   */
63  package com.buckosoft.fibs.domain.gameEvent;
64  
65  import com.buckosoft.fibs.domain.Board;
66  
67  
68  
69  /** Define one player move.  i.e. "You move 6-2 8-2 ."
70   * @author Dick Balaska
71   * @since 2011/05/11
72   * @version $Revision$ <br> $Date$
73   * @see com.buckosoft.fibs.BuckoFIBS.GameManager
74   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/domain/gameEvent/GameEventMove.java">cvs GameEventMove.java</a>
75   */
76  public class GameEventMove extends GameEvent {
77  
78  	private	int[]	moves;
79  	private	int		direction;
80  
81  	@Override
82  	public Type getType() {
83  		return(Type.Move);
84  	}
85  
86  	@Override
87  	public Life getLife() {
88  		return(Life.Persistent);
89  	}
90  
91  	/** Get an array of ints that represents this turn's dice moves.
92  	 * Each die is a pair of ints, Start point and end point.
93  	 * Special values are 0 for home and 25 for bar.
94  	 * @return The moves
95  	 */
96  	public int[]	getMoves() {
97  		return(moves);
98  	}
99  
100 	/**
101 	 * @return the direction
102 	 */
103 	public int getDirection() {
104 		return direction;
105 	}
106 
107 	/**
108 	 * @param direction the direction to set
109 	 */
110 	public void setDirection(int direction) {
111 		this.direction = direction;
112 	}
113 
114 	/** Parse one of the move strings. <br>
115 	 * '<code>lollilalla moves 1-7 7-9 . </code><br>
116 	 * '<code>The only possible move is 5-off 5-off 5-off 4-off . </code><br>
117 	 * '<code>mpopj can't move. </code><br>
118 	 * '<code>BlunderBot_IV moves 24-off 24-off 24-off </code>' (note: last move of game has no period)<br>
119 	 * '<code>jcapell moves 1-off .</code>
120 	 * 
121 	 * @param s The fibs string.
122 	 */
123 	public void parse(String s) {
124 		s = s.trim();		// remove possible trailing space.
125 		if (s.charAt(s.length()-1) == '.')
126 			s = s.substring(0, s.length()-1);
127 		s = s.trim();
128 		String[] ss = s.split(" ");
129 		playerName = ss[0];
130 		moves = new int[(ss.length-2)*2];
131 		int	k = 0;
132 		for (int i=2; i<ss.length; i++) {
133 			String[] st = ss[i].split("-");
134 			if (st.length != 2)
135 				throw new RuntimeException("Bad move: '" + s + "' st='" + ss[i] + "'");
136 			for (int j=0; j<st.length; j++) {
137 				if (st[j].equals("bar"))
138 					moves[k] = Board.Bar;
139 				else if (st[j].equals("off"))
140 					moves[k] = Board.Home;
141 				else {
142 					try {
143 						moves[k] = Integer.parseInt(st[j]);
144 					} catch (NumberFormatException e) {
145 						//e.printStackTrace();
146 						moves[k] = 0;
147 					}
148 				}
149 				k++;
150 			}
151 		}
152 	}
153 	
154 	/** Parse a move that comes in different.
155 	 * @param s 'Bearing off: 6 o 2 o '
156 	 */
157 	public void parseBearingOff(String s) {
158 		this.playerName = "You";
159 		String ss[] = s.split(" ");
160 		moves = new int[ss.length-2];
161 		for (int k=0, i=2; i<ss.length; i+=2, k+=2) {
162 			moves[k] = Integer.parseInt(ss[i]);
163 			moves[k+1] = Board.Home;
164 		}
165 	}
166 
167 	public String	toString() {
168 		String s = this.playerName + " move";
169 		for (int i=0; i<moves.length; i+=2)
170 			s += " " + moves[i] + "-" + moves[i+1];
171 		
172 		return(s);
173 	}
174 }