Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class ChessGame {
- public class Piece {
- int x;
- int y;
- boolean isDead;
- int color;
- public Piece(int x, int y, boolean isDead, int color) {
- super();
- this.x = x;
- this.y = y;
- this.isDead = isDead;
- this.color = color;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public boolean isDead() {
- return isDead;
- }
- public void setDead(boolean isDead) {
- this.isDead = isDead;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return false;
- }
- }
- public class Square {
- int x;
- int y;
- Piece piece;
- int color;
- public Square(int x, int y) {
- super();
- this.x = x;
- this.y = y;
- piece = null;
- }
- public Piece occupySpot(Piece p) {
- Piece old = this.piece;
- if (old != null) {
- old.isDead = true;
- }
- this.piece = p;
- return old;
- }
- public boolean isOccupied() {
- return this.piece != null;
- }
- public Piece releaseSpot() {
- Piece old = this.piece;
- this.piece = null;
- return old;
- }
- public Piece getPiece() {
- return piece;
- }
- }
- public class King extends Piece {
- public King(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public class Rook extends Piece {
- public Rook(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public class Pawn extends Piece {
- public Pawn(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public class Knight extends Piece {
- public Knight(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public class Bishop extends Piece {
- public Bishop(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public class Queen extends Piece {
- public Queen(int x, int y, boolean isDead, int color) {
- super(x, y, isDead, color);
- }
- @Override
- public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
- return true;
- }
- }
- public static class Commands {
- int curX, curY, desX, desY;
- Piece piece;
- public Commands(int curX, int curY, int desX, int desY, Piece piece) {
- super();
- this.curX = curX;
- this.curY = curY;
- this.desX = desX;
- this.desY = desY;
- this.piece = piece;
- }
- public Piece getPiece() {
- return piece;
- }
- }
- public class Player {
- List<Piece> pieces = new ArrayList<Piece>();
- List<Commands> commands = new ArrayList<Commands>();
- int color;
- public Player(int color) {
- super();
- this.color = color;
- initializePieces(color);
- }
- public void initializePieces(int color) {
- if (this.color == 1) {
- for (int i = 0; i < 8; i++) { // draw pawns
- pieces.add(new Pawn(2, i, true, 1));
- }
- pieces.add(new Rook(0, 0, true, 1));
- pieces.add(new Rook(0, 7, true, 1));
- pieces.add(new Bishop(0, 2, true, 1));
- pieces.add(new Bishop(0, 5, true, 1));
- pieces.add(new Knight(0, 1, true, 1));
- pieces.add(new Knight(0, 6, true, 1));
- pieces.add(new Queen(0, 3, true, 1));
- pieces.add(new King(0, 4, true, 1));
- } else {
- for (int i = 0; i < 8; i++) { // draw pawns
- pieces.add(new Pawn(6, i, true, 0));
- }
- pieces.add(new Rook(7, 0, true, 0));
- pieces.add(new Rook(7, 7, true, 0));
- pieces.add(new Bishop(7, 2, true, 0));
- pieces.add(new Bishop(7, 5, true, 0));
- pieces.add(new Knight(7, 1, true, 0));
- pieces.add(new Knight(7, 6, true, 0));
- pieces.add(new Queen(7, 3, true, 0));
- pieces.add(new King(7, 4, true, 0));
- }
- }
- public List<Piece> getPieces() {
- // TODO Auto-generated method stub
- return pieces;
- }
- public Commands getCurrentCmd() {
- return commands.get(commands.size() - 1);
- }
- public void removeCurrentCmd() {
- commands.remove(commands.size() - 1);
- }
- public void addCommand(Commands cmd) {
- commands.add(cmd);
- // TODO Auto-generated method stub
- }
- }
- public static class Board {
- Square[][] spots;
- private boolean win; // mark the win or not
- public Board() {
- win = false;
- spots = new Square[8][8];
- }
- public void initialize(Player p) {
- for (int i = 0; i < p.getPieces().size(); i++) {
- spots[p.getPieces().get(i).getX()][p.getPieces().get(i).getY()].occupySpot(p.getPieces().get(i));
- }
- }
- public boolean executeMove(Player p) {
- Commands cmd = p.getCurrentCmd();
- Piece piece = cmd.getPiece();
- if (!piece.isValid(this, cmd.curX, cmd.curY, cmd.desX, cmd.desY)) {
- p.removeCurrentCmd();
- return false;
- }
- if (spots[cmd.desX][cmd.desY] != null && spots[cmd.desX][cmd.desY].color == piece.color) {
- return false;
- }
- Piece taken = spots[cmd.desX][cmd.desY].occupySpot(piece);
- if (taken != null && taken instanceof King) {
- this.win = true;
- }
- spots[cmd.curX][cmd.curY].releaseSpot();
- return true;
- }
- public boolean getWin() {
- return win;
- }
- }
- public static class Game {
- final static Board board = new Board();
- Player p1;
- Player p2;
- public boolean enterPlayer(Player p) {
- if (p1 == null)
- this.p1 = p;
- else if (p2 == null)
- this.p2 = p;
- else
- return false;
- board.initialize(p);
- return true;
- }
- public void processTurn(Player p) {
- do {
- // sample command
- Commands cmd = new Commands(p.getPieces().get(0).x, p.getPieces().get(0).y, 2, 3, p.getPieces().get(0));
- p.addCommand(cmd);
- } while (!board.executeMove(p));
- }
- public void startGame() {
- // player enter the game:
- // enterPlayer(new ComputerPlayer("Computer"));
- // enterPlayer(new HumanPlayer("Bill"));
- while (true) {
- processTurn(p1);
- if (this.board.getWin()) {
- System.out.println("P1 win!");
- break;
- }
- processTurn(p2);
- if (this.board.getWin()) {
- System.out.println("P2 win!");
- break;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment