3D Arrays on Hangman game - java

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 843 Views - Last Post: 11 January 2013 - 06:52 PM Rate Topic: -----

#1 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

3D Arrays on Hangman game - java

Posted 10 January 2013 - 05:26 PM

  public static String getWords(String cat, String diff)
  {
    Random generator = new Random();
    String name = "";
    String topic[][][] = new String[3][3][3];
    while(cat.equalsIgnoreCase("places")){
      if(diff == "easy"){
        topic[0][0][0] = "Paris";
        topic[0][0][1] = "London";
        topic[0][0][2] = "Sydney";}
      if(diff == "medium"){
        topic[0][1][0] = "Toronto";
        topic[0][1][1] = "Florida";
        topic[0][1][2] = "Frankfurt";}
      else if(diff == "Extreme"){
        topic[0][2][0] = "Barcelona";
        topic[0][2][1] = "Vancouver";
        topic[0][2][2] = "Zimbabwe";}
    }
    while(cat.equalsIgnoreCase("games")){
      if(diff == "easy"){
        topic[1][0][0] = "Halo";      //             \\
        topic[1][0][1] = "Fifa";     //  Easy-Games   \\              
        topic[1][0][2] = "GTA";}     //                 \\    
      else if(diff == "Medium"){
        topic[1][1][0] = "Skyrim";
        topic[1][1][1] = "HITMAN";
        topic[1][1][2] = "Batman";}
      else if(diff == "Extreme"){
        topic[1][2][0] = "Minecraft";
        topic[1][2][1] = "Zombieville";
        topic[1][2][2] = "BoderLands";}
    }
    if(cat.equalsIgnoreCase("cars")){
      if(diff == "easy"){
        topic[2][0][0] = "Acura";
        topic[2][0][1] = "Audi";
        topic[2][0][2] = "Bmw";}
      else if(diff == "Medium"){
        topic[2][1][0] = "Bentley";
        topic[2][1][1] = "Buggati";
        topic[2][1][2] = "Honda";}
      else if(diff == "Extreme"){
        topic[2][2][0] = "Lamborghini";
        topic[2][2][1] = "Rolls-Royce";
        topic[2][2][2] = "Mercedes";}
    } 
    
    return cat;
  } 
}


This are my 3d arrays for the game Hangman, like this is my first time doing it, the arrays are for the difficulty chosen and the category, is there a way that this can be called in the main method and use it in this type:

 System.out.println("Choose a category -> places / games / cars")


so when the person chooses a category it will than ask for the difficulty, than it will find a word from those two things given

Is This A Good Question/Topic? 0
  • +

Replies To: 3D Arrays on Hangman game - java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,474
  • Joined: 27-December 08

Re: 3D Arrays on Hangman game - java

Posted 10 January 2013 - 05:40 PM

Yes- you call it and pass it the parameters, just like any other method. Make sure to read up on methods.

I don't understand why you keep populating a new array in the method. It would make more sense really to populate the array once. Then in the method, pick a random index once you narrow down the 1D array from the name and category.

I really think a 3D array is a poor design though. I think a Map, even a nested Map<String, Map<String, List<String>>> would be better practice. Map the category to its words. Then the nested Map would relate easiness to the List of words associated with it.
Was This Post Helpful? 0
  • +
  • -

#3 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 10 January 2013 - 06:23 PM

