Пример #1
0
  private boolean checkGameStatus(Position pos) {

    Piece[][] grid = board.getCurrentBoard();
    int row = 0, column = 0, diagonal = 0, antiDiagonal = 0;
    int size = board.getSize();
    int x = pos.getX();
    int y = pos.getY();
    for (int i = 0; i < size; i++) {
      if (!board.isEmptyPosition(new Position(x, i))
          && board.getPiece(new Position(x, i)).getType() == turn) column++;
      if (!board.isEmptyPosition(new Position(i, y))
          && board.getPiece(new Position(i, y)).getType() == turn) row++;
      if (!board.isEmptyPosition(new Position(i, i))
          && board.getPiece(new Position(i, i)).getType() == turn) diagonal++;
      if (!board.isEmptyPosition(new Position(i, size - 1 - i))
          && board.getPiece(new Position(i, size - 1 - i)).getType() == turn) antiDiagonal++;
    }
    System.out.print(row + " " + column + " " + diagonal + " " + antiDiagonal + "\n");
    if (column == size || row == size || diagonal == size || antiDiagonal == size) {
      if (turn == X) {
        System.out.println("Player X has won!");
      } else if (turn == O) {
        System.out.println("Player O has won!");
      }
      return false;
    }
    return true;
  }
Пример #2
0
 @Override
 public void mouseClicked(MouseEvent mouseEvent) {
   Board b = bgui.getGridView();
   if (b.getAction() == Board.Action.ADD && bgui.getSelectedComponent().equals("Gizmo")) {
     String gizmoShape = bgui.getGizmoShape();
     Point mouseP = MouseInfo.getPointerInfo().getLocation();
     Point gridP = b.getLocationOnScreen();
     int x = mouseP.x - gridP.x;
     int y = mouseP.y - gridP.y;
     boolean added = false;
     switch (gizmoShape) {
       case "Circle":
         added = m.addCircularBumper(x, y, 0, "circle");
         break;
       case "Triangle":
         added = m.addTriangularBumper(x, y, 0, "triangle");
         break;
       case "Square":
         added = m.addSquareBumper(x, y, 0, "square");
         break;
       case "Teleporter":
         added = m.addTeleporterBumper(x, y, 0, "teleporter");
         break;
       default:
     }
     if (added) {
       bgui.setMessageColor(Color.GREEN);
       bgui.setMessage(gizmoShape + " added!");
     } else {
       bgui.setMessageColor(Color.RED);
       bgui.setMessage("That space is occupied, " + gizmoShape + " cannot be added");
     }
   }
 }
Пример #3
0
 public boolean validMove(Position pos) {
   if (!board.withinBounds(pos)) {
     return false;
   }
   if (!board.isEmptyPosition(pos)) {
     return false;
   }
   return true;
 }
Пример #4
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;
 }
Пример #5
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;
 }
 @Override
 public void mouseClicked(MouseEvent mouseEvent) {
   Board b = bgui.getGridView();
   if (b.getAction() == Board.Action.KEY_DISCONNECT) {
     Point mouseP = MouseInfo.getPointerInfo().getLocation();
     Point gridP = b.getLocationOnScreen();
     int x = mouseP.x - gridP.x;
     int y = mouseP.y - gridP.y;
     x -= x % 20;
     y -= y % 20;
     f = m.findFlipper(x, y);
     String keys = "";
     if (f != null) {
       for (KeyConnectionFlipper kcf : m.getKeyConnectionsFlipper()) {
         if (kcf.getFlipper().equals(f)) {
           keys += ("'" + KeyEvent.getKeyText(kcf.getKeyID()) + "', ");
         }
       }
       if (keys.length() > 2) {
         keys = keys.substring(0, keys.length() - 2);
       }
       abs = null;
       b.requestFocus();
       bgui.setMessageColor(Color.BLACK);
       bgui.setMessage(
           "This flipper is connected to keys "
               + keys
               + ". Press key to remove connection to it.");
     } else if (m.getAbsorber() != null
         && x <= m.getAbsorber().getXBottomRight()
         && x >= m.getAbsorber().getXTopLeft()
         && y <= m.getAbsorber().getYBottomRight()
         && y >= m.getAbsorber().getYTopLeft()) {
       for (KeyConnectionAbs kca : m.getKeyConnectionsAbs()) {
         keys += ("'" + KeyEvent.getKeyText(kca.getKeyID()) + "', ");
       }
       if (keys.length() > 2) {
         keys = keys.substring(0, keys.length() - 2);
       }
       abs = m.getAbsorber();
       b.requestFocus();
       bgui.setMessageColor(Color.BLACK);
       bgui.setMessage(
           "This absorber is connected to keys "
               + keys
               + ". Press key to remove connection to it.");
     }
   }
 }
Пример #7
0
  public boolean isKingInCheck() {

    Point kingsLocation = null;
    for (Entry<Point, Piece> p : b.entrySet()) {
      if (p.getValue() instanceof King && isWhitesTurn == p.getValue().getColor()) {
        kingsLocation = p.getKey();
      }
    }
    for (Entry<Point, Piece> p : b.entrySet()) {
      if ((isWhitesTurn != p.getValue().getColor()) && validTake(p.getKey(), kingsLocation)) {
        return true;
      }
    }
    return false;
  }
