Beispiel #1
0
  @Override
  public void draw(Graphics2D g2d, Surface s) {

    g2d.setPaint(getColor());
    g2d.fill(getSurface());

    if (getHover() && !selected) {
      Stroke outl = new BasicStroke(2f);
      g2d.setStroke(outl);
      if (getColor() != Color.red) g2d.setPaint(Color.red);
      else g2d.setPaint(Color.BLACK);
      g2d.drawPolygon(getSurface());
    } else if (selected) {
      float[] dash = {4f, 0f, 2f};
      BasicStroke outl =
          new BasicStroke(
              2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash, getFramesAlive() * .2f);
      g2d.setStroke(outl);

      if (ownedby.equals(Player.NONE)) {
        g2d.setPaint(Color.black);
      } else if (ownedby.equals(Player.COMPUTER)) {
        g2d.setPaint(Color.RED);
      } else if (ownedby.equals(Player.PLAYER)) {
        g2d.setPaint(Color.BLUE);
      }

      g2d.drawPolygon(getSurface());
    }

    g2d.setPaint(Color.BLUE.brighter());
    for (Territory t : neighbors) {
      double distance;
      if (CalculateDistance(this.capital.x, this.capital.y, t.GetCapital().x, t.GetCapital().y)
          < 800) g2d.drawLine(this.capital.x, this.capital.y, t.GetCapital().x, t.GetCapital().y);
      else {
        if (CalculateDistance(this.capital.x, this.capital.y, 0, 0) < 500) {
          g2d.drawLine(this.capital.x, this.capital.y, 0, 70);
        } else if (CalculateDistance(this.capital.x, this.capital.y, 0, 0) > 500) {
          g2d.drawLine(this.capital.x, this.capital.y, 1280, 70);
        }
      }
    }

    if (ownedby.equals(Player.NONE)) {
      g2d.setPaint(Color.black);
    } else if (ownedby.equals(Player.COMPUTER)) {
      g2d.setPaint(Color.RED);
    } else if (ownedby.equals(Player.PLAYER)) {
      g2d.setPaint(Color.BLUE);
    }

    g2d.setFont(new Font("TimesRoman", Font.PLAIN, 18));
    g2d.setStroke(new BasicStroke(1f));
    g2d.fillOval(capital.x - 10, capital.y - 10, 20, 20);
    g2d.setPaint(Color.BLACK);
    g2d.drawOval(capital.x - 10, capital.y - 10, 20, 20);
    g2d.setPaint(Color.WHITE);
    g2d.drawString("" + armies, capital.x - 5, capital.y + 5);
  }
Beispiel #2
0
 // Get any neighbor that does not match the owner of this territory
 public Territory GetEnemyNeighbor() {
   for (Territory t : neighbors) {
     if (!t.CheckOwner().equals(this.ownedby)) {
       return t;
     }
   }
   return null;
 }
Beispiel #3
0
 // Check if the parameterized territory is a neighbor and returns the neighbor or null
 public Territory IsNeighbor(String neighborName) {
   for (Territory t : neighbors) {
     if (t.getTerritoryName().equals(neighborName)) {
       return t;
     }
   }
   return null;
 }
Beispiel #4
0
 // Check if the parameterized territory is a neighbor and returns true or false
 public boolean IsNeighbor(Territory neighTer) {
   for (Territory t : neighbors) {
     if (t.equals(neighTer)) {
       return true;
     }
   }
   return false;
 }
Beispiel #5
0
 // Checks if this territory has the ability to attack the parameterized territory
 public boolean CanAttack(Territory toBeAttacked) {
   for (Territory t : neighbors) {
     // check if the parameterized territory is one of the neighbors and its owner does not match
     // with this owner
     if (t.equals(toBeAttacked) && !this.ownedby.equals(t.ownedby)) {
       return true;
     }
   }
   return false;
 }
