diff --git a/Board.class b/Board.class index f8ab061..14395b1 100644 Binary files a/Board.class and b/Board.class differ diff --git a/Board.java b/Board.java index 80f0aa7..3ca5909 100644 --- a/Board.java +++ b/Board.java @@ -9,7 +9,7 @@ public class Board { board[i][2] = new Pawn(i + 1, 2, "Black"); } for (int i = 0; i <= 7; i++) { - board[i][7] = new Pawn(i + 1, 7, "Black"); + board[i][7] = new Pawn(i + 1, 7, "White"); } } @@ -20,4 +20,12 @@ public class Board { } } } + + public static boolean inBounds(Position pos) { + return (pos.x >= 0 && pos.x <= 7) && (pos.y >= 0 && pos.y <= 7); + } + + public boolean isOpen(Position pos) { + return this.board[pos.x][pos.y] == null; + } } diff --git a/Knight.java b/Knight.java new file mode 100644 index 0000000..95c7fb0 --- /dev/null +++ b/Knight.java @@ -0,0 +1,24 @@ +import java.util.*; +import javax.swing.ImageIcon; + +public class Knight extends Piece { + static int[] xDir = {-1, -2, -2, -1, 1, 2, 2, 1}; + static int[] yDir = {-2, -1, 1, 2, 2, 1, -1, -2}; + + public Knight(int x, int y, String color) { + super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage()); + } + + public ArrayList getLegalMoves(Board board) { + ArrayList positions = new ArrayList(); + + for (int i : xDir) { + for (int j : yDir) { + Position test = new Position(pos.x + i, pos.y + j); + if (!Board.inBounds(test)) continue; + } + } + + return positions; + } +} diff --git a/Pawn.class b/Pawn.class index 4ac6511..8d2957e 100644 Binary files a/Pawn.class and b/Pawn.class differ diff --git a/Pawn.java b/Pawn.java index 31d309a..cfcdbaa 100644 --- a/Pawn.java +++ b/Pawn.java @@ -1,9 +1,40 @@ -import java.awt.*; +import java.util.*; import javax.swing.ImageIcon; public class Pawn extends Piece { + boolean hasMoved; + int colorDir; public Pawn(int x, int y, String color) { - super(x, y, new ImageIcon("Sprites/" + color + "/pawn.png").getImage()); + super(x, y, color, new ImageIcon("sprites/" + color + "/pawn.png").getImage()); + hasMoved = false; + colorDir = color.equals("White") ? -1 : 1; + } + + public ArrayList getLegalMoves(Board board) { + ArrayList positions = new ArrayList(); + // diagonal moves (captures) + if (Board.inBounds(new Position(pos.x + 1, pos.y + colorDir)) + && !board.isOpen(new Position(pos.x + 1, pos.y + colorDir)) + && board.board[pos.x + 1][pos.y + colorDir].color != this.color) { + positions.add(new Position(pos.x + 1, pos.y + colorDir)); + } + if (Board.inBounds(new Position(pos.x - 1, pos.y + colorDir)) + && !board.isOpen(new Position(pos.x - 1, pos.y + colorDir)) + && board.board[pos.x + 1][pos.y + colorDir].color != this.color) { + positions.add(new Position(pos.x - 1, pos.y + colorDir)); + } + + // one square in front: if blocked return early + if (Board.inBounds(new Position(pos.x, pos.y + colorDir)) + && board.isOpen(new Position(pos.x, pos.y + colorDir))) { + positions.add(new Position(pos.x, pos.y + colorDir)); + } else return positions; + + // two squares in front + if (!hasMoved && board.isOpen(new Position(pos.x, pos.y + 2 * colorDir))) { + positions.add(new Position(pos.x, pos.y + 2 * colorDir)); + } + return positions; } } diff --git a/Piece.class b/Piece.class index 7985385..472ecfc 100644 Binary files a/Piece.class and b/Piece.class differ diff --git a/Piece.java b/Piece.java index 1cd1439..4acc428 100644 --- a/Piece.java +++ b/Piece.java @@ -3,16 +3,22 @@ import java.util.*; public class Piece { ArrayList legalMoves; - int x, y; + Position pos; Image sprite; + String color; - public Piece(int x, int y, Image sprite) { - this.x = x; - this.y = y; + public Piece(int x, int y, String color, Image sprite) { + this.pos = new Position(x, y); + this.color = color; this.sprite = sprite; } public void draw(Graphics g) { - g.drawImage(sprite, x * 40, y * 40, null); + g.drawImage(sprite, pos.x * 40, pos.y * 40, null); + } + + public ArrayList getLegalMoves(Board board) { + System.out.println("parent method not overriden"); + return null; } } diff --git a/Position.class b/Position.class new file mode 100644 index 0000000..3031a6b Binary files /dev/null and b/Position.class differ diff --git a/Position.java b/Position.java new file mode 100644 index 0000000..9edf1cd --- /dev/null +++ b/Position.java @@ -0,0 +1,8 @@ +public class Position { + int x, y; + + public Position(int x, int y) { + this.x = x; + this.y = y; + } +}