thing is that we are only allowed to use arrays, this is my updated method, i canèt compile it for some reasons
   public static String getWords(String cat, String diff)
  {
    Random generator = new Random();
    String topic[][][] = new String[3][3][3];
    
    int location = generator.nextInt( 3 );
    
    //initialize array
    topic[0][0][0] = "Paris";
    topic[0][0][1] = "London";//  Easy-Places   \\       
    topic[0][0][2] = "Sydney";
    
    topic[0][1][0] = "Toronto"; //medium places
    topic[0][1][1] = "Florida";
    topic[0][1][2] = "Frankfurt";
    
    topic[0][2][0] = "Barcelona"; //hard places
    topic[0][2][1] = "Vancouver";
    topic[0][2][2] = "Zimbabwe";
    
    topic[1][0][0] = "Halo";      //             \\
    topic[1][0][1] = "Fifa";     //  Easy-Games   \\              
    topic[1][0][2] = "GTA";     //                 \\    
    
    topic[1][1][0] = "Skyrim"; //med games
    topic[1][1][1] = "HITMAN";
    topic[1][1][2] = "Batman";
    
    topic[1][2][0] = "Minecraft"; //hard games
    topic[1][2][1] = "Zombieville";
    topic[1][2][2] = "BoderLands";
    
    topic[2][0][0] = "Acura"; //easy cars
    topic[2][0][1] = "Audi";
    topic[2][0][2] = "Bmw";
    
    topic[2][1][0] = "Bentley"; //med cars
    topic[2][1][1] = "Buggati";
    topic[2][1][2] = "Honda";
    
    topic[2][2][0] = "Lamborghini"; //hard cars
    topic[2][2][1] = "Rolls-Royce";
    topic[2][2][2] = "Mercedes";
    
   if (cat == "places")
    {
      if (diff == "easy")
        return topic[0][0][location];
        if (diff == "med")
        return topic[0][1][location];
        if (diff == "hard")
        return topic[0][2][location];
    }     
    if (cat == "games")
    {
      if (diff == "easy")
        return topic[1][0][location];
        if (diff == "med")
        return topic[1][1][location];
        if (diff == "hard")
        return topic[1][2][location];
    }      
    if (cat == "cars")
    {
      if (diff == "easy")
        return topic[2][0][location];
        if (diff == "med")
        return topic[2][1][location];
        if (diff == "hard")
        return topic[2][2][location];
    } 

This post has been edited by IsukAtJava: 10 January 2013 - 06:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,474
  • Joined: 27-December 08

Re: 3D Arrays on Hangman game - java

Posted 10 January 2013 - 06:25 PM

Two things:
-Compare Strings using the equals() method over the == operator
-I don't know what location is or what the compilation errors are. You need to tell us.
Was This Post Helpful? 1
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: 3D Arrays on Hangman game - java

Posted 10 January 2013 - 06:39 PM

You shouldn't use the equality operator to compare strings. Use String.equals. As stated, you should try and initialize your data structure once and create an easy way to retrieve a word of a certain category and difficulty. A map is one way. Here's an array example.

import java.util.Random;

class Main
{
    private final int EASY = 0, MED = 1;
    private final int PLACES = 0, CARS = 1;
    private Random rand = new Random();
    
    //x - difficulty, y - cats, z - words 
    private final String[][][] words = 
    {
        {
            {"EasyPlace1", "EasyPlace2"}, 
            {"EasyCar1", "EasyCar2"}
            
        },
        {
            {"MedPlace1", "MedPlace2"}, 
            {"MedCar1", "MedCar2"}
        }                            
    };
    
    private String getHangmanWord(int diff, int cat)
    {
        return words[diff][cat][rand.nextInt(2)];
    }
    
    private void run()
    {
        System.out.println(getHangmanWord(EASY, CARS));
        System.out.println(getHangmanWord(MED, PLACES));
    }
    
    public static void main (String[] args) throws java.lang.Exception
    {
        new Main().run();
    }
    
}


This post has been edited by blackcompe: 10 January 2013 - 06:43 PM

Was This Post Helpful? 1
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 05:27 AM

You really only seem to need a simple 1D array for this. e.g.
if ("places".equals(cat) && "easy".equals(diff)) {
	String [] items = { "Paris", "London", "Sydney" };
	return items[generator.nextInt(items.length)];
} else if ("places".equals(cat) && "med".equals(diff)) {
	String [] items = { "Toronto", "Florida", "Frankfurt" };
	return items[generator.nextInt(items.length)];
//...



If you MUST use a 3D array, you can at least make it easier with less magic values:
final int DIFF_EASY = 0, DIFF_MED = 1, DIFF_HARD = 2;
final int CAT_PLACES = 0, CAT_GAMES = 1, CAT_CARS = 2;
final int CHOICE_SIZE = 3;

topic[DIFF_EASY][CAT_PLACES][0] = "Paris";
topic[DIFF_EASY][CAT_PLACES][1] = "London";
topic[DIFF_EASY][CAT_PLACES][2] = "Sydney";

int diffVal;
if (diff.equals("easy")) { diffVal = DIFF_EASY;
} else if (diff.equals("med")) { diffVal = DIFF_MED;
} else if (diff.equals("hard")) { diffVal = DIFF_HARD;
} else {
	System.out.println("Oops, invalid diff");
	return null;
}
// ...

return topic[diffVal][catVal][generator.nextInt(CHOICE_SIZE)];



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#7 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 01:32 PM

View Postbaavgai, on 11 January 2013 - 05:27 AM, said:

You really only seem to need a simple 1D array for this. e.g.
if ("places".equals(cat) && "easy".equals(diff)) {
	String [] items = { "Paris", "London", "Sydney" };
	return items[generator.nextInt(items.length)];
} else if ("places".equals(cat) && "med".equals(diff)) {
	String [] items = { "Toronto", "Florida", "Frankfurt" };
	return items[generator.nextInt(items.length)];
//...



If you MUST use a 3D array, you can at least make it easier with less magic values:
final int DIFF_EASY = 0, DIFF_MED = 1, DIFF_HARD = 2;
final int CAT_PLACES = 0, CAT_GAMES = 1, CAT_CARS = 2;
final int CHOICE_SIZE = 3;

topic[DIFF_EASY][CAT_PLACES][0] = "Paris";
topic[DIFF_EASY][CAT_PLACES][1] = "London";
topic[DIFF_EASY][CAT_PLACES][2] = "Sydney";

int diffVal;
if (diff.equals("easy")) { diffVal = DIFF_EASY;
} else if (diff.equals("med")) { diffVal = DIFF_MED;
} else if (diff.equals("hard")) { diffVal = DIFF_HARD;
} else {
	System.out.println("Oops, invalid diff");
	return null;
}
// ...

return topic[diffVal][catVal][generator.nextInt(CHOICE_SIZE)];



Hope this helps.


when i try that it gives me an error saying there is no catVal
but it does make sense and thank you very much,
only problem is the catVal
also how can u call it in like 1 parameter per time so first it asks the user for category than it asks the user for the difficulty
Was This Post Helpful? 0
  • +
  • -

#8 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 01:59 PM

 System.out.println("Choose a category-> places / games / cars");
      words = getWords(input.next(),null);
      System.out.println("Choose a level: Easy, Medium, Hard");
      words = getWords(null, input.next());


this doesn't work like, my program just crashes, is there a reason this does this?
Was This Post Helpful? 0
  • +
  • -

#9 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:06 PM

Quote

this doesn't work like, my program just crashes, is there a reason this does this?


What's the thrown exception? Could it because you're sending null into a method that's not expecting it?
Was This Post Helpful? 0
  • +
  • -

#10 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:11 PM

View Postblackcompe, on 11 January 2013 - 02:06 PM, said:

Quote

this doesn't work like, my program just crashes, is there a reason this does this?


What's the thrown exception? Could it because you're sending null into a method that's not expecting it?

I'm doing it because i have my parameters like this:
  public static String getWords(String cat, String diff)

and that i don't want to use them together cause i want a println, so like for cat i want t use the first print statement and than for diff i want the second diff
is there a way i can do that??
Was This Post Helpful? 0
  • +
  • -

#11 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:20 PM

Quote

and that i don't want to use them together cause i want a println, so like for cat i want t use the first print statement and than for diff i want the second diff
is there a way i can do that??


I don't understand what you're talking about. I see not one line in any of the snippets you've posted where a System.out.println call is executed. Even if there was, what you're saying doesn't make much sense.

Quote

I'm doing it because i have my parameters like this:

public static String getWords(String cat, String diff)



You ought to be sending arguments with non-null values.

As I said before, clearly state how the program is crashing by telling us what the exception is? Do you know what that is? And as I said, it probably has something to do with you sending null into a method. Post the problematic code in full, so we can see what you're talking about.

This post has been edited by blackcompe: 11 January 2013 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:25 PM

This is my Array Method...
public static String getWords(String cat, String diff)
  {
    String name = "";
    Random generator = new Random();
    String topic[][][] = new String[3][3][3];
    int location = generator.nextInt( 3 );
    
    //initialize array
    topic[0][0][0] = "Paris";
    topic[0][0][1] = "London";//  Easy-Places   \\       
    topic[0][0][2] = "Sydney";
    
    topic[0][1][0] = "Toronto"; //medium places
    topic[0][1][1] = "Florida";
    topic[0][1][2] = "Frankfurt";
    
    topic[0][2][0] = "Barcelona"; //hard places
    topic[0][2][1] = "Vancouver";
    topic[0][2][2] = "Zimbabwe";
    
    topic[1][0][0] = "Halo";      //      
    topic[1][0][1] = "Fifa";     //  Easy-Games               
    topic[1][0][2] = "Gta";     //                    
    
    topic[1][1][0] = "Skyrim"; //med games
    topic[1][1][1] = "Hitman";
    topic[1][1][2] = "Batman";
    
    topic[1][2][0] = "Minecraft"; //hard games
    topic[1][2][1] = "Zombieville";
    topic[1][2][2] = "BoderLands";
    
    topic[2][0][0] = "Acura"; //easy cars
    topic[2][0][1] = "Audi";
    topic[2][0][2] = "Bmw";
    
    topic[2][1][0] = "Bentley"; //med cars
    topic[2][1][1] = "Buggati";
    topic[2][1][2] = "Honda";
    
    topic[2][2][0] = "Lamborghini"; //hard cars
    topic[2][2][1] = "Rolls-royce";
    topic[2][2][2] = "Mercedes";
    
    if (cat.equalsIgnoreCase( "places"))
    {
      if (diff.equalsIgnoreCase("easy"))
        name = topic[0][0][location];
      if (diff.equalsIgnoreCase("medium"))
        name = topic[0][1][location];
      if (diff.equalsIgnoreCase("hard"))
        name = topic[0][2][location];
    }     
    else if (cat.equalsIgnoreCase("games"))
    {
      if (diff.equalsIgnoreCase("easy"))
        name = topic[1][0][location];
      if (diff.equalsIgnoreCase("medium"))
        name = topic[1][1][location];
      if (diff.equalsIgnoreCase("hard"))
        name = topic[1][2][location];
    }      
     if (cat.equalsIgnoreCase("cars"))
    {
      if (diff.equalsIgnoreCase("easy"))
        name = topic[2][0][location];
      if (diff.equalsIgnoreCase("medium"))
        name = topic[2][1][location];
      if (diff.equalsIgnoreCase("hard"))
        name = topic[2][2][location];
      
    }
    return name.toLowerCase();
    
  }
}  



How can i call that in? like how can i ask the user what difficulty and category they want to play with

in the main method with using input.next(); and System.out.println
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:38 PM

Quote

How can i call that in? like how can i ask the user what difficulty and category they want to play with


There are numerous ways, but I suggest using a Scanner to retrieve user input. Specifically, use the Scanner.nextLine method to retrieve an entire line, rather than using Scanner.next. It would be wise to do error checking, but I suppose that's lesser of a priority. Once you have the user input, send it to your method. See the official tutorial on I/O for more info. The following is an example.

public static void main(String[] args)
     {
          Scanner kbd = new Scanner(System.in);
          System.out.println("Enter diff: ");
          String diff = kbd.nextLine();
          System.out.println("Enter cat: ");
          String cat = kbd.nextLine();
          System.out.println("Choosen word: " + getWords(cat, diff));
     }


Was This Post Helpful? 0
  • +
  • -

#14 IsukAtJava  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 35
  • Joined: 31-December 12

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 02:56 PM

View Postblackcompe, on 11 January 2013 - 02:38 PM, said:

Quote

How can i call that in? like how can i ask the user what difficulty and category they want to play with


There are numerous ways, but I suggest using a Scanner to retrieve user input. Specifically, use the Scanner.nextLine method to retrieve an entire line, rather than using Scanner.next. It would be wise to do error checking, but I suppose that's lesser of a priority. Once you have the user input, send it to your method. See the official tutorial on I/O for more info. The following is an example.

public static void main(String[] args)
     {
          Scanner kbd = new Scanner(System.in);
          System.out.println("Enter diff: ");
          String diff = kbd.nextLine();
          System.out.println("Enter cat: ");
          String cat = kbd.nextLine();
          System.out.println("Choosen word: " + getWords(cat, diff));
     }



I have to make the choices given to get the word from the arrays and than make that word into dashes so this is what i tried using urs...though it dosen't work
System.out.println("Enter cat: ");
      String diff = input.nextLine();
      System.out.println("Enter diff: ");
      String cat = input.nextLine();
      words = getWords(cat,diff);
      dashes = makeDashes(words);

Was This Post Helpful? 0
  • +
  • -

#15 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,529
  • Joined: 05-May 05

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 03:11 PM

Quote

I have to make the choices given to get the word from the arrays and than make that word into dashes so this is what i tried using urs...though it dosen't work


You keep saying such and such doesn't work. I'm not a mind reader. Be detailed in your posts. You asked how to read user input and choose a word and I showed you. Not to mention you keep jumping from topic to topic. Let's see the code (in full) that you're running. What's makeDashes? Post the code. What's the output? What are you expecting to see? Also considering using an online IDE to show your code running.

This post has been edited by blackcompe: 11 January 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3