Skip to content
Snippets Groups Projects
Commit c083eaf6 authored by Jae Woo Kim's avatar Jae Woo Kim
Browse files

1.2 submission

parent f7fabb60
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,8 @@ import java.awt.GridBagLayout; ...@@ -7,6 +7,8 @@ import java.awt.GridBagLayout;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
...@@ -21,10 +23,14 @@ import javax.swing.JMenu; ...@@ -21,10 +23,14 @@ import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import board.Board; import board.Board;
import board.Cell;
import player.Player.id; import player.Player.id;
import board.Coordinate; import board.Coordinate;
import pieces.Piece;
// Source citing: got tips from https://www.youtube.com/watch?v=w9HR4VJ8DAw // Source citing: got tips from https://www.youtube.com/watch?v=w9HR4VJ8DAw
...@@ -34,6 +40,10 @@ public class ChessGui{ ...@@ -34,6 +40,10 @@ public class ChessGui{
private final GuiBoard boardFrame; private final GuiBoard boardFrame;
private final Board board; 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 BOARD_DIMENSION = new Dimension(400,400);
private final static Dimension CELL_DIMENSION = new Dimension(10,10); private final static Dimension CELL_DIMENSION = new Dimension(10,10);
private final static String PIECE_IMG_PATH = "PieceIMG/"; private final static String PIECE_IMG_PATH = "PieceIMG/";
...@@ -79,7 +89,7 @@ public class ChessGui{ ...@@ -79,7 +89,7 @@ public class ChessGui{
} }
private class GuiBoard extends JPanel{ private class GuiBoard extends JPanel{
final List<Cell> cells; final List<GuiCell> cells;
GuiBoard(){ GuiBoard(){
super(new GridLayout(8,8)); super(new GridLayout(8,8));
...@@ -88,7 +98,7 @@ public class ChessGui{ ...@@ -88,7 +98,7 @@ public class ChessGui{
Color blackColor = Color.decode("#71A8D1"); Color blackColor = Color.decode("#71A8D1");
Color whiteColor = Color.decode("#F7F9FB"); Color whiteColor = Color.decode("#F7F9FB");
for(int i = 0; i< 64; i++) { 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); this.cells.add(cell);
if ((i/8+i%8) % 2 == 0) { if ((i/8+i%8) % 2 == 0) {
cells.get(i).setBackground(whiteColor); //loop through all the cells and set colors cells.get(i).setBackground(whiteColor); //loop through all the cells and set colors
...@@ -100,25 +110,109 @@ public class ChessGui{ ...@@ -100,25 +110,109 @@ public class ChessGui{
validate(); 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; private final int cellCode;
int xCoord;
Cell(final GuiBoard board, final int cellCode){ int yCoord;
GuiCell(final GuiBoard guiboard, final int cellCode){
super(new GridBagLayout()); super(new GridBagLayout());
setSize(CELL_DIMENSION); setSize(CELL_DIMENSION);
this.cellCode = cellCode; // code calculated by row and column this.cellCode = cellCode; // code calculated by row and column
assignCellPiece(); // if piece exists on board, assign the piece on the board. assignCellPiece(); // if piece exists on board, assign the piece on the board.
validate(); 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() { private void assignCellPiece() {
this.removeAll(); this.removeAll();
int xCoord = cellCode/8; // calculate x coordinate int xCoord = cellCode/8; // calculate x coordinate
int yCoord = cellCode%8; // calculate y coordinate int yCoord = cellCode%8; // calculate y coordinate
if(board.getCell(xCoord,yCoord).getPieceOnCell() != null) { 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")); 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))); add(new JLabel(new ImageIcon(img)));
} catch (IOException e) { } catch (IOException e) {
......
...@@ -19,6 +19,7 @@ public class Chameleon extends Piece{ ...@@ -19,6 +19,7 @@ public class Chameleon extends Piece{
super.setName("Chameleon"); super.setName("Chameleon");
} }
Piece actualPiece;
/** /**
* @param coord is the coordinate that the piece is on. * @param coord is the coordinate that the piece is on.
...@@ -28,21 +29,16 @@ public class Chameleon extends Piece{ ...@@ -28,21 +29,16 @@ public class Chameleon extends Piece{
* Determines the possible <Coordinates> that the chameleon can move to. * 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) { public List<Coordinate> possibleMoves(Coordinate coord, Board board) {
switch (this.getName()) { switch (this.getName()) {
case "Pawn": Pawn pawn = new Pawn(Player,coord); case "Pawn": actualPiece = new Pawn(Player,coord);
return pawn.possibleMoves(coord, board); case "Bishop": actualPiece = new Bishop(Player,coord);
case "Bishop": Bishop bishop = new Bishop(Player,coord); case "Rook": actualPiece = new Rook(Player,coord);
return bishop.possibleMoves(coord, board); case "Knight": actualPiece = new Knight(Player,coord);
case "Rook": Rook rook = new Rook(Player,coord); case "Queen": actualPiece = new Queen(Player,coord);
return rook.possibleMoves(coord, board); default: actualPiece = new Pawn(Player,coord);
case "Knight": Knight knight = new Knight(Player,coord); return actualPiece.possibleMoves(coord, board);
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);
} }
} }
......
...@@ -92,8 +92,8 @@ public class Player{ ...@@ -92,8 +92,8 @@ public class Player{
if(player == id.WHITE) { if(player == id.WHITE) {
playerId = id.WHITE; playerId = id.WHITE;
// pieces.put("7,3",new Rook(playerId,new Coordinate(7,3))); // for check testing // 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 Rook(playerId,new Coordinate(0,0)));
pieces.put("0,0",new Dabbaba(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,1",new Knight(playerId,new Coordinate(0,1)));
pieces.put("0,2",new Bishop(playerId,new Coordinate(0,2))); pieces.put("0,2",new Bishop(playerId,new Coordinate(0,2)));
pieces.put("0,3",new Queen(playerId,new Coordinate(0,3))); pieces.put("0,3",new Queen(playerId,new Coordinate(0,3)));
...@@ -101,8 +101,8 @@ public class Player{ ...@@ -101,8 +101,8 @@ public class Player{
pieces.put("0,5",new Bishop(playerId,new Coordinate(0,5))); 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,6",new Knight(playerId,new Coordinate(0,6)));
pieces.put("0,7",new Rook(playerId,new Coordinate(0,7))); 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 Pawn(playerId,new Coordinate(1,0)));
pieces.put("1,0",new Chameleon(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,1",new Pawn(playerId,new Coordinate(1,1)));
pieces.put("1,2",new Pawn(playerId,new Coordinate(1,2))); pieces.put("1,2",new Pawn(playerId,new Coordinate(1,2)));
pieces.put("1,3",new Pawn(playerId,new Coordinate(1,3))); pieces.put("1,3",new Pawn(playerId,new Coordinate(1,3)));
......
...@@ -46,16 +46,6 @@ public class ChameleonTest { ...@@ -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").getName(),"Bishop");
assertEquals(board.getPlayer(id.WHITE).getPlayerPieces().get("7,2") instanceof pieces.Chameleon, true); 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);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment