This is my implementation of the classic "Hangman" word-guessing game.
Class LePendu :
public class LePendu {
// Variable
public static int NB_ERREURS_MAX=10;
static ArrayList<Character> playerGuess = new ArrayList<Character>();
static ArrayList <String> wrongGuess = new ArrayList<String>();
static ArrayList <String> playerWord = new ArrayList<String>();
static String penduDessin [][] = new String[10][10];
// Le Main :
public static void main(String args[]) throws FileNotFoundException {
Scanner sc = new Scanner (new File("C:/Users/admin/pendu.txt"));
Scanner charP = new Scanner (System.in);
while(sc.hasNext()) {
playerWord.add(sc.nextLine());
}
// Intitialisation Hangman :
initialize2dArray();
// Wordchoice :
Random motAleatoire = new Random();
String word = playerWord.get(motAleatoire.nextInt(playerWord.size()));
// Loop for the game :
int chance = 0;
while(true) {
penduEntier();
drawPendu(chance);
if (chance>word.length()) {
System.out.println("Perdu!!!!");
break;
}
displayWord(word, playerGuess);
if (!getPlayerChar(charP, word, playerGuess)) {
chance ++;
};
if(displayWord(word, playerGuess)) {
System.out.println("Victoire");
break;
}
System.out.println("Tentez votre chance pour le mot");
if(charP.nextLine().equals(word)) {
System.out.println("Victoire");
break;
} else {
System.out.println("Mot incorrect, veuillez réessayer");
}
}
}
// Méthode getPlayerChar :
private static boolean getPlayerChar(Scanner charP, String word, ArrayList<Character> playerGuess) {
System.out.println("Entrez une lettre svp");
String charPlayer = charP.nextLine();
playerGuess.add(charPlayer.charAt(0));
return word.contains(charPlayer);
}
// Méthode displayWord :
private static boolean displayWord(String word, ArrayList<Character> playerGuess) {
int count = 0;
for (int i = 0;i<word.length();i++) {
if(playerGuess.contains(word.charAt(i))) {
System.out.print(word.charAt(i));
count++;
}
else {
System.out.print("-");
}
}
System.out.println("");
return (word.length() == count);
}
// Draw Hangman Methode :
public static void penduEntier(){
// Loop through the entire 2D array to display Hangman image
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
System.out.print(penduDessin[row][column]);
}
System.out.println();
}
}
// Tableau 2D Hangman :
public static void drawPendu(int chance) {
// The hangman will be erected from bottom to top, that is first gallos
// base will be made and then step by step hangman will be generated.
switch(chance) {
// we do not need add break in the cases because when players misses
// word after 8 chances then complete image of hangman is created.
case 8:
// Create legs of man
penduDessin[6][6] = "/";
penduDessin[7][5] = "/";
penduDessin[6][8] = "\\";
penduDessin[7][9] = "\\";
case 6:
// Create hands of man
penduDessin[4][6] = "/";
penduDessin[5][5] = "/";
penduDessin[4][8] = "\\";
penduDessin[5][9] = "\\";
case 5:
// Create tummy of man
for(int i = 3; i <= 5; i++)
penduDessin[i][7] = "|";
case 4:
// Create eyes and nose of man
penduDessin[2][6] = "*";
penduDessin[2][7] = "!";
penduDessin[2][8] = "*";
case 3:
// Create face of man
penduDessin[2][5] = "(";
penduDessin[2][9] = ")";
case 2:
// Create gallos
for(int i = 3; i <= 7; i++)
penduDessin[0][i] = "#";
penduDessin[1][7] = "#";
case 1:
// Create gallos
for(int i = 0; i <= 9; i++)
penduDessin[i][3] = "|";
case 0:
// Create base of gallos
for(int i = 0; i <= 8; i++)
penduDessin[8][i] = "_";
break;
default:
break;
}
}
// Initialize the hangman with space :
public static void initialize2dArray()
{
// Initialize the 2d array with blank spaces before printing the
// actual image of hangman.
for (int row = 0; row < 10; row++)
{
for (int column = 0; column < 10; column++)
{
penduDessin[row][column] = " ";
}
}
}
}
Thx for your help