3D Arrays on Hangman game - java

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

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

#16 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 - 03:23 PM

View Postblackcompe, on 11 January 2013 - 03:11 PM, said:

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.

import java.util.Scanner;//imports a Scanner Class
import java.util.Random;//imports a Randomizer

public class HangmanMain
{
  public static void main (String[] args)
  {
    System.out.println("\t\t\t\t************Welcome to Hangman*************");//Intro to the game
    Scanner input = new Scanner(System.in);//Instalizes a new Scanner called input 
    boolean done = false, reset = true;//new boolean variable done and reset that is set to false and true
    String guess, guesses = "";//new String Variable guess
    char letter;//new char variable
    int wrongGuess = 6;//new interger variable wrongGuess that stores the value 6 in it, which is the max wrongGuess the user can have
    StringBuffer dashes;//new StringBuffer variable dashes
    String words = "";

    while(reset == true)
    {
      System.out.println("Choose a category-> places / games / cars");//Asks the user what types of word they want
      words = getWords(input.next(),input.nextLine());//creates a new String and itand chooses the word from the method wordsToChoose from the answer given
      System.out.println("Choose a level: Easy, Medium, Hard");
      words = getWords(null,input.next());
      dashes = makeDashes(words);//makes dashes for each of the letter in the word
      done = false;
      while(done == false)//when done is false, it does this
      {
        startStatements(dashes, guesses);
        guess = input.next();//takes the answer
        // process the guess
        if (guess.length() > 1)//if the length of the answer was more than 1 letter it will do this:
        {
          wordGuess(guess, words);
          done=true;//now done is true so the loop will stop
        }
        else // if the user only puts a letter it will process a single letter guess
        {
          letter = guess.charAt(0);
          guesses += letter;
          if (words.indexOf(letter) < 0)  //if the letter given is not in the word it will do this:
          {
            wrongGuess--;//this takes out 1 wrongGuess from six everytime the user gets the letter wrong
            wrongGuessCounter(wrongGuess, words);
            if (wrongGuess == 0)
              done = true;//done becomes true and the loop stops
          }
          else //if the letter is in the words
          {
            matchLetter(words, dashes, letter);// put it in dashes where it belongs 
            if(words.equalsIgnoreCase(dashes.toString()))//if the user fills in the dashes using only letters it will do this
            {
              System.out.println("\nYOU WIN!!!!");//print out you win
              done = true;
            }
          }
        }
        while(done)
        {
          System.out.println("\nDo you want to play again?");
          String answer = input.next();
          
          if(answer.equalsIgnoreCase("no"))
          {
            System.out.println("GOOD-BYE, THANKS FOR PLAYING"); reset = false; done = true; break;
          }
          else
            reset = true; done = true; guess=""; guesses=""; wrongGuess=6; break;// reset the variaables
        }
      }
    } 
  }
  public static void wrongGuessCounter(int wrongGuess, String words)
  {
    if((wrongGuess <= 5)&&(wrongGuess >= 1))
    {
      System.out.println("Guesses left: " + wrongGuess);
    }//prints out this when the wrongGuess are between 5-1
    else if(wrongGuess == 0)
    {//when the wrongGuess becomes 0
      System.out.println("Not Bad! Unfortunately you're all out of guesses. My word was " + words);//prints the real word
    }
  }
  public static void wordGuess(String guess, String words)
  {
    if (guess.equalsIgnoreCase(words))//if the guess was the word
      System.out.println("You got it, Good Job!!");//it prints out this on the screen and the games ends
    else //if it dosen't match the word
      System.out.println("Thats not the word, the word was: " + words);//it prints out this on the screen and the games ends
  }
  public static void startStatements(StringBuffer dashes, String guesses)
  {
    System.out.println("Here is your word: " + dashes);//prints out the word in dashes 
    System.out.println("Guesses so far: " + guesses);//the guessess the user made so far
    System.out.print("enter a guess (letter or word): ");//this asks the user to enter a guess
  }
  public static void matchLetter(String words, StringBuffer dashes, char letter)
  {
    for (int index = 0; index < words.length(); index++)
      if (words.charAt(index) == letter)
      dashes.setCharAt(index, letter);
    System.out.print("Good guess: ");
  }
  public static StringBuffer makeDashes(String s)
  {
    StringBuffer dashes = new StringBuffer(s.length());
    for (int count=0; count < s.length(); count++)
      dashes.append('-');
    return dashes;
  }
  
  
  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();
    
  }
}  




this is the full code
Was This Post Helpful? 0
  • +
  • -

#17 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 03:36 PM

Ok, so after running your program, a NullPointerException is thrown. This is what I initially asked you to post. It's very important that you get familiar with exceptions because they are the first thing people will ask you about.

Exception in thread "main" java.lang.NullPointerException
	at Hangman.getWords(Hangman.java:232)
	at Hangman.main(Hangman.java:44)



That exception is telling you the error is here:

