Java text guess console game.

strings.txt

hello world
the flying keyboard
lorem ipsum
programming
word guess

Note that after words there should be new line character (enter/return/…).

Main.java

import java.util.Scanner;

// © 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main {
    public static void main(String[] args) {
        StringLoader stringLoader = new StringLoader();
        Scanner scanner = new Scanner(System.in);

        String play = "y";

        while (play.equals("y")){
            WordGuess wordGuess = new WordGuess(stringLoader.load("strings.txt"));
            wordGuess.play();

            System.out.println("Play again? y/n");
            scanner.nextLine();
        }
    }
}

StringLoader.java

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

// © 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class StringLoader {
    public String load(String fileName){
        Path path = Paths.get(fileName);

        try {
            Stream<String> lines = Files.lines(path);

            String line = lines.skip(ThreadLocalRandom.current().nextLong(Files.lines(path).count())).findFirst().get();

            lines.close();

            return line;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}

WordGuess.java

import java.util.Collections;
import java.util.Scanner;

// © 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class WordGuess {
    private final String stringToGuess;
    StringBuilder enteredString;

    public WordGuess(String stringToGuess) {
        this.stringToGuess = stringToGuess;
        //Creates string of stringToGuess.length() size filled with "_".
        enteredString = new StringBuilder(
                String.join("", Collections.nCopies(stringToGuess.length(), String.valueOf("_"))));

        replaceLetter(" ");
    }

    public void play(){
        Scanner scanner = new Scanner(System.in);

        int triesLeft = 10;
        boolean isOver = false;

        String letter;

        while(!isOver){
            System.out.println(enteredString);

            System.out.print("Enter letter: ");
            letter = scanner.nextLine();

            if(stringToGuess.contains(letter)){
                replaceLetter(letter);
            }else{
                --triesLeft;

                System.out.println("Tries left: " + triesLeft);

                if(triesLeft <= 0){
                    System.out.println("Game Over!");

                    isOver = true;
                }
            }

            if(!enteredString.toString().contains("_")){
                System.out.println();
                System.out.println("You found the answer!");
                System.out.println(enteredString);
                System.out.println("You won!");

                isOver = true;
            }

            System.out.println();
        }
    }

    private void replaceLetter(String letter){
        int letterIndex = stringToGuess.indexOf(letter);

        while (letterIndex >= 0) {
            enteredString.replace(letterIndex, letterIndex + 1, letter);
            letterIndex = stringToGuess.indexOf(letter, letterIndex + 1);
        }
    }
}

 



Java Text Guess Console Game
Tagged on:                 

Leave a Reply

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

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close