Package general

Examples of general.Piece


        if (isSrcSelected()) {
          if (showLegalMoves) {
            //paint own piece's background
            paintSquare(getSelectedSrc(), blue);
            //paint legal moves background
            Piece piece = board.notationToPiece(getSelectedSrc());
            //get all movements that are allowed for the selected piece
            ArrayList<ArrayList<Integer>> legalMoves = move.possiblePieceMoves(piece, false);
            ArrayList<Integer> x = legalMoves.get(0); //list of row numbers
            ArrayList<Integer> y = legalMoves.get(1); //list of column numbers
            ListIterator<Integer> xList = x.listIterator()//row iterator
View Full Code Here


     * @param src
     * @param dest
     */
    public void movePiece(Board board, Move move, Player player, String src, String dest) {
      //get piece to move
      Piece p = board.notationToPiece(src);
      //array coordinates for new destination
    Index newLoc = board.notationToIndex(dest);
    //remove captured piece from the board
    board.removePiece(newLoc.getX(), newLoc.getY());
    p.setRow(newLoc.getX()); //change piece row
    p.setCol(newLoc.getY()); //change piece column
    //place source and destination square to history of moves
    if (player.getSide()==0) { //if white
      //add white piece move to history
      move.getHistoryOfMoves().addWhiteMove(src, dest);
    } else if (player.getSide()==1) { //if black
View Full Code Here

    public boolean isDestSqValid(String srcSq, String destSq, Move move, Board board, Player player) {
      //check source square notation for validity
      if (!move.checkSqValidity(destSq)) {
        return false;
      }
    Piece piece = board.notationToPiece(srcSq);
    //get all movements that are allowed for the selected piece
    ArrayList<ArrayList<Integer>> legalMoves = move.possiblePieceMoves(piece, false);
    //array coordinates for new destination
    Index newLoc = board.notationToIndex(destSq);
    //find out if destination location is included in the legal moves list
View Full Code Here

    ArrayList<ArrayList<Piece>> pieces = getPiecesPossibleToCapture(side, false);
    ArrayList<Piece> ownPieces = pieces.get(0);
    ArrayList<Piece> enemyPieces = pieces.get(1);
    ListIterator<Piece> ownList = ownPieces.listIterator();
    ListIterator<Piece> enemyList = enemyPieces.listIterator();
    Piece ownPiece, enemyPiece;
    while (ownList.hasNext() && enemyList.hasNext()) {
      ownPiece = ownList.next();
      enemyPiece = enemyList.next();
      System.out.print(board.coordinatesToNotation(ownPiece.getRow(), ownPiece.getCol()));
      System.out.print(", ");
      System.out.println(board.coordinatesToNotation(enemyPiece.getRow(), enemyPiece.getCol()));
   
  }
View Full Code Here

    ArrayList<ArrayList<Piece>> pieces = getPiecesPossibleToCapture(side, false);
    ArrayList<Piece> ownPieces = pieces.get(0);
    ArrayList<Piece> enemyPieces = pieces.get(1);
    ListIterator<Piece> ownList = ownPieces.listIterator();
    ListIterator<Piece> enemyList = enemyPieces.listIterator();
    Piece ownPiece, enemyPiece;
    int highestValue = 0, tempValue = 0;
    while (ownList.hasNext() && enemyList.hasNext()) {
      ownPiece = ownList.next();
      enemyPiece = enemyList.next();
      tempValue = enemyPiece.getValue();
View Full Code Here

   * @param list
   * @param length
   */
  public void sortPieceArray(Piece[] list, int length) {
      int outOfOrder, location;
      Piece temp;
      //from second until the end of the array
      for(outOfOrder = 1; outOfOrder < length; outOfOrder++) {
        //if out of order, move to the right place
          if(list[outOfOrder].getValue() < list[outOfOrder - 1].getValue()) {
              temp = list[outOfOrder];
              location = outOfOrder;
              do { //move down the array until correct place is found
                  list[location] = list[location-1];
                  location--;
              }
              while (location > 0 && list[location-1].getValue() > temp.getValue());
              list[location] = temp;
          }
      }
  }
View Full Code Here

   * @param side
   */
  public void printHighestValuePieceAbleToCapture(int side) {
    Piece[] p = highestValuePieceAbleToCapture(side);
    try {
      Piece ownP = p[0];
      Piece enemyP = p[1];
      System.out.print(board.coordinatesToNotation(ownP.getRow(), ownP.getCol()));
      System.out.print(", ");
      System.out.println(board.coordinatesToNotation(enemyP.getRow(), enemyP.getCol()));
    } catch(NullPointerException e){
      //System.out.println("NullPointerException");
    }
  }
View Full Code Here

   */
  public void defense(Player player) {
    //get an array containing enemy piece and the highest value piece it can capture
    Piece[] highestValPiece = highestValuePieceAbleToCapture(player.getEnemySide());
    //get the highest value piece that enemy can capture
    Piece pieceInDanger = highestValPiece[1];
    //find possible squares where piece can move
    ArrayList<ArrayList<Integer>> possibleMoves = move.possiblePieceMoves(pieceInDanger, false);
    ArrayList<Integer> possibleMovesX = possibleMoves.get(0); //get x coordinates
    ArrayList<Integer> possibleMovesY = possibleMoves.get(1); //get y coordinates
    //find possible squares enemy pieces can move
    ArrayList<ArrayList<Integer>> possibleEnemyMoves = getAllSquaresPossibleToMove(player.getEnemySide());
    ArrayList<Integer> possibleEnemyMovesX = possibleEnemyMoves.get(0); //get x coordinates
    ArrayList<Integer> possibleEnemyMovesY = possibleEnemyMoves.get(1); //get y coordinates
    //find a square own piece can move safely
    /*
     * compare possible own piece movements to enemy piece movements to find
     * a safe square.
     */
    ListIterator<Integer> pmxList, pmyList; //iterators for possible own piece moves
    pmxList = possibleMovesX.listIterator()//row iterator
    pmyList = possibleMovesY.listIterator()//column iterator
    //convert arraylist of enemy x movement coordinates into an array
    int[] pexArray = new int[possibleEnemyMovesX.size()];
      for (int i=0; i < pexArray.length; i++) {
        pexArray[i] = possibleEnemyMovesX.get(i).intValue();
      }
      //convert arraylist of enemy y movement coordinates into an array
    int[] peyArray = new int[possibleEnemyMovesY.size()];
      for (int i=0; i < peyArray.length; i++) {
        peyArray[i] = possibleEnemyMovesY.get(i).intValue();
      }
    int pmx=0, pmy=0, pex, pey;
    boolean match = true;
    //check possible own piece moves until safe square is found or possible moves run out
    while (pmxList.hasNext() && pmyList.hasNext() && match) {
      match = false; //reset to false
      //get next own piece move
      pmx = pmxList.next();
      pmy = pmyList.next();
      enemy: for (int i=0; i<pexArray.length; i++) {
        //get enemy piece move
        pex = pexArray[i];
        pey = peyArray[i];
        //compare
        //if match found no need to check other enemy moves
        if (pmx==pex && pmy==pey) {
          match = true;
          break enemy;
        }
      }
    }
    //if none of the enemy moves match -> move own piece to that square
    if (!match) {
      move.doMove(player, board.coordinatesToNotation(pieceInDanger.getRow(),
          pieceInDanger.getCol()), board.coordinatesToNotation(pmx, pmy));
    } else if (match) {
      /*
       * if the piece can't be safed by moving it to a safe square
       * try to trade a less valuable piece.
       *
 
View Full Code Here

    ownList = ownPieceArray.listIterator()
    enemyList = enemyPieceArray.listIterator();
    if (!ownPieceArray.isEmpty()) { //no pieces to capture
      //while pieces left and suitable move not found
      while (ownList.hasNext() && enemyList.hasNext() && !moveDone) {
        Piece ownPiece = ownList.next(); //own piece
        Piece enemyPiece = enemyList.next(); //piece to capture
        /*
         * before making move, check that own piece can't be captured during next move
         * or at least the captured enemy piece is more valuable
         */
        //temporarily make target square empty
        board.removePiece(enemyPiece.getRow(), enemyPiece.getCol()); //remove piece from the square
        //get all squares enemy can move during the next turn (now including the square
        //where enemy's piece was located)
        ArrayList<ArrayList<Integer>> possibleEnemyMoves = getAllSquaresPossibleToMove(player.getEnemySide());
        //place the removed piece on board
        board.addPiece(enemyPiece);
        ArrayList<Integer> possibleEnemyMovesX = possibleEnemyMoves.get(0); //get x coordinates
        ArrayList<Integer> possibleEnemyMovesY = possibleEnemyMoves.get(1); //get y coordinates
        //convert arraylist of enemy x movement coordinates into an array
        int[] pexArray = new int[possibleEnemyMovesX.size()];
          for (int i=0; i < pexArray.length; i++) {
            pexArray[i] = possibleEnemyMovesX.get(i).intValue();
          }
          //convert arraylist of enemy y movement coordinates into an array
        int[] peyArray = new int[possibleEnemyMovesY.size()];
          for (int i=0; i < peyArray.length; i++) {
            peyArray[i] = possibleEnemyMovesY.get(i).intValue();
          }
          //check if enemy can do counter attack on next move
          boolean capturedNextRound = false;
        comp: for (int i=0; i<pexArray.length; i++) {
          //compare
          if (enemyPiece.getRow()==pexArray[i] && enemyPiece.getCol()==peyArray[i]) {
            capturedNextRound = true;
            break comp;
          }
        }
          //check is enemy piece is more valuable
          boolean enemyMoreValuable = false;
          //if enemy piece is at least as valuable as own piece
          if (enemyPiece.getValue() >= ownPiece.getValue()) {
            enemyMoreValuable = true;
          }
        /*
         * do move if enemy can't counter attack during next move or it can but
         * traded piece is less valuable
         */
          if (!capturedNextRound || (capturedNextRound && enemyMoreValuable)) {
          move.doMove(player, board.coordinatesToNotation(ownPiece.getRow(),
              ownPiece.getCol()), board.coordinatesToNotation(enemyPiece.getRow(),
              enemyPiece.getCol()));
          moveDone=true;
          } else {
          advance(player);
          }
      }
View Full Code Here

    //put list of pieces in a random order
    Collections.shuffle(allPiecesFromOneSide);
    int amountOfPieces = allPiecesFromOneSide.size();
    int piecesChecked = 0;
    boolean advancingMove = false;
    Piece reservePiece = new Piece(), lastHope = new Piece();
    int reserveX=0, reserveY=0, lastHopeX=0, lastHopeY=0;
    Piece p;
    ArrayList<ArrayList<Integer>> moves;
    ArrayList<Integer> xList, yList, moveCheckOrder;
    int moveAmount;
    int moveLoc;
    boolean moveDone = false;
    //until advancing move is found or no pieces left to check
    while (!advancingMove && piecesChecked<amountOfPieces) {
      //get a piece
      p = allPiecesFromOneSide.get(piecesChecked);
      //get all possible moves that piece can do
      moves = move.possiblePieceMoves(p, false);
      xList = moves.get(0); //x coordinates
      yList = moves.get(1); //y coordinates
      moveAmount = xList.size(); //amount of moves
      moveLoc = 0; //move index in list
      //create order to check moves
      moveCheckOrder = new ArrayList<Integer>();
      for (int i=0; i<moveAmount; i++) {
        moveCheckOrder.add(i);
      }
      //randomize order in which moves are checked
      Collections.shuffle(moveCheckOrder);
      int counter = 0;
      if (moveAmount>0) { //if there's moves at all
        //save first move as a reserve/last chance move
        lastHope = p;
        int firstMove = moveCheckOrder.get(0);
        lastHopeX=xList.get(firstMove);
        lastHopeY=yList.get(firstMove);
        //while advancing move not found and still moves left
        while (!advancingMove && counter<moveAmount) {
          moveLoc = moveCheckOrder.get(counter); //get next move
          //check if move is safe (enemy can't attack during next move)
          if (isSquareSafe(xList.get(moveLoc), yList.get(moveLoc), player)) {
            //best move is to advance/go forward: white -vertical, black +vertical
            if(player.getSide()==0) { //if white side
              //if advancing move
              if(xList.get(moveLoc)<p.getRow()) {
                advancingMove=true;
              }
            }
            else { //black side
              //if advancing move
              if(xList.get(moveLoc)>p.getRow()) {
                advancingMove=true
             
            }
            //if not found a safe piece which can move forward
            //put piece in reserve because at least it's safe
            if(!advancingMove) {
              reservePiece=p;
              reserveX=xList.get(moveLoc);
              reserveY=yList.get(moveLoc);
            }
          }
          counter++;
        }
        if (advancingMove) { //advancing move found
          move.doMove(player, board.coordinatesToNotation(p.getRow(),
              p.getCol()), board.coordinatesToNotation(xList.get(moveLoc),
              yList.get(moveLoc)));
          moveDone=true;
        }
      }
      piecesChecked++;
View Full Code Here

TOP

Related Classes of general.Piece

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.