Ejemplo n.º 1
0
  public String castleQueenside(Square candidateSquare) {
    if (piece == 0) {

      int row;
      int column;
      if (color == 1) {
        row = 1;
        column = 1;
      } else {
        row = 8;
        column = 1;
      }
      int cell = square.getCellFromCoord(row, column);
      Square sq = (Square) board.getComponent(cell);
      Piece pc = (Piece) sq.getComponent(0);

      int newCell;
      if (pc.getColor() == 1) {
        newCell = square.getCellFromCoord(1, 4);
      } else {
        newCell = square.getCellFromCoord(8, 4);
      }
      Square newSquare = (Square) board.getComponent(newCell);
      // move rook
      movePiece(pc, newSquare, sq);
      return ("0-0-0");
    }
    return "";
  }
Ejemplo n.º 2
0
 /**
  * Realise le roque (deplace les pieces)
  *
  * @param myCoup (le coup joue)
  */
 public void realiserRoque(Coup myCoup) {
   Piece roi = null;
   Piece tour = null;
   if (myCoup.estPetitRoque) {
     if (myCoup.getCouleur().getColor() == 1) // blanc
     {
       roi = this.getPiecePosition(4, 0);
       tour = this.getPiecePosition(7, 0);
       roi.setPos(6, 0);
       tour.setPos(5, 0);
     } else {
       roi = this.getPiecePosition(4, 7);
       tour = this.getPiecePosition(7, 7);
       roi.setPos(6, 7);
       tour.setPos(5, 7);
     }
   } else {
     if (myCoup.getCouleur().getColor() == 1) // blanc
     {
       roi = this.getPiecePosition(4, 0);
       tour = this.getPiecePosition(0, 0);
       roi.setPos(2, 0);
       tour.setPos(3, 0);
     } else {
       roi = this.getPiecePosition(4, 7);
       tour = this.getPiecePosition(0, 7);
       roi.setPos(2, 7);
       tour.setPos(3, 7);
     }
   }
 }
Ejemplo n.º 3
0
 private void movePiece(Piece p, Square destinationSquare, Square originSquare) {
   p.setVisible(false);
   originSquare.removeAll();
   destinationSquare.add(p);
   p.setSquare(destinationSquare);
   p.setVisible(true);
 }
Ejemplo n.º 4
0
 /**
  * Retourne la piece positionné a telle coordonnée x & y
  *
  * @param x
  * @param y
  * @return la piece a la position donnee , null si la position est vide
  */
 public Piece getPiecePosition(int x, int y) {
   for (Piece onePiece : this.pieceList) {
     if (onePiece.getPos().equals(getPosition(x, y))) {
       return onePiece;
     }
   }
   return null;
 }
Ejemplo n.º 5
0
 /**
  * @param pos
  * @return la piece a la position donnee , null si la position est vide
  */
 public Piece getPiecePosition(Position pos) {
   for (Piece onePiece : this.pieceList) {
     if (onePiece.getPos().equals(pos)) {
       return onePiece;
     }
   }
   return null;
 }
Ejemplo n.º 6
0
 public synchronized void add_block(int pieceIndex, int offset, int size, byte[] b) {
   Piece p = this.pieces.get(pieceIndex);
   p.writeBlock(b, offset);
   if (p.haveAllBlocks() == 1) {
     this.finished_pieces[pieceIndex] = true;
     // System.out.println("verified");
   }
   return;
 }
Ejemplo n.º 7
0
 /**
  * @param posDepart
  * @param posArrivee
  * @param isPrise
  */
 public void annulerDeplacerPiecePourTest(
     Position posDepart, Position posArrivee, boolean isPrise) {
   Piece pieceD = null;
   pieceD = this.getPiecePosition(posArrivee);
   pieceList.remove(pieceD);
   pieceD.setPos(posDepart);
   if (isPrise) {
     piecePourTest.setPos(posArrivee);
     pieceList.add(piecePourTest);
   }
   piecePourTest = null;
   pieceList.add(pieceD);
 }
