diff --git a/chess/src/gui/ChessGui.java b/chess/src/gui/ChessGui.java index f636b2f8fee1797fb0cccd2a179cca2cab2ee847..814952c155302b9725aa357cab898e31869bd8bf 100644 --- a/chess/src/gui/ChessGui.java +++ b/chess/src/gui/ChessGui.java @@ -7,6 +7,8 @@ import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -21,10 +23,14 @@ import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; +import javax.swing.SwingUtilities; + import board.Board; +import board.Cell; import player.Player.id; import board.Coordinate; +import pieces.Piece; // Source citing: got tips from https://www.youtube.com/watch?v=w9HR4VJ8DAw @@ -34,6 +40,10 @@ public class ChessGui{ private final GuiBoard boardFrame; private final Board board; + private Cell sourceCoordinate; + private Cell destinationCoordinate; + private Piece movingPiece; + private final static Dimension BOARD_DIMENSION = new Dimension(400,400); private final static Dimension CELL_DIMENSION = new Dimension(10,10); private final static String PIECE_IMG_PATH = "PieceIMG/"; @@ -79,7 +89,7 @@ public class ChessGui{ } private class GuiBoard extends JPanel{ - final List<Cell> cells; + final List<GuiCell> cells; GuiBoard(){ super(new GridLayout(8,8)); @@ -88,7 +98,7 @@ public class ChessGui{ Color blackColor = Color.decode("#71A8D1"); Color whiteColor = Color.decode("#F7F9FB"); for(int i = 0; i< 64; i++) { - final Cell cell = new Cell(this,i); + final GuiCell cell = new GuiCell(this,i); this.cells.add(cell); if ((i/8+i%8) % 2 == 0) { cells.get(i).setBackground(whiteColor); //loop through all the cells and set colors @@ -100,25 +110,109 @@ public class ChessGui{ validate(); } + + public void updateGuiBoard(Board board) { + // TODO Auto-generated method stub + removeAll(); + for(GuiCell cell : cells) { + cell.updateCell(board); + add(cell); + } + validate(); + repaint(); + } } - private class Cell extends JPanel { + private class GuiCell extends JPanel { private final int cellCode; - - Cell(final GuiBoard board, final int cellCode){ + int xCoord; + int yCoord; + GuiCell(final GuiBoard guiboard, final int cellCode){ super(new GridBagLayout()); setSize(CELL_DIMENSION); this.cellCode = cellCode; // code calculated by row and column assignCellPiece(); // if piece exists on board, assign the piece on the board. validate(); + xCoord = cellCode/8; + yCoord = cellCode%8; + + + addMouseListener(new MouseListener(){ + + public void mouseClicked(final MouseEvent e) { + System.out.println("hi0"); + if(SwingUtilities.isLeftMouseButton(e)) { + if(sourceCoordinate == null) { + System.out.println(xCoord); + System.out.println(yCoord); + sourceCoordinate = board.getCell(xCoord, yCoord); //from coord + movingPiece = sourceCoordinate.getPieceOnCell(); + if(movingPiece == null) sourceCoordinate = null; + System.out.println(sourceCoordinate.getPieceOnCell()); + System.out.println("hi2"); + } + else { + destinationCoordinate = board.getCell(xCoord, yCoord); + board.makeMove(sourceCoordinate.getPieceOnCell().getCoord(), destinationCoordinate.getPieceOnCell().getCoord()); //moveto Coord + System.out.println(sourceCoordinate.getPieceOnCell().getCoord()); + System.out.println(destinationCoordinate.getPieceOnCell().getCoord()); + } + + } + SwingUtilities.invokeLater(new Runnable() { + public void run() { + guiboard.updateGuiBoard(board); + } + }); + + } + + @Override + public void mousePressed(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseReleased(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } + + }); } + public void updateCell(Board board) { + // TODO Auto-generated method stub + assignCellPiece(); + if ((xCoord+yCoord) % 2 == 0) { + this.setBackground(Color.decode("#F7F9FB")); //loop through all the cells and set colors + } else { + this.setBackground(Color.decode("#71A8D1")); + } + validate(); + repaint(); + } + + private void assignCellPiece() { this.removeAll(); int xCoord = cellCode/8; // calculate x coordinate int yCoord = cellCode%8; // calculate y coordinate if(board.getCell(xCoord,yCoord).getPieceOnCell() != null) { - try{ System.out.println(PIECE_IMG_PATH + board.getCell(xCoord,yCoord).toString().substring(0,3)+".png"); + try{ final BufferedImage img = ImageIO.read(new File(PIECE_IMG_PATH + board.getCell(xCoord,yCoord).toString().substring(0,3)+".png")); add(new JLabel(new ImageIcon(img))); } catch (IOException e) { diff --git a/chess/src/pieces/Chameleon.java b/chess/src/pieces/Chameleon.java index 3e484a6fde5d46c9475d427b314c34187e0da363..611ed9535bb1d6e5ab69af165e1c258eac50ae1f 100644 --- a/chess/src/pieces/Chameleon.java +++ b/chess/src/pieces/Chameleon.java @@ -19,6 +19,7 @@ public class Chameleon extends Piece{ super.setName("Chameleon"); } + Piece actualPiece; /** * @param coord is the coordinate that the piece is on. @@ -28,21 +29,16 @@ public class Chameleon extends Piece{ * Determines the possible <Coordinates> that the chameleon can move to. */ - // add coordinates in all possible directions to possibleCoords while not adding coordinates that are out of bounds or leaping over other pieces. + // add possible moves to be that of the captured piece public List<Coordinate> possibleMoves(Coordinate coord, Board board) { switch (this.getName()) { - case "Pawn": Pawn pawn = new Pawn(Player,coord); - return pawn.possibleMoves(coord, board); - case "Bishop": Bishop bishop = new Bishop(Player,coord); - return bishop.possibleMoves(coord, board); - case "Rook": Rook rook = new Rook(Player,coord); - return rook.possibleMoves(coord, board); - case "Knight": Knight knight = new Knight(Player,coord); - return knight.possibleMoves(coord, board); - case "Queen": Queen queen = new Queen(Player,coord); - return queen.possibleMoves(coord, board); - default: Pawn pawn1 = new Pawn(Player,coord); - return pawn1.possibleMoves(coord, board); + case "Pawn": actualPiece = new Pawn(Player,coord); + case "Bishop": actualPiece = new Bishop(Player,coord); + case "Rook": actualPiece = new Rook(Player,coord); + case "Knight": actualPiece = new Knight(Player,coord); + case "Queen": actualPiece = new Queen(Player,coord); + default: actualPiece = new Pawn(Player,coord); + return actualPiece.possibleMoves(coord, board); } } diff --git a/chess/src/player/Player.java b/chess/src/player/Player.java index 438b627f399fcde61689505aa7f0345d4759b8bc..969b753adf21ca5bdf9898e2aba348c96c11bfe4 100644 --- a/chess/src/player/Player.java +++ b/chess/src/player/Player.java @@ -92,8 +92,8 @@ public class Player{ if(player == id.WHITE) { playerId = id.WHITE; // pieces.put("7,3",new Rook(playerId,new Coordinate(7,3))); // for check testing -// pieces.put("0,0",new Rook(playerId,new Coordinate(0,0))); - pieces.put("0,0",new Dabbaba(playerId,new Coordinate(0,0))); + pieces.put("0,0",new Rook(playerId,new Coordinate(0,0))); + // pieces.put("0,0",new Dabbaba(playerId,new Coordinate(0,0))); pieces.put("0,1",new Knight(playerId,new Coordinate(0,1))); pieces.put("0,2",new Bishop(playerId,new Coordinate(0,2))); pieces.put("0,3",new Queen(playerId,new Coordinate(0,3))); @@ -101,8 +101,8 @@ public class Player{ pieces.put("0,5",new Bishop(playerId,new Coordinate(0,5))); pieces.put("0,6",new Knight(playerId,new Coordinate(0,6))); pieces.put("0,7",new Rook(playerId,new Coordinate(0,7))); - // pieces.put("1,0",new Pawn(playerId,new Coordinate(1,0))); - pieces.put("1,0",new Chameleon(playerId,new Coordinate(1,0))); + pieces.put("1,0",new Pawn(playerId,new Coordinate(1,0))); + // pieces.put("1,0",new Chameleon(playerId,new Coordinate(1,0))); pieces.put("1,1",new Pawn(playerId,new Coordinate(1,1))); pieces.put("1,2",new Pawn(playerId,new Coordinate(1,2))); pieces.put("1,3",new Pawn(playerId,new Coordinate(1,3))); diff --git a/chess/src/tests/ChameleonTest.java b/chess/src/tests/ChameleonTest.java index 5d208936a53e97dde2cab4a23579014c0ab9f881..62784c751a8ed609dc4e30c1cc94489a0a5b9bf0 100644 --- a/chess/src/tests/ChameleonTest.java +++ b/chess/src/tests/ChameleonTest.java @@ -46,16 +46,6 @@ public class ChameleonTest { assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("7,2").getName(),"Bishop"); assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("7,2") instanceof pieces.Chameleon, true); - board.makeMove(new Coordinate(6,5), new Coordinate(5,5)); - board.makeMove(new Coordinate(7,2), new Coordinate(5,0)); - assertEquals(board.getCell(7, 2).getPieceOnCell(),null); - assertEquals(board.getCell(5, 0).getPieceOnCell().getName(),"Bishop"); - assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("5,0").getName(),"Bishop"); - assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("5,0") instanceof pieces.Chameleon, true); - board.makeMove(new Coordinate(5,5), new Coordinate(4,5)); - board.makeMove(new Coordinate(5,0), new Coordinate(5,1)); - assertEquals(board.getCell(5, 1).getPieceOnCell(),null); - assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("5,0") instanceof pieces.Chameleon, true); } }