Beispiel #1
0
  private void go(String input) {

    int gameState = myrisk.getGame().getState();

    if (gameState == RiskGame.STATE_ROLLING || gameState == RiskGame.STATE_DEFEND_YOURSELF) {

      // this does not close it, just resets its params
      blockInput();
    }

    myrisk.parser(input);
  }
Beispiel #2
0
  @Override
  public void paintComponent(Graphics2D g) {

    int csrc = myrisk.hasArmiesInt(c1num);
    int cdes = myrisk.hasArmiesInt(c2num);
    int color1 = myrisk.getColorOfOwner(c1num);
    int color2 = myrisk.getColorOfOwner(c2num);

    int imageAreaHeight = getImageAreaHeight();
    int heightOfComponents =
        ((MoveDialog.DialogLayout) getContentPane().getLayout())
            .getHeightOfComponents(getContentPane());
    // this is the MIDDLE of the images area
    int xOffset = getContentPane().getWidth() / 2;
    int yOffset =
        (getContentPane().getHeight() - heightOfComponents) / 2
            + imageAreaHeight / 4
            + getContentPane().getY();

    // g.setColor(0xFFFF0000);
    // g.drawRect( (getContentPane().getWidth()-imageAreaHeight)/2 ,
    // (getContentPane().getHeight()-heightOfComponents)/2 + getContentPane().getY(),
    // imageAreaHeight, imageAreaHeight);

    MoveDialog.paintMove(
        g,
        xOffset,
        yOffset,
        c1img,
        c2img,
        color1,
        color2,
        myrisk.getCountryCapital(c1num),
        myrisk.getCountryCapital(c2num),
        myrisk.getCountryName(c1num),
        myrisk.getCountryName(c2num),
        csrc,
        cdes,
        0);

    // #####################################################
    // ################## drawing DICE!!!!! ################

    int[] diceXs = getDiceX();
    int ax = diceXs[0], dx = diceXs[1];

    int y1 = yOffset + imageAreaHeight / 4; // top of dice
    int y2 = y1 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);
    int y3 = y2 + red_dice.getHeight() + XULLoader.adjustSizeToDensity(2);

    // just in case in the middle of the draw the att and def get set to null
    int[] atti = att;
    int[] defi = def;

    // this is the max defend dice allowed for this battle
    int deadDice = myrisk.hasArmiesInt(c2num);
    if (deadDice > myrisk.getGame().getMaxDefendDice()) {
      deadDice = myrisk.getGame().getMaxDefendDice();
    }

    // if we need input
    if (max != 0) {
      // selecting the number of attacking dice
      if (canRetreat) {
        g.drawSprite(red_dice, DICE_NORMAL, ax, y1);

        if (noda > 1) {
          g.drawSprite(red_dice, DICE_NORMAL, ax, y2);
        } else if (max > 1) {
          g.drawSprite(red_dice, DICE_DARK, ax, y2);
        }

        if (noda > 2) {
          g.drawSprite(red_dice, DICE_NORMAL, ax, y3);
        } else if (max > 2) {
          g.drawSprite(red_dice, DICE_DARK, ax, y3);
        }

        // draw the dead dice
        g.drawSprite(blue_dice, DICE_DARK, dx, y1);
        if (deadDice > 1) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y2);
        }
        if (deadDice > 2) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y3);
        }
      }
      // selecting the number of dice to defend
      else {
        g.drawSprite(blue_dice, DICE_NORMAL, dx, y1);

        if (nodd > 1) {
          g.drawSprite(blue_dice, DICE_NORMAL, dx, y2);
        } else if (max > 1) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y2);
        }

        if (nodd > 2) {
          g.drawSprite(blue_dice, DICE_NORMAL, dx, y3);
        } else if (max > 2) {
          g.drawSprite(blue_dice, DICE_DARK, dx, y3);
        }
      }
    }
    // battle open and waiting for the attacker to select there number of dice
    else if (atti == null && defi == null && !spinD) {

      // draw the dead dice
      g.drawSprite(blue_dice, DICE_DARK, dx, y1);
      if (deadDice > 1) {
        g.drawSprite(blue_dice, DICE_DARK, dx, y2);
      }
      if (deadDice > 2) {
        g.drawSprite(blue_dice, DICE_DARK, dx, y3);
      }

      if (!spinA) {
        // draw dead dice for attacker
        int AdeadDice = myrisk.hasArmiesInt(c1num) - 1;
        // we assume that the attacker can attack with max of 3 dice

        g.drawSprite(red_dice, DICE_DARK, ax, y1);
        if (AdeadDice > 1) {
          g.drawSprite(red_dice, DICE_DARK, ax, y2);
        }
        if (AdeadDice > 2) {
          g.drawSprite(red_dice, DICE_DARK, ax, y3);
        }
      }
    }

    // #####################################################
    // ##################### END DICE ######################

    final int SPINS_OFFSET = 3;

    if (spinA) {
      g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y1);
      if (noda > 1) {
        g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y2);
      }
      if (noda > 2) {
        g.drawSprite(red_dice, SPINS_OFFSET + r.nextInt(6), ax, y3);
      }
      // g.drawString("ROLLING ATTACKER " + noda +"    " + Math.random() , 50, 100);

      if (spinD) {
        g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y1);
        if (nodd > 1) {
          g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y2);
        }
        if (nodd > 2) {
          g.drawSprite(blue_dice, SPINS_OFFSET + r.nextInt(6), dx, y3);
        }
        // g.drawString("ROLLING DEFENDER " + nodd +"    " + Math.random(), 300, 100);
      }
    }

    DirectGraphics g2 = DirectUtils.getDirectGraphics(g.getGraphics());

    int offset = (int) (red_dice.getWidth() / (29D / 4D) + 0.5);
    int bottom = red_dice.getHeight() - offset - 1; // TODO not sure why -1??
    int halfDice = red_dice.getHeight() / 2;

    if (atti != null && defi != null) {
      {
        int yCoords[] = {y1 + offset, y1 + bottom, y1 + halfDice};
        if (defi[0] >= atti[0]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }
      if (atti.length > 1 && defi.length > 1) {
        int yCoords[] = {y2 + offset, y2 + bottom, y2 + halfDice};
        if (defi[1] >= atti[1]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }
      if (atti.length > 2 && defi.length > 2) {
        int yCoords[] = {y3 + offset, y3 + bottom, y3 + halfDice};
        if (defi[2] >= atti[2]) {
          int xCoords[] = {dx + offset, dx + offset, ax + bottom};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_BLUE);
        } else {
          int xCoords[] = {ax + bottom, ax + bottom, dx + offset};
          g2.fillPolygon(xCoords, 0, yCoords, 0, xCoords.length, COLOR_RED);
        }
      }

      // draw attacker dice
      drawDice(true, atti[0], ax, y1, g);
      if (atti.length > 1) {
        drawDice(true, atti[1], ax, y2, g);
      }
      if (atti.length > 2) {
        drawDice(true, atti[2], ax, y3, g);
      }

      // draw defender dice
      drawDice(false, defi[0], dx, y1, g);
      if (defi.length > 1) {
        drawDice(false, defi[1], dx, y2, g);
      }
      if (defi.length > 2) {
        drawDice(false, defi[2], dx, y3, g);
      }
    }
  }
  public void actionPerformed(ActionEvent a) {

    String command = a.getActionCommand();

    if ("refresh".equals(command)) {

      countriesModel.fireTableDataChanged();
      continentsModel.fireTableDataChanged();
      cardsModel.fireTableDataChanged();
      cardsModel2.fireTableDataChanged();
      playersModel.fireTableDataChanged();
      gameInfo.fireTableDataChanged();
      commands.fireTableDataChanged();

      repaint();
    } else if ("flash".equals(command)) {
      MainMenu.newMainMenuFrame(myrisk, JFrame.DISPOSE_ON_CLOSE);
    } else if ("aiwait".equals(command)) {

      Object[] message = new Object[2];
      message[0] = new JLabel("AI wait time (in milliseconds):");
      message[1] = new JSpinner(new SpinnerNumberModel(AIManager.getWait(), 0, 10000, 100));

      String[] options = {"OK", "cancel"};

      int result =
          JOptionPane.showOptionDialog(
              this, // the parent that the dialog blocks
              message, // the dialog message array
              "AI Options", // the title of the dialog window
              JOptionPane.OK_CANCEL_OPTION, // option type
              JOptionPane.PLAIN_MESSAGE, // message type
              null, // optional icon, use null to use the default icon
              options, // options string array, will be made into buttons
              options[0] // option that should be made into a default button
              );

      if (result == JOptionPane.OK_OPTION) {
        AIManager.setWait(((Integer) ((JSpinner) message[1]).getValue()).intValue());
      }
    } else if ("allcards".equals(command)) {

      if (myrisk.getGame() != null
          && myrisk.getGame().getState() != RiskGame.STATE_NEW_GAME
          && myrisk.getGame().getCards() != null) {

        Frame frame = RiskUIUtil.findParentFrame(this);

        CardsDialog cardsDialog = new CardsDialog(frame, pp, false, myrisk, false);
        Dimension frameSize = frame.getSize();
        Dimension aboutSize = cardsDialog.getPreferredSize();
        int x = frame.getLocation().x + (frameSize.width - aboutSize.width) / 2;
        int y = frame.getLocation().y + (frameSize.height - aboutSize.height) / 2;
        if (x < 0) x = 0;
        if (y < 0) y = 0;
        cardsDialog.setLocation(x, y);

        cardsDialog.populate(myrisk.getGame().getCards());

        cardsDialog.setVisible(true);
      }
    } else if ("checkMapServer".equals(command)) {
      // get all maps
      List<Map> maps = MapUpdateService.getMaps(MapChooser.MAP_PAGE, Collections.EMPTY_LIST);
      Set<String> ids = new HashSet();
      Set<String> errors = new HashSet();
      for (Map map : maps) {
        String fileUID =
            RiskUtil.replaceAll(MapChooser.getFileUID(map.getMapUrl()), " ", "").toLowerCase();
        if (ids.contains(fileUID)) {
          errors.add(fileUID);
        } else {
          ids.add(fileUID);
        }
      }

      if (errors.isEmpty()) {
        JOptionPane.showMessageDialog(this, "No errors found.");
      } else {
        JOptionPane.showMessageDialog(this, "Error found with map: " + errors);
      }
    } else {
      throw new RuntimeException("TestTab: unknown command found: " + command);
    }
  }