Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to use an array that has int values "keyed" to a String name.

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to use an array that has int values "keyed" to a String name.

    I am trying to write a program that organizes results from a tournament of computer games using bots. I want to do interesting things with the data like rank the bots on the results of each map, results of all the maps, and on their averages ... and so on
    I am new to arrays so maybe arrays are a bad choice for this, so please correct me if I'm wrong.
    So each tournament consists of 5 maps, there are 5 players in each map ("daemia", "xaero", "ezflow", "slash", "doom").
    I have used two classes: Bot.java - has methods to retrieve the bots frag (aka "kill") data, like average frags, total frags and also the bots name.
    RankBotsVariant.java - has the main method in which I create 5 Bot objects and put their scores in after the tournament (the Bot
    constructor has space for 5 int results and a String name).
    I am having trouble ranking the bots in terms of their results maybe you can help me or give some advice

    //RankBotsVariant.java
    //imports Bot objects from Bot.class
     
    import java.text.DecimalFormat;
     
    public class RankBotsVariant  {
     
      public static void main (String[] args)  {
     
      Bot bot1 = new Bot ("Daemia", 11, 4, 6, 3, 5);
      Bot bot2 = new Bot ("Xaero", 5, 5, 6, 9, 10);
      Bot bot3 = new Bot ("EZfl0w", 5, 8, 3, 6, 9);
      Bot bot4 = new Bot ("Slash", 6, 5, 8, 11, 4);
      Bot bot5 = new Bot ("D00m", 5, 3, 7, 5, 19);
     
      //print fragsAllMaps
      System.out.println ("***Total Frags -All Maps***");
      System.out.println ("\tDAEMIA: " + bot1.getFragsAllMaps());
      System.out.println ("\tXAERO: " + "\t" + bot2.getFragsAllMaps());
      System.out.println ("\tezFL0W: " + bot3.getFragsAllMaps());
      System.out.println ("\tSLASH: " + "\t" + bot4.getFragsAllMaps());
      System.out.println ("\tDOOM: " + "\t" + bot5.getFragsAllMaps());
     
      //best average
      System.out.println ("***Average Frag-rate - Over All Maps***");
      System.out.println ("\tDAEMIA: "  + bot1.getAvgFrags());
      System.out.println ("\tXAERO: " + "\t" + bot2.getAvgFrags());
      System.out.println ("\tezFL0W: " + bot3.getAvgFrags());
      System.out.println ("\tSLASH: " + "\t" + bot4.getAvgFrags());
      System.out.println ("\tDOOM: " + "\t" + bot5.getAvgFrags());
     
     
      //prints the winner of each map:
      System.out.println ("*******Winner Of Each Map*********");
     
      String [] maps = {"LostWorld", "DM18", "Shibam", "TheBouncyMap",
                        "Hearth"};
      int [] data = {bot1.getfm1(), bot2.getfm1(), bot3.getfm1(), 
                     bot4.getfm1(),bot5.getfm1()};
     
      for (int map = 0; map < maps.length; map++)  {
          System.out.println ("The winner of " + maps[map] + " was ");
     
          int largest = data[0];
          for (int i = 0; i < data.length; i++)  {
            if (data[i] > largest)  {
                largest = data[i];
            }
          }
          System.out.println ( + largest);
     
      }
     
     
      }
    }
    //Bot.java
    //Holds get methods for frag data used in the RankBotsVarint class. 
     
    import java.util.Arrays;
     
    public class Bot  {
     
      public int fm1, fm2, fm3, fm4, fm5; //frags map 1 , etc..
      public int fragsfm1, fragsfm2, fragsfm3, fragsfm4, fragsfm5;
      public int fragsAllMaps = 0;
      public double avgFrags = 0;
      public String botName, winner, victor;
      private final int MAPS = 5;
     
     
      public Bot (String alias, int m1, int m2, int m3, int m4, int m5)  {
     
        botName = alias;
        fm1 = m1;
        fm2 = m2;
        fm3 = m3;
        fm4 = m4;
        fm5 = m5; 
      }
     
      public int getFragsAllMaps ()  {
        fragsAllMaps = fm1 + fm2 + fm3 + fm4 + fm5;
        return fragsAllMaps;
      }
     
      public String getBotName ()  {
        return botName;
      }
     
      public double getAvgFrags ()  {
        avgFrags = (fm1 + fm2 + fm3 + fm4 + fm5)  / (double)MAPS;
        return avgFrags;
      }
     
      public int getfm1 ()  {
      fragsfm1 = fm1;
      return fragsfm1;
      }
     
      public int getfm2 ()  {
      fragsfm2 = fm2;
      return fragsfm2;
      }
     
      public int getfm3 ()  {
      fragsfm3 = fm3;
      return fragsfm3;
      }
     
      public int getfm4 ()  {
      fragsfm4 = fm4;
      return fragsfm4;
      }
     
      public int getfm5 ()  {
      fragsfm5 = fm5;
      return fragsfm5;
      }
    }


    --- Update ---

    What i'm having trouble with is the halfway down the RankBotsVariant class where it says
     //prints the winner of each map:
    I cant get it to print the winners name or rank all the bots from 1-5 which is what i'd really like to do.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    Why isn't this method (and the rest like it):
    public int getfm4 ()  {
      fragsfm4 = fm4;
      return fragsfm4;
      }
    Simply:
    public int getfm4 ()  {
      return fm4;
      }
    What's the purpose of fragsfm4?

    Why have the number of maps hardcoded into the Bot class? Look at all of the work you'll have to do if you decide to add a 6th or 7th map. I recommend you include the number of maps as a parameter in the Bot() constructor and from that create an array (or collection of your choice) of map scores.

    As for finding the highest bot score for each map or ranking the bots scores in order, there should be a method in your tournament class that checks each bot's score and returns those (there could be ties) with the highest score for each map. Break the problem down into parts, making each part as simple as possible, and then write a method for each part.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    I took what you wrote into account and everything. Unfortunately I still cant progress very far. I tried drawing the class relationships out on paper but I'm still stuck. Problem is when I try to put the data of each bot into an array I can sort that data - no worries - but i lose the fact that that frag total was related to a particular bot. For example if map 1 went like this :
    daemia 10 frags, xaero 5 frags, ezflow 8 frags, doom 13 frags. I'll use arrays.sort to rank the frags but then i'm left with 13, 10, 8, 5. Which bears no relationship to which bot won or lost.
    But i'll post my changes anyway:
    //Bot.java
     
    public class Bot  {
     
      String botName;
      final int MAPS = 4;   //change when necessary
      int players = 4;   //change when necessary
     
      public Bot (String alias, int terra, int pool)  {
                 alias = botName;
                 terra = MAPS;
                 pool = players;
      }
     
      //stores the results of each map seperately
      public void addResult (int m1, int m2, int m3, int m4)  {
     
             int [] map1 = new int [MAPS];
             for (int map = 0; map < MAPS; map++)  {
                 Bot [] bots = {daemia, xaero, ezflow, doom};
                 for (int i = 0; i < 4; i++)  {
                 }
             }    
      }
    }

    //Tournament.java
     
    import java.text.DecimalFormat;
     
    public class Tournament  {
     
      public static void main (String[] args)  {
     
       Bot daemia = new Bot ("DAEMIA");
       Bot xaero = new Bot ("XAERO");
       Bot ezflow = new Bot ("EZFLOW");
       Bot doom = new Bot ("DOOM");
     
      daemia.addResult (5, 7, 3, 9);
      xaero.addResult (7, 8, 3, 11);
      ezflow.addResult (6, 8, 5, 6);
      doom.addResult (11, 4, 3, 7);
     
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    use arrays.sort to rank the frags
    That doesn't work with parallel arrays. Any changing of the position of an element in one array must be matched by changing the corresponding elements in the other arrays in the same way.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    Wait, why can't you use a Map instead of two arrays (a Map being the data structure, not being your game map)?
    The Map key would be the name of the map, while the Map's value would be the data?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    Well thanks for your help on this one guys but I'm going to throw in the towell. I had a good read of the collections and map APIs and I think its too hard a problem to solve. I'm going to move on to the next one in my textbook

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    Nonsense! It's a difficult problem to solve with parallel arrays - near enough to impossible - so you should change the design to eliminate use of parallel arrays. Each Bot instance should record how many frags that bot achieved for each map. Then write a method that collects each bot's number of frags for each map and report the results. The results could be presented in a table or a scoreboard kind of display:
    ---------Number of frags per Map----------
    Bot Name  Map1   Map2   Map3   Map4   Map5
    name1      a      b      x       y      z
    name2      a      b      x       y      z
    name3      a      b      x       y      z
    name4      a      b      x       y      z
    name5      a      b      x       y      z
    etc . . .
     
    and/or a table of winners for each map

  8. #8
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    Thats true. I had this in my earliest effort where each bot had a score for each map. In the constructor I called it then fm1, fm2 ,fm3 , fm4 , fm5.
    public class Bot  {
     
      public int fm1, fm2, fm3, fm4, fm5; //frags map 1 , etc..
     
      //constructor:
      public Bot (String alias, int m1, int m2, int m3, int m4, int m5)  {
     
          botName = alias;
          fm1 = m1;
          fm2 = m2;
          fm3 = m3;
          fm4 = m4;
          fm5 = m5; 
      }
    and I entered my data here:
     public static void main (String[] args)  {
     
      Bot bot1 = new Bot ("Daemia", 11, 4, 6, 3, 5);
      Bot bot2 = new Bot ("Xaero", 5, 5, 6, 9, 10);
      Bot bot3 = new Bot ("EZfl0w", 5, 8, 3, 6, 9);
      Bot bot4 = new Bot ("Slash", 6, 5, 8, 11, 4);
      Bot bot5 = new Bot ("D00m", 5, 3, 7, 5, 19);

    How would you write a good method to compare the data, say..... to find the winner of fm1? Not asking you to do the work for me or anything its just I've really tried getting this for a LONG time with many different methods and I cant get what I need.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to use an array that has int values "keyed" to a String name.

    When you see variable names like m1, m2, m3, . . . , mX the analytic half of your brain should smack the creative half. Using one- or two-letter variable names is bad programming practice, but adding a number to any variable name so there can be multiples of that variable is ANTI-programming. So I'd start by fixing that:

    Bot[] bots = new Bot[5];
    Map[] maps = new Map[5];

    And an instance variable in the Bot class:

    int[] mapScores;

    or

    List<Integer> mapScores;

    Then a method that finds the maximum in a collection is one of the first exercises someone studying collections does right before swapping elements and then sorting them.

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. "possible loss of precision, found double, required int" HELP
    By kkatchh in forum Loops & Control Statements
    Replies: 3
    Last Post: November 6th, 2011, 10:50 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM