0% found this document useful (0 votes)
380 views3 pages

Hangman Game

This document contains the code for a Hangman game program written in Java. It includes: 1) A main method that initializes variables like a random access file and scanner, welcomes the player, and calls methods to play the game and ask if they want to replay. 2) A PlayGame method that handles gameplay - getting a random word, displaying guesses/body parts, checking letters, updating the hidden word, and ending if won/lost. 3) A CarryOn method to ask the player if they want to replay, and set replay to true/false based on their input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
380 views3 pages

Hangman Game

This document contains the code for a Hangman game program written in Java. It includes: 1) A main method that initializes variables like a random access file and scanner, welcomes the player, and calls methods to play the game and ask if they want to replay. 2) A PlayGame method that handles gameplay - getting a random word, displaying guesses/body parts, checking letters, updating the hidden word, and ending if won/lost. 3) A CarryOn method to ask the player if they want to replay, and set replay to true/false based on their input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hangmangame;
import java.io.RandomAccessFile;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
* @author 13011967
*/
public class HangManGame {
static boolean replay = true; //creates a boolean named replay and sets it a
s true
private static Scanner kb = new Scanner(System.in); //initializes the scanne
r class
public static void main(String[] args) {
RandomAccessFile raf; // declares a random access file as raf
try{
System.out.println("Welcome to the game of HangMan"); //friendly message
welcominging to game
raf= new RandomAccessFile("Hangman","rw"); //gets the text file "Hangman
"
String x; //declared varible
while(replay==true){ //sets a while loop which will not end unless repl
ay is set to false
x=raf.readLine(); //gets a word from the text file and stores in in the
varible x
PlayGame(x); //Initializes method and send the varible accross
CarryOn();
}
}
catch(Exception e){ //catches and exceptions/errors
JOptionPane.showMessageDialog(null,e); //displays any errors/exceptions
}
}
public static void PlayGame(String word){ //method to play the game
word =word.toLowerCase(); //converts all characters in the string to lower
case
String hiddenWord = ""; //declared varible
for(int i=0;i<word.length();i++){ //loop the size of the word
hiddenWord+="-"; //add - to hidden word
}
//declared varibles
String tempHidden = "";
String guesses = "";
int bodyparts = 6;
String letter;
boolean finished = false; //sets the boolean finished to equal false
while(finished == false ){ // contionously loop until finished is equal t
o true
System.out.println("Number of body parts left:"+bodyparts); // display
s body parts
System.out.println("Guesses:"+guesses); //displayes guessed letters

System.out.println(hiddenWord); //displays the hidden word as ----- an


d updates if letter is guessed correctly
System.out.println("Enter a Letter: "); //enter a letter message
letter = kb.nextLine();//stores the letter in the varible "letter"
for(int x =0;x<word.length();x++){ //loops the length of the word
if(letter.charAt(0)==word.charAt(x)) {
//if the letter which is
being made into a char is equal to a letter in
// word then is true
tempHidden +=word.charAt(x); //if true stores the char in tempHidden
}
else{
tempHidden +=hiddenWord.charAt(x);
//else if false
tempHidden will add a }
}
String splitWord[] = word.split(""); // declares a varible to be used
to store the split word
for(int z=0;z<splitWord.length;z++){ //loops the length of the split
word
if(letter.equalsIgnoreCase(splitWord[z])) {//if the letter is equal
to the split word then true
break; // if true then break out of loop
}
else if(z==splitWord.length-1){ //else z = the length of the split w
ord - 1
bodyparts--; // minus a body part
break;//end loop
}
}
hiddenWord = tempHidden; //store what in temp hidden onto hiddenword
tempHidden = "";//set temphidden to empty
if(hiddenWord.equalsIgnoreCase(word) ){ //if the hiddenword equals the
word
finished = true; //then finshed is equal to true and ends method
System.out.println("Well done you guessed the word!:"+word); //sho
w the word finished
}
if(bodyparts ==0){ //if the bodyparts are equal to zero
finished=true;//finsiehd is equal to ture and ends method
System.out.println("Boo you lose!:"+word); //shows word with a boo
message
}
String split[] = guesses.split(" "); //splits the guesses and stores i
t as split
for(int i =0;i<split.length;i++) //loops the length of the split
{
if(letter.equalsIgnoreCase(split[i])) //if the letter is equal to the
split then true
{
System.out.println("letter has allready been entered"); //if true t
hen display this message
}
else if(i==split.length-1){ //else
guesses += letter+" "; //add the letter to guesses
}
}

}
}
public static void CarryOn(){ // method used to ask user if he wants to play
again
System.out.println("Do you want to play again?"+"\n"+"Y/N"); //message t
o where the user must enter Y or N to play again
String choice = kb.nextLine(); //stores choice in the varible "choice"
if(choice.equalsIgnoreCase("y")){ //if choice is equal to y then true
System.out.println("New Game"+"\n"); //display new game
}
else if(choice.equalsIgnoreCase("n")){ //or if if choice is equal to n
replay = false; //replay = flase which will end the game
}
else{
System.out.println("Inaccurate choice. Ending program"); //else if not
y or n is enter
System.exit(0);
//the game will terminate
}
}
}
//end

You might also like