Пример #1
0
  public void pawnPromotion(Point p) {

    if (b.get(p) instanceof Pawn && p.getY() == 8) {
      b.remove(p);
      b.put(p, new Queen(true));
    } else if (b.get(p) instanceof Pawn && p.getY() == 1) {
      b.remove(p);
      b.put(p, new Queen(false));
    }
  }
Пример #2
0
 public boolean validTake(Point start, Point end) {
   Piece piece = b.get(start);
   Piece ending = b.get(end);
   if (ending != null
       && piece.validCapture(start, end)
       && checkIfPathIsClear(start, end)
       && piece.getColor() != ending.getColor()) {
     return true;
   }
   return false;
 }
Пример #3
0
 public boolean movePiece(Point startLocation, Point endLocation) {
   Piece piece = b.get(startLocation);
   if (piece.pieceMovement(startLocation, endLocation)
       && checkIfPathIsClear(startLocation, endLocation)) {
     b.put(endLocation, b.get(startLocation));
     b.remove(startLocation);
     piece.setHasNotMoved(false);
     return true;
   }
   return false;
 }
Пример #4
0
 /** Метод очищает поле от костяшек и заполняет новыми */
 public void createField() {
   mGamePanel.removeAll(); // очищаем поле от предыдущих костяшек
   for (int i = 0; i < SIDE_SIZE; i++)
     for (int j = 0; j < SIDE_SIZE; j++)
       if (mBoard.get(j, i) != 0) {
         mLabels[i][j] = new Label(); // создаем новые костяшек
         mGamePanel.add(mLabels[i][j]);
         mLabels[i][j].setSize(CELL_SIZE, CELL_SIZE);
         mLabels[i][j].setLocation(j * CELL_SIZE, i * CELL_SIZE);
         mLabels[i][j].setText(Byte.toString(mBoard.get(j, i))); // заполняем их числами из поля
       } else // если поле пустое, то объекта не создаем
       mLabels[i][j] = null;
 }
Пример #5
0
 public boolean takePiece(Point startLocation, Point endLocation) {
   Piece piece = b.get(startLocation);
   Piece ending = b.get(endLocation);
   if (piece.validCapture(startLocation, endLocation)
       && checkIfPathIsClear(startLocation, endLocation)
       && piece.getColor() != ending.getColor()) {
     b.remove(endLocation);
     b.put(endLocation, b.get(startLocation));
     b.remove(startLocation);
     piece.setHasNotMoved(false);
     return true;
   }
   return false;
 }
Пример #6
0
 /** Test of get method, of class Board. */
 @Test
 public void testGetString() {
   System.out.println("get");
   Board obj = new Board();
   assertEquals(Piece.BP, obj.get("b1"));
   assertEquals(Piece.BP, obj.get("g8"));
   assertEquals(Piece.WP, obj.get("a7"));
   assertEquals(Piece.WP, obj.get("h2"));
   assertEquals(Piece.EMP, obj.get("a1"));
   assertEquals(Piece.EMP, obj.get("a8"));
   assertEquals(Piece.EMP, obj.get("h1"));
   assertEquals(Piece.EMP, obj.get("h8"));
   assertEquals(Piece.EMP, obj.get("b2"));
   assertEquals(Piece.EMP, obj.get("g7"));
 }
Пример #7
0
  public boolean castling(Point k1, Point k2, Point r1, Point r2) {
    Piece k = b.get(k1);
    Piece r = b.get(r1);

    if (checkIfPathIsClear(k1, r1) && k.pieceMovement(k1, k2) && r.pieceMovement(r1, r2)) {
      b.put(k2, b.get(k1));
      b.put(r2, b.get(r1));
      b.remove(k1);
      b.remove(r1);
      k.setHasNotMoved(false);
      r.setHasNotMoved(false);
      return true;
    }
    return false;
  }
Пример #8
0
  @Override
  public void paint(Graphics g) {

    if (g instanceof Graphics2D)
      ((Graphics2D) g)
          .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    final Color evenColor = new Color(0xfece9e);
    final Color oddColor = new Color(0xd08c47);

    for (int y = 0; y < Board.height; y++) {
      for (int x = 0; x < Board.width; x++) {
        if (((x ^ y) & 1) == 0) g.setColor(evenColor);
        else g.setColor(oddColor);

        g.fillRect(x * 32, y * 32, 32, 32);
      }
    }

    for (int y = 0; y < Board.height; y++) {
      for (int x = 0; x < Board.width; x++) {
        if (currentMove == null || currentMove.piece.x != x || currentMove.piece.y != y)
          drawPiece(g, x * 32, y * 32, board.get(x, y));
      }
    }

    if (currentMove != null) {

      if (g instanceof Graphics2D) {

        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));

        if (currentMove.possibleMoves != null)
          for (Move m : currentMove.possibleMoves) {
            drawPiece(
                g, m.p1.x * 32, m.p1.y * 32, board.get(currentMove.piece.x, currentMove.piece.y));
          }

        ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
      }

      drawPiece(
          g,
          currentMove.position.x + currentMove.offset.x,
          currentMove.position.y + currentMove.offset.y,
          board.get(currentMove.piece.x, currentMove.piece.y));
    }
  }
Пример #9
0
  /**
   * Paints the graphic component
   *
   * @param g Graphic component
   */
  public void paint(Graphics g) {
    if (environment != null) {
      Sudoku env = (Sudoku) environment;
      Board board = env.getBoard();

      int n = SudokuLanguage.DIGITS;
      int sqrt_n = (int) (Math.sqrt(n) + 0.1);

      g.setColor(Color.lightGray);
      Font font = g.getFont();
      g.setFont(new Font(font.getName(), font.getStyle(), 20));
      for (int i = 0; i < n; i++) {
        int ci = getCanvasValue(i);
        if (i % sqrt_n == 0) {
          g.drawLine(ci, DRAW_AREA_SIZE + MARGIN, ci, MARGIN);
          g.drawLine(DRAW_AREA_SIZE + MARGIN, ci, MARGIN, ci);
        }

        for (int j = 0; j < n; j++) {
          int cj = getCanvasValue(j);
          int value = board.get(i, j);
          if (value > 0) {
            g.setColor(Color.black);
            g.drawString("" + value, cj + CELL_SIZE / 5, ci + CELL_SIZE);
            g.setColor(Color.lightGray);
          }
        }
      }
      g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN);
      g.drawLine(DRAW_AREA_SIZE + MARGIN, DRAW_AREA_SIZE + MARGIN, MARGIN, DRAW_AREA_SIZE + MARGIN);
    }
  }
Пример #10
0
 public boolean noMoves() {
   T048 copy = new T048();
   for (int r = 0; r < _board.size(); r++) {
     for (int c = 0; c < _board.size(); c++) {
       copy._board.set(r, c, _board.get(r, c));
     }
   }
   return !(copy.swipeU() || copy.swipeD() || copy.swipeL() || copy.swipeR());
 }
Пример #11
0
  public ArrayList<Point> validMove(Point p) {
    ArrayList<Point> validMoves = new ArrayList<Point>();
    Piece piece = b.get(p);

    for (int i = 1; i <= 8; i++) {
      for (int j = 1; j <= 8; j++) {
        Piece p2 = b.get(new Point(i, j));
        if ((p2 == null && piece.pieceMovement(p, new Point(i, j))
                || (validTake(p, new Point(i, j))))
            && !isKingInCheck()
            && checkIfPathIsClear(p, new Point(i, j))) {
          validMoves.add(new Point(i, j));
          //	System.out.println(p + " " + i + " " + j );
        }
      }
    }
    return validMoves;
  }
Пример #12
0
  /**
   * <b>Method: </b>convertFromProtocol</br> <b>Usage: </b>{@code convertFromProtocol(string)}</br>
   * -------------------------------</br> Splits the String protocol received as an array. </br>
   * part[0] indicates the type of Move received. </br> part[1] indicates the location of the source
   * as RowCol without any marker in between. </br> part[2] indicates the locaiton of the
   * destination as RowCol without any maker in between. </br> ex) "Move 34 54" represents a normal
   * Move with piece at (3,4) to be moved to (5,4) on the board.
   *
   * @param protocol defined in a network Chess game
   * @return Move represented by protocol
   */
  private Move convertFromProtocol(String protocol) {
    Board board = getBoard();

    String[] part = protocol.split(" ");
    String type = part[0];
    Piece target =
        board.get(
            new Location(
                Integer.parseInt(part[1].substring(0, 1)),
                Integer.parseInt(part[1].substring(1, 2))));

    Location destination =
        new Location(
            Integer.parseInt(part[2].substring(0, 1)), Integer.parseInt(part[2].substring(1, 2)));

    Move move = null;
    if (type.equals("move")) move = new Move(target, destination);
    else if (type.equals("castle")) move = new Castle(target, board.get(destination));
    else if (type.equals("promotion")) move = new Promotion(target, destination);

    return move;
  }
