0

So I am currently creating my own version of hangman for java (I'm new to Java) and have two classes, the main and a letter class. I am wanting to create individual objects for the purpose of having multiple letters to make up the word that is guessed. The issue I am having is whenever I am trying to set a letter object's letter to something, it sets all of the letter objects to the input, which makes the word all the same letter. Any help would be appreciated!!

Code:

import java.util.Scanner;
import java.util.List;
import java.text.*;

public class Main
{
  public static String primaryWord;
  public static String difficulty;
  public static boolean finishedGame = false;
  public static int tries = 0;
  public static int stars = 5;
  public static String localWord;
  public static String guess;
  public static Letter Letterone = new Letter ();
  public static Letter Lettertwo = new Letter ();
  public static Letter Letterthree = new Letter ();
  public static Letter Letterfour = new Letter ();
  public static Letter Letterfive = new Letter ();
  public static Letter Lettersix = new Letter ();
  public static Letter Letterseven = new Letter ();
  public static Letter Lettereight = new Letter ();
  public static Letter Letternine = new Letter ();
  public static Letter Letterten = new Letter ();
  public static boolean setWords = false;
  

  public static void main (String[]args)
  {

    String[]hardWordArray =
    {
    "lantern", "pickle", "turtle", "children", "pencil", "picture",
    "ground", "feeder", "blanket", "cardboard", "camera", "pillow",
    "houses", "tomato",};
    String[]easyWordArray =
    {
    "dog", "fig", "ant", "rat", "rock", "out", "shot", "bake", "tree",
    "cake", "Earth", "fuzzy", "lambs", "words", "find", "metal",
    "flower", "shirt", "vase", "tape",};


    System.out.println
      ("Welcome to Hangman! Would you like to start an easy or hard game?");
      
    difficultyTest ();
    
    if (difficulty.equals ("hard"))
      {
    primaryWord =
      hardWordArray[(int) (Math.random () * hardWordArray.length)];
      }
    if (difficulty.equals ("easy"))
      {
    primaryWord =
      easyWordArray[(int) (Math.random () * easyWordArray.length)];
      }
    
    System.out.println(primaryWord);
    
    for(int u = primaryWord.length(); u > 0; u--){
        
        if(!(localWord == null)){
        localWord = localWord + "_";
        }
        else{
            localWord = "_";
        }
    }
    


    if (primaryWord.length () >= 10)
      {
    
    Letterten.setLetter (primaryWord.substring (9, 10));
      }
    if (primaryWord.length () >= 9)
      {
    
    Letternine.setLetter (primaryWord.substring (8, 9));

      }
    if (primaryWord.length () >= 8)
      {
    
    Lettereight.setLetter (primaryWord.substring (7, 8));

      }
    if (primaryWord.length () >= 7)
      {
    
    Letterseven.setLetter (primaryWord.substring (6, 7));

      }
    if (primaryWord.length () >= 6)
      {
    
    Lettersix.setLetter (primaryWord.substring (5, 6));

      }
    if (primaryWord.length () >= 5)
      {
    
    Letterfive.setLetter (primaryWord.substring (4, 5));

      }
    if (primaryWord.length () >= 4)
      {
    
    Letterfour.setLetter (primaryWord.substring (3, 4));

      }
    
    Letterthree.setLetter (primaryWord.substring (2, 3));
      
    Lettertwo.setLetter (primaryWord.substring (1, 2));

    Letterone.setLetter (primaryWord.substring (0, 1));
      
      System.out.println("one is " + Letterone.getLetter());
      System.out.println("two is " + Lettertwo.getLetter());
      System.out.println("three is " + Letterthree.getLetter());
      System.out.println("four is " + Letterfour.getLetter());
      System.out.println("five is " + Letterfive.getLetter());
      System.out.println("six is " + Lettersix.getLetter());
      System.out.println("seven is " + Letterseven.getLetter());
      System.out.println("eight is " + Lettereight.getLetter());
      System.out.println("nine is " + Letternine.getLetter());
      System.out.println("ten is " + Letterten.getLetter());
      
      
      
      
      while (!finishedGame){
          update();
      }
      
      
      
      
  }
  public static void update(){
      Scanner scan = new Scanner (System.in);
      line();
      System.out.println("Tries: " + tries);
      System.out.print("Stars: ");
      
      for (int i = stars; i >= 1; i--){
          System.out.print("*");
      }
      System.out.println("");
      
      for (int m = 0; m < localWord.length(); m++){
          System.out.print(localWord.substring(m, m + 1));
          System.out.print(" ");
      }
      System.out.println("");
      System.out.println("");
      
      
      
      
      
      System.out.println("Please type in your guess.");
      guess = scan.nextLine();
      tries++;
      System.out.println(search(guess));
      System.out.println(Letterone.getLetter());
      if(search(guess) == -1){
          stars--;
      }
      else{
          StringBuffer originalString = new StringBuffer(localWord);
          originalString.replace(search(guess) - 1, search(guess), guess);
          localWord = originalString.toString();
      }
      
  }
  
  
  public static void line ()
  {
    System.out.println ("--------------------------------");
  }



  public static void difficultyTest ()
  {
    Scanner myObj = new Scanner (System.in);
    boolean DifficultyAsked = true;

    while (DifficultyAsked == true)
      {

    difficulty = myObj.nextLine ();

    if (difficulty.equals ("hard"))
      {
        DifficultyAsked = false;
        System.out.println ("You have  selected a hard game.");
      }
    if (difficulty.equals ("easy"))
      {
        DifficultyAsked = false;
        System.out.println ("You have selected an easy game.");
      }
    else if (DifficultyAsked == true)
      {
        System.out.println ("Please type a valid difficulty.");
      }
      }

  }
  public static int search(String searchLetter){
    
    if (Letterone.searchForLetter(searchLetter)){
        return 1;
    }
    if (Lettertwo.searchForLetter(searchLetter)){
        return 2;
    }
    if (Letterthree.searchForLetter(searchLetter)){
        return 3;
    }
    if (Letterfour.searchForLetter(searchLetter)){
        return 4;
    }
    if (Letterfive.searchForLetter(searchLetter)){
        return 5;
    }
    if (Lettersix.searchForLetter(searchLetter)){
        return 6;
    }
    if (Letterseven.searchForLetter(searchLetter)){
        return 7;
    }
    if (Lettereight.searchForLetter(searchLetter)){
        return 8;
    }
    if (Letternine.searchForLetter(searchLetter)){
        return 9;
    }
    if (Letterten.searchForLetter(searchLetter)){
        return 10;
    }
    return -1;
    
  }
}

and here's the Letter class:

public class Letter{
    
    private static String letter = new String("");
    
    public static void setLetter(String str){
        
        letter = str;
    }
    public static String getLetter(){
        return letter;
    }
    public static boolean searchForLetter(String searchLetter){
        return searchLetter.equals(letter);
        
    }
}

Thanks in advance if anyone wants to help!~ spaghetti code go brrrrr

PS no, I can't just use chars, my teacher wants me to use classes instead.

Nat
  • 1
  • 1

1 Answers1

0

Just a thought, try this idea and see what you think.

In your Main class, create a method named startApp() and take everything you currently have in the main() method and cut it, then paste it into the new startApp() method. for example:

private void startApp(String[] args) {
    System.out.println("Welcome to Hangman! Would you like to start an easy or hard game?");
  
    difficultyTest ();

    if (difficulty.equals ("hard")) {
        primaryWord = hardWordArray[(int) (Math.random () * hardWordArray.length)];
    }
    else {
        primaryWord = easyWordArray[(int) (Math.random () * easyWordArray.length)];
    }

    System.out.println(primaryWord);

    for(int u = primaryWord.length(); u > 0; u--) {
        if(!(localWord == null)) {
            localWord = localWord + "_";
        }
        else {
            localWord = "_";
        }
    }

    int pwl = primaryWord.length();
    if (pwl >= 10) {
        Letterten.setLetter(primaryWord.substring (9, 10));
    }
    else if (pwl >= 9) {
        Letternine.setLetter(primaryWord.substring (8, 9));
    }
    else if (pwl >= 8) {
        Lettereight.setLetter(primaryWord.substring (7, 8));
    }
    else if (pwl >= 7) {
        Letterseven.setLetter (primaryWord.substring (6, 7));
    }
    else if (pwl >= 6) {
        Lettersix.setLetter (primaryWord.substring (5, 6));
    }
    else if (pwl >= 5) {
        Letterfive.setLetter (primaryWord.substring (4, 5));
    }
    else if (pwl >= 4) {
        Letterfour.setLetter (primaryWord.substring (3, 4));
    }

    Letterthree.setLetter (primaryWord.substring (2, 3));
    Lettertwo.setLetter (primaryWord.substring (1, 2));
    Letterone.setLetter (primaryWord.substring (0, 1));

    System.out.println("one is " + Letterone.getLetter());
    System.out.println("two is " + Lettertwo.getLetter());
    System.out.println("three is " + Letterthree.getLetter());
    System.out.println("four is " + Letterfour.getLetter());
    System.out.println("five is " + Letterfive.getLetter());
    System.out.println("six is " + Lettersix.getLetter());
    System.out.println("seven is " + Letterseven.getLetter());
    System.out.println("eight is " + Lettereight.getLetter());
    System.out.println("nine is " + Letternine.getLetter());
    System.out.println("ten is " + Letterten.getLetter());
  
    while (!finishedGame){
        update();
    }
}    

Now, in the main() method, enter this one line of code (comment optional):

public static void main (String[]args) {
    // App started this way to avoid the need for statics.
    new Main().startApp(args);
}

Now...get rid of the static modifier from everything including in the Letter class.

You may want to make the hardWordArray[] and hardWordArray[] String arrays as public static class member variables so that they can be accessed from any class but it's not necessary in this case, for example:

public Main {

    public static String[] hardWordArray =  {
                       "lantern", "pickle", "turtle", "children", "pencil", "picture",
                       "ground", "feeder", "blanket", "cardboard", "camera", "pillow",
                       "houses", "tomato" };

    public static String[] easyWordArray = {
                       "dog", "fig", "ant", "rat", "rock", "out", "shot", "bake", 
                       "tree", "cake", "Earth", "fuzzy", "lambs", "words", "find", 
                       "metal", "flower", "shirt", "vase", "tape" };    

    public String primaryWord;
    public String difficulty;
    public boolean finishedGame = false;
    public int tries = 0;
    public int stars = 5;
    public String localWord;
    public String guess;
    public Letter Letterone = new Letter ();
    public Letter Lettertwo = new Letter ();
    public Letter Letterthree = new Letter ();
    public Letter Letterfour = new Letter ();
    public Letter Letterfive = new Letter ();
    public Letter Lettersix = new Letter ();
    public Letter Letterseven = new Letter ();
    public Letter Lettereight = new Letter ();
    public Letter Letternine = new Letter ();
    public Letter Letterten = new Letter ();
    public boolean setWords = false;

    // .... The rest of your class code here ....
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • Thank you so much for your help! However, when I try to do what you suggest, I get a whole lot of errors that I have no Idea what they mean lol This is what I get: Main.java:5: error: class, interface, or enum expected public static void main (String[]args) { ^ Main.java:8: error: class, interface, or enum expected } ^ Letter.java:5: error: invalid method declaration; return type required public setLetter(String str){ ^ Just imagine over a few hundred more of these errors lol any ideas? – Nat Apr 26 '22 at 14:12
  • @Nat, Do Not remove the `static` modifier from the `main()` method declaration. It needs to be there but that should be the **only** method that is static in this particular use case. – DevilsHnd - 退職した Apr 26 '22 at 14:40
  • I have just did all this with your code and it's fine. Well there are some other issues but no errors and the application runs fine. Be sure to read all the instructions within the answer post I provided. – DevilsHnd - 退職した Apr 26 '22 at 14:47
  • I did what you said, but I still get 100+ errors... my fault lol. I don't know how to fix it though. Here's what I have right now: Main class: https://pastebin.com/dXPDe19k Letter class: https://pastebin.com/Qrpf6uBn StartApp class: https://pastebin.com/90FRnReY Any help would mean the world to me!! – Nat Apr 26 '22 at 15:29
  • Like I said @Nat, read the instructions carefully. In the first line of instruction (almost the very first line of the answer post), what does is say? `startApp()` is a ***method*** within the **Main** class. ;) [You can see an example here](https://controlc.com/5d5c4cf1). – DevilsHnd - 退職した Apr 26 '22 at 19:57