View Javadoc
1   /**************************************************************************
2    Copyright 2005 Webstersmalley
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15   *************************************************************************/
16  /*
17   * Created on 23-Aug-2005
18   */
19  package com.webstersmalley.chessweb.model;
20  
21  /***
22   * Simple coordinate system for the position of a piece on the board.
23   * 
24   * @author Matthew Smalley
25   */
26  public final class Position implements Cloneable {
27      /*** Position along the board. */
28      private String file;
29  
30      /*** Position up the board. */
31      private int rank;
32  
33      /***
34       * @return Returns the file.
35       */
36      public String getFile() {
37          return file;
38      }
39  
40      /***
41       * @param file
42       *            The file to set.
43       */
44      public void setFile(final String file) {
45          this.file = file;
46      }
47  
48      /***
49       * @return Returns the rank.
50       */
51      public int getRank() {
52          return rank;
53      }
54  
55      /***
56       * @param rank
57       *            The rank to set.
58       */
59      public void setRank(final int rank) {
60          this.rank = rank;
61      }
62  
63      /***
64       * Default constructor.
65       */
66      public Position() {
67  
68      }
69  
70      /***
71       * Convenience constructor.
72       * 
73       * @param file
74       *            the file coordinate
75       * @param rank
76       *            the rank coordinate
77       */
78      public Position(final String file, final int rank) {
79          this.file = file;
80          this.rank = rank;
81      }
82  
83      /***
84       * Convenience constructor.
85       * 
86       * @param algebraicNotation
87       *            the algebraicNotation
88       */
89      public Position(final String algebraicNotation) {
90          String file = algebraicNotation.substring(0, 1);
91          String rank = algebraicNotation.substring(1);
92          this.file = file;
93          this.rank = new Integer(rank).intValue();
94      }
95  
96      /***
97       * Returns a String representation.
98       * 
99       * @return String the string
100      */
101     public String toString() {
102         return "" + file + rank;
103     }
104 
105     /***
106      * Return the modulus of the square number. Useful for working out
107      * blank/white of the square.
108      * 
109      * @return the modulus
110      */
111     public int getModulus() {
112         return (getFileNumber() + rank) % 2;
113     }
114 
115     /***
116      * Return the file number.
117      * 
118      * @return the file number
119      */
120     public int getFileNumber() {
121         int i = 0;
122         while (!FILE_NAMES[i].equals(file)) {
123             i++;
124         }
125         return i + 1;
126     }
127 
128     /*** Helper array for index Constructor. * */
129     private static final String[] FILE_NAMES = { 
130         "a", "b", "c", "d", "e", "f",
131             "g", "h" };
132 
133     /***
134      * Create a Position based on file and rank (0-index) coordinates.
135      * 
136      * @param file
137      *            the file
138      * @param rank
139      *            the rank
140      */
141     public Position(final int file, final int rank) {
142         this(FILE_NAMES[file], rank + 1);
143     }
144 
145     /***
146      * Create a clone of this position.
147      * 
148      * @return the clone
149      * @throws CloneNotSupportedException
150      *             (This should not happen!)
151      */
152     public Object clone() throws CloneNotSupportedException {
153         return super.clone();
154     }
155 
156     /***
157      * Whether two objects are equal.
158      * 
159      * @param obj
160      *            the comparator
161      * @return whether they are equal
162      * @override java.lang.Object.equals
163      */
164     public boolean equals(final Object obj) {
165         if (obj == null || (!(obj instanceof Position))) {
166             return false;
167         } else {
168             Position pos = (Position) obj;
169             return (pos.getFile().equals(getFile())
170                     && (pos.getRank() == getRank()));
171         }
172     }
173 
174     /***
175      * Get the hash code of the Object.
176      * 
177      * @return the hashCode
178      */
179     public int hashCode() {
180         return toString().hashCode();
181     }
182 
183     /***
184      * Adds a vector onto the position.
185      * @param fileIncrement the change in file
186      * @param rankIncrement the change in rank
187      */
188     public void add(final int fileIncrement, final int rankIncrement) {
189         rank += rankIncrement;
190         file = FILE_NAMES[getFileNumber() + fileIncrement - 1];
191     }
192     
193     /***
194      * Returns the algebraic notation of this position.
195      * @return the algebraic notation.
196      */
197     public String getAlgebraicNotation() {
198         return file + rank;
199     }
200     
201     /***
202      * Resets the position based on the algebraic notation.
203      * @param algebraicNotation the position
204      */
205     public void setAlgebraicNotation(final String algebraicNotation) {
206         try {
207             String file = algebraicNotation.substring(0, 1);
208             String rank = algebraicNotation.substring(1);
209             this.file = file;
210             this.rank = new Integer(rank).intValue();
211             getFileNumber();
212         } catch (NumberFormatException e) {
213             throw new RuntimeException("Invalid notation format.");
214         } catch (StringIndexOutOfBoundsException e) {
215             throw new RuntimeException("Invalid notation format.");
216         }
217     }
218 }