Selection display + fixes to positions

main
Brett 2022-12-17 12:54:05 -05:00
parent ca397ad4c4
commit 2dac55a528
5 changed files with 100 additions and 29 deletions

View File

@ -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;

View File

@ -39,19 +39,19 @@ public abstract class ChessPiece {
protected ArrayList<Move> getCardinalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
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<Move> getDiagonalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
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));

View File

@ -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);

View File

@ -23,31 +23,41 @@ public class Pawn extends ChessPiece {
@Override
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
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();

View File

@ -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) {
}
}