Ejemplo n.º 8
0
 void removeFriendsAndBorders(Piece piece, ArrayList<Point> points) {
   boolean up = piece.isUp();
   for (Iterator<Point> it = points.iterator(); it.hasNext(); ) {
     Point point = it.next();
     if (point != Poussin.PROMOTING
         && (point.x < 0 || point.y < 0 || point.x >= GRID_WIDTH || point.y >= GRID_HEIGHT)) {
       it.remove();
       continue;
     }
     Piece pieceOnSquare = getPosition().getPiece(point.x, point.y);
     if (pieceOnSquare != null && pieceOnSquare.isUp() == up) it.remove();
   }
 }
Ejemplo n.º 9
0
 /**
  * Deplace une piece sans faire de test
  *
  * @param posDepart
  * @param posArrivee
  * @return s'il y a une prise de piece
  */
 public boolean deplacerPiecePourTest(Position posDepart, Position posArrivee) {
   Piece pieceD = null;
   boolean prise = false;
   pieceD = this.getPiecePosition(posDepart);
   piecePourTest = this.getPiecePosition(posArrivee);
   if (piecePourTest != null) {
     prise = true;
     pieceList.remove(piecePourTest);
   }
   pieceD.setPos(posArrivee);
   pieceList.remove(pieceD);
   pieceList.add(pieceD);
   return prise;
 }
Ejemplo n.º 10
0
  public void addFreeSquares(ArrayList<Point> points) { // add all squares
    for (int i = 0; i < GRID_WIDTH; i++) {
      for (int j = 0; j < GRID_HEIGHT; j++) {
        points.add(new Point(i, j));
      }
    }

    // remove all pieces
    for (Piece piece : game.getPieces()) {
      if (!piece.isOut()) {
        points.remove(piece.getLocation());
      }
    }
  }
Ejemplo n.º 11
0
 public void move(Piece p, Position pos) {
   String s =
       ask(
           "move:"
               + p.getPos().getX()
               + ":"
               + p.getPos().getY()
               + ":"
               + pos.getX()
               + ":"
               + pos.getY()
               + ";");
   System.out.println(s);
 }
Ejemplo n.º 12
0
 /**
  * Deplacer la piece un coup,
  *
  * @param pieceDepart
  * @param posArrivee
  * @return
  */
 public boolean realiserCoup(Piece pieceDepart, Position posArrivee) {
   if (pieceList.contains(pieceDepart)) {
     pieceList.remove(this.getPiecePosition(posArrivee));
     for (Piece piece : pieceList) {
       if (piece.equals(pieceDepart)) {
         pieceDepart.setPos(posArrivee);
         pieceList.remove(piece);
         pieceList.add(pieceDepart);
         break;
       }
     }
     pieceDepart.setPlayed();
     return true;
   } else return false;
 }
Ejemplo n.º 13
0
 public String toString() {
   if (piece == null) {
     StringBuffer buf = new StringBuffer();
     buf.append(Board.getFileChar(file));
     buf.append(rank);
     return buf.toString();
   } else return piece.toString();
 }
Ejemplo n.º 14
0
  /**
   * Piece download completion handler.
   *
   * <p>When a piece is completed, and valid, we announce to all connected peers that we now have
   * this piece.
   *
   * <p>We use this handler to identify when all of the pieces have been downloaded. When that's the
   * case, we can start the seeding period, if any.
   *
   * @param peer The peer we got the piece from.
   * @param piece The piece in question.
   */
  @Override
  public void handlePieceCompleted(SharingPeer peer, Piece piece) throws IOException {
    final SharedTorrent torrent = peer.getTorrent();
    synchronized (torrent) {
      if (piece.isValid()) {
        // Make sure the piece is marked as completed in the torrent
        // Note: this is required because the order the
        // PeerActivityListeners are called is not defined, and we
        // might be called before the torrent's piece completion
        // handler is.
        torrent.markCompleted(piece);
        logger.debug(
            "Completed download of {}, now has {}/{} pieces.",
            new Object[] {
              piece, torrent.getCompletedPieces().cardinality(), torrent.getPieceCount()
            });

        // Send a HAVE message to all connected peers
        PeerMessage have = PeerMessage.HaveMessage.craft(piece.getIndex());
        for (SharingPeer remote : getConnectedPeers()) {
          remote.send(have);
        }
      }

      if (torrent.isComplete()) {
        logger.info("Last piece validated and completed, " + "download is complete.");
        torrent.finish();

        try {
          this.announce
              .getCurrentTrackerClient(torrent)
              .announce(
                  TrackerMessage.AnnounceRequestMessage.RequestEvent.COMPLETED, true, torrent);
        } catch (AnnounceException ae) {
          logger.warn("Error announcing completion event to " + "tracker: {}", ae.getMessage());
        }

        torrent.setClientState(ClientState.SEEDING);
        if (seed == 0) {
          peer.unbind(false);
          this.announce.removeTorrent(torrent);
        }
      }
    }
  }
Ejemplo n.º 15
0
  Set<FooBar> getDecomposeList(int value, Piece piece, Map<Integer, Set<FooBar>> decompositionMap) {
    // logger.debug(String.format("value[%s] piece[%s]", value, piece));
    if (value < piece.getValue()) return Collections.EMPTY_SET;

    Set<FooBar> result = new HashSet<FooBar>();

    if (value == piece.getValue()) {
      FooBar fooBar = new FooBar();
      fooBar.add(piece);
      result.add(fooBar);
    }

    for (FooBar fooBar : decompositionMap.get(value - piece.getValue())) {
      result.add(fooBar.copy().add(piece));
    }

    return result;
  }
Ejemplo n.º 16
0
  public Enonce1(int to) {
    decompositionMap.put(0, Collections.EMPTY_SET);

    for (int i = 1; i < to + 1; i++) {
      Set<FooBar> decomposeList = new HashSet<FooBar>();
      for (Piece p : Piece.values()) {
        decomposeList.addAll(getDecomposeList(i, p, decompositionMap));
      }
      decompositionMap.put(i, decomposeList);
    }
  }
Ejemplo n.º 17
0
  /**
   * Realise un déplacement
   *
   * @param myCoup
   * @return
   */
  @SuppressWarnings("null")
  public boolean realiserCoup(Coup myCoup) {

    Piece pieceD = null;
    Position posA = null;
    piecePourTest = null;

    try {
      pieceD = myCoup.getPieceDepart();
      posA = myCoup.getPosArrivee();
      myCoup.setPiecePrise(this.getPiecePosition(posA));
    } catch (NullPointerException e) {
      e.getStackTrace();
    }
    if (myCoup.getRoque() == false) {
      if (pieceList.contains(pieceD)) {
        if (pieceD.positionAccessibleChessboard(this).contains(posA)) {
          pieceList.remove(this.getPiecePosition(posA));
          for (Piece piece : pieceList) {
            if (piece.equals(pieceD)) {
              pieceD.setPlayed();
              pieceD.setPos(posA);
              pieceList.remove(piece);
              pieceList.add(pieceD);
              break;
            }
          }
          return true;
        }
      } else return false;
    } else {
      realiserRoque(myCoup);
    }
    return false;
  }
Ejemplo n.º 18
0
  /**
   * Handles the mouse location click.
   *
   * @param loc the location that was clicked
   * @return true because the click has been handled
   */
  @Override
  public boolean locationClicked(Location loc) {
    if (game.getTurn() instanceof SmartComputerCheckerPlayer) // ignore all clicks on CPU's turn
    {
      return true;
    }

    if (lastMove != null
        && lastMove.isJump()
        && lastMove.getPiece().canJump()) // locks selection onto jump-chaining piece
    {
      Piece lastPiece = lastMove.getPiece();

      if (!loc.equals(lastPiece.getLocation())
          && !lastPiece.getAllowedMoves().contains(loc)) // consumes irrelevant clicks
      {
        return true;
      }
    }
    if (getGrid().get(loc) != null && !(getGrid().get(loc) instanceof PieceTile)) {
      Piece p = getGrid().get(loc);
      if (pieceSelected) {
        game.undisplayMoves(playerPiece);
      }
      game.displayMoves(p); // selection and highlight available moves
      playerPiece = p;
      pieceSelected = true;
    } else if (pieceSelected && !playerPiece.getAllowedMoves().contains(loc)) {
      pieceSelected = false;
      game.undisplayMoves(playerPiece); // undo selection and highlight
      playerPiece = null;
    } else if (pieceSelected && playerPiece.getAllowedMoves().contains(loc)) {
      pieceSelected = false;
      game.undisplayMoves(playerPiece); // undo selection and highlight
      setPlayerLocation(loc);
    }

    return true;
  }
Ejemplo n.º 19
0
  /** @param - start is the starting coordinate */
  @Override
  public HashSet<Coordinate> getMoves(Coordinate start) {
    HashSet<Coordinate> moves = new HashSet<Coordinate>();
    int x = start.getXCoordinate();
    int y = start.getYCoordinate();
    // left diagonal
    moves.add(new Coordinate(x - 1, y - 1));
    // right diagonal
    moves.add(new Coordinate(x + 1, y - 1));

    super.movesOnBoard(moves);
    return moves;
  }
Ejemplo n.º 20
0
 /** *************************************************************************************** */
 public void selectCell() {
   if (p != null) {
     p.highlight = true;
   }
 }
Ejemplo n.º 21
0
 /** *************************************************************************************** */
 public void deSelect() {
   if (p != null) {
     p.deHighlight();
   }
 }
Ejemplo n.º 22
0
  private void setListForKing() {
    moveList.clear();
    squareList.clear();
    int row = square.getRow();
    int column = square.getColumn();
    int c1, c2, c3, c4, c5, c6, c7, c8;
    String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8;
    if (((row + 1) < 9) && (column + 1) < 9) {
      c1 = square.getCellFromCoord(row + 1, column + 1);
      cell1 = square.getPositionWithArg(c1);
      Square s = (Square) board.getComponent(c1);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell1);
          squareList.add(s);
        }
      } else {
        moveList.add(cell1);
        squareList.add(s);
      }
    }
    if (((row + 1) < 9) && (column - 1) > 0) {
      c2 = square.getCellFromCoord(row + 1, column - 1);
      cell2 = square.getPositionWithArg(c2);
      Square s = (Square) board.getComponent(c2);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell2);
          squareList.add(s);
        }
      } else {
        moveList.add(cell2);
        squareList.add(s);
      }
    }
    if ((row + 1) < 9) {
      c3 = square.getCellFromCoord(row + 1, column);
      cell3 = square.getPositionWithArg(c3);
      Square s = (Square) board.getComponent(c3);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell3);
          squareList.add(s);
        }
      } else {
        moveList.add(cell3);
        squareList.add(s);
      }
    }
    if ((column + 1) < 9) {
      c4 = square.getCellFromCoord(row, column + 1);
      cell4 = square.getPositionWithArg(c4);
      Square s = (Square) board.getComponent(c4);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell4);
          squareList.add(s);
        }
      } else {
        moveList.add(cell4);
        squareList.add(s);
      }
    }
    if ((column - 1) > 0) {
      c5 = square.getCellFromCoord(row, column - 1);
      cell5 = square.getPositionWithArg(c5);
      Square s = (Square) board.getComponent(c5);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell5);
          squareList.add(s);
        }
      } else {
        moveList.add(cell5);
        squareList.add(s);
      }
    }
    if (((row - 1) > 0) && (column - 1) > 0) {
      c6 = square.getCellFromCoord(row - 1, column - 1);
      cell6 = square.getPositionWithArg(c6);
      Square s = (Square) board.getComponent(c6);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell6);
          squareList.add(s);
        }
      } else {
        moveList.add(cell6);
        squareList.add(s);
      }
    }
    if (((row - 1) > 0) && (column + 1) < 9) {
      c7 = square.getCellFromCoord(row - 1, column + 1);
      cell7 = square.getPositionWithArg(c7);
      Square s = (Square) board.getComponent(c7);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell7);
          squareList.add(s);
        }
      } else {
        moveList.add(cell7);
        squareList.add(s);
      }
    }
    if ((row - 1) > 0) {
      c8 = square.getCellFromCoord(row - 1, column);
      cell8 = square.getPositionWithArg(c8);
      Square s = (Square) board.getComponent(c8);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell8);
          squareList.add(s);
        }
      } else {
        moveList.add(cell8);
        squareList.add(s);
      }
    }

    if (queensideClear()) {

      int cell = square.getCellFromCoord(1, 3);
      String c = square.getPositionWithArg(cell);
      moveList.add(c);
      cell = square.getCellFromCoord(8, 3);
      c = square.getPositionWithArg(cell);
      moveList.add(c);
    }
    if (kingsideClear()) {

      int cell = square.getCellFromCoord(1, 7);
      String c = square.getPositionWithArg(cell);
      moveList.add(c);
      cell = square.getCellFromCoord(8, 7);
      c = square.getPositionWithArg(cell);
      moveList.add(c);
    }
  }
