added captures, movement, legalmoves hover, and basics of game
This commit is contained in:
@@ -24,6 +24,6 @@ public class Bishop extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
return null;
|
return getPseudoLegalMoves(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
Board.java
32
Board.java
@@ -26,6 +26,28 @@ public class Board {
|
|||||||
board = new Piece[8][8];
|
board = new Piece[8][8];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void capture(Piece capturing, Piece captured) {
|
||||||
|
board[capturing.pos.x][capturing.pos.y] = null;
|
||||||
|
board[captured.pos.x][captured.pos.y] = capturing;
|
||||||
|
capturing.pos.x = captured.pos.x;
|
||||||
|
capturing.pos.y = captured.pos.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(Piece p, Position posi) {
|
||||||
|
board[p.pos.x][p.pos.y] = null;
|
||||||
|
board[posi.x][posi.y] = p;
|
||||||
|
p.pos = posi;
|
||||||
|
if (p instanceof Pawn) {
|
||||||
|
((Pawn) p).hasMoved = true;
|
||||||
|
}
|
||||||
|
if (p instanceof King) {
|
||||||
|
((King) p).hasMoved = true;
|
||||||
|
}
|
||||||
|
if (p instanceof Rook) {
|
||||||
|
((Rook) p).hasMoved = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
for (Piece[] row : board) {
|
for (Piece[] row : board) {
|
||||||
for (Piece p : row) {
|
for (Piece p : row) {
|
||||||
@@ -43,11 +65,17 @@ public class Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Piece getPiece(Position pos) {
|
public Piece getPiece(Position pos) {
|
||||||
return this.board[pos.x][pos.y];
|
if (inBounds(pos)) {
|
||||||
|
return this.board[pos.x][pos.y];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Piece getPiece(int x, int y) {
|
public Piece getPiece(int x, int y) {
|
||||||
return this.board[x][y];
|
if (inBounds(new Position(x, y))) {
|
||||||
|
return this.board[x][y];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(int x, int y, Piece p) {
|
public void setPiece(int x, int y, Piece p) {
|
||||||
|
|||||||
61
Chess.java
61
Chess.java
@@ -1,9 +1,10 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
public class Chess extends JPanel implements ActionListener {
|
public class Chess extends JPanel implements ActionListener, MouseListener {
|
||||||
|
|
||||||
// pieces stuff
|
// pieces stuff
|
||||||
Board board;
|
Board board;
|
||||||
@@ -14,6 +15,10 @@ public class Chess extends JPanel implements ActionListener {
|
|||||||
Timer gameTimer;
|
Timer gameTimer;
|
||||||
Color creme = new Color(254, 245, 218);
|
Color creme = new Color(254, 245, 218);
|
||||||
Color brown = new Color(121, 92, 50);
|
Color brown = new Color(121, 92, 50);
|
||||||
|
Piece selected;
|
||||||
|
ArrayList<Position> legalMoves;
|
||||||
|
Image hover;
|
||||||
|
String turnColor;
|
||||||
|
|
||||||
public Chess(int boardWidth, int boardHeight) {
|
public Chess(int boardWidth, int boardHeight) {
|
||||||
this.boardWidth = boardWidth;
|
this.boardWidth = boardWidth;
|
||||||
@@ -21,11 +26,16 @@ public class Chess extends JPanel implements ActionListener {
|
|||||||
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
|
||||||
setBackground(Color.WHITE);
|
setBackground(Color.WHITE);
|
||||||
setFocusable(true);
|
setFocusable(true);
|
||||||
|
addMouseListener(this);
|
||||||
|
|
||||||
board = new Board();
|
board = new Board();
|
||||||
|
selected = null;
|
||||||
|
legalMoves = null;
|
||||||
|
turnColor = "White";
|
||||||
gameTimer = new Timer(200, this);
|
gameTimer = new Timer(200, this);
|
||||||
|
|
||||||
|
hover = new ImageIcon("sprites/hover.png").getImage();
|
||||||
|
|
||||||
gameTimer.start();
|
gameTimer.start();
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
@@ -44,6 +54,13 @@ public class Chess extends JPanel implements ActionListener {
|
|||||||
|
|
||||||
// draw pieces
|
// draw pieces
|
||||||
board.draw(g);
|
board.draw(g);
|
||||||
|
|
||||||
|
// draw legalMoves
|
||||||
|
if (legalMoves != null) {
|
||||||
|
for (Position p : legalMoves) {
|
||||||
|
g.drawImage(hover, (p.x + 1) * 40, (p.y + 1) * 40, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
@@ -55,4 +72,44 @@ public class Chess extends JPanel implements ActionListener {
|
|||||||
gameLoop();
|
gameLoop();
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void mouseEntered(MouseEvent e) {}
|
||||||
|
|
||||||
|
public void mousePressed(MouseEvent e) {}
|
||||||
|
|
||||||
|
public void mouseReleased(MouseEvent e) {}
|
||||||
|
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
int col = (e.getX() / 40) - 1;
|
||||||
|
int row = (e.getY() / 40) - 1;
|
||||||
|
Position pos = new Position(col, row);
|
||||||
|
Piece newSelected = board.getPiece(col, row);
|
||||||
|
// if selecting one of our own pieces
|
||||||
|
if (newSelected != null && newSelected.color.equals(turnColor)) {
|
||||||
|
legalMoves = newSelected.getLegalMoves(board);
|
||||||
|
selected = newSelected;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// if selecting an enemy piece -- must have one of ours selected
|
||||||
|
else if (newSelected != null
|
||||||
|
&& selected != null
|
||||||
|
&& selected.color.equals(turnColor)
|
||||||
|
&& !newSelected.color.equals(turnColor)
|
||||||
|
&& legalMoves.contains(pos)) {
|
||||||
|
board.capture(selected, newSelected);
|
||||||
|
selected = null;
|
||||||
|
legalMoves = null;
|
||||||
|
turnColor = turnColor.equals("White") ? "Black" : "White";
|
||||||
|
}
|
||||||
|
// if selecting a blank square -- must have one of ours selected
|
||||||
|
else if (selected != null && selected.color.equals(turnColor) && legalMoves.contains(pos)) {
|
||||||
|
|
||||||
|
board.move(selected, pos);
|
||||||
|
selected = null;
|
||||||
|
legalMoves = null;
|
||||||
|
turnColor = turnColor.equals("White") ? "Black" : "White";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mouseExited(MouseEvent e) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,6 @@ public class Knight extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
return null;
|
return getPseudoLegalMoves(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,6 @@ public class Pawn extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
return null;
|
return getPseudoLegalMoves(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import java.awt.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public abstract class Piece {
|
public abstract class Piece {
|
||||||
|
public static int SIZE = 40;
|
||||||
Position pos;
|
Position pos;
|
||||||
Image sprite;
|
Image sprite;
|
||||||
String color;
|
String color;
|
||||||
|
|||||||
@@ -5,4 +5,9 @@ public class Position {
|
|||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
Position pos = (Position) o;
|
||||||
|
return pos.x == this.x && pos.y == this.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,6 @@ public class Queen extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
return null;
|
return getPseudoLegalMoves(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,6 @@ public class Rook extends Piece {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
return null;
|
return getPseudoLegalMoves(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Sprites/hover.png
Normal file
BIN
Sprites/hover.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 299 B |
Reference in New Issue
Block a user