Created
August 25, 2017 20:33
-
-
Save adeelibr/ee2350c1c3b8a112cec0e79d13cf72fb to your computer and use it in GitHub Desktop.
A simple command line game of Hangman made in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Hangman; | |
import java.util.Scanner; | |
import java.util.Random; | |
public class Built { | |
public static void main(String[] args){ | |
Scanner scanner = new Scanner(System.in); | |
Random random = new Random(); | |
String guesses[] = { | |
"reddit", "facebook", "java", "assignment", | |
"game", "hello", "islam", "religion", "internet", "face"}; | |
boolean weArePlaying = true; | |
while(weArePlaying){ | |
System.out.println("Lets Start Playing Hangman ver 0.1"); | |
int randomNumber = random.nextInt(guesses.length); //random.nextInt(10); | |
char randomWordToGuess[] = guesses[randomNumber].toCharArray(); // java -> j,a,v,a | |
int ammountOfGuesses = randomWordToGuess.length; //total tries to guess a word. | |
char playerGuess[] = new char[ammountOfGuesses]; // "_ _ _ _ _ _ _ _" | |
for(int i=0; i<playerGuess.length; i++){ // Assign empty dashes at start "_ _ _ _ _ _ _ _" | |
playerGuess[i] = '_'; | |
} | |
boolean wordIsGuessed = false; | |
int tries = 0; | |
while(!wordIsGuessed && tries != ammountOfGuesses){ | |
System.out.println("Current Guesses: "); | |
print(playerGuess); | |
System.out.printf("You have %d ammount of tries left.\n", ammountOfGuesses-tries); | |
System.out.println("Enter a single character: "); | |
char input = scanner.nextLine().charAt(0); | |
tries++; | |
if(input == '-'){ | |
wordIsGuessed = true; | |
weArePlaying = false; | |
} else{ | |
for(int i=0; i<randomWordToGuess.length; i++){ | |
if(randomWordToGuess[i] == input){ | |
playerGuess[i] = input; | |
} | |
} | |
if(isTheWordGuessed(playerGuess)){ | |
wordIsGuessed = true; | |
System.out.println("Congratulations"); | |
} | |
} | |
} /* End of wordIsGuessed */ | |
if(!wordIsGuessed){ | |
System.out.println("You ran out of guesses."); | |
} | |
System.out.println("Would you like to play again? (yes/no) "); | |
String choice = scanner.nextLine(); | |
if(choice.equals("no")){ | |
weArePlaying = false; | |
} | |
}/*End of We Are Playing*/ | |
System.out.println("Game Over!"); | |
} | |
public static void print(char array[]){ | |
for(int i=0; i<array.length; i++){ // Assign empty dashes at start "_ _ _ _ _ _ _ _" | |
System.out.print(array[i] + " "); | |
} | |
System.out.println(); | |
} | |
public static boolean isTheWordGuessed(char[] array){ | |
boolean condition = true; | |
for(int i=0; i<array.length; i++){ | |
if(array[i] == '_'){ | |
condition = false; | |
} | |
} | |
return condition; | |
} | |
} |
These codes are that as simple as I look for and understandable. Thanks for your efforts. :-)
thank you for your kind comments guys :))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks For sharing, These codes are very Helpful!