Ejemplo n.º 23
0
  private void setListForKnight() {
    moveList.clear();
    squareList.clear();
    /*switch(square.getCellNumber()) {

    	case 56 :  moveList.add("c2");moveList.add("b3"); break;
    	case 57 :  moveList.add("d2");moveList.add("a3");moveList.add("c3"); break;
    	case 58 :  moveList.add("a2");moveList.add("e2");moveList.add("b3");moveList.add("d3"); break;
    	case 59 :  moveList.add("b2");moveList.add("f2");moveList.add("c3");moveList.add("e3"); break;
    	case 60 :  moveList.add("c2");moveList.add("g2");moveList.add("d3");moveList.add("f3"); break;
    	case 61 :  moveList.add("d2");moveList.add("h2");moveList.add("e3");moveList.add("g3"); break;
    	case 62 :  moveList.add("e2");moveList.add("f3");moveList.add("h3"); break;
    	case 63 :  moveList.add("f2");moveList.add("g3"); break;
    	case 48 :  moveList.add("c1");moveList.add("c3");moveList.add("b4"); break;
    	case 49 :  moveList.add("d1");moveList.add("d3");moveList.add("a4");moveList.add("c4"); break;
    	case 50 :  moveList.add("a1");moveList.add("e1");moveList.add("a3");moveList.add("e3");moveList.add("b4");moveList.add("d4"); break;
    	case 51 :  moveList.add("b1");moveList.add("f1");moveList.add("b3");moveList.add("f3");moveList.add("c4");moveList.add("e4"); break;
    	case 52 :  moveList.add("c1");moveList.add("g1");moveList.add("c3");moveList.add("g3");moveList.add("d4");moveList.add("f4"); break;
    	case 53 :  moveList.add("d1");moveList.add("h1");moveList.add("d3");moveList.add("h3");moveList.add("e4");moveList.add("g4"); break;
    	case 54 :  moveList.add("e1");moveList.add("e3");moveList.add("f4");moveList.add("h4"); break;
    	case 55 :  moveList.add("f1");moveList.add("f3");moveList.add("g4"); break;
    	case 40 :  moveList.add("b1");moveList.add("c2");moveList.add("c4");moveList.add("b5"); break;
    	case 41 :  moveList.add("a1");moveList.add("c1");moveList.add("d2");moveList.add("d4");moveList.add("a5");moveList.add("c5"); break;
    	case 42 :  moveList.add("b1");moveList.add("d1");moveList.add("a2");moveList.add("e2");moveList.add("a4");moveList.add("e4");moveList.add("b5");moveList.add("d5"); break;
    	case 43 :  moveList.add("c1");moveList.add("e1");moveList.add("b2");moveList.add("f2");moveList.add("b4");moveList.add("f4");moveList.add("c5");moveList.add("e5"); break;
    	case 44 :  moveList.add("d1");moveList.add("f1");moveList.add("c2");moveList.add("g2");moveList.add("c4");moveList.add("g4");moveList.add("d5");moveList.add("f5"); break;
    	case 45 :  moveList.add("e1");moveList.add("g1");moveList.add("d2");moveList.add("h2");moveList.add("d4");moveList.add("h4");moveList.add("e5");moveList.add("g5"); break;
    	case 46 :  moveList.add("f1");moveList.add("h1");moveList.add("e2");moveList.add("e4");moveList.add("f5");moveList.add("h5"); break;
    	case 47 :  moveList.add("g1");moveList.add("f2");moveList.add("f4");moveList.add("g5"); break;
    	case 32 :  moveList.add("b2");moveList.add("c3");moveList.add("c5");moveList.add("b6"); break;
    	case 33 :  moveList.add("a2");moveList.add("c2");moveList.add("d3");moveList.add("d5");moveList.add("a6");moveList.add("c6"); break;
    	case 34 :  moveList.add("b2");moveList.add("d2");moveList.add("a3");moveList.add("e3");moveList.add("a5");moveList.add("e5");moveList.add("b6");moveList.add("d6"); break;
    	case 35 :  moveList.add("c2");moveList.add("e2");moveList.add("b3");moveList.add("f3");moveList.add("b5");moveList.add("f5");moveList.add("c6");moveList.add("e6"); break;
    	case 36 :  moveList.add("d2");moveList.add("f2");moveList.add("c3");moveList.add("g3");moveList.add("c5");moveList.add("g5");moveList.add("d6");moveList.add("f6"); break;
    	case 37 :  moveList.add("e2");moveList.add("g2");moveList.add("d3");moveList.add("h3");moveList.add("d5");moveList.add("h5");moveList.add("e6");moveList.add("g6"); break;
    	case 38 :  moveList.add("f2");moveList.add("h2");moveList.add("e3");moveList.add("e5");moveList.add("f6");moveList.add("h6"); break;
    	case 39 :  moveList.add("g2");moveList.add("f3");moveList.add("f5");moveList.add("g6"); break;
    	case 24 :  moveList.add("b3");moveList.add("c4");moveList.add("c6");moveList.add("b7"); break;
    	case 25 :  moveList.add("a3");moveList.add("c3");moveList.add("d4");moveList.add("d6");moveList.add("a7");moveList.add("c7"); break;
    	case 26 :  moveList.add("b3");moveList.add("d3");moveList.add("a4");moveList.add("e4");moveList.add("a6");moveList.add("e6");moveList.add("b7");moveList.add("d7"); break;
    	case 27 :  moveList.add("c3");moveList.add("e3");moveList.add("b4");moveList.add("f4");moveList.add("b6");moveList.add("f6");moveList.add("c7");moveList.add("e7"); break;
    	case 28 :  moveList.add("d3");moveList.add("f3");moveList.add("c4");moveList.add("g4");moveList.add("c6");moveList.add("g6");moveList.add("d7");moveList.add("f7"); break;
    	case 29 :  moveList.add("e3");moveList.add("g3");moveList.add("d4");moveList.add("h4");moveList.add("d6");moveList.add("h6");moveList.add("e7");moveList.add("g7"); break;
    	case 30 :  moveList.add("f3");moveList.add("h3");moveList.add("e4");moveList.add("e6");moveList.add("f7");moveList.add("h7"); break;
    	case 31 :  moveList.add("g3");moveList.add("f4");moveList.add("f6");moveList.add("g7"); break;
    	case 16 :  moveList.add("b4");moveList.add("c5");moveList.add("c7");moveList.add("b8"); break;
    	case 17 :  moveList.add("a4");moveList.add("c4");moveList.add("d5");moveList.add("d7");moveList.add("a8");moveList.add("c8"); break;
    	case 18 :  moveList.add("b4");moveList.add("d4");moveList.add("a5");moveList.add("e5");moveList.add("a7");moveList.add("e7");moveList.add("b8");moveList.add("d8"); break;
    	case 19 :  moveList.add("c4");moveList.add("e4");moveList.add("b5");moveList.add("f5");moveList.add("b7");moveList.add("f7");moveList.add("c8");moveList.add("e8"); break;
    	case 20 :  moveList.add("d4");moveList.add("f4");moveList.add("c5");moveList.add("g5");moveList.add("c7");moveList.add("g7");moveList.add("d8");moveList.add("f8"); break;
    	case 21 :  moveList.add("e4");moveList.add("g4");moveList.add("d5");moveList.add("h5");moveList.add("d7");moveList.add("h7");moveList.add("e8");moveList.add("g8"); break;
    	case 22 :  moveList.add("f4");moveList.add("h4");moveList.add("e5");moveList.add("e7");moveList.add("f8");moveList.add("h8"); break;
    	case 23 :  moveList.add("g4");moveList.add("f5");moveList.add("f7");moveList.add("g8"); break;
    	case 8 :  moveList.add("b5");moveList.add("c6");moveList.add("c8"); break;
    	case 9 :  moveList.add("a5");moveList.add("c5");moveList.add("d6");moveList.add("d8"); break;
    	case 10 :  moveList.add("b5");moveList.add("d5");moveList.add("a6");moveList.add("e6");moveList.add("a8");moveList.add("e8"); break;
    	case 11 :  moveList.add("c5");moveList.add("e5");moveList.add("b6");moveList.add("f6");moveList.add("b8");moveList.add("f8"); break;
    	case 12 :  moveList.add("d5");moveList.add("f5");moveList.add("c6");moveList.add("g6");moveList.add("c8");moveList.add("g8"); break;
    	case 13 :  moveList.add("e5");moveList.add("g5");moveList.add("d6");moveList.add("h6");moveList.add("d8");moveList.add("h8"); break;
    	case 14 :  moveList.add("f5");moveList.add("h5");moveList.add("e6");moveList.add("e8"); break;
    	case 15 :  moveList.add("g5");moveList.add("f6");moveList.add("f8"); break;
    	case 0 :  moveList.add("b6");moveList.add("c7"); break;
    	case 1 :  moveList.add("a6");moveList.add("c6");moveList.add("d7"); break;
    	case 2 :  moveList.add("b6");moveList.add("d6");moveList.add("a7");moveList.add("e7"); break;
    	case 3 :  moveList.add("c6");moveList.add("e6");moveList.add("b7");moveList.add("f7"); break;
    	case 4 :  moveList.add("d6");moveList.add("f6");moveList.add("c7");moveList.add("g7"); break;
    	case 5 :  moveList.add("e6");moveList.add("g6");moveList.add("d7");moveList.add("h7"); break;
    	case 6 :  moveList.add("f6");moveList.add("h6");moveList.add("e7");moveList.add("NS"); break;
    	case 7 :  moveList.add("g6");moveList.add("f7"); break;

    }*/
    int row = square.getRow();
    int column = square.getColumn();
    int c1, c2, c3, c4, c5, c6, c7, c8;
    String cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8;
    if (((row + 1) < 9) && (column - 2) > 0) {
      c1 = square.getCellFromCoord(row + 1, column - 2);
      cell1 = square.getPositionWithArg(c1);
      Square s = (Square) board.getComponent(c1);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell1);
          squareList.add(s);
        }
      } else {
        moveList.add(cell1);
        squareList.add(s);
      }
    }
    if (((row + 2) < 9) && (column - 1) > 0) {
      c2 = square.getCellFromCoord(row + 2, column - 1);
      cell2 = square.getPositionWithArg(c2);
      Square s = (Square) board.getComponent(c2);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell2);
          squareList.add(s);
        }
      } else {
        moveList.add(cell2);
        squareList.add(s);
      }
    }
    if (((row + 1) < 9) && (column + 1) < 9) {
      c3 = square.getCellFromCoord(row + 2, column + 1);
      cell3 = square.getPositionWithArg(c3);
      Square s = (Square) board.getComponent(c3);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell3);
          squareList.add(s);
        }
      } else {
        moveList.add(cell3);
        squareList.add(s);
      }
    }
    if (((row + 1) < 9) && (column + 2) < 9) {
      c4 = square.getCellFromCoord(row + 1, column + 2);
      cell4 = square.getPositionWithArg(c4);
      Square s = (Square) board.getComponent(c4);
      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell4);
          squareList.add(s);
        }
      } else {
        moveList.add(cell4);
        squareList.add(s);
      }
    }
    if (((row - 1) > 0) && (column - 2) > 0) {
      c5 = square.getCellFromCoord(row - 1, column - 2);
      cell5 = square.getPositionWithArg(c5);
      Square s = (Square) board.getComponent(c5);

      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell5);
          squareList.add(s);
        }
      } else {
        moveList.add(cell5);
        squareList.add(s);
      }
    }
    if (((row - 2) > 0) && (column - 1) > 0) {
      c6 = square.getCellFromCoord(row - 2, column - 1);
      cell6 = square.getPositionWithArg(c6);
      Square s = (Square) board.getComponent(c6);

      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell6);
          squareList.add(s);
        }
      } else {
        moveList.add(cell6);
        squareList.add(s);
      }
    }
    if (((row - 2) > 0) && (column + 1) < 9) {
      c7 = square.getCellFromCoord(row - 2, column + 1);
      cell7 = square.getPositionWithArg(c7);
      Square s = (Square) board.getComponent(c7);

      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell7);
          squareList.add(s);
        }
      } else {
        moveList.add(cell7);
        squareList.add(s);
      }
    }
    if (((row - 1) > 0) && (column + 2) < 9) {
      c8 = square.getCellFromCoord(row - 1, column + 2);
      cell8 = square.getPositionWithArg(c8);
      Square s = (Square) board.getComponent(c8);

      if (s.getComponentCount() > 0) {
        Piece p = (Piece) s.getComponent(0);
        if (p.getColor() != color) {
          moveList.add(cell8);
          squareList.add(s);
        }
      } else {
        moveList.add(cell8);
        squareList.add(s);
      }
    }
  }
