Post: Hangman Java
11-30-2016, 08:15 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I have a project that I am currently doing for my assessment in college. I have the basic program created, I just need to add three levels.

Level 1 - Easy
Only 6 character words

Level 2 - Moderate
Only 8 character words

Level 3 - Hard
Only 10 character words.

I will upload the source code I have so far, and hopefully some of you can end my torture of trying to figure this out, if I have to use an array or switch statement.*

SOURCE CODE /////////////////////////////////////////////////////////

public static void gettheword() throws FileNotFoundException{

Random wrand = new Random();
int wnum = wrand.nextInt(5)+1;
int go =1, i=0;
boolean gameover = false;
File wordFile=new File("HangmanWords.txt");
FileReader wfr = new FileReader(wordFile);
System.out.println("Position of Random Word:"+wnum);
int count =0;
Scanner wordinput = new Scanner(wordFile);
Scanner read = new Scanner(System.in);
System.out.println("Please enter a letter for the game to start");
char letter = read.next().charAt(0);
String displayedword = "-----";




String word1,word2,word3,word4,word5, hangmanword="";
while ((go<=10) && (!gameover)) // more values in file?
{

word1 = wordinput.next();
i++;
if(i==wnum)
{

hangmanword=word1;
}
word2 = wordinput.next();
i++;
if(i==wnum)
{

hangmanword=word2;
}
word3 = wordinput.next();
i++;
if(i==wnum)
{

hangmanword=word2;
}
if(i==wnum)
{

hangmanword=word3;
}
word4 = wordinput.next();
i++;
if(i==wnum)
{

hangmanword=word4;
}
word5 = wordinput.next();
i++;
if(i==wnum)
{

hangmanword=word5;
}


while((count <10) && (!gameover))
{
for (i=0;i<hangmanword.length();i++)
{
if (hangmanword.charAt(i) == letter)
{
System.out.print(letter + "");
count++;
displayedword = displayedword.substring(0,i)+letter+displayedword.substring(i+1,5);

}
else
{
System.out.print("-" + "");
}


}
System.out.println("");
go++;
if(hangmanword.equals(displayedword))
{

gameover = true;
System.out.println("The word is : " +hangmanword);

}
if(go >=11)
{
System.out.println("Gameover, the word is "+hangmanword);
gameover = true;

}

else if (!gameover)
{

System.out.println(displayedword);
System.out.println("Guess a letter");
letter = read.next().charAt(0);

}

}
}
}

}

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo