Hey guys, I'm making a hangman project just for fun, and i was wondering if you could help me.
- I have 5 classes
- class 1- main (basically controlls everything except recognizing where the letters are located in the word and printing the hangman picture)
class 2- char_array (sees if the letter is in the word multiple times, and prints the letters the operator has figured out (ex: bumb-e) )
class 3 - graphics (just full of methods that print the hangman on the screen depending on how many letters you have gotten wrong)
- class 4 - numbers (just has one method to decide what hangman picture to display)
- class 5 - wordlist - a class that chooses a word from a list of many)
Where I am stuck is that the variables in the char_array class reset each time the init() or some_function() that gets called. Therefore, if i have a word with two of the same letter, the count gets messed up AND the the word with the dashes that the user sees (ex: G--gle) gets reset every time, so only the letter that you just entered will show up(ex: i enter g: G--g--, then i enter o: -oo---)
Here is the main class
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
import java.util.HashMap;
import java.io.*;
public class main{
public static void main(String[] args){
graphics graphics = new graphics();
numbers numbers = new numbers();
WordList words = new WordList();
main main = new main();
Scanner input = new Scanner(System.in);
//String word[] = {"h", "i"};
String word = new String(words.getword());
String guessed_letters = new String("");
int guesses_right = 0;
int guesses_wrong = 0;
int length = word.length();
System.out.println("Hello, this is a hangman game. Good luck!");
System.out.println(word);
while(guesses_right != length && guesses_wrong != 6){
System.out.println("Please enter a letter: ");
String x = input.nextLine();
if(isAlpha(x) && !(x.length() >1)){
x = x.toLowerCase();
if(word.contains(x) && !(guessed_letters.contains(x))){
System.out.println("------------------------------");
System.out.println("Good job, that is in the word");
char_array char_array = new char_array();
guessed_letters = guessed_letters + x;
char_array.init(word, x);
char_array.some_function();
System.out.println("DEBUG" + char_array.counter);
guesses_right = guesses_right + char_array.counter;
guesses_right++;
numbers.display_hangman(guesses_wrong);
System.out.println("Guessed_letters:" + guessed_letters);
}
else if (guessed_letters.contains(x)){
System.out.println("------------------------------");
System.out.println("I'm sorry, but you have already entered that number");
}
else if (!word.contains(x)){
//guessed_letters = guessed_letters + x;
if (guessed_letters.contains(x)){
System.out.println("------------------------------");
System.out.println("I'm sorry, but you have already entered that number.");
}
else{
System.out.println("------------------------------");
System.out.println("Sorry, that is not in the word");
guesses_wrong++;
numbers.display_hangman(guesses_wrong);
}
guessed_letters = guessed_letters + x;
}
}else{
System.out.println("I'm sorry, but it appears you have entered something that is invalid");
}
}
if( guesses_wrong == 6){
System.out.println("I'm sorry but it appears that you have lost. The word was " + word + ". Maybe try again?");
}
else{
System.out.println("Congratulations!! You have won. The word was " + word + ". Maybe try again?");
}
}
public static boolean isAlpha(String name){
return name.matches("[a-zA-Z]+");
}
}
-------------------------------------------------------------------------------
here is the char_array class
import java.lang.String;
public class char_array {
public void init(String a, String b){
word = a;
x = b;
}
main main = new main();
graphics graphics = new graphics();
public String word;
public String x;
public int counter = 0;
public void some_function() {
//variable to increase in case of double letter
char what_user_sees[] = new char[word.length()];
char charArray[]=word.toCharArray();
for(int i = 0; i <word.length(); i++){
what_user_sees[i] = '-';
}
for(int h=0;h<word.length();h++){
if (charArray[h] == x.charAt(0)){
what_user_sees[h] = x.charAt(0);
counter++;
//System.out.println(what_user_sees);
}
}
System.out.println(what_user_sees); //print the thing with the hash marks
}
}
These classes seem to be the ones that are causing the trouble. Could you guys modify the code for me so that the variables don't get reset every time?
Thanks in advance,
Brady
I would post the other classes, but they will just take up space, and i really don't think you will need them.
PS - if you guys could give me some better coding styles, that would be great. I'm just a beginner, So I'll take any advice that I can get.