if (cat.equalsIgnoreCase("places")) {



And the source of that error is because cat is null, which is what I told you in the first place. If you go further down the stack trace, it says line 44 is where you called getWords (with the null argument), so let's examine it.

words = getWords(null, input.next());



Nothing to be said. Don't send null into a method that doesn't expect it. How about sending the category?

 words = getWords(input.next(), input.nextLine());// creates a new
                                                                // String and
                                                                // itand chooses
                                                                // the word from
                                                                // the method
                                                                // wordsToChoose
                                                                // from the
                                                                // answer given
               System.out.println("Choose a level: Easy, Medium, Hard");
               words = getWords(null, input.next());



This code is incorrect. Study the example I just gave you and fix it. Don't use Scanner.next either. When you do you'll see that the program is close to working.

Edit: Change every use of Scanner.next to Scanner.nextLine, and trim the string with String.trim to remove any whitespace the user may accidentally enter. E.g.

String input = scnr.nextLine().trim();


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

Was This Post Helpful? 1
  • +
  • -

#18 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 - 03:45 PM

i don't send the category right away because i want to ask the user first for the category than when they answer i want to ask for the difficulty
if i do nextLine, it works but it dosen't give me an option to write an println statement to ask for the difficulty

BTW Thank you for taking your time and helping me out

it does this..
Choose a category-> places / games / cars
 [Input Box]
 [Input Box]
Choose a level: Easy, Medium, Hard
Here is your word: ---



it creates 2 input boxs i want it to do this but i don't know how
Choose a category-> places / games / cars
 [ Input Box]
Choose a level: Easy, Medium, Hard
 [Input Box]
Here is your word: ---




ok so i trimmed it......

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

Was This Post Helpful? 0
  • +
  • -

#19 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 03:49 PM

Quote

i don't send the category right away because i want to ask the user first for the category than when they answer i want to ask for the difficulty
if i do nextLine, it works but it dosen't give me an option to write an println statement to ask for the difficulty


I think you're understanding of methods is fundamentally flawed, but for the sake of getting your program working just trust in what I'm telling you and use the example I gave you. And use nextLine where applicable. I'm looking at your program ... working. Your choice ...

Quote

BTW Thank you for taking your time and helping me out

it does this..

Choose a category-> places / games / cars

 [Input Box]

 [Input Box]

Choose a level: Easy, Medium, Hard

Here is your word: ---



it creates 2 input boxs i want it to do this but i don't know how

Choose a category-> places / games / cars

 [ Input Box]

Choose a level: Easy, Medium, Hard

 [Input Box]

Here is your word: ---


If you'd just use what I gave you, you'd have what you want. Post your updated code.

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

Was This Post Helpful? 1
  • +
  • -

#20 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 - 04:01 PM

this one
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));
     }




?

import java.util.Scanner;//imports a Scanner Class
import java.util.Random;//imports a Randomizer

public class HangmanMain
{
  public static void main (String[] args)
  {
    System.out.println("\t\t\t\t************Welcome to Hangman*************");//Intro to the game
    Scanner input = new Scanner(System.in);//Instalizes a new Scanner called input 
    boolean done = false, reset = true;//new boolean variable done and reset that is set to false and true
    String guess, guesses = "";//new String Variable guess
    char letter;//new char variable
    int wrongGuess = 6;//new interger variable wrongGuess that stores the value 6 in it, which is the max wrongGuess the user can have
    StringBuffer dashes;//new StringBuffer variable dashes
    String words = "";

    while(reset == true)
    {
      System.out.println("Choose a category-> places / games / cars");//Asks the user what types of word they want
      String cat = input.nextLine().trim();//creates a new String and itand chooses the word from the method wordsToChoose from the answer given
      System.out.println("Choose a level: Easy, Medium, Hard");
      String diff = input.nextLine().trim();
      dashes = makeDashes(words);//makes dashes for each of the letter in the word
      done = false;
      while(done == false)//when done is false, it does this
      {
        startStatements(dashes, guesses);
        guess = input.next();//takes the answer
        // process the guess
        if (guess.length() > 1)//if the length of the answer was more than 1 letter it will do this:
        {
          wordGuess(guess, words);
          done=true;//now done is true so the loop will stop
        }
        else // if the user only puts a letter it will process a single letter guess
        {
          letter = guess.charAt(0);
          guesses += letter;
          if (words.indexOf(letter) < 0)  //if the letter given is not in the word it will do this:
          {
            wrongGuess--;//this takes out 1 wrongGuess from six everytime the user gets the letter wrong
            wrongGuessCounter(wrongGuess, words);
            if (wrongGuess == 0)
              done = true;//done becomes true and the loop stops
          }
          else //if the letter is in the words
          {
            matchLetter(words, dashes, letter);// put it in dashes where it belongs 
            if(words.equalsIgnoreCase(dashes.toString()))//if the user fills in the dashes using only letters it will do this
            {
              System.out.println("\nYOU WIN!!!!");//print out you win
              done = true;
            }
          }
        }
        while(done)
        {
          System.out.println("\nDo you want to play again?");
          String answer = input.next();
          
          if(answer.equalsIgnoreCase("no"))
          {
            System.out.println("GOOD-BYE, THANKS FOR PLAYING"); reset = false; done = true; break;
          }
          else
            reset = true; done = true; guess=""; guesses=""; wrongGuess=6; break;// reset the variaables
        }
      }
    } 
  }
  public static void wrongGuessCounter(int wrongGuess, String words)
  {
    if((wrongGuess <= 5)&&(wrongGuess >= 1))
    {
      System.out.println("Guesses left: " + wrongGuess);
    }//prints out this when the wrongGuess are between 5-1
    else if(wrongGuess == 0)
    {//when the wrongGuess becomes 0
      System.out.println("Not Bad! Unfortunately you're all out of guesses. My word was " + words);//prints the real word
    }
  }
  public static void wordGuess(String guess, String words)
  {
    if (guess.equalsIgnoreCase(words))//if the guess was the word
      System.out.println("You got it, Good Job!!");//it prints out this on the screen and the games ends
    else //if it dosen't match the word
      System.out.println("Thats not the word, the word was: " + words);//it prints out this on the screen and the games ends
  }
  public static void startStatements(StringBuffer dashes, String guesses)
  {
    System.out.println("Here is your word: " + dashes);//prints out the word in dashes 
    System.out.println("Guesses so far: " + guesses);//the guessess the user made so far
    System.out.print("enter a guess (letter or word): ");//this asks the user to enter a guess
  }
  public static void matchLetter(String words, StringBuffer dashes, char letter)
  {
    for (int index = 0; index < words.length(); index++)
      if (words.charAt(index) == letter)
      dashes.setCharAt(index, letter);
    System.out.print("Good guess: ");
  }
  public static StringBuffer makeDashes(String s)
  {
    StringBuffer dashes = new StringBuffer(s.length());
    for (int count=0; count < s.length(); count++)
      dashes.append('-');
    return dashes;
  }
  
  
  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();
    
  }
}  




