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
18
19 package com.webstersmalley.chessweb.model;
20
21 /***
22 * Represents a cell on a chess board.
23 *
24 * @author Matthew Smalley
25 */
26 public final class Square {
27
28 /*** Static representing the colour black. * */
29 private static final String COLOUR_BLACK = "black";
30
31 /*** Static representing the colour white. * */
32 private static final String COLOUR_WHITE = "white";
33
34 /*** This cell's colour. * */
35 private String colour;
36
37 /*** This cell's piece. * */
38 private Piece piece;
39
40 /*** The cell's position. */
41 private Position position;
42
43 /***
44 * @return Returns the position.
45 */
46 public Position getPosition() {
47 return position;
48 }
49
50 /***
51 * @param position The position to set.
52 */
53 public void setPosition(final Position position) {
54 this.position = position;
55 }
56
57 /***
58 * Construct a new cell. Pass in the position so we can work out if it's a
59 * black or a white cell.
60 *
61 * @param position
62 * the position
63 */
64 public Square(final Position position) {
65 if (position.getModulus() == 1) {
66 colour = COLOUR_WHITE;
67 } else {
68 colour = COLOUR_BLACK;
69 }
70 this.position = position;
71 }
72
73 /***
74 * Returns the cell's colour.
75 *
76 * @return the colour
77 */
78 public String getColour() {
79 return colour;
80 }
81
82 /***
83 * Returns whether a Piece is in this cell.
84 *
85 * @return whether a Piece is in this cell.
86 */
87 public boolean hasPiece() {
88 return piece != null;
89 }
90
91 /***
92 * @return Returns the piece.
93 */
94 public Piece getPiece() {
95 return piece;
96 }
97
98 /***
99 * @param piece
100 * The piece to set.
101 */
102 public void setPiece(final Piece piece) {
103 this.piece = piece;
104 }
105
106 /***
107 * Returns the team name of the piece, or null if none.
108 *
109 * @return the team name
110 */
111 public String getTeamName() {
112 if (piece == null) {
113 return null;
114 } else {
115 return piece.getTeam().toString();
116 }
117 }
118
119 /***
120 * Returns the contents as a String suitable for rendering in HTML. Useful
121 * for views!
122 *
123 * @return the string
124 */
125 public String getHtmlContents() {
126 if (piece == null) {
127 return "blank";
128
129 } else {
130 return piece.getIcon();
131
132 }
133 }
134 }