Ejemplo n.º 24
0
  public Set<Move> getAllowedMoves() {
    // If there is already a winn
    if (gipfBoardState.players.winner() != null) {
      return Collections.emptySet();
    }

    // Create a set of incomplete moves containing the starting positions and directions for the
    // current piece
    Set<Move> potentialMoves = getPotentialStartMoves(getCurrentPiece());

    // If the current piece is a GIPF piece, the player is also allowed to place normal pieces.
    if (getCurrentPiece().getPieceType() == GIPF)
      potentialMoves.addAll(
          getPotentialStartMoves(Piece.of(NORMAL, getCurrentPiece().getPieceColor())));

    // These moves are marked as complete so a temporary game won't ask for user input.
    potentialMoves.stream().forEach(m -> m.isCompleteMove = true);

    Set<Move> potentialMovesIncludingLineSegmentRemoval = new HashSet<>();
    for (Move potentialMove : potentialMoves) {
      try {
        Map<Position, Piece> temporaryPieceMap = new HashMap<>(getGipfBoardState().getPieceMap());
        temporaryPieceMap.put(potentialMove.startPos, potentialMove.addedPiece);
        movePiecesTowards(
            temporaryPieceMap, potentialMove.getStartingPosition(), potentialMove.getDirection());

        Set<List<Pair<PieceColor, Line.Segment>>> RLineOrderingsSet =
            getRemovableLineOrderingsSetFromGipfBoard(
                temporaryPieceMap, getCurrentPiece().getPieceColor());
        if (RLineOrderingsSet.size() > 0) {
          for (List<Pair<PieceColor, Line.Segment>> RLineOrdering : RLineOrderingsSet) {
            Set<Position> piecesToWhite = new HashSet<>();
            Set<Position> piecesToBlack = new HashSet<>();
            Set<Position> piecesRemoved = new HashSet<>();

            for (Pair<PieceColor, Line.Segment> RLine : RLineOrdering) {
              Line.Segment removedSegment = RLine.getValue();

              // The color of the player who removed the line
              PieceColor colorRemoved = RLine.getKey();

              // Determine per segment to whom the pieces are given. Pieces can only be given to the
              // player
              // who removed the line, or deleted from the game.
              Set<Position> occupiedPositions =
                  removedSegment.getOccupiedPositions(temporaryPieceMap);
              Set<Position> piecesFromSegmentBackToReserve =
                  occupiedPositions
                      .stream()
                      .filter(
                          position ->
                              temporaryPieceMap.get(position).getPieceColor() == colorRemoved)
                      .collect(toSet());
              Set<Position> piecesFromSegmentRemoved =
                  occupiedPositions
                      .stream()
                      .filter(position -> !piecesFromSegmentBackToReserve.contains(position))
                      .collect(toSet());

              if (colorRemoved == WHITE) piecesToWhite.addAll(piecesFromSegmentBackToReserve);
              if (colorRemoved == BLACK) piecesToBlack.addAll(piecesFromSegmentBackToReserve);
              piecesRemoved.addAll(piecesFromSegmentRemoved);
            }

            // And finally add the move
            // the constructor will define this as a complete move, because all the parameters have
            // a value.
            potentialMovesIncludingLineSegmentRemoval.add(
                new Move(
                    potentialMove.addedPiece,
                    potentialMove.startPos,
                    potentialMove.direction,
                    piecesToWhite,
                    piecesToBlack,
                    piecesRemoved));
          }
        } else {
          // If no line segments can be removed, just add the original move
          potentialMovesIncludingLineSegmentRemoval.add(potentialMove);
        }
      } catch (InvalidMoveException e) {
        // We don't consider this move if it is invalid
      }
    }

    return potentialMovesIncludingLineSegmentRemoval;
  }