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.