weijie90 / international-chess
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
625a820
international-chess / Chessboard.java
| f5fc268e » | weijie90 | 2008-09-30 | 1 | /**************** | |
| 2 | * Name: Koh Wei Jie | ||||
| 3 | * | ||||
| 4 | * Purpose of this file: To work as the chessboard for the game. | ||||
| 5 | * Uses graphics for the board and pieces, as well as drag-and- | ||||
| 6 | * drop event handling. | ||||
| 7 | * | ||||
| 8 | ****************/ | ||||
| 9 | |||||
| 10 | import java.util.*; | ||||
| 11 | import java.awt.event.*; | ||||
| 12 | import java.lang.Math.*; | ||||
| 13 | import java.awt.*; | ||||
| 14 | import javax.swing.*; | ||||
| 15 | import java.awt.image.*; | ||||
| 16 | import game.*; | ||||
| 17 | |||||
| 18 | |||||
| 19 | public class Chessboard extends JComponent implements MouseMotionListener, MouseListener{ | ||||
| 20 | private ImageIcon boardbg; | ||||
| 21 | private ImageIcon redSq; | ||||
| 22 | private ImageIcon greenSq; | ||||
| 23 | |||||
| 24 | private Dimension totalSize = new Dimension(402, 402); | ||||
| 25 | |||||
| 26 | private final int sqLen = 50; | ||||
| 27 | |||||
| 28 | private String imgDir = "images/"; | ||||
| 29 | private String boardImgDir = imgDir + "board/"; | ||||
| 30 | private String bPiecesImgDir = imgDir + "b_pieces/"; | ||||
| 31 | private String wPiecesImgDir = imgDir + "w_pieces/"; | ||||
| 32 | |||||
| 33 | private Board board; | ||||
| 34 | |||||
| 35 | private Piece beingDragged = null; | ||||
| 36 | private int beingDraggedX = -1; | ||||
| 37 | private int beingDraggedY = -1; | ||||
| 38 | |||||
| 39 | private int originalX = -1; | ||||
| 40 | private int originalY = -1; | ||||
| 41 | |||||
| 42 | private boolean isWhite; | ||||
| 43 | private boolean myTurn; | ||||
| 44 | |||||
| 45 | private ChessController cc; | ||||
| 46 | |||||
| 47 | private Vector<int[]> greenSqLocations = new Vector<int[]>(); | ||||
| 48 | |||||
| 49 | public Chessboard(boolean isWhite, ChessController cc){ | ||||
| 50 | this.isWhite = isWhite; | ||||
| 51 | this.myTurn = isWhite; | ||||
| 52 | this.cc = cc; | ||||
| 53 | this.boardbg = new ImageIcon(this.boardImgDir + "board.png"); | ||||
| 54 | this.greenSq = new ImageIcon(this.boardImgDir + "green_square.png"); | ||||
| 55 | this.redSq = new ImageIcon(this.boardImgDir + "red_square.png"); | ||||
| 56 | |||||
| 57 | this.board = new Board(isWhite); | ||||
| 58 | |||||
| 59 | this.addMouseMotionListener(this); | ||||
| 60 | this.addMouseListener(this); | ||||
| 61 | } | ||||
| 62 | |||||
| 63 | //make this component of a fixed size | ||||
| 64 | public Dimension getMinimumSize(){ return this.totalSize; } | ||||
| 65 | public Dimension getMaximumSize(){ return this.totalSize; } | ||||
| 66 | public Dimension getPreferredSize() { return this.totalSize; } | ||||
| 67 | |||||
| 68 | public void mouseMoved(MouseEvent e){ } | ||||
| 69 | public void mouseEntered(MouseEvent e){ } | ||||
| 70 | public void mouseExited(MouseEvent e){ } | ||||
| 71 | public void mouseClicked(MouseEvent e){ } | ||||
| 72 | |||||
| 73 | public Vector<int[]> calcRedSqLocations(){ | ||||
| 74 | Vector<int[]> canCapture = new Vector<int[]>(); | ||||
| 75 | for (int[] p : this.greenSqLocations){ | ||||
| 76 | if (p != null){ | ||||
| 77 | if (this.board.isOccupied(p[0], p[1]) || this.board.isEnPassantLoc(p[0], p[1])){ | ||||
| 78 | canCapture.add(p); | ||||
| 79 | } | ||||
| 80 | } | ||||
| 81 | } | ||||
| 82 | return canCapture; | ||||
| 83 | } | ||||
| 84 | |||||
| 85 | public void movePiece(int fromX, int fromY, int toX, int toY){ | ||||
| 86 | //called when the opponent moves a piece | ||||
| 87 | this.board.movePiece(fromX, fromY, toX, toY); | ||||
| 88 | this.repaint(); | ||||
| 89 | this.myTurn = true; | ||||
| 90 | } | ||||
| 91 | |||||
| 92 | public void clearBeingMoved(){ | ||||
| 93 | this.beingDragged = null; | ||||
| 94 | this.beingDraggedX = -1; | ||||
| 95 | this.beingDraggedY = -1; | ||||
| 96 | this.greenSqLocations.clear(); | ||||
| 97 | this.repaint(); | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | //Drag and drop | ||||
| 101 | public void mousePressed(MouseEvent e){ | ||||
| 102 | if (!myTurn){ | ||||
| 103 | return; | ||||
| 104 | } | ||||
| 105 | |||||
| 106 | Point mouseLoc = e.getPoint(); | ||||
| 107 | int xLoc = (int)(Math.floor(mouseLoc.getX() / this.sqLen)); | ||||
| 108 | int yLoc = (int)(Math.floor(mouseLoc.getY() / this.sqLen)); | ||||
| 109 | |||||
| 110 | if (this.board.getPieces()[yLoc][xLoc] != null){ | ||||
| 111 | this.beingDraggedX = xLoc; | ||||
| 112 | this.beingDraggedY = yLoc; | ||||
| 113 | |||||
| 114 | this.originalX = xLoc; | ||||
| 115 | this.originalY = yLoc; | ||||
| 116 | |||||
| 117 | this.beingDragged = this.board.getPieces()[yLoc][xLoc]; | ||||
| 118 | if (this.beingDragged.getIsWhite() == this.isWhite){ | ||||
| 119 | this.greenSqLocations = this.beingDragged.possiblePositions(this.board, xLoc, yLoc); | ||||
| 120 | } | ||||
| 121 | if (this.greenSqLocations.isEmpty()){ | ||||
| 122 | clearBeingMoved(); | ||||
| 123 | } | ||||
| 124 | } | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | public void mouseDragged(MouseEvent e){ | ||||
| 128 | this.repaint(); | ||||
| 129 | if ((!myTurn) || (this.greenSqLocations.size() == 0) || (this.beingDragged.getIsWhite() != this.isWhite)){ | ||||
| 130 | return; | ||||
| 131 | } | ||||
| 132 | |||||
| 133 | Point mouseLoc = e.getPoint(); | ||||
| 134 | this.beingDraggedX = (int)(Math.floor(mouseLoc.getX() / this.sqLen)); | ||||
| 135 | this.beingDraggedY = (int)(Math.floor(mouseLoc.getY() / this.sqLen)); | ||||
| 136 | } | ||||
| 137 | |||||
| 138 | public void mouseReleased(MouseEvent e){ | ||||
| 139 | this.repaint(); | ||||
| 140 | if (!myTurn || (this.greenSqLocations.size() == 0) || (this.beingDragged.getIsWhite() != this.isWhite)){ | ||||
| 141 | clearBeingMoved(); | ||||
| 142 | return; | ||||
| 143 | } | ||||
| 144 | |||||
| 145 | Point mouseLoc = e.getPoint(); | ||||
| 146 | int xLoc = (int)(Math.floor(mouseLoc.getX() / this.sqLen)); | ||||
| 147 | int yLoc = (int)(Math.floor(mouseLoc.getY() / this.sqLen)); | ||||
| 148 | |||||
| 149 | if (!(xLoc > 7 || yLoc > 7 || xLoc < 0 || yLoc < 0)){ | ||||
| 150 | if (!(xLoc == originalX && yLoc == originalY)){ | ||||
| 151 | for (int[] sq : this.greenSqLocations){ | ||||
| 152 | if (sq[0] == xLoc && sq[1] == yLoc){ | ||||
| 153 | this.board.movePiece(originalX, originalY, xLoc, yLoc); | ||||
| 154 | this.cc.movePiece(originalX, originalY, xLoc, yLoc); | ||||
| 155 | this.myTurn = false; | ||||
| 156 | |||||
| 157 | clearBeingMoved(); | ||||
| 158 | this.repaint(); | ||||
| 159 | break; | ||||
| 160 | } | ||||
| 161 | this.beingDraggedX = this.originalX; | ||||
| 162 | this.beingDraggedY = this.originalY; | ||||
| 163 | } | ||||
| 164 | this.greenSqLocations.clear(); | ||||
| 165 | } | ||||
| 166 | else{ | ||||
| 167 | clearBeingMoved(); | ||||
| 168 | } | ||||
| 169 | } | ||||
| 170 | else{ | ||||
| 171 | clearBeingMoved(); | ||||
| 172 | this.repaint(); | ||||
| 173 | } | ||||
| 174 | this.greenSqLocations.clear(); | ||||
| 175 | this.repaint(); | ||||
| 176 | if (this.board.isCheckMate(!this.isWhite)){ | ||||
| 177 | this.cc.checkmateOpp(); | ||||
| 178 | } | ||||
| 179 | else if (this.board.isStalemate(!this.isWhite)){ | ||||
| 180 | this.cc.stalemateOpp(); | ||||
| 181 | } | ||||
| 182 | else if (this.board.isCheck(!this.isWhite)){ | ||||
| 183 | this.cc.checkOpp(); | ||||
| 184 | } | ||||
| 185 | else if (this.board.canPromote(this.isWhite)){ | ||||
| 186 | String pieces[] = {"Queen", "Rook", "Bishop", "Knight"}; | ||||
| 187 | int promoteTo = JOptionPane.showOptionDialog(null, | ||||
| 188 | "Please select a piece to this pawn to.", | ||||
| 189 | "Promotion", | ||||
| 190 | JOptionPane.DEFAULT_OPTION, | ||||
| 191 | JOptionPane.PLAIN_MESSAGE, | ||||
| 192 | null, | ||||
| 193 | pieces, | ||||
| 194 | pieces[0]); | ||||
| 195 | |||||
| 196 | int xPos = board.promote(promoteTo, isWhite); | ||||
| 197 | this.cc.promote(promoteTo, xPos); | ||||
| 198 | this.repaint(); | ||||
| 199 | } | ||||
| 200 | this.repaint(); | ||||
| 201 | } | ||||
| 202 | |||||
| 203 | public void promoteOpp(int promoteTo, int xPos){ | ||||
| 204 | switch (promoteTo){ | ||||
| 205 | case 0: | ||||
| 206 | this.board.getPieces()[7][xPos] = new Queen(!isWhite); | ||||
| 207 | break; | ||||
| 208 | case 1: | ||||
| 209 | this.board.getPieces()[7][xPos] = new Rook(!isWhite); | ||||
| 210 | break; | ||||
| 211 | case 2: | ||||
| 212 | this.board.getPieces()[7][xPos] = new Bishop(!isWhite); | ||||
| 213 | break; | ||||
| 214 | case 3: | ||||
| 215 | this.board.getPieces()[7][xPos] = new Knight(!isWhite); | ||||
| 216 | break; | ||||
| 217 | } | ||||
| 218 | } | ||||
| 219 | |||||
| 220 | protected ImageIcon makePiece(boolean white, String type){ | ||||
| 221 | String colour; | ||||
| 222 | String dir; | ||||
| 223 | if (white){ | ||||
| 224 | colour = "w"; | ||||
| 225 | dir = this.wPiecesImgDir; | ||||
| 226 | } | ||||
| 227 | else{ | ||||
| 228 | colour = "b"; | ||||
| 229 | dir = this.bPiecesImgDir; | ||||
| 230 | } | ||||
| 231 | return new ImageIcon(dir + colour + "_" + type + ".png"); | ||||
| 232 | } | ||||
| 233 | |||||
| 234 | public String genFilename(Piece p){ | ||||
| 235 | String name = ""; | ||||
| 236 | if (p.getIsWhite()){ | ||||
| 237 | name += wPiecesImgDir + "w"; | ||||
| 238 | } | ||||
| 239 | else{ | ||||
| 240 | name += bPiecesImgDir + "b"; | ||||
| 241 | } | ||||
| 242 | name += "_"; | ||||
| 243 | name += p.getName() + ".png"; | ||||
| 244 | return name; | ||||
| 245 | } | ||||
| 246 | |||||
| 247 | public void paint(Graphics g){ | ||||
| 248 | super.paint(g); | ||||
| 249 | //background image of the checkered board | ||||
| 250 | g.drawImage(this.boardbg.getImage(), 0, 0, 402, 402, this); | ||||
| 251 | |||||
| 252 | //green squares | ||||
| 253 | if (this.greenSqLocations != null){ | ||||
| 254 | for (int[] i : greenSqLocations){ | ||||
| 255 | g.drawImage(this.greenSq.getImage(), | ||||
| 256 | i[0] * this.sqLen + 1, | ||||
| 257 | i[1] * this.sqLen + 1, | ||||
| 258 | this.sqLen, | ||||
| 259 | this.sqLen, | ||||
| 260 | this); | ||||
| 261 | } | ||||
| 262 | for (int[] i : calcRedSqLocations()){ | ||||
| 263 | if (i != null){ | ||||
| 264 | g.drawImage(this.redSq.getImage(), | ||||
| 265 | i[0] * this.sqLen + 1, | ||||
| 266 | i[1] * this.sqLen + 1, | ||||
| 267 | this.sqLen, | ||||
| 268 | this.sqLen, | ||||
| 269 | this); | ||||
| 270 | } | ||||
| 271 | } | ||||
| 272 | } | ||||
| 273 | |||||
| 274 | //individual pieces | ||||
| 275 | for (int i = 0; i < this.board.getPieces().length; i++){ | ||||
| 276 | for (int j = 0; j < this.board.getPieces()[i].length; j++){ | ||||
| 277 | if (this.board.getPieces()[i][j] != null){ | ||||
| 278 | //for each piece | ||||
| 279 | Piece piece = this.board.getPieces()[i][j]; | ||||
| 280 | ImageIcon icon = new ImageIcon(genFilename(piece)); | ||||
| 281 | if (!piece.equals(this.beingDragged)){ | ||||
| 282 | g.drawImage(icon.getImage(), j * this.sqLen, i * this.sqLen, this.sqLen, this.sqLen, this); | ||||
| 283 | } | ||||
| 284 | } | ||||
| 285 | } | ||||
| 286 | } | ||||
| 287 | |||||
| 288 | //the piece being dragged | ||||
| 289 | if (this.beingDragged != null){ | ||||
| 290 | ImageIcon icon = new ImageIcon(genFilename(this.beingDragged)); | ||||
| 291 | g.drawImage(icon.getImage(), this.beingDraggedX * this.sqLen, this.beingDraggedY * this.sqLen, this.sqLen, this.sqLen, this); | ||||
| 292 | } | ||||
| 293 | } | ||||
| 294 | } | ||||
