Esempio n. 1
0
  // Inicijalno postavljanje tenkica, inace se radi izvlacenjem karata - ubacicemo kasnije ako bude
  // potrebe
  public static void tankSetup() {

    playerA = (RizikoPlayer) ((GameEngine) game).getPlayers().get(0);
    playerB = (RizikoPlayer) ((GameEngine) game).getPlayers().get(1);

    //        currentPlayer = playerA;

    ArrayList<IBoardElement> boardElements =
        (ArrayList<IBoardElement>) ((ArrayList) game.getBoard().getAllElements()).clone();

    ResolutionAction[] actions = null;

    for (int i = 0; i < 9; i++) {
      Collections.shuffle(boardElements);
      RisikoBoardElement t = (RisikoBoardElement) boardElements.remove(0);
      RizikoFigure riz =
          new RizikoFigure(
              FigureType.tank,
              ((RizikoPlayer) game.getCurrentPlayer()).getColor(),
              game.getCurrentPlayer(),
              t.getDefaultStack(),
              t,
              t.getBoard(),
              true);
      riz.setOrigin(new Point(40, 20));
      t.setOwner((RizikoPlayer) game.getCurrentPlayer());
      actions =
          new ResolutionAction[] {
            ResolutionAction.create(ActionEnum.AddFigure)
                .usingFigure(riz)
                .forPlayer(game.getCurrentPlayer())
                .toStack(t.getDefaultStack())
          };
      ukupnoTenkova--;
      try {
        game.executeResolution(actions);
      } catch (InvalidGameAction e) {
        e.printStackTrace();
      }
    }
  }
Esempio n. 2
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();
  }