I need help completing this program the right way please help 1. Your program mu
ID: 3680669 • Letter: I
Question
I need help completing this program the right way please help
1. Your program must have an array of words (Strings) from which one of the words is randomly chosen to play a round of the game. Please don’t include more that 5 words so that I can easily test your program:
String [ ] words = { “javascript”, “declaration”, “object”, “class”, “failing” };
2. Your program must contain at least one character array such as:
char [ ] alphabet;
3. Your program must contain at least 3 method definitions not including the main method definition.
4. One of the method definitions must be a method called: explode
The explode method is sent a string such as: “javascript” and returns an array of characters containing the letters that appear in the words: ‘j’ ‘a’ ‘v’ ‘a’ ‘s’ ‘c’ ‘r’ ‘I’ ‘p’ ‘t’
this is what i have so far
but i also cant get the little man to form when you start guessing and i dont know if im missing something.
private static List<Character> guessedCharList = new ArrayList<Character>();
private static List<Character> validCharList = new ArrayList<Character>();
private static String rndWord = null;
private static int numberOfWrongGuess = 0;
public static void main(String[] args)
{
String[] words = { "javascript", "declaration", "object", "program",
"failing" };
// generate random word from list
Random rnd = new Random();
rndWord = words[rnd.nextInt(words.length)];
System.out.println(rndWord);
// gets length of generated word
char[] displayArray = new char[rndWord.length()];
// displays "_" for number of chars in word
for (int i = 0; i < rndWord.length(); i++) {
displayArray[i] = '_';
}
char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', +'x', 'y', 'z' };
String hangman = null;
boolean finished = false;
do {
hangman = "Let's Play Hangman!!" + " " + "-------------" + " "
+ "|" + " " + "|" + " " + "|" + " " + "|" + " " + "|"
+ " " + "|" + " " + "|" + " " + "|" + " " + " "
+ Arrays.toString(displayArray) + " " + " ";
JOptionPane.showMessageDialog(null,
hangman + " " + Arrays.toString(alphabet) + " ");
guess();
for (int i = 0; i < rndWord.length(); i++) {
if (validCharList.contains(rndWord.charAt(i)))
displayArray[i] = rndWord.charAt(i);
else
displayArray[i] = '_';
}
for (char c : displayArray) {
if (c == '_') {
finished = false;
break;
} else
finished = true;
}
if (numberOfWrongGuess >= 5) { // 5 wrong guess: "he was hanged."
JOptionPane.showMessageDialog(null, "Man was hanged!");
finished = true;
}
} while (!finished);
hangman = "Let's Play Hangman!!" + " " + "-------------" + " " + "|"
+ " " + "|" + " " + "|" + " " + "|" + " " + "|" + " "
+ "|" + " " + "|" + " " + "|" + " " + " "
+ Arrays.toString(displayArray) + " " + " ";
JOptionPane.showMessageDialog(null,
hangman + " " + Arrays.toString(alphabet) + " ");
}
// get letter
public static String guess() {
String guessStr = "";
while (guessStr == null || guessStr.trim().isEmpty()
|| guessStr.length() > 1) {
guessStr = JOptionPane.showInputDialog("Enter a letter to guess: ");
}
char c = guessStr.toLowerCase().toCharArray()[0];
if (guessedCharList.contains(new Character(c))) {
JOptionPane
.showMessageDialog(null, "You already give that answer!");
guess();
}
guessedCharList.add(new Character(c));
if (rndWord.contains(c + "")) {
validCharList.add(new Character(c));
return c + "";
} else {
numberOfWrongGuess++;
return "";
}
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JOptionPane;
public class HangMan {
private static List<Character> guessedCharList = new ArrayList<Character>();
private static List<Character> validCharList = new ArrayList<Character>();
private static String rndWord = null;
private static int numberOfWrongGuess = 0;
public static void main(String[] args) {
String[] words = { "javascript", "declaration", "object", "program", "failing" };
// generate random word from list
rndWord = random_number(words);
System.out.println(rndWord);
// gets length of generated word
char[] displayArray = new char[rndWord.length()];
// displays "_" for number of chars in word
for (int i = 0; i < rndWord.length(); i++) {
displayArray[i] = '_';
}
char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', +'x', 'y', 'z' };
String hangman = null;
boolean finished = false;
do {
hangman = "Let's Play Hangman!!" + " " + "-------------" + " "
+ "|" + " " + "|" + " " + "|" + " " + "|" + " " + "|"
+ " " + "|" + " " + "|" + " " + "|" + " " + " "
+ Arrays.toString(displayArray) + " " + " ";
JOptionPane.showMessageDialog(null,hangman + " " + Arrays.toString(alphabet) + " ");
guess();
char[] explode_result = new char[rndWord.length()];
explode_result = explode(rndWord);
for (int i = 0; i < rndWord.length(); i++) {
if (validCharList.contains(rndWord.charAt(i)))
displayArray[i] = rndWord.charAt(i);
else
displayArray[i] = '_';
}
for (char c : displayArray) {
if (c == '_') {
finished = false;
break;
} else
finished = true;
}
if (numberOfWrongGuess >= 5) { // 5 wrong guess: "he was hanged."
JOptionPane.showMessageDialog(null, "Man was hanged!");
finished = true;
}
} while (!finished);
hangman = "Let's Play Hangman!!" + " " + "-------------" + " " + "|"
+ " " + "|" + " " + "|" + " " + "|" + " " + "|" + " "
+ "|" + " " + "|" + " " + "|" + " " + " "
+ Arrays.toString(displayArray) + " " + " ";
JOptionPane.showMessageDialog(null,
hangman + " " + Arrays.toString(alphabet) + " ");
}
public static String random_number(String[] words)
{
Random rnd = new Random();
return words[rnd.nextInt(words.length)];
}
public static char[] explode (String s){
char[] result = new char[s.length()];
for (int i = 0; i < s.length() ;i++ ) {
result[i] = s.charAt(i);
}
return result;
}
public static String guess() {
String guessStr = "";
while (guessStr == null || guessStr.trim().isEmpty()
|| guessStr.length() > 1) {
guessStr = JOptionPane.showInputDialog("Enter a letter to guess: ");
}
char c = guessStr.toLowerCase().toCharArray()[0];
if (guessedCharList.contains(new Character(c))) {
JOptionPane.showMessageDialog(null, "You already give that answer!");
guess();
}
guessedCharList.add(new Character(c));
if (rndWord.contains(c + "")) {
validCharList.add(new Character(c));
return c + "";
} else {
numberOfWrongGuess++;
return "";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.