added rook, created bishop file
This commit is contained in:
72
Bishop.java
Normal file
72
Bishop.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
|
public class Bishop extends Piece {
|
||||||
|
boolean hasMoved;
|
||||||
|
|
||||||
|
public Bishop(int x, int y, String color) {
|
||||||
|
super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage());
|
||||||
|
hasMoved = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
|
ArrayList<Position> positions = new ArrayList<Position>();
|
||||||
|
|
||||||
|
// check left
|
||||||
|
for (int i = pos.x - 1; i >= 0; i--) {
|
||||||
|
Position test = new Position(i, pos.y);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check right
|
||||||
|
for (int i = pos.x + 1; i <= 7; i++) {
|
||||||
|
Position test = new Position(i, pos.y);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check up
|
||||||
|
for (int i = pos.y + 1; i <= 7; i++) {
|
||||||
|
Position test = new Position(pos.x, i);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check down
|
||||||
|
for (int i = pos.y - 1; i >= 0; i--) {
|
||||||
|
Position test = new Position(pos.x, i);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return positions;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Board.class
BIN
Board.class
Binary file not shown.
@@ -6,10 +6,10 @@ public class Board {
|
|||||||
public Board() {
|
public Board() {
|
||||||
board = new Piece[8][8];
|
board = new Piece[8][8];
|
||||||
for (int i = 0; i <= 7; i++) {
|
for (int i = 0; i <= 7; i++) {
|
||||||
board[i][2] = new Pawn(i + 1, 2, "Black");
|
board[i][1] = new Pawn(i, 1, "Black");
|
||||||
}
|
}
|
||||||
for (int i = 0; i <= 7; i++) {
|
for (int i = 0; i <= 7; i++) {
|
||||||
board[i][7] = new Pawn(i + 1, 7, "White");
|
board[i][6] = new Pawn(i, 6, "White");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
Knight.class
BIN
Knight.class
Binary file not shown.
BIN
Pawn.class
BIN
Pawn.class
Binary file not shown.
BIN
Piece.class
BIN
Piece.class
Binary file not shown.
@@ -1,8 +1,7 @@
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Piece {
|
public abstract class Piece {
|
||||||
ArrayList<Integer> legalMoves;
|
|
||||||
Position pos;
|
Position pos;
|
||||||
Image sprite;
|
Image sprite;
|
||||||
String color;
|
String color;
|
||||||
@@ -17,10 +16,7 @@ public class Piece {
|
|||||||
g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
|
g.drawImage(sprite, pos.x * 40, pos.y * 40, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Position> getLegalMoves(Board board) {
|
public abstract ArrayList<Position> getLegalMoves(Board board);
|
||||||
System.out.println("parent method not overriden");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean colorMatches(Piece p) {
|
public boolean colorMatches(Piece p) {
|
||||||
return this.color.equals(p.color);
|
return this.color.equals(p.color);
|
||||||
|
|||||||
BIN
Rook.class
Normal file
BIN
Rook.class
Normal file
Binary file not shown.
72
Rook.java
Normal file
72
Rook.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
|
public class Rook extends Piece {
|
||||||
|
boolean hasMoved;
|
||||||
|
|
||||||
|
public Rook(int x, int y, String color) {
|
||||||
|
super(x, y, color, new ImageIcon("sprites/" + color + "/rook.png").getImage());
|
||||||
|
hasMoved = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Position> getLegalMoves(Board board) {
|
||||||
|
ArrayList<Position> positions = new ArrayList<Position>();
|
||||||
|
|
||||||
|
// check left
|
||||||
|
for (int i = pos.x - 1; i >= 0; i--) {
|
||||||
|
Position test = new Position(i, pos.y);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check right
|
||||||
|
for (int i = pos.x + 1; i <= 7; i++) {
|
||||||
|
Position test = new Position(i, pos.y);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check up
|
||||||
|
for (int i = pos.y + 1; i <= 7; i++) {
|
||||||
|
Position test = new Position(pos.x, i);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check down
|
||||||
|
for (int i = pos.y - 1; i >= 0; i--) {
|
||||||
|
Position test = new Position(pos.x, i);
|
||||||
|
if (board.isOpen(test)) {
|
||||||
|
positions.add(test);
|
||||||
|
continue;
|
||||||
|
} else if (board.getPiece(test).colorMatches(this)) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
positions.add(test);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return positions;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user