Hangman Console Game in Java and Microsoft .net

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:

  1. GetMovie: This will fetch a random movie for playing one round of the game.
  2. 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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!

2 thoughts on “Hangman Console Game in Java and Microsoft .net

  1. Just desire to say your article is as astonishing. The clarity in your post is simply nice and i can assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work.

Leave a Reply

Your email address will not be published. Required fields are marked *