it doesn't work, i don't know how it work for u
Was This Post Helpful? 0
  • +
  • -

#21 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 04:03 PM

Quote

it doesn't work, i don't know how it work for u


Well, it certainly wouldn't if you neglected to call getWords?
Was This Post Helpful? 1
  • +
  • -

#22 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 - 04:16 PM

View Postblackcompe, on 11 January 2013 - 04:03 PM, said:

Quote

it doesn't work, i don't know how it work for u


Well, it certainly wouldn't if you neglected to call getWords?

can i call it like this?
words = getWords(cat,diff);

Was This Post Helpful? 0
  • +
  • -

#23 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 04:18 PM

Quote

can i call it like this?


Try it. And if you get an error I guess not.
Was This Post Helpful? 1
  • +
  • -

#24 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 - 04:19 PM

View Postblackcompe, on 11 January 2013 - 04:18 PM, said:

Quote

can i call it like this?


Try it. And if you get an error I guess not.

ok it works now THANK YOU SO MUCH!!
YOU ARE THE BEST!!
Was This Post Helpful? 0
  • +
  • -

#25 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 04:20 PM

Quote

ok it works now THANK YOU SO MUCH!!
YOU ARE THE BEST!!


Ha! Glad you got it! Feels good doesn't it?
Was This Post Helpful? 1
  • +
  • -

#26 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 - 04:25 PM

View Postblackcompe, on 11 January 2013 - 04:20 PM, said:

Quote

ok it works now THANK YOU SO MUCH!!
YOU ARE THE BEST!!


Ha! Glad you got it! Feels good doesn't it?

Yes it does

fuck yeaaaaa
gimmi code
Was This Post Helpful? -1
  • +
  • -

#27 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 - 05:26 PM

FUCK!!!!!
GIMMI FUCKEN CODE
FUCK FUCK FUCK
Was This Post Helpful? -1
  • +
  • -

#28 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 05:33 PM

View PostIsukAtJava, on 11 January 2013 - 03:32 PM, said:

when i try that it gives me an error saying there is no catVal


It is only a partial example. The hope is that, if it makes sense, you'll write the rest on your own.


View PostIsukAtJava, on 11 January 2013 - 03:32 PM, said:

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


This doesn't really follow from your code, which takes two parameters. You could write methods to ask the user questions and pass the results to that function.
Was This Post Helpful? 1
  • +
  • -

#29 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,688
  • Joined: 27-December 08

Re: 3D Arrays on Hangman game - java

Posted 11 January 2013 - 06:41 PM

IsukAtJava- Please keep the language professional.
Was This Post Helpful? 0
  • +
  • -

#30 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 - 06:50 PM

View Postmacosxnerd101, on 11 January 2013 - 06:41 PM, said:

IsukAtJava- Please keep the language professional.


no i won't, what the fuck r u going to do?
Was This Post Helpful? -3
  • +
  • -

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