View Javadoc

1   /*
2    * AI Soccer Project - network gaming environment for AI warriors.
3    * Copyright (C) 2001-2004  Marcin Werla, Pawel Widera
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, you can find it here:
17   * http://www.gnu.org/licenses/gpl.html
18   */
19  
20  package aigames.soccer.field.constructor;
21  
22  import aigames.soccer.field.FieldPoint;
23  
24  import java.awt.Point;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  
29  
30  /***
31   * Field costructor for typical soccer game field.
32   * @version $Id: StandardConstructor.java,v 1.10 2004/05/08 21:55:30 mwerla Exp $
33   */
34  public class StandardConstructor implements Constructor {
35      private int fieldWidth;
36      private int fieldHeight;
37      private int goalHeight;
38      private Collection gameOverPoints;
39  
40      /***
41       * Class constructor.
42       * @param width Field width.
43       * @param height Field height.
44       * @param goal Goal height.
45       * @throws IllegalArgumentException Thrown when field sizes are wrong.
46       */
47      public StandardConstructor(int width, int height, int goal)
48          throws IllegalArgumentException {
49          fieldWidth = width;
50          fieldHeight = height;
51          goalHeight = goal;
52  
53          if (((fieldHeight % 2) != 0) || ((fieldWidth % 2) != 0) ||
54                  ((goalHeight % 2) != 0) || (goalHeight >= fieldHeight)) {
55              throw new IllegalArgumentException(
56                  "Field dimensions should be even numbers!");
57          }
58  
59          gameOverPoints = new ArrayList();
60  
61          for (int i = (fieldHeight - goalHeight) / 2;
62                  i <= ((fieldHeight + goalHeight) / 2); i++) {
63              gameOverPoints.add(new Point(0, i));
64              gameOverPoints.add(new Point(fieldWidth + 2, i));
65          }
66      }
67  
68      /***
69       * @see aigames.soccer.field.constructor.Constructor#getNewField()
70       */
71      public FieldPoint[][] getNewField() {
72          FieldPoint[][] field = new FieldPoint[fieldWidth + 3][fieldHeight + 1];
73  
74          //top border
75          for (int i = 1; i <= fieldWidth; i++) {
76              field[i][0] = new FieldPoint(new int[] {
77                          FieldPoint.NO_EDGE, FieldPoint.NO_EDGE,
78                          FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE
79                      });
80          }
81  
82          //bottom border
83          for (int i = 2; i <= fieldWidth; i++) {
84              field[i][fieldHeight] = new FieldPoint(new int[] {
85                          FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE,
86                          FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE
87                      });
88          }
89  
90          //upper part
91          for (int i = 1; i < ((fieldHeight - goalHeight) / 2); i++) {
92              //left border
93              field[1][i] = new FieldPoint(new int[] {
94                          FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
95                          FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
96                      });
97  
98              //right border
99              field[fieldWidth + 1][i] = new FieldPoint(new int[] {
100                         FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE,
101                         FieldPoint.NO_EDGE, FieldPoint.NO_EDGE
102                     });
103         }
104 
105         //lower part
106         for (int i = ((fieldHeight + goalHeight) / 2) + 1; i < fieldHeight;
107                 i++) {
108             //left border
109             field[1][i] = new FieldPoint(new int[] {
110                         FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
111                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
112                     });
113 
114             //right border
115             field[fieldWidth + 1][i] = new FieldPoint(new int[] {
116                         FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE,
117                         FieldPoint.NO_EDGE, FieldPoint.NO_EDGE
118                     });
119         }
120 
121         //the middle of a field
122         for (int i = 2; i <= fieldWidth; i++)
123             for (int j = 1; j < fieldHeight; j++) {
124                 field[i][j] = new FieldPoint(new int[] {
125                             FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE,
126                             FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
127                         });
128             }
129 
130         //in goal I
131         for (int i = ((fieldHeight - goalHeight) / 2) + 1;
132                 i < ((fieldHeight + goalHeight) / 2); i++) {
133             //left border
134             field[0][i] = new FieldPoint(new int[] {
135                         FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
136                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
137                     });
138 
139             //right goal
140             field[fieldWidth + 1][i] = new FieldPoint(new int[] {
141                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE,
142                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
143                     });
144         }
145 
146         //in goal II
147         for (int i = ((fieldHeight - goalHeight) / 2) + 1;
148                 i <= ((fieldHeight + goalHeight) / 2); i++) {
149             //left goal
150             field[1][i] = new FieldPoint(new int[] {
151                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE,
152                         FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
153                     });
154 
155             //right border
156             field[fieldWidth + 2][i] = new FieldPoint(new int[] {
157                         FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE,
158                         FieldPoint.NO_EDGE, FieldPoint.NO_EDGE
159                     });
160         }
161 
162         //left in goal top corner
163         field[1][(fieldHeight - goalHeight) / 2] = new FieldPoint(new int[] {
164                     FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
165                     FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE
166                 });
167 
168         //left border goal top corner
169         field[0][(fieldHeight - goalHeight) / 2] = new FieldPoint(new int[] {
170                     FieldPoint.NO_EDGE, FieldPoint.NO_EDGE,
171                     FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE
172                 });
173 
174         //left border goal bottom corner
175         field[0][(fieldHeight + goalHeight) / 2] = new FieldPoint(new int[] {
176                     FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
177                     FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE
178                 });
179 
180         //right in goal top corner
181         field[fieldWidth + 1][(fieldHeight - goalHeight) / 2] = new FieldPoint(new int[] {
182                     FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE,
183                     FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE
184                 });
185 
186         //right in goal bottom corner
187         field[fieldWidth + 1][(fieldHeight + goalHeight) / 2] = new FieldPoint(new int[] {
188                     FieldPoint.BLANK_EDGE, FieldPoint.BLANK_EDGE,
189                     FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE
190                 });
191 
192         //right bottom border corner
193         field[fieldWidth + 1][fieldHeight] = new FieldPoint(new int[] {
194                     FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE,
195                     FieldPoint.NO_EDGE, FieldPoint.NO_EDGE
196                 });
197 
198         //left bottom border corner
199         field[1][fieldHeight] = new FieldPoint(new int[] {
200                     FieldPoint.FIELD_EDGE, FieldPoint.BLANK_EDGE,
201                     FieldPoint.FIELD_EDGE, FieldPoint.NO_EDGE
202                 });
203 
204         for (int i = 0; i < field.length; i++) {
205             for (int j = 0; j < field[i].length; j++) {
206                 if (field[i][j] == null) {
207                     field[i][j] = new FieldPoint(new int[] {
208                                 FieldPoint.NO_EDGE, FieldPoint.NO_EDGE,
209                                 FieldPoint.NO_EDGE, FieldPoint.NO_EDGE
210                             });
211                 }
212             }
213         }
214 
215         return field;
216     }
217 
218     /***
219      * @see aigames.soccer.field.constructor.Constructor#getStartingPosition()
220      */
221     public Point getStartingPosition() {
222         return new Point((fieldWidth / 2) + 1, fieldHeight / 2);
223     }
224 
225     /***
226      * @see aigames.soccer.field.constructor.Constructor#isGameOverPosition(java.awt.Point)
227      */
228     public boolean isGameOverPosition(Point position) {
229         return gameOverPoints.contains(position);
230     }
231 
232     /***
233      * @see aigames.soccer.field.constructor.Constructor#isGoalPosition(Point)
234      */
235     public int isGoalPosition(Point position) {
236         int result = 0;
237 
238         if (isGameOverPosition(position)) {
239             if (position.x == 0) {
240                 return -1;
241             } else if (position.x == (fieldWidth + 2)) {
242                 return 1;
243             }
244         }
245 
246         return result;
247     }
248 }