1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package aigames.soccer;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28 /***
29 * This class represents single move (i.e. sequence of steps) in soccer game.
30 * @version $Id: Move.java,v 1.6 2004/05/08 21:55:29 mwerla Exp $
31 */
32 public class Move {
33 private static final Map translationMap = new HashMap();
34
35 /*** Step north. */
36 public static final Integer N = new Integer(1);
37
38 /*** Step north-east. */
39 public static final Integer NE = new Integer(2);
40
41 /*** Step east. */
42 public static final Integer E = new Integer(3);
43
44 /*** Step south-east. */
45 public static final Integer SE = new Integer(4);
46
47 /*** Step south. */
48 public static final Integer S = new Integer(5);
49
50 /*** Step south-west. */
51 public static final Integer SW = new Integer(6);
52
53 /*** Step west. */
54 public static final Integer W = new Integer(7);
55
56 /*** Step north-west. */
57 public static final Integer NW = new Integer(8);
58
59 static {
60 translationMap.put(S, new int[] { 0, 1 });
61 translationMap.put(SE, new int[] { 1, 1 });
62 translationMap.put(E, new int[] { 1, 0 });
63 translationMap.put(NE, new int[] { 1, -1 });
64 translationMap.put(N, new int[] { 0, -1 });
65 translationMap.put(NW, new int[] { -1, -1 });
66 translationMap.put(W, new int[] { -1, 0 });
67 translationMap.put(SW, new int[] { -1, 1 });
68 }
69
70 private Collection steps = new ArrayList();
71
72 /***
73 * Creates move with steps passed as parameter.
74 * @param steps Array of steps that move consists of.
75 * @throws InvalidMoveException Thrown when steps are invalid.
76 */
77 public Move(int[] steps) throws InvalidMoveException {
78 addStepSequence(steps);
79 }
80
81 /***
82 * Creates move with steps passed as parameter.
83 * @param steps Array of steps that move consists of.
84 * @throws InvalidMoveException Thrown when steps are invalid.
85 */
86 public Move(Integer[] steps) throws InvalidMoveException {
87 addStepSequence(steps);
88 }
89
90 /***
91 * Returns geometric translation corresponding to given direction.
92 * @param direction Step direction.
93 * @return geometric translation corresponding to given direction.
94 */
95 public static int[] getTranslation(Integer direction) {
96 return (int[]) translationMap.get(direction);
97 }
98
99 /***
100 * Adds sequnece of steps to move instance.
101 * @param steps Array of steps to add.
102 * @throws InvalidMoveException Thrown when steps are invalid.
103 */
104 public void addStepSequence(Integer[] steps) throws InvalidMoveException {
105 for (int i = 0; i < steps.length; i++) {
106 addStep(steps[i]);
107 }
108 }
109
110 /***
111 * Adds single step to move instance.
112 * @param step Step to add.
113 * @throws InvalidMoveException Thrown when step is invalid.
114 */
115 public void addStep(Integer step) throws InvalidMoveException {
116 if (translationMap.containsKey(step)) {
117 steps.add(step);
118 } else {
119 throw new InvalidMoveException("Invalid step value");
120 }
121 }
122
123 /***
124 * Adds single step to move instance.
125 * @param step Step to add.
126 * @throws InvalidMoveException Thrown when step is invalid.
127 */
128 public void addStep(int step) throws InvalidMoveException {
129 addStep(new Integer(step));
130 }
131
132 /***
133 * Adds sequnece of steps to move instance.
134 * @param steps Array of steps to add.
135 * @throws InvalidMoveException Thrown when steps are invalid.
136 */
137 public void addStepSequence(int[] steps) throws InvalidMoveException {
138 for (int i = 0; i < steps.length; i++) {
139 addStep(new Integer(steps[i]));
140 }
141 }
142
143 /***
144 * Returns Collection of steps in this move instance.
145 * @return Collection of steps in this move instance.
146 */
147 public Collection getSteps() {
148 return this.steps;
149 }
150 }