Beispiel #6
0
  /**
   * paintComponent schreibt alle Laendernamen an die entsprechende Stelle der Landkarte
   * (Hintergrundgrafik) und zeichnet die Laendergrenzen auf der Landkarte nach.
   *
   * @param g Zeichenflaeche der Landkarte
   */
  public void paintComponent(Graphics g) {
    Shape shape;

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
    Font myFont = new Font("Times New Roman", Font.BOLD, 12);
    g2d.setFont(myFont);

    g2d.setStroke(new BasicStroke(2.0f));

    Territory territory;
    Color color;

    ListIterator territories = worldMap.getTerritories().listIterator();

    while (territories.hasNext()) {
      territory = (Territory) territories.next();

      if (territory.getOwner() != null) {
        color = territory.getOwner().getPlayerColor();
      } else {
        color = Color.WHITE;
      }

      g2d.setColor(color);
      g2d.drawString(
          territory.getName(),
          (int) territory.getMidpoint().getX() - 15,
          (int) territory.getMidpoint().getY() - 10);

      g2d.drawString(
          new Integer(territory.getArmySize()).toString(),
          (int) territory.getMidpoint().getX(),
          (int) territory.getMidpoint().getY());
    }

    if (territoryBattle.size() != 0) {
      for (int j = 0; j < territoryBattle.size(); j++) {
        g2d.setColor(territoryBattle.get(j).getOwner().getPlayerColor());
        // g2d.fillPolygon(territoryTmp.getFrontiers());   Sieht bei unseren Grenzen nicht huebsch
        // aus
        g2d.drawPolygon(territoryBattle.get(j).getFrontiers());
      }
    }

    repaint();
  }
Beispiel #7
0
  // Is used if one Territory attacks another returns true if ownedby player has won
  public boolean Attack(Territory defterritory) {

    // Used Variables get initialized
    int attackarmys;
    int defarmys;

    // Get positioned armys on this territory which is attacking and cap them on 3
    attackarmys = (this.CheckArmyCount() - 1);
    if (attackarmys >= 3) {
      attackarmys = 3;
    }
    // Get positioned armys on the other territory which is defending and cap them on 2
    defarmys = (defterritory.CheckArmyCount());
    if (defarmys >= 2) {
      defarmys = 2;
    }
    // If 1 or more army are attacking
    if (attackarmys >= 1) {

      // Create Random Generator
      Random randomGenerator = new Random();

      // Create Arrays for representing the dices
      int[] attackeyes = new int[attackarmys];
      int[] defeyes = new int[defarmys];

      // Get random numbers for the attackdices
      for (int i = 0; i < attackarmys; i++) {
        attackeyes[i] = randomGenerator.nextInt(6) + 1;
      }
      // Get random numbers for the defensedices
      for (int i = 0; i < defarmys; i++) {
        defeyes[i] = randomGenerator.nextInt(6) + 1;
      }
      // Sort the Arrays by number
      java.util.Arrays.sort(attackeyes);
      java.util.Arrays.sort(defeyes);

      // Go through the arrays starting with the highest element
      for (int i = 1; i < (defarmys + 1) && i < (attackarmys + 1); i++) {
        // Check if the attackdice is greater than the defensedice
        if (attackeyes[attackarmys - i] > defeyes[defarmys - i]) {
          System.out.println(
              "Rollwin:"
                  + i
                  + " - Eyes:"
                  + attackeyes[attackarmys - i]
                  + "/"
                  + defeyes[defarmys - i]);
          // If 1 army of the defending territory was removed and the corresponding army was beaten
          // (completely)
          if (defterritory.removeArmies(1, this.ownedby)) {
            // Take over the territory with the attacking armys
            defterritory.enforceArmies(attackarmys - i, this.ownedby);
            // And remove them by their former territory
            this.removeArmies(attackarmys - i, this.ownedby);
            // Return true for a successful attack
            return true;
          }

        } else {
          System.out.println(
              "Rolllose:"
                  + i
                  + " - Eyes:"
                  + attackeyes[attackarmys - i]
                  + "/"
                  + defeyes[defarmys - i]);
          // Else remove one army of the attackers
          this.removeArmies(1, this.ownedby);
        }
      }
    }
    return false;
  }