예제 #1
0
  public static void main(String[] args) {

    //        JFrame jw = new JFrame();

    JFrame jw = new JFrame();
    JPanel jp = new JPanel();

    // matrica koja nece trebati da postoji kad ima listenera

    jw.add(jp);
    jw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    game = new GameEngine(jp);

    try {
      game.setupGame(
          new IGameDefinition() {

            @Override
            @SuppressWarnings("EmptyCatchBlock")
            public BufferedImage getBackground() {
              return null;
            }

            @Override
            public List<IPlayer> getPlayers() {
              RizikoPlayer a = new RizikoPlayer("Player Crveni", FigureColor.RED);
              RizikoPlayer b = new RizikoPlayer("Player Plavi", FigureColor.BLUE);
              return Arrays.asList(b, a);
            }

            @Override
            public IBoard getBoard() {
              return new WorldBoard();
            }
          });
    } catch (InvalidGameSetup invalidGameSetup) {
      invalidGameSetup.printStackTrace();
    }

    game.addBoardClickResolutionListener(
        (gameEngine, board, boardCoordinates, player, targetElement) -> {
          if (((WorldBoard) board).getBoardElementAt(boardCoordinates) != null) {

            ResolutionAction[] actions = null;
            currentPlayer = (RizikoPlayer) game.getCurrentPlayer();
            RisikoBoardElement t =
                (RisikoBoardElement) ((WorldBoard) board).getBoardElementAt(boardCoordinates);

            int numberOfTanks = 0;

            for (IFigure f : t.getDefaultStack().getFigures()) {
              if (((RizikoFigure) f).getType() == FigureType.tank) numberOfTanks++;
            }
            int numberOfFlags = t.getDefaultStack().getFigures().size() - numberOfTanks;
            RizikoFigure riz = null;
            if (t.getOwner() == null) {
              t.setOwner(currentPlayer);
            }
            // gleda se ko je Owner teritorije, ako nije owner ne moze da stavi tenkic
            if (t.getOwner() == currentPlayer) {
              if (ukupnoTenkova > 0) {
                //
                //                        if (numberOfTanks < 5) {
                //                            riz = new RizikoFigure(FigureType.tank,
                // currentPlayer.getColor(), currentPlayer, t.getDefaultStack(), t, t.getBoard());
                //                        } else
                //                            riz = new RizikoFigure(FigureType.flag,
                // currentPlayer.getColor(), currentPlayer, t.getDefaultStack(), t, t.getBoard());
                //                        if (numberOfTanks < 3) {
                //                            riz.setOrigin(new Point((int) riz.getOrigin().getX() +
                // t.getWidth() / 4 - 20 * numberOfFlags, (int) riz.getOrigin().getY() +
                // t.getHeight() / 4));
                //                            actions = new
                // ResolutionAction[]{ResolutionAction.create(ActionEnum.AddFigure).usingFigure(riz).forPlayer(currentPlayer).toStack(t.getDefaultStack())};
                //                        } else if (numberOfTanks < 5) {
                //                            riz.setOrigin(new Point((int) riz.getOrigin().getX() +
                // t.getWidth() / 4 - 60 - 20 * numberOfFlags, (int) riz.getOrigin().getY() +
                // t.getHeight() / 4 + 20));
                //                            actions = new
                // ResolutionAction[]{ResolutionAction.create(ActionEnum.AddFigure).usingFigure(riz).forPlayer(currentPlayer).toStack(t.getDefaultStack())};
                //                        } else if (numberOfTanks == 5) {
                //                            int n = t.getDefaultStack().getFigures().size() -
                // numberOfTanks + 1;
                //                            t.getDefaultStack().getFigures().clear();
                //                            for (int i = 0; i < n; i++) {
                //                                System.out.println(numberOfFlags);
                //                                riz.setOrigin(new Point((int)
                // riz.getOrigin().getX() + t.getWidth() / 4 - 20 * i, (int) riz.getOrigin().getY()
                // + t.getHeight() / 4));
                //                                actions = new
                // ResolutionAction[]{ResolutionAction.create(ActionEnum.AddFigure).usingFigure(riz).forPlayer(currentPlayer).toStack(t.getDefaultStack())};
                //                            }
                //                        }
                ukupnoTenkova--;
                RizikoBoardElementVisualPresenter elementVisualPrez =
                    ((RizikoBoardElementVisualPresenter) t.getVisualPresentation());
                int no = elementVisualPrez.getNumberOfTanks();
                elementVisualPrez.setNumberOfTanks(no + 1);
                riz =
                    new RizikoFigure(
                        FigureType.tank,
                        currentPlayer.getColor(),
                        currentPlayer,
                        t.getDefaultStack(),
                        t,
                        t.getBoard(),
                        false);
                actions =
                    new ResolutionAction[] {
                      ResolutionAction.create(ActionEnum.AddFigure)
                          .usingFigure(riz)
                          .forPlayer(currentPlayer)
                          .toStack(t.getDefaultStack())
                    };
              } else {

                // deselktovanje svih elemanta
                for (IBoardElement element : game.getBoard().getAllElements()) {
                  RisikoBoardElement rizElement = (RisikoBoardElement) element;
                  RizikoBoardElementVisualPresenter elementVisualPrez =
                      ((RizikoBoardElementVisualPresenter) rizElement.getVisualPresentation());
                  if (elementVisualPrez.isHighlighted()) elementVisualPrez.removeHighlighted();
                }

                RisikoBoardElement attackSource =
                    (RisikoBoardElement) ((WorldBoard) board).getBoardElementAt(boardCoordinates);

                // selektovanje elemenata svih koje trenutni player moze da napadne
                for (IBoardElement target : attackSource.getAllConnectedBoardElements().keySet()) {
                  RisikoBoardElement targetRiz = (RisikoBoardElement) target;
                  RizikoBoardElementVisualPresenter visualPrez =
                      ((RizikoBoardElementVisualPresenter) targetRiz.getVisualPresentation());
                  if (targetRiz.getOwner() != currentPlayer) {
                    visualPrez.setHighlighted();
                  }
                }
              }
            }
            try {
              game.executeResolution(actions);
            } catch (InvalidGameAction e) {
              e.printStackTrace();
            }
          }
          return new ResolutionAction[] {ResolutionAction.create(ActionEnum.NoOperation)};
        });

    jw.setSize(480, 510);
    jw.setResizable(false);
    jw.setLocationRelativeTo(null);
    jw.setVisible(true);

    try {
      game.startGame();
    } catch (InvalidGameSetup invalidGameSetup) {
      invalidGameSetup.printStackTrace();
    }
    tankSetup();
  }