Code:
/**
* TODO:
* Make it print dashes for the letters.
* Make it print the letter where index is.
* Make it continue after start.
* Add letter to the continue method.
* Compare random word and selected indexes.
* Find index of random word, use that index to put the letter into that index
* then replace the '_' with the letter with that index.
* Index with Element of the word
* ...
*/
/** Import necessary packages*/
import java.util.Random;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.lang.String;
public class Hangman extends BasicGame
{
/**Call all of the variables*/
Random randy = new Random();
private int guesses;
private final int maxGuesses = 6;
private String myGeneratedRandomWord = getWords();
private String letter;
private char letterSelected;
private String[] words = {"apple", "beret", "arose", "along", "beamy", "becks", "decks", "barks",
"stark", "start", "stabs", "baggy", "asked", "asset", "asses", "audit",
"bowls", "boxes", "seats", "balls", "boats", "boxer", "brick", "bound",
"brass", "caked", "braid", "caged", "essay", "fault", "dents", "dutch",
"ethos", "dunks", "pains", "faxes", "mummy", "mixer", "mills", "might",
"moral", "teeth", "wings", "works", "walls", "tolls", "crawl", "toxin",
"bangs", "tough"};
/**Get the random word from the array*/
public String getWords (){
String randomWord = words[randy.nextInt( words.length)];
return randomWord;
}
/**Get the indexes of the letter of the random word indices don't start with 1*/
public char getSelected(){
return letterSelected;
}
/**Finds index of the letter of randomWord*/
public int getIndex(){
int index = getWords().indexOf(getSelected());
return index;
}
/**Get the first input, set strikes to zero, get the random word*/
public void start(){
guesses = 0;
getWords();
String s = JOptionPane.showInputDialog(null, "Make your guess!");
letterSelected = s.charAt(0);
if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
// it contains the letter
}
else {
// wrong guess
guesses++;
}
}
/**Continue the game on, checks if the letter is there if not then apply the letter*/
public void continueGame(){
String s = JOptionPane.showInputDialog(null, "Make another guess!");
letterSelected = s.charAt(0);
if(myGeneratedRandomWord.indexOf(letterSelected ) != -1) {
// it contains the letter
getIndex(getWords());
}
else {
// wrong guess
guesses++;
}
}
/** Runner basically*/
public void mainScreen (){
start();
while (guesses != maxGuesses){
continueGame();
checkBodyParts();
}
gameOver();
}
/**Gets the */
/**After certain strikes head is called etc.*/
public void checkBodyParts(){
if(guesses == 1){
JOptionPane.showMessageDialog(null, "You now have a head! Wrong guesses left: 5");
}
if(guesses == 2){
JOptionPane.showMessageDialog(null, "You now have a body! Wrong guesses left: 4");
}
if(guesses == 3){
JOptionPane.showMessageDialog(null, "You now have a right arm! Wrong guesses left: 3");
}
if(guesses == 4){
JOptionPane.showMessageDialog(null, "You now have a left arm! Wrong guesses left: 2");
}
if(guesses == 5){
JOptionPane.showMessageDialog(null, "You now have a right leg! Wrong guesses left: 1");
}
if(guesses == maxGuesses){
JOptionPane.showMessageDialog(null, "You lose!");
}
}
/**You lose.*/
public void gameOver(){
JOptionPane.showMessageDialog(null, "Game Over!");
System.exit(0);
}
}