Пример #13
0
  /*====================================
  adds a new tile to the board in a random location
  1/10 chance of the tile being a 4, 9/10 of a 2
  ====================================*/
  public void addNewTile() {
    int valToAdd;
    if (Math.random() * 10 < 1) valToAdd = 4;
    else valToAdd = 2;

    int r = (int) (Math.random() * 4);
    int c = (int) (Math.random() * 4);

    // making sure its not overwriting an existing value
    while (_board.get(r, c) != 0) {
      r = (int) (Math.random() * 4);
      c = (int) (Math.random() * 4);
    }

    _board.set(r, c, valToAdd);
  }
Пример #14
0
 /**
  * Puts the piece at src at the location dest. If src is null, the piece at dest is just removed.
  * If dest is null, it has the same effect for the piece at src.
  *
  * @param src
  * @param dest
  */
 public void replace(Location src, Location dest) {
   if (src == null) remove(dest);
   else if (dest == null) remove(src);
   else {
     Piece s = board.get(src);
     if (s.redOwner()) {
       redPieces.remove(src);
       redPieces.add(dest);
       bluPieces.remove(dest);
     } else {
       bluPieces.remove(src);
       bluPieces.add(dest);
       redPieces.remove(dest);
     }
     board.put(dest, s);
   }
 }
Пример #15
0
  /*====================================
  swipe right: swipes right, combining necessary tiles
  returns true if a swipe was able to happen and did happen,
  false if you cannot swipe right
  ====================================*/
  public boolean swipeR() {
    // to compare to new one after a swipe
    String old = _board.toString();

    // for each row
    for (int r = 0; r < 4; r++) {
      // first to combine similar tiles
      // for the first 3 tiles
      for (int c = 3; c > 0; c--) {
        // if you find a meaningful element . . .
        if (!_board.get(r, c).equals(0)) {
          // for the rest of the tiles . . .
          for (int n = c - 1; n >= 0; n--) {
            if (_board.get(r, c).equals(_board.get(r, n))) {
              _board.set(r, c, 2 * (Integer) _board.get(r, c));
              _board.set(r, n, 0);
              break;
            } else if (!_board.get(r, n).equals(0)) break;
          }
        }
      }
      // then to get rid of 0s and move everything over
      // for the first 3 tiles again
      for (int c = 3; c > 0; c--) {
        // if you find a 0
        if (_board.get(r, c).equals(0)) {
          // for the rest of the tiles . . .
          for (int n = c - 1; n >= 0; n--) {
            if (!_board.get(r, n).equals(0)) {
              _board.set(r, c, _board.get(r, n));
              _board.set(r, n, 0);
              break;
            }
          }
        }
      }
    }
    if (old.equals(_board.toString())) return false;
    return true;
  }