Пример #8
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;
  }
 @Override
 public void keyPressed(KeyEvent keyEvent) {
   Board b = bgui.getGridView();
   if (b.getAction() == Board.Action.KEY_DISCONNECT) {
     if (f != null) {
       boolean removed = false;
       for (KeyConnectionFlipper kcf : m.getKeyConnectionsFlipper()) {
         if (kcf.getFlipper().equals(f) && (kcf.getKeyID() == keyEvent.getKeyCode())) {
           m.getKeyConnectionsFlipper().remove(kcf);
           bgui.setMessageColor(Color.GREEN);
           bgui.setMessage("Key '" + KeyEvent.getKeyText(kcf.getKeyID()) + "' is removed!");
           removed = true;
           break;
         }
       }
       if (!removed) {
         bgui.setMessageColor(Color.RED);
         bgui.setMessage(
             "Key '"
                 + KeyEvent.getKeyText(keyEvent.getKeyCode())
                 + "' is not connected "
                 + " to that flipper! ");
       }
     } else if (abs != null) {
       boolean removed = false;
       for (KeyConnectionAbs kca : m.getKeyConnectionsAbs()) {
         if ((kca.getKeyID() == keyEvent.getKeyCode())) {
           m.getKeyConnectionsAbs().remove(kca);
           bgui.setMessageColor(Color.GREEN);
           bgui.setMessage("Key '" + KeyEvent.getKeyText(kca.getKeyID()) + "' is removed!");
           removed = true;
           break;
         }
       }
       if (!removed) {
         bgui.setMessageColor(Color.RED);
         bgui.setMessage(
             "Key '"
                 + KeyEvent.getKeyText(keyEvent.getKeyCode())
                 + "' is not connected "
                 + " to that absorber! ");
       }
     } else {
       bgui.setMessageColor(Color.YELLOW);
       bgui.setMessage("No Component chosen yet");
     }
   }
 }
Пример #10
0
  private boolean place(Command command) {

    if (!command.hasSecondWord() || !command.hasThirdWord()) {
      System.out.println("Please enter a valid position");
      return true;
    }

    int x = Integer.parseInt(command.getSecondWord());
    int y = Integer.parseInt(command.getThirdWord());
    Position pos = new Position(x, y);

    Piece piece = null;
    if (validMove(pos)) {
      if (turn == X) {
        piece = new X();
      } else if (turn == O) {
        piece = new O();
      }
      board.addPiece(pos, piece);

      view.update(board);
      view.display();
      return checkGameStatus(pos);
    } else {
      System.out.println("Invalid Position");
      return place(parser.getCommand());
    }
  }
Пример #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
  public boolean checkForCheckMate() {

    boolean checkMate = true;

    for (Entry<Point, Piece> p : b.entrySet()) {
      if (isWhitesTurn == p.getValue().getColor() && validMove(p.getKey()).size() != 0) {
        checkMate = false;
      }
    }

    return checkMate;
  }
Пример #13
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));
    }
  }
Пример #14
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;
 }
Пример #15
0
 public void placePiece(Point point, Piece piece) {
   b.put(point, piece);
 }
Пример #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();
    }
  }
Пример #17
0
  public boolean checkIfPathIsClear(Point startPosition, Point endPosition) {
    boolean isPathClear = true;

    Point difference = new Point();
    difference.setLocation(
        Math.abs(endPosition.getX() - startPosition.getX()),
        Math.abs(endPosition.getY() - startPosition.getY()));

    if (difference.getX() > 0 && difference.getY() == 0) {
      int largerPlace =
          (int)
              (startPosition.getX() > endPosition.getX()
                  ? startPosition.getX()
                  : endPosition.getX());
      for (int j = (int) (largerPlace - (difference.getX() - 1)); j < largerPlace; j++) {
        if (b.containsKey(new Point(j, (int) startPosition.getY()))) {
          isPathClear = false;
        }
      }

    } else if ((int) difference.getY() > 0 && (int) difference.getX() == 0) {
      int largerPlace = 0;
      if (startPosition.getY() > endPosition.getY()) {
        largerPlace = (int) startPosition.getY();
      } else {
        largerPlace = (int) endPosition.getY();
      }
      for (int i = largerPlace - ((int) difference.getY() - 1); i < largerPlace; i++) {
        if (b.containsKey(new Point((int) startPosition.getX(), i))) {
          isPathClear = false;
        }
      }
    } else if (difference.getX() == difference.getY()) {
      int leftPoint =
          (int)
              (startPosition.getX() < endPosition.getX()
                  ? startPosition.getY()
                  : endPosition.getY());
      int rightPoint =
          (int)
              (startPosition.getX() > endPosition.getX()
                  ? startPosition.getY()
                  : endPosition.getY());
      int largerPlace =
          (int)
              (startPosition.getX() > endPosition.getX()
                  ? startPosition.getX()
                  : endPosition.getX());

      int slope = leftPoint - rightPoint;

      int y = slope > 0 ? leftPoint - 1 : leftPoint + 1;

      for (int j = (int) (largerPlace - (difference.getX() - 1)); j < largerPlace; j++) {
        if (b.containsKey(new Point(j, y))) {
          isPathClear = false;
        }
        y = slope > 0 ? y - 1 : y + 1;
      }
    }

    return isPathClear;
  }