mjain

Chess Game OOPS Design

Jul 24th, 2019
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1.  
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class ChessGame {
  7.  
  8.     public class Piece {
  9.         int     x;
  10.         int     y;
  11.         boolean isDead;
  12.         int     color;
  13.  
  14.         public Piece(int x, int y, boolean isDead, int color) {
  15.             super();
  16.             this.x = x;
  17.             this.y = y;
  18.             this.isDead = isDead;
  19.             this.color = color;
  20.         }
  21.  
  22.         public int getX() {
  23.             return x;
  24.         }
  25.  
  26.         public void setX(int x) {
  27.             this.x = x;
  28.         }
  29.  
  30.         public int getY() {
  31.             return y;
  32.         }
  33.  
  34.         public void setY(int y) {
  35.             this.y = y;
  36.         }
  37.  
  38.         public boolean isDead() {
  39.             return isDead;
  40.         }
  41.  
  42.         public void setDead(boolean isDead) {
  43.             this.isDead = isDead;
  44.         }
  45.  
  46.         public int getColor() {
  47.             return color;
  48.         }
  49.  
  50.         public void setColor(int color) {
  51.             this.color = color;
  52.         }
  53.  
  54.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  55.             return false;
  56.         }
  57.  
  58.     }
  59.  
  60.     public class Square {
  61.         int   x;
  62.         int   y;
  63.         Piece piece;
  64.         int   color;
  65.  
  66.         public Square(int x, int y) {
  67.             super();
  68.             this.x = x;
  69.             this.y = y;
  70.             piece = null;
  71.         }
  72.  
  73.         public Piece occupySpot(Piece p) {
  74.             Piece old = this.piece;
  75.             if (old != null) {
  76.                 old.isDead = true;
  77.             }
  78.             this.piece = p;
  79.             return old;
  80.         }
  81.  
  82.         public boolean isOccupied() {
  83.             return this.piece != null;
  84.         }
  85.  
  86.         public Piece releaseSpot() {
  87.             Piece old = this.piece;
  88.             this.piece = null;
  89.             return old;
  90.         }
  91.  
  92.         public Piece getPiece() {
  93.             return piece;
  94.         }
  95.  
  96.     }
  97.  
  98.     public class King extends Piece {
  99.         public King(int x, int y, boolean isDead, int color) {
  100.             super(x, y, isDead, color);
  101.         }
  102.  
  103.         @Override
  104.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  105.             return true;
  106.         }
  107.     }
  108.     public class Rook extends Piece {
  109.         public Rook(int x, int y, boolean isDead, int color) {
  110.             super(x, y, isDead, color);
  111.         }
  112.  
  113.         @Override
  114.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  115.             return true;
  116.         }
  117.     }
  118.     public class Pawn extends Piece {
  119.         public Pawn(int x, int y, boolean isDead, int color) {
  120.             super(x, y, isDead, color);
  121.         }
  122.  
  123.         @Override
  124.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  125.             return true;
  126.         }
  127.     }
  128.  
  129.     public class Knight extends Piece {
  130.         public Knight(int x, int y, boolean isDead, int color) {
  131.             super(x, y, isDead, color);
  132.         }
  133.  
  134.         @Override
  135.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  136.             return true;
  137.         }
  138.     }
  139.  
  140.     public class Bishop extends Piece {
  141.         public Bishop(int x, int y, boolean isDead, int color) {
  142.             super(x, y, isDead, color);
  143.         }
  144.  
  145.         @Override
  146.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  147.             return true;
  148.         }
  149.     }
  150.     public class Queen extends Piece {
  151.         public Queen(int x, int y, boolean isDead, int color) {
  152.             super(x, y, isDead, color);
  153.         }
  154.  
  155.         @Override
  156.         public boolean isValid(Board board, int fromX, int fromY, int toX, int toY) {
  157.             return true;
  158.         }
  159.     }
  160.     public static class Commands {
  161.         int   curX, curY, desX, desY;
  162.  
  163.         Piece piece;
  164.  
  165.         public Commands(int curX, int curY, int desX, int desY, Piece piece) {
  166.             super();
  167.             this.curX = curX;
  168.             this.curY = curY;
  169.             this.desX = desX;
  170.             this.desY = desY;
  171.             this.piece = piece;
  172.         }
  173.  
  174.         public Piece getPiece() {
  175.             return piece;
  176.         }
  177.  
  178.     }
  179.  
  180.     public class Player {
  181.         List<Piece>    pieces   = new ArrayList<Piece>();
  182.         List<Commands> commands = new ArrayList<Commands>();
  183.         int            color;
  184.  
  185.         public Player(int color) {
  186.             super();
  187.             this.color = color;
  188.             initializePieces(color);
  189.         }
  190.  
  191.         public void initializePieces(int color) {
  192.             if (this.color == 1) {
  193.                 for (int i = 0; i < 8; i++) { // draw pawns
  194.                     pieces.add(new Pawn(2, i, true, 1));
  195.                 }
  196.                 pieces.add(new Rook(0, 0, true, 1));
  197.                 pieces.add(new Rook(0, 7, true, 1));
  198.                 pieces.add(new Bishop(0, 2, true, 1));
  199.                 pieces.add(new Bishop(0, 5, true, 1));
  200.                 pieces.add(new Knight(0, 1, true, 1));
  201.                 pieces.add(new Knight(0, 6, true, 1));
  202.                 pieces.add(new Queen(0, 3, true, 1));
  203.                 pieces.add(new King(0, 4, true, 1));
  204.             } else {
  205.                 for (int i = 0; i < 8; i++) { // draw pawns
  206.                     pieces.add(new Pawn(6, i, true, 0));
  207.                 }
  208.                 pieces.add(new Rook(7, 0, true, 0));
  209.                 pieces.add(new Rook(7, 7, true, 0));
  210.                 pieces.add(new Bishop(7, 2, true, 0));
  211.                 pieces.add(new Bishop(7, 5, true, 0));
  212.                 pieces.add(new Knight(7, 1, true, 0));
  213.                 pieces.add(new Knight(7, 6, true, 0));
  214.                 pieces.add(new Queen(7, 3, true, 0));
  215.                 pieces.add(new King(7, 4, true, 0));
  216.             }
  217.         }
  218.  
  219.         public List<Piece> getPieces() {
  220.             // TODO Auto-generated method stub
  221.             return pieces;
  222.         }
  223.  
  224.         public Commands getCurrentCmd() {
  225.             return commands.get(commands.size() - 1);
  226.         }
  227.  
  228.         public void removeCurrentCmd() {
  229.             commands.remove(commands.size() - 1);
  230.         }
  231.  
  232.         public void addCommand(Commands cmd) {
  233.             commands.add(cmd);
  234.             // TODO Auto-generated method stub
  235.  
  236.         }
  237.     }
  238.  
  239.     public static class Board {
  240.         Square[][]      spots;
  241.         private boolean win;  // mark the win or not
  242.  
  243.         public Board() {
  244.             win = false;
  245.             spots = new Square[8][8];
  246.         }
  247.  
  248.         public void initialize(Player p) {
  249.             for (int i = 0; i < p.getPieces().size(); i++) {
  250.                 spots[p.getPieces().get(i).getX()][p.getPieces().get(i).getY()].occupySpot(p.getPieces().get(i));
  251.             }
  252.         }
  253.  
  254.         public boolean executeMove(Player p) {
  255.             Commands cmd = p.getCurrentCmd();
  256.             Piece piece = cmd.getPiece();
  257.             if (!piece.isValid(this, cmd.curX, cmd.curY, cmd.desX, cmd.desY)) {
  258.                 p.removeCurrentCmd();
  259.                 return false;
  260.             }
  261.             if (spots[cmd.desX][cmd.desY] != null && spots[cmd.desX][cmd.desY].color == piece.color) {
  262.                 return false;
  263.             }
  264.  
  265.             Piece taken = spots[cmd.desX][cmd.desY].occupySpot(piece);
  266.             if (taken != null && taken instanceof King) {
  267.                 this.win = true;
  268.             }
  269.             spots[cmd.curX][cmd.curY].releaseSpot();
  270.             return true;
  271.  
  272.         }
  273.  
  274.         public boolean getWin() {
  275.             return win;
  276.         }
  277.     }
  278.  
  279.     public static class Game {
  280.         final static Board board = new Board();
  281.         Player             p1;
  282.         Player             p2;
  283.  
  284.         public boolean enterPlayer(Player p) {
  285.             if (p1 == null)
  286.                 this.p1 = p;
  287.             else if (p2 == null)
  288.                 this.p2 = p;
  289.             else
  290.                 return false;
  291.  
  292.             board.initialize(p);
  293.             return true;
  294.         }
  295.  
  296.         public void processTurn(Player p) {
  297.             do {
  298.                 // sample command
  299.                 Commands cmd = new Commands(p.getPieces().get(0).x, p.getPieces().get(0).y, 2, 3, p.getPieces().get(0));
  300.                 p.addCommand(cmd);
  301.             } while (!board.executeMove(p));
  302.         }
  303.  
  304.         public void startGame() {
  305.             // player enter the game:
  306.             // enterPlayer(new ComputerPlayer("Computer"));
  307.             // enterPlayer(new HumanPlayer("Bill"));
  308.  
  309.             while (true) {
  310.                 processTurn(p1);
  311.                 if (this.board.getWin()) {
  312.                     System.out.println("P1 win!");
  313.                     break;
  314.                 }
  315.                 processTurn(p2);
  316.                 if (this.board.getWin()) {
  317.                     System.out.println("P2 win!");
  318.                     break;
  319.                 }
  320.             }
  321.         }
  322.     }
  323.  
  324. }
Add Comment
Please, Sign In to add comment