This game is a console game application based on hangman, a popular game worldwide.
There is a text file with a list of movies in it, one per line. When the game begins, the user is shown a movie title with the letters obscured by asterisks or stars.
The user is asked to guess a letter, not any non-alphabetic character. If the letter does not occur in the movie title, the player will be alerted that the guess is wrong. Also, the player will lose a chance out of a maximum of 5 chances. If all chances are gone and the movie title is still not guessed, the game is lost.
If all letters are guessed within 5 chances, the game is won. The player is then asked if he wants to continue playing the game. If yes, the game goes to another round. If not, the game exits.
The roles and functions are divided into 2 classes:
- GetMovie: This will fetch a random movie for playing one round of the game.
- GamePlay: Handles the playing of the game.
The code listings for these classes are given below. The game has two versions: Java and C#
Java Version
Get Movie Class
/* * 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 com.Hangman; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; /** * * */ public class GetMovie { static ArrayList movies = new ArrayList(); static String movie; static String path; GetMovie(String pathToFile) { path = pathToFile; } public static String getRandMovie() { String randWord = ""; try{ InputStream istream = new FileInputStream(path); InputStreamReader isr = new InputStreamReader(istream); BufferedReader br = new BufferedReader(isr); String movie; while((movie = br.readLine()) != null) { movies.add(movie); } int size = movies.size(); int idx = (new java.util.Random()).nextInt(size); randWord = movies.get(idx); } catch(FileNotFoundException f) { System.out.println(f.getMessage()); System.exit(0); } catch(IOException io) { System.out.println(io.getMessage()); System.exit(0); } return randWord; } }
GamePlay Class
/* * 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 com.Hangman; import java.util.Scanner; /** * * */ public class GamePlay { static char[] movieChars; static char[] guessChars; static int chances = 5; static void renderMovie(String movie) { movieChars = movie.toCharArray(); guessChars = new char[movieChars.length]; for(int i = 0; i< movieChars.length; i++) { if(Character.isLetter(movieChars[i])) { System.out.print("* "); } else { System.out.print(movieChars[i] + " "); guessChars[i] = movieChars[i]; } } System.out.println(); } private static boolean playChar(char ch) { int matchCount = 0; for(int i = 0; i < movieChars.length; i++) { if(Character.toLowerCase(movieChars[i]) == Character.toLowerCase(ch)) { matchCount++; guessChars[i] = movieChars[i]; } } if(matchCount == 0) { chances--; System.out.println("WRONG Guess! You have "+chances+" chances left!"); } /**/ for(int i = 0; i< guessChars.length; i++) { if(guessChars[i] == '\0') { System.out.print("* "); } else { System.out.print(guessChars[i]+ " "); } } System.out.println(); String movie = new String(movieChars); String guessed = new String(guessChars); return movie.equals(guessed); } public static void main(String[] args) { GetMovie gm = new GetMovie("movies.txt"); char choice = '\0'; System.out.println("/***************WELCOME TO JAVA HANGMAN**************/"); do{ chances = 5; String movie = GetMovie.getRandMovie(); renderMovie(movie); System.out.print("Enter a letter: "); Scanner sc = new Scanner(System.in); char gchar = sc.next().charAt(0); while(!playChar(gchar)) { if(chances == 0) { break; } System.out.print("Enter a letter: "); sc = new Scanner(System.in); gchar = sc.next().charAt(0); } if(chances == 0) { System.out.println("You LOSE! The movie was: \n"+movie); } else { System.out.println("You WIN!"); } System.out.print("\nPlay again (y/n)?: "); sc = new Scanner(System.in); choice = sc.next().charAt(0); } while(choice == 'y' || choice == 'Y'); System.out.println("EXITING..."); System.exit(0); } }
Copy, compile, run and ENJOY!
C# Version
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleHangman { class GetMovie { private static List lines = new List(); static string movie; static string path; public GetMovie(string pathToFile) { path = pathToFile; } internal static string getRandMovie() { System.IO.StreamReader file = new System.IO.StreamReader(@path); string line; while ((line = file.ReadLine()) != null) { lines.Add(line); } int count = lines.Count; Random r = new Random(); int index = r.Next(count); string line1 = lines[index]; file.Close(); return line1; //working } } class GamePlay { static char[] movieChars; static char[] guessChars; static int chances = 5; static void renderMovie(String movie) { movieChars = movie.ToCharArray(); guessChars = new char[movieChars.Length]; for (int i = 0; i < movieChars.Length; i++) { if (Char.IsLetter(movieChars[i])) { Console.Write("* "); } else { Console.Write(movieChars[i] + " "); guessChars[i] = movieChars[i]; } } Console.WriteLine(); } private static bool playChar(char ch) { int matchCount = 0; for (int i = 0; i < movieChars.Length; i++) { if (Char.ToLower(movieChars[i]) == Char.ToLower(ch)) { matchCount++; guessChars[i] = movieChars[i]; } } if (matchCount == 0) { chances--; Console.WriteLine("WRONG Guess! You have " + chances + " chances left!"); } /**/ for (int i = 0; i < guessChars.Length; i++) { if (guessChars[i] == '\0') { Console.Write("* "); } else { Console.Write(guessChars[i] + " "); } } Console.WriteLine(); string movie = new string(movieChars); string guessed = new string(guessChars); return (movie == guessed); } public static void Main(string[] args) { GetMovie gm = new GetMovie("..\\..\\movies.txt"); char choice = '\0'; Console.WriteLine("/***************WELCOME TO C# HANGMAN**************/"); do { chances = 5; String movie = GetMovie.getRandMovie(); renderMovie(movie); Console.Write("Enter a letter: "); char gchar = Convert.ToChar(Console.ReadLine()); while (!playChar(gchar)) { if (chances == 0) { break; } Console.Write("Enter a letter: "); gchar = Convert.ToChar(Console.ReadLine()); } if (chances == 0) { Console.WriteLine("You LOSE! The movie was: \n" + movie); } else { Console.WriteLine("You WIN!"); } Console.Write("\nPlay again (y/n)?: "); choice = Convert.ToChar(Console.ReadLine()); } while (choice == 'y' || choice == 'Y'); Console.WriteLine("Press a key to exit..."); Console.ReadKey(); } } }
See the video below. COPY, COMPILE, RUN and ENJOY!