From 2dac55a528fb1e24ee46b372c7a30339717140c6 Mon Sep 17 00:00:00 2001 From: Brett Date: Sat, 17 Dec 2022 12:54:05 -0500 Subject: [PATCH] Selection display + fixes to positions --- src/chess/Board.java | 24 +++++++-------- src/chess/ChessPiece.java | 12 ++++---- src/chess/King.java | 4 +-- src/chess/Pawn.java | 24 ++++++++++----- src/ui/Display.java | 65 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/chess/Board.java b/src/chess/Board.java index 09fa632..4de2dd1 100644 --- a/src/chess/Board.java +++ b/src/chess/Board.java @@ -23,14 +23,14 @@ public class Board { board[size()-4][0] = new King(this, true, size() - 4, 0); // black - board[0][size()-1] = new Rook(this, false,0, 0); - board[size()-1][size()-1] = new Rook(this, false, size() - 1, 0); - board[1][size()-1] = new Knight(this, false, 1, 0); - board[size()-2][size()-1] = new Knight(this, false, size() - 2, 0); - board[2][size()-1] = new Bishop(this, false, 2, 0); - board[size()-3][size()-1] = new Bishop(this, false, size() - 3, 0); - board[3][size()-1] = new Queen(this, false, 3, 0); - board[size()-4][size()-1] = new King(this, false, size() - 4, 0); + board[0][size()-1] = new Rook(this, false,0, size()-1); + board[size()-1][size()-1] = new Rook(this, false, size() - 1, size()-1); + board[1][size()-1] = new Knight(this, false, 1, size()-1); + board[size()-2][size()-1] = new Knight(this, false, size() - 2, size()-1); + board[2][size()-1] = new Bishop(this, false, 2, size()-1); + board[size()-3][size()-1] = new Bishop(this, false, size() - 3, size()-1); + board[3][size()-1] = new Queen(this, false, 3, size()-1); + board[size()-4][size()-1] = new King(this, false, size() - 4, size()-1); } public boolean movePiece(int x, int y, int newX, int newY){ @@ -64,9 +64,9 @@ public class Board { } public ChessPiece get(int x, int y){ - if (x < 0 || x > board.length) + if (x < 0 || x >= board.length) return null; - if (y < 0 || y > board.length) + if (y < 0 || y >= board.length) return null; return board[x][y]; } @@ -76,9 +76,9 @@ public class Board { } protected boolean set(int x, int y, ChessPiece piece){ - if (x < 0 || x > board.length) + if (x < 0 || x >= board.length) return false; - if (y < 0 || y > board.length) + if (y < 0 || y >= board.length) return false; board[x][y] = piece; return true; diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index 79b394f..caaec22 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -39,19 +39,19 @@ public abstract class ChessPiece { protected ArrayList getCardinalMoves(int length){ ArrayList moves = new ArrayList(); - for (int i = 0; i < length; i++){ + for (int i = 1; i <= length; i++){ // cardinals - moves.add(new Move(x - i, 0)); - moves.add(new Move(x + i, 0)); - moves.add(new Move(0, y - i)); - moves.add(new Move(0, y + i)); + moves.add(new Move(x - i, y)); + moves.add(new Move(x + i, y)); + moves.add(new Move(x, y - i)); + moves.add(new Move(x, y + i)); } return moves; } protected ArrayList getDiagonalMoves(int length){ ArrayList moves = new ArrayList(); - for (int i = 0; i < length; i++){ + for (int i = 1; i <= length; i++){ // cardinals moves.add(new Move(x - i, y - i)); moves.add(new Move(x + i, y + i)); diff --git a/src/chess/King.java b/src/chess/King.java index eaf8798..bc3f615 100644 --- a/src/chess/King.java +++ b/src/chess/King.java @@ -7,8 +7,8 @@ import static ui.Display.loadImage; public class King extends ChessPiece { - private Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png"); - private Image blackKing = loadImage("./resources/chess_piece_2_white_king.png"); + private final Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png"); + private final Image blackKing = loadImage("./resources/chess_piece_2_white_king.png"); public King(Board b, boolean isWhite, int x, int y) { super(b,isWhite,x,y); diff --git a/src/chess/Pawn.java b/src/chess/Pawn.java index 083ea64..2127631 100644 --- a/src/chess/Pawn.java +++ b/src/chess/Pawn.java @@ -23,31 +23,41 @@ public class Pawn extends ChessPiece { @Override public ArrayList getMoves() { ArrayList moves = new ArrayList(); - moves.add(new Move(x, y + 1)); - if (isFirstMove) - moves.add(new Move(x, y + 2)); + if (isWhite) { + moves.add(new Move(x, y + 1)); + if (isFirstMove) + moves.add(new Move(x, y + 2)); + } else { + moves.add(new Move(x, y - 1)); + if (isFirstMove) + moves.add(new Move(x, y - 2)); + } ChessPiece neighbour = null; if (isWhite){ // En passant - if ((neighbour = b.get(x-1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour)) moves.add(new Move(x-1, y + 1, Move.SpecialConditions.leftEnPassant)); // En passant - if ((neighbour = b.get(x+1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour)) moves.add(new Move(x+1, + 1, Move.SpecialConditions.rightEnPassant)); } else { // unfortunately have to flip the direction depending on player type // En passant - if ((neighbour = b.get(x-1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour)) moves.add(new Move(x-1, y - 1, Move.SpecialConditions.leftEnPassant)); // En passant - if ((neighbour = b.get(x+1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour)) moves.add(new Move(x+1, - 1, Move.SpecialConditions.rightEnPassant)); } return moves; } + private boolean checkNeighbourEnPassant(ChessPiece neighbour){ + return neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove() && neighbour.isWhite != this.isWhite; + } + @Override public void applySpecialMove(Move moveWithSpecial){ var specialCondition = moveWithSpecial.getSpecialCondition(); diff --git a/src/ui/Display.java b/src/ui/Display.java index 21849d7..fa43024 100644 --- a/src/ui/Display.java +++ b/src/ui/Display.java @@ -1,16 +1,24 @@ package ui; import chess.Board; +import chess.Move; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.text.AttributeSet; import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; import java.io.File; import java.io.IOException; import java.text.AttributedString; -public class Display extends JFrame { +public class Display extends JFrame implements MouseListener { + + private static int xOffset = 128, yOffset = 128; + private static int width = 64, height = 64; + private int movingPointX = -1, movingPointY = -1; + private int selectionPointX = -1, selectionPointY = -1; private Board b; @@ -21,6 +29,7 @@ public class Display extends JFrame { this.setVisible(true); this.setEnabled(true); this.setTitle("Ratatta"); + this.addMouseListener(this); } @Override @@ -29,9 +38,35 @@ public class Display extends JFrame { for (int i = 0; i < b.size(); i++){ for (int j = 0; j < b.size(); j++){ if (b.get(i,j) != null) - g.drawImage(b.get(i,j).getImage(), (i+2) * 64, (j+2) * 64, null); + g.drawImage(b.get(i,j).getImage(), xOffset + (i) * width, yOffset + (j) * height, null); } } + if (movingPointX != -1 && movingPointY != -1) { + var piece = b.get(movingPointX, movingPointY); + if (piece != null) { + var moves = piece.getMoves(); + for (Move m : moves) { + drawSelectionRect(g, m); + } + } + } + // handle user input (mouse clicking) + if (selectionPointX != -1 && selectionPointY != -1){ + int localPointX = (selectionPointX - xOffset) / width; + int localPointY = (b.size()-1) - ((selectionPointY - yOffset) / height); + var piece = b.get(localPointX, localPointY); + if (piece != null) { + movingPointX = localPointX; + movingPointY = localPointY; + } + System.out.println(localPointX + " " + localPointY); + selectionPointY = -1; + selectionPointX = -1; + } + } + + private void drawSelectionRect(Graphics g, Move m){ + g.drawRect(xOffset + (m.getX() * width), yOffset + ((b.size()-1 - m.getY()) * height), width, height); } public boolean update(){ @@ -45,4 +80,30 @@ public class Display extends JFrame { throw new RuntimeException(e); } } + + @Override + public void mouseClicked(MouseEvent mouseEvent) { + this.selectionPointX = mouseEvent.getX(); + this.selectionPointY = mouseEvent.getY(); + } + + @Override + public void mousePressed(MouseEvent mouseEvent) { + + } + + @Override + public void mouseReleased(MouseEvent mouseEvent) { + + } + + @Override + public void mouseEntered(MouseEvent mouseEvent) { + + } + + @Override + public void mouseExited(MouseEvent mouseEvent) { + + } }