One Love: Putting It All Together

12.01.2018 |

Episode #10 of the course An introduction to Java programming by Alex Theedom

 

Welcome to the last lesson in a series of ten lessons about the Java programming language. You made it, congratulations! You really are a Java programmer! Let’s get started. Today, you are going to combine all you have learned into one Hangman game.

 

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 10.

• Create a class file called Hangman.

• Delete the line, System.out.println(“Hello Codiva”);.

Now you are ready to start coding.

 

Step 2: Start Coding

This lesson is going to require your full attention, as it is a little tricky, but you have already learned all you need to know to write the fun game of Hangman. There are only three pieces of code you will need that you have not learned:

• the .replaceAll(“.”, “*”) method, which replaces all the letters in the word with * (asterisk)

• the .toCharArray() method, which converts the word into an array of letters

• the .indexOf(letter) method, which finds the index of the given letter in a word

The game’s requirements are:

• An array to store a list of words to guess.

• To randomly chose a word from the list.

• An array to store a mask of the word. You do this by replacing all the letters with * and then converting it to an array of letters (see above for a hint).

• To count down the attempts. Set this to 10.

• An infinite loop in which the game is actually played. This is technically called the game loop.

• While in the game loop, you will need to write code that:

○ Asks for a letter guess.

○ Checks to see if the guessed letter is in the array of letters created from the word randomly chosen from the word array.

○ If it contains that letter, then replace the * (asterisk) in the letter array with the actual letter.

○ Now test to see if the entire word has been guessed, and if so, end the game with a message of congratulations.

○ If the letter was incorrect, then the number of attempts remaining must be reduced by one, and if there are no attempts left, the game must end.

Now that you know what is required, try to program the game without peeking at the solution below.

 

Step 3: Check and Run Your Program

Here is the code in full:

import java.util.Random;
import java.util.Scanner;

class Hangman {
   public static void main(String[] args) {

     int lives = 10;

     Scanner scanner = new Scanner(System.in);

     String[] words = {“CAT”,“BAT”,“MOUSE”};
     String word = words[new Random().nextInt(2)];
     char[] mask = word.replaceAll(“.”, “*”).toCharArray();

     while (true){

       System.out.println(“Guess Letter: “);
       String letter = scanner.next();

       if(word.contains(letter)){
         System.out.println(“Correct”);
         int index = word.indexOf(letter);
         mask[index] = letter.charAt(0);
         System.out.println(“Current Guess: “ + new String(mask));
         if(new String(mask).equals(word)){
          System.out.println(“Congratulations You Win”);
           break;
       }
       } else {
         lives–;
         if(lives < 0) break;
       }

     }
   }
}

Your solution might be slightly different from mine. That doesn’t matter, as long as it conforms to the requirements for the game and works.

You can now run your program by clicking the Run button in the top right-hand corner of the screen. Good luck, I hope you win.

Congratulations, you have successfully completed the Java programming language email course!

Thank you very much for taking my email course. I hope you’ve enjoyed it. You should now be ready to start becoming a professional Java programmer. I look forward to working with you in the future.

 

Recommended book

Introduction to Logic by Paul Herrick

 

Share with friends