Selection display + fixes to positions
parent
ca397ad4c4
commit
2dac55a528
|
@ -23,14 +23,14 @@ public class Board {
|
||||||
board[size()-4][0] = new King(this, true, size() - 4, 0);
|
board[size()-4][0] = new King(this, true, size() - 4, 0);
|
||||||
|
|
||||||
// black
|
// black
|
||||||
board[0][size()-1] = new Rook(this, false,0, 0);
|
board[0][size()-1] = new Rook(this, false,0, size()-1);
|
||||||
board[size()-1][size()-1] = new Rook(this, false, size() - 1, 0);
|
board[size()-1][size()-1] = new Rook(this, false, size() - 1, size()-1);
|
||||||
board[1][size()-1] = new Knight(this, false, 1, 0);
|
board[1][size()-1] = new Knight(this, false, 1, size()-1);
|
||||||
board[size()-2][size()-1] = new Knight(this, false, size() - 2, 0);
|
board[size()-2][size()-1] = new Knight(this, false, size() - 2, size()-1);
|
||||||
board[2][size()-1] = new Bishop(this, false, 2, 0);
|
board[2][size()-1] = new Bishop(this, false, 2, size()-1);
|
||||||
board[size()-3][size()-1] = new Bishop(this, false, size() - 3, 0);
|
board[size()-3][size()-1] = new Bishop(this, false, size() - 3, size()-1);
|
||||||
board[3][size()-1] = new Queen(this, false, 3, 0);
|
board[3][size()-1] = new Queen(this, false, 3, size()-1);
|
||||||
board[size()-4][size()-1] = new King(this, false, size() - 4, 0);
|
board[size()-4][size()-1] = new King(this, false, size() - 4, size()-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean movePiece(int x, int y, int newX, int newY){
|
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){
|
public ChessPiece get(int x, int y){
|
||||||
if (x < 0 || x > board.length)
|
if (x < 0 || x >= board.length)
|
||||||
return null;
|
return null;
|
||||||
if (y < 0 || y > board.length)
|
if (y < 0 || y >= board.length)
|
||||||
return null;
|
return null;
|
||||||
return board[x][y];
|
return board[x][y];
|
||||||
}
|
}
|
||||||
|
@ -76,9 +76,9 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean set(int x, int y, ChessPiece piece){
|
protected boolean set(int x, int y, ChessPiece piece){
|
||||||
if (x < 0 || x > board.length)
|
if (x < 0 || x >= board.length)
|
||||||
return false;
|
return false;
|
||||||
if (y < 0 || y > board.length)
|
if (y < 0 || y >= board.length)
|
||||||
return false;
|
return false;
|
||||||
board[x][y] = piece;
|
board[x][y] = piece;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -39,19 +39,19 @@ public abstract class ChessPiece {
|
||||||
|
|
||||||
protected ArrayList<Move> getCardinalMoves(int length){
|
protected ArrayList<Move> getCardinalMoves(int length){
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
for (int i = 0; i < length; i++){
|
for (int i = 1; i <= length; i++){
|
||||||
// cardinals
|
// cardinals
|
||||||
moves.add(new Move(x - i, 0));
|
moves.add(new Move(x - i, y));
|
||||||
moves.add(new Move(x + i, 0));
|
moves.add(new Move(x + i, y));
|
||||||
moves.add(new Move(0, y - i));
|
moves.add(new Move(x, y - i));
|
||||||
moves.add(new Move(0, y + i));
|
moves.add(new Move(x, y + i));
|
||||||
}
|
}
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ArrayList<Move> getDiagonalMoves(int length){
|
protected ArrayList<Move> getDiagonalMoves(int length){
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
for (int i = 0; i < length; i++){
|
for (int i = 1; i <= length; i++){
|
||||||
// cardinals
|
// cardinals
|
||||||
moves.add(new Move(x - i, y - i));
|
moves.add(new Move(x - i, y - i));
|
||||||
moves.add(new Move(x + i, y + i));
|
moves.add(new Move(x + i, y + i));
|
||||||
|
|
|
@ -7,8 +7,8 @@ import static ui.Display.loadImage;
|
||||||
|
|
||||||
public class King extends ChessPiece {
|
public class King extends ChessPiece {
|
||||||
|
|
||||||
private Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png");
|
private final Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png");
|
||||||
private Image blackKing = loadImage("./resources/chess_piece_2_white_king.png");
|
private final Image blackKing = loadImage("./resources/chess_piece_2_white_king.png");
|
||||||
|
|
||||||
public King(Board b, boolean isWhite, int x, int y) {
|
public King(Board b, boolean isWhite, int x, int y) {
|
||||||
super(b,isWhite,x,y);
|
super(b,isWhite,x,y);
|
||||||
|
|
|
@ -23,31 +23,41 @@ public class Pawn extends ChessPiece {
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Move> getMoves() {
|
public ArrayList<Move> getMoves() {
|
||||||
ArrayList<Move> moves = new ArrayList<Move>();
|
ArrayList<Move> moves = new ArrayList<Move>();
|
||||||
|
if (isWhite) {
|
||||||
moves.add(new Move(x, y + 1));
|
moves.add(new Move(x, y + 1));
|
||||||
if (isFirstMove)
|
if (isFirstMove)
|
||||||
moves.add(new Move(x, y + 2));
|
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;
|
ChessPiece neighbour = null;
|
||||||
|
|
||||||
if (isWhite){
|
if (isWhite){
|
||||||
// En passant
|
// 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));
|
moves.add(new Move(x-1, y + 1, Move.SpecialConditions.leftEnPassant));
|
||||||
// En passant
|
// 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));
|
moves.add(new Move(x+1, + 1, Move.SpecialConditions.rightEnPassant));
|
||||||
} else {
|
} else {
|
||||||
// unfortunately have to flip the direction depending on player type
|
// unfortunately have to flip the direction depending on player type
|
||||||
// En passant
|
// 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));
|
moves.add(new Move(x-1, y - 1, Move.SpecialConditions.leftEnPassant));
|
||||||
// En passant
|
// 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));
|
moves.add(new Move(x+1, - 1, Move.SpecialConditions.rightEnPassant));
|
||||||
}
|
}
|
||||||
|
|
||||||
return moves;
|
return moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkNeighbourEnPassant(ChessPiece neighbour){
|
||||||
|
return neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove() && neighbour.isWhite != this.isWhite;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applySpecialMove(Move moveWithSpecial){
|
public void applySpecialMove(Move moveWithSpecial){
|
||||||
var specialCondition = moveWithSpecial.getSpecialCondition();
|
var specialCondition = moveWithSpecial.getSpecialCondition();
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import chess.Board;
|
import chess.Board;
|
||||||
|
import chess.Move;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.text.AttributeSet;
|
import javax.swing.text.AttributeSet;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.AttributedString;
|
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;
|
private Board b;
|
||||||
|
|
||||||
|
@ -21,6 +29,7 @@ public class Display extends JFrame {
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
this.setEnabled(true);
|
this.setEnabled(true);
|
||||||
this.setTitle("Ratatta");
|
this.setTitle("Ratatta");
|
||||||
|
this.addMouseListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -29,9 +38,35 @@ public class Display extends JFrame {
|
||||||
for (int i = 0; i < b.size(); i++){
|
for (int i = 0; i < b.size(); i++){
|
||||||
for (int j = 0; j < b.size(); j++){
|
for (int j = 0; j < b.size(); j++){
|
||||||
if (b.get(i,j) != null)
|
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(){
|
public boolean update(){
|
||||||
|
@ -45,4 +80,30 @@ public class Display extends JFrame {
|
||||||
throw new RuntimeException(e);
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue