pawn moves check
This commit is contained in:
BIN
Board.class
BIN
Board.class
Binary file not shown.
10
Board.java
10
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;
|
||||
}
|
||||
}
|
||||
|
||||
24
Knight.java
Normal file
24
Knight.java
Normal file
@@ -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<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
BIN
Pawn.class
BIN
Pawn.class
Binary file not shown.
35
Pawn.java
35
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<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Piece.class
BIN
Piece.class
Binary file not shown.
16
Piece.java
16
Piece.java
@@ -3,16 +3,22 @@ import java.util.*;
|
||||
|
||||
public class Piece {
|
||||
ArrayList<Integer> 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<Position> getLegalMoves(Board board) {
|
||||
System.out.println("parent method not overriden");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Position.class
Normal file
BIN
Position.class
Normal file
Binary file not shown.
8
Position.java
Normal file
8
Position.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class Position {
|
||||
int x, y;
|
||||
|
||||
public Position(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user