Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.97 KB | None | 0 0
  1. /*Board.java*/
  2. import java.lang.Math;
  3. /**
  4.  * Represents a Board configuration of a game of Checkers61bl
  5.  * @author Kyle Liang
  6.  */
  7.  
  8. public class Board {
  9.  private Piece[][] board;
  10.  private int[][] color;
  11.  private int turn;
  12.  private int count_click;
  13.  private int count_move;
  14.  private int x1,x2,y1,y2; /**
  15.    *  Define any variables associated with a Board object here.  These
  16.    *  variables MUST be private.
  17.    */
  18.  
  19.   /**
  20.    * Constructs a new Board
  21.    * @param  shouldBeEmpty if true, add no pieces
  22.    *///check
  23.   public Board(boolean shouldBeEmpty) {
  24.     int i,j;
  25.     board= new Piece[8][8];
  26.     color= new int[8][8];
  27.     turn=0;
  28.     count_click=0;
  29.     count_move=0;
  30.     x1=0;
  31.     x2=0;
  32.     y1=0;
  33.     y2=0;
  34.     // YOUR CODE HERE
  35.     if(!shouldBeEmpty){
  36.      for(i=0;i<8;i++){
  37.         for(j=0;j<8;j++){
  38.         if(j==7){
  39.         if(i%2!=0){
  40.           this.board[i][j]=new Piece(1);
  41.         }
  42.     }
  43.        else if(j==5){
  44.         if(i%2!=0){
  45.         this.board[i][j]=new BombPiece(1);
  46.        }      
  47.     }
  48.        else if(j==6){
  49.         if(i%2==0){
  50.          this.board[i][j]=new ShieldPiece(1);
  51.       }
  52.     }
  53.    
  54.      
  55.         if(j==1){
  56.         if(i%2!=0){
  57.          this.board[i][j]=new ShieldPiece(0);
  58.       }
  59.     }
  60.         else if(j==2){
  61.         if(i%2==0){
  62.          this.board[i][j]=new BombPiece(0);
  63.       }
  64.     }
  65.        else if(j==0){
  66.         if(i%2==0){
  67.         this.board[i][j]=new Piece(0);
  68.          }
  69.         }
  70.       }
  71.     }
  72.   }
  73. }
  74.  
  75.   /**
  76.    * gets the Piece at coordinates (x, y)
  77.    * @param  x X-coordinate of Piece to get
  78.    * @param  y Y-coordinate of Piece to get
  79.    * @return   the Piece at (x, y)
  80.    *///check
  81.   public Piece pieceAt(int x, int y) {
  82.      if(x>=0&&y>=0&&x<=7&&y<=7)
  83.      return board[x][y];
  84.      else return null;
  85.     // YOUR CODE HERE
  86.   }
  87.  
  88.   /**
  89.    * Places a Piece at coordinate (x, y)
  90.    * @param p Piece to place
  91.    * @param x X coordinate of Piece to place
  92.    * @param y Y coordinate of Piece to place
  93.    *///check
  94.   public void place(Piece p, int x, int y) {
  95.     if(x>=0&&y>=0&&x<=7&&y<=7)
  96.       if(p!=null)
  97.         board[x][y]=p;
  98.   }
  99.  
  100.   /**
  101.    * Removes a Piece at coordinate (x, y)
  102.    * @param  x X coordinate of Piece to remove
  103.    * @param  y Y coordinate of Piece to remove
  104.    * @return   Piece that was removed
  105.    *///check
  106.   public Piece remove(int x, int y) {
  107.     Piece temp= new Piece(0);
  108.     if(x>=0&&y>=0&&x<=7&&y<=7){
  109.       if(board[x][y]!=null){
  110.        temp=board[x][y];
  111.     board[x][y]=null;
  112.     return temp;
  113.   }
  114.     else{
  115.       System.out.println("There is no piece.");
  116.       return null;
  117.     }
  118.      }
  119.     else{
  120.       System.out.println("It's out of boundary.");
  121.       return null;
  122.  
  123.     }
  124.      // YOUR CODE HERE
  125.   }
  126.  
  127.   /**
  128.    * Determines if a Piece can be selected
  129.    * @param  x X coordinate of Piece
  130.    * @param  y Y coordinate of Piece to select
  131.    * @return   true if the Piece can be selected
  132.    */
  133.      public boolean canSelect(int x, int y) {  
  134.      if(x>=0&&x<=7&&y>=0&&y<=7){
  135.        if(board[x][y]==null){
  136.          if(this.count_move==0&&this.count_click==1) return false;
  137.          else{
  138.             if(board[this.x1][this.y1].isKing()){
  139.                 if((Math.abs(this.y1-y)==1&&Math.abs(this.x1-x)==1)||(Math.abs(this.y1-y)==2)&&(Math.abs(this.x1-x)==2)){
  140.                   if(Math.abs(this.y1-y)==1){
  141.                     if(count_move==0)
  142.                     return true;
  143.                     else return false;
  144.                   }
  145.                   else{
  146.                     if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
  147.                      if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
  148.                       return true;
  149.                     }
  150.                     else{
  151.                       return false;
  152.                     }
  153.                   }
  154.                   else return false;
  155.                   }//remark
  156.                 }
  157.                 else{
  158.                   return false;
  159.                 }
  160.               }
  161.               //mark
  162.                else{
  163.                 if(this.turn==0){
  164.                   if((y-this.y1==1&&Math.abs(x-this.x1)==1)||(y-this.y1==2&&Math.abs(x-this.x1)==2)){
  165.                     if(y-this.y1==1&&Math.abs(x-this.x1)==1){
  166.                          if(count_move==0)
  167.                          return true;
  168.                          else return false;
  169.                     }
  170.                     else{
  171.                       if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
  172.                       if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
  173.                           return true;
  174.                       }
  175.                       else{
  176.                             return false;
  177.                      }
  178.                    }
  179.                    else  return false;
  180.                   }//remark
  181.                   }
  182.                   else{
  183.                     return false;
  184.                   }
  185.                 }
  186.                 //marker
  187.                 else {
  188.                  if((this.y1-y==1&&Math.abs(x-this.x1)==1)||(this.y1-y==2&&Math.abs(x-this.x1)==2)){
  189.                     if(this.y1-y==1&&Math.abs(x-this.x1)==1){
  190.                         if(count_move==0)
  191.                         return true;
  192.                         else
  193.                         return false;
  194.                       }
  195.                     else{
  196.                       if(board[x+(this.x1-x)/2][y+(this.y1-y)/2]!=null){
  197.                       if(board[x+(this.x1-x)/2][y+(this.y1-y)/2].side()!=turn){
  198.                       return true;
  199.                       }
  200.                       else{
  201.                        return false;
  202.                       }
  203.                     }
  204.                     else return false;
  205.                     }//marker
  206.                   }
  207.                   else{
  208.                     return false;
  209.                   }
  210.                 }
  211.                 //marker
  212.               }
  213.  
  214.  
  215.               //mark    
  216.          }
  217.        }
  218.        else{
  219.         if(this.count_move==0&&this.count_click==1){
  220.           if(board[x][y].side()==turn) return true;
  221.           else return false;
  222.         }
  223.         else return false;
  224.        }
  225.      }
  226.      else return false;
  227. }  
  228.   /**
  229.    * Selects a square. If no Piece is active, selects the Piece and
  230.    * makes it active. If a Piece is active, performs a move if an empty
  231.    * place is selected. Else, allows you to reselect Pieces
  232.    * @param x X coordinate of place to select
  233.    * @param y Y coordinate of place to select
  234.    */
  235.   public void select(int x, int y) {
  236.         for(int i=0;i<8;i++){
  237.           for(int j=0;j<8;j++){
  238.             color[i][j]=0;
  239.           }
  240.         }
  241.  
  242.         if(x>=0&&y>=0&&x<=7&&y<=7){
  243.           color[x][y]=1;
  244.       }
  245.   }
  246.  
  247.   /**
  248.    * Moves the active piece to coordinate (x, y)
  249.    * @param p Piece to move
  250.    * @param x1 Original X coordinate of p
  251.    * @param y1 Origin Y coordinate of p
  252.    * @param x X coordinate to move to
  253.    * @param y Y coordinate to move to
  254.    */
  255.   public void move(int x1, int y1, int x2, int y2) {
  256.     Piece temp= new Piece(-1);
  257.     temp= board[x1][y1];
  258.     board[x1][y1]=board[x2][y2];
  259.     board[x2][y2]=temp;
  260.     if(Math.abs(x2-x1)==2){
  261.       if(this.board[x1+(x2-x1)/2][y1+(y2-y1)/2].isking=true)
  262.         board[x2][y2].isking=true;
  263.        this.board[x1+(x2-x1)/2][y1+(y2-y1)/2]=null;
  264.    
  265.       if(board[x2][y2] instanceof BombPiece){
  266.           board[x2][y2]=null;
  267.            if(y2+1<=7){
  268.             if(!(board[x2][y2+1] instanceof ShieldPiece))
  269.             board[x2][y2+1]=null;
  270.           }
  271.           if(y2-1>=0){
  272.             if(!(board[x2][y2-1] instanceof ShieldPiece))
  273.             board[x2][y2-1]=null;
  274.           }
  275.         if(x2+1<=7){
  276.           if(!(board[x2+1][y2] instanceof ShieldPiece))
  277.           board[x2+1][y2]=null;
  278.  
  279.           if(y2+1<=7){
  280.             if(!(board[x2+1][y2+1] instanceof ShieldPiece))
  281.             board[x2+1][y2+1]=null;
  282.           }
  283.           if(y2-1>=0){
  284.             if(!(board[x2+1][y2-1] instanceof ShieldPiece))
  285.             board[x2+1][y2-1]=null;
  286.           }
  287.         }
  288.         else if(x2-1>=0){
  289.           if(!(board[x2-1][y2] instanceof ShieldPiece))
  290.           board[x2-1][y2]=null;
  291.           if(y2+1<=7){
  292.             if(!(board[x2-1][y2+1] instanceof ShieldPiece))
  293.             board[x2-1][y2+1]=null;
  294.           }
  295.           if(y2-1>=0){
  296.             if(!(board[x2-1][y2-1] instanceof ShieldPiece))
  297.             board[x2-1][y2-1]=null;
  298.           }
  299.         }
  300.       }
  301.     // YOUR CODE HERE  
  302.   }
  303. }
  304.  
  305.   /**
  306.    * Determines if the turn can end
  307.    * @return true if the turn can end
  308.    */
  309.   public boolean canEndTurn() {
  310.    if(count_move>0)
  311.     return true;
  312.    else
  313.     return false;
  314.     // YOUR CODE HERE
  315.   }
  316.  
  317.   /**
  318.    * Ends the current turn. Changes the player.
  319.    */
  320.   public void endTurn() {
  321.   if (StdDrawPlus.isSpacePressed()&&this.canEndTurn()) {
  322.         this.turn = 1 - this.turn;
  323.         this.count_move=0;
  324.         this.count_click=0;
  325.         this.x1=0;
  326.         this.x2=0;
  327.         this.y1=0;
  328.         this.y2=0;
  329.       }
  330.     }
  331.  
  332.   /**
  333.    * Returns the winner of the game
  334.    * @return The winner of this game
  335.    */
  336.   public String winner() {
  337.       int num_fire=0;
  338.       int num_water=0;
  339.       for(int i=0;i<8;i++){
  340.         for(int j=0;j<8;j++){
  341.         if(this.board[i][j]!=null){
  342.           if(this.board[i][j].side()==0){
  343.             num_fire++;
  344.           }
  345.           else{
  346.             num_water++;
  347.           }
  348.         }
  349.         }
  350.       }
  351.       if(num_fire!=0&&num_water==0) return "fire";
  352.       else if(num_fire==0&&num_water!=0) return "water";
  353.       else if(num_fire==0&&num_water==0) return "tie";
  354.       else return null;
  355.     }
  356.   private void drawBoard(int[][] color) {
  357.     for (int i = 0; i < board.length; i++) {
  358.       for (int j = 0; j < board[0].length; j++) {
  359.         if ((i + j) % 2 == 0) {
  360.           if(color[i][j]==0){
  361.           StdDrawPlus.setPenColor(StdDrawPlus.GRAY);
  362.         }
  363.         else{
  364.          StdDrawPlus.setPenColor(StdDrawPlus.WHITE);
  365.         }
  366.         } else {
  367.           if(color[i][j]==0){
  368.           StdDrawPlus.setPenColor(StdDrawPlus.RED);
  369.         }
  370.         else{
  371.           StdDrawPlus.setPenColor(StdDrawPlus.WHITE);
  372.         }
  373.         }
  374.         StdDrawPlus.filledSquare(i + .5, j + .5, .5);
  375.     if(board[i][j]!=null){
  376.       if(board[i][j].isKing()){
  377.         if(board[i][j].side()==0){
  378.           if(board[i][j] instanceof BombPiece){
  379.               StdDrawPlus.picture(i + .5, j + .5, "img/bomb-fire-crowned.png", 1, 1);
  380.             }
  381.         else if(board[i][j] instanceof ShieldPiece){
  382.               StdDrawPlus.picture(i + .5, j + .5, "img/shield-fire-crowned.png", 1, 1);
  383.             }
  384.         else if(board[i][j]!=null){
  385.               StdDrawPlus.picture(i + .5, j + .5, "img/pawn-fire-crowned.png", 1, 1);
  386.             }  
  387.            }
  388.       else if(board[i][j].side()==1){
  389.             if(board[i][j] instanceof BombPiece){
  390.               StdDrawPlus.picture(i + .5, j + .5, "img/bomb-water-crowned.png", 1, 1);
  391.             }
  392.             else if(board[i][j] instanceof ShieldPiece){
  393.               StdDrawPlus.picture(i + .5, j + .5, "img/shield-water-crowned.png", 1, 1);
  394.             }
  395.             else if(board[i][j]!=null){
  396.               StdDrawPlus.picture(i + .5, j + .5, "img/pawn-water-crowned.png", 1, 1);
  397.             }
  398.            }
  399.         }
  400.       else{
  401.         if(board[i][j].side()==0){
  402.             if(board[i][j] instanceof BombPiece){
  403.               StdDrawPlus.picture(i + .5, j + .5, "img/bomb-fire.png", 1, 1);
  404.             }
  405.             else if(board[i][j] instanceof ShieldPiece){
  406.               StdDrawPlus.picture(i + .5, j + .5, "img/shield-fire.png", 1, 1);
  407.             }
  408.             else if(board[i][j]!=null){
  409.               StdDrawPlus.picture(i + .5, j + .5, "img/pawn-fire.png", 1, 1);
  410.             }  
  411.           }
  412.         else if(board[i][j].side()==1){
  413.              if(board[i][j] instanceof BombPiece){
  414.               StdDrawPlus.picture(i + .5, j + .5, "img/bomb-water.png", 1, 1);
  415.             }
  416.             else if(board[i][j] instanceof ShieldPiece){
  417.               StdDrawPlus.picture(i + .5, j + .5, "img/shield-water.png", 1, 1);
  418.             }
  419.             else if(board[i][j]!=null){
  420.               StdDrawPlus.picture(i + .5, j + .5, "img/pawn-water.png", 1, 1);
  421.               }
  422.             }
  423.           }
  424.         }      
  425.       }
  426.     }
  427.   }
  428.   public static void main(String[] args) {
  429.     Board b=new Board(false);
  430.     StdDrawPlus.setScale(0, 8);
  431.     while(true){
  432.     b.drawBoard(b.color);
  433.     StdDrawPlus.show(10);
  434.     if (StdDrawPlus.mousePressed()) {
  435.          int x_click = (int) StdDrawPlus.mouseX();
  436.          int y_click = (int) StdDrawPlus.mouseY();
  437.          if(x_click>=0&&x_click<=7&&y_click<=7&&y_click>=0){                                             //mark
  438.          b.count_click++;
  439.          if(b.board[x_click][y_click]!=null&&b.board[b.x1][b.y1]!=null&&b.count_click==2&&b.count_move==0){
  440.           b.count_click=1;
  441.           x_click=8;
  442.           y_click=8;
  443.  
  444.          }
  445.          if(b.canSelect(x_click,y_click)){
  446.  
  447.          b.x2=x_click;
  448.          b.y2=y_click;
  449.          b.select(b.x2,b.y2);
  450.         }
  451.         else{
  452.          b.count_click=0;
  453.        }
  454.         }                                                //mark
  455.         if(b.count_click>1){
  456.           if(!(b.x1==b.x2&&b.y1==b.y2)){
  457.           if(b.canSelect(b.x2,b.y2)){
  458.           b.move(b.x1,b.y1,b.x2,b.y2);
  459.  
  460.           b.count_move++;
  461.           b.count_click=0;
  462.           }
  463.         }
  464.        }
  465.          b.x1=b.x2;
  466.          b.y1=b.y2;
  467.  
  468.        }
  469.        System.out.println(b.count_click);
  470.        // determine is king or not
  471.        for(int i=0;i<8;i++){
  472.         if(b.board[i][0]!=null){
  473.           if(b.board[i][0].side()==1){
  474.             b.board[i][0].isking=true;
  475.           }
  476.         }
  477.         if(b.board[i][7]!=null){
  478.           if(b.board[i][7].side()==0){
  479.             b.board[i][7].isking=true;
  480.           }
  481.         }
  482.        }
  483.  
  484.          if(b.winner()!=null){
  485.          System.out.println(b.winner());
  486.          }
  487.          b.endTurn();
  488.  
  489.          
  490.       }
  491.     }
  492.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement