Permalink
Browse files

Board item types basic handling added (eg. selected, last, winning)

  • Loading branch information...
1 parent df3ac4e commit 9fb699053b1c26c9f319becd29241bf00ca59ce4 @loony-bean committed Sep 12, 2011
@@ -0,0 +1,9 @@
+package com.example.kindle.boardgame;
+
+public class Board2DItemType
+{
+ public static final int NORMAL = 0;
+ public static final int SELECTED = 1;
+ public static final int WINNING = 2;
+ public static final int LAST = 3;
+}
@@ -18,4 +18,14 @@
* @return Piece's position on game board.
*/
public IPosition2D getPosition();
+
+ /**
+ * Returns type of the piece on board. Piece type is
+ * a special value that is associated with each piece
+ * and can can tell if it is selected, put recently,
+ * is part of a winning set, etc.
+ *
+ * @return Type of the board piece.
+ */
+ public int getType();
}
@@ -35,6 +35,17 @@ public Object clone()
return new Position2D(this.row, this.col);
}
+ /** {@inheritDoc} */
+ public boolean equals(Object other)
+ {
+ if (this == other) return true;
+ if (other == null) return false;
+ if (getClass() != other.getClass()) return false;
+
+ Position2D otherPosition = (Position2D) other;
+ return (this.row == otherPosition.row && this.col == otherPosition.col);
+ }
+
/** Row number on board. */
private int row;
@@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Random;
+import com.example.kindle.boardgame.Board2DItemType;
import com.example.kindle.boardgame.IBoard2D;
import com.example.kindle.boardgame.IBoard2DItem;
import com.example.kindle.boardgame.IPiece;
@@ -87,7 +88,13 @@ public ArrayList getItems()
{
for (int i = 0; i < this.board.length; i++)
{
- IBoard2DItem item = new BoardItem(this.board[i][j], new Position2D(i, j));
+ IPosition2D pos = new Position2D(i, j);
+ int type = Board2DItemType.NORMAL;
+ if (this.lastTurn != null && this.lastTurn.getPosition().equals(pos))
+ {
+ type = Board2DItemType.LAST;
+ }
+ IBoard2DItem item = new BoardItem(this.board[i][j], pos, type);
result.add(item);
}
}
@@ -6,10 +6,11 @@
public class BoardItem implements IBoard2DItem
{
- public BoardItem(Piece piece, IPosition2D position)
+ public BoardItem(final Piece piece, final IPosition2D position, int type)
{
this.piece = piece;
this.position = position;
+ this.type = type;
}
public IPiece getPiece()
@@ -22,6 +23,17 @@ public IPosition2D getPosition()
return this.position;
}
+ public int getType()
+ {
+ return this.type;
+ }
+
+ public void setType(int type)
+ {
+ this.type = type;
+ }
+
private final Piece piece;
private final IPosition2D position;
+ private int type;
}
@@ -9,6 +9,7 @@
import org.json.simple.JSONObject;
+import com.example.kindle.boardgame.Board2DItemType;
import com.example.kindle.boardgame.IPiece;
import com.example.kindle.boardgame.IPosition2D;
import com.example.kindle.utils.FileHelper;
@@ -82,28 +83,37 @@ protected BoardLayout createLayout(final Dimension size)
public void paintBoard(final Graphics g, final Component parent)
{
}
+
+ public String getImageId(final Color color, int type)
+ {
+ String id = (color == Color.black) ? "black" : "white";
+ if (type == Board2DItemType.LAST)
+ {
+ id += "_last";
+ }
+
+ return id + ".png";
+ }
public void paintBoardItem(final Graphics g, final Component parent, final BoardItem item)
{
BoardLayout l = this.getLayout(parent.getSize());
-
IPiece piece = item.getPiece();
+
if (piece != null)
{
- Color color = piece.getPlayer().getColor();
- String id = (color == Color.black) ? "black.png" : "white.png";
+ String id = getImageId(piece.getPlayer().getColor(), item.getType());
IPosition2D pos = item.getPosition();
this.paintImageToGrid(g, parent, id, l, pos.row(), pos.col());
}
}
- protected void paintPiece(final Graphics g, final Component parent, final IPiece piece, final Rectangle rect)
+ protected void paintPiece(final Graphics g, final Component parent, final IPiece piece, final Rectangle rect, int type)
{
if (piece != null)
{
- Color color = piece.getPlayer().getColor();
- String id = (color == Color.black) ? "black.png" : "white.png";
+ String id = getImageId(piece.getPlayer().getColor(), type);
this.paintImage(g, parent, id, rect);
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -62,6 +62,6 @@ public void paintBoardItem(final Graphics g, final Component parent, final Board
int x = l.boardRect.x + item.getPosition().row()*(l.pieceSizeX + l.pieceGapX);
- this.paintPiece(g, parent, item.getPiece(), new Rectangle(x, y, l.pieceSizeX, l.pieceSizeY));
+ this.paintPiece(g, parent, item.getPiece(), new Rectangle(x, y, l.pieceSizeX, l.pieceSizeY), item.getType());
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9fb6990

Please sign in to comment.