Пример #16
0
  public void run() {
    for (Behavior bee : io.getBehaviors()) {
      // Getting Pieces
      if (bee.getPiece() != null) {
        placePiece(bee.getStartPoint(), bee.getPiece());
        System.out.println(bee.toString());
      } else if (isWhitesTurn == b.get(bee.getStartPoint()).getColor()) {

        //			make into methods

        // Taking pieces
        if (this.b.get(bee.getEndPoint()) != null) {
          if (takePiece(bee.getStartPoint(), bee.getEndPoint())) {
            System.out.println(bee.toString());
          } else {
            System.err.println("Cannot take " + bee.toString());
          }
          pawnPromotion(bee.getEndPoint());
        }
        // castling
        else if (bee.getP3() != null && bee.getP4() != null) {
          if (castling(bee.getStartPoint(), bee.getEndPoint(), bee.getP3(), bee.getP4())) {
            System.out.println(bee.toString());
          } else {
            System.err.println("Cannot move " + bee.toString());
          }

        }
        // moving pieces
        else if (this.b.get(bee.getEndPoint()) == null) {
          if (movePiece(bee.getStartPoint(), bee.getEndPoint())) {
            System.out.println(bee.toString());
          } else {
            System.err.println("Cannot move " + bee.toString());
          }
          pawnPromotion(bee.getEndPoint());
        }

        isWhitesTurn = !isWhitesTurn;

        if (checkForCheckMate() && isWhitesTurn) {
          System.out.println("Checkmate. Black wins!");
        } else if (checkForCheckMate() && !isWhitesTurn) {
          System.out.println("Checkmate. White wins!");
        } else if (checkForCheckMate() && !isKingInCheck()) {
          System.out.println("StaleMate.");
        } else if (isKingInCheck()) {
          System.out.println("The King is in check");
        }

      } else {
        if (isWhitesTurn) {
          System.out.println("Its not Black's turn!");
        } else if (!isWhitesTurn) {
          System.out.println("Its not White's turn!");
        }
      }

      cd.displayBoard();
    }
  }
    private int calcBoard(Board board) {
      // prepare list for checks
      char[][] connectCellCheckList = {{}, {}, {}, {}};

      String line;
      Map<String, Integer> pattern =
          getPatternList(board.emptySymbol, board.ownSymbol, board.oppSymbol);

      int patternScore = 0;
      int score = 0;
      boolean diag5;
      boolean diag6;

      // check all fields and look for connect 4s
      for (int col = 0; col < board.boardCols; col++) {
        for (int row = board.boardRows - 1; row >= 0; row--) {
          connectCellCheckList = new char[][] {{}, {}, {}, {}};

          // check entire column
          if (row > 4) {
            connectCellCheckList[0] =
                new char[] {
                  board.get(col, row),
                  board.get(col, row - 1),
                  board.get(col, row - 2),
                  board.get(col, row - 3),
                  board.get(col, row - 4),
                  board.get(col, row - 5)
                };
          }
          // check entire row
          if (col < board.boardCols - 6) {
            connectCellCheckList[1] =
                new char[] {
                  board.get(col, row),
                  board.get(col + 1, row),
                  board.get(col + 2, row),
                  board.get(col + 3, row),
                  board.get(col + 4, row),
                  board.get(col + 5, row),
                  board.get(col + 6, row)
                };
          }
          // check diagnoal l-2-r
          if ((col > 2 && row == board.boardRows - 1) || (col == board.boardCols - 1 && row > 2)) {
            diag5 =
                (col > 3 && row == board.boardRows - 1) || (col == board.boardCols - 1 && row > 3);
            diag6 = (col > 4 && row == board.boardRows - 1);
            connectCellCheckList[2] =
                new char[] {
                  board.get(col, row),
                  board.get(col - 1, row - 1),
                  board.get(col - 2, row - 2),
                  board.get(col - 3, row - 3),
                  (diag5 ? board.get(col - 4, row - 4) : '\0'),
                  (diag6 ? board.get(col - 5, row - 5) : '\0')
                };
          }
          // check diagnoal l-2-r
          if ((col < board.boardCols - 3 && row == board.boardRows - 1) || (col == 0 && row > 2)) {
            diag5 =
                (col < board.boardCols - 4 && row == board.boardRows - 1) || (col == 0 && row > 3);
            diag6 = (col < board.boardCols - 5 && row == board.boardRows - 1);
            connectCellCheckList[3] =
                new char[] {
                  board.get(col, row),
                  board.get(col + 1, row - 1),
                  board.get(col + 2, row - 2),
                  board.get(col + 3, row - 3),
                  (diag5 ? board.get(col + 4, row - 4) : '\0'),
                  (diag6 ? board.get(col + 5, row - 5) : '\0')
                };
          }

          for (char[] cells : connectCellCheckList) {
            if (cells.length >= 4) {
              line = String.copyValueOf(cells);
              for (String key : pattern.keySet()) {
                if (line.contains(key)) {
                  score += pattern.get(key);
                  // System.out.println("Pattern [" + key + "," + pattern.get(key) + "]\t found in "
                  // + line);
                }
              }
            }
          }
        }
      }

      return score;
    }