2
\$\begingroup\$

Recently I made simple hangman game in Java.

Main.java

package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Type a passwords");
        String word = input.nextLine();
        char[] password = word.toCharArray();
        char[] passwordView = new char[password.length];
        for (int w = 0; w < passwordView.length; w++) {
            passwordView[w] = '_';
        }

        int badAnswers = 0;
        char letter = 'x';
        String guess;
        boolean success;
        String answers = "";


        while (badAnswers < 11) {
            success = false;
            System.out.println("Type a char.");
            while (!success) {
                guess = input.nextLine();
                if (guess.length() == 1 && !(answers.contains(guess))) {
                    letter = guess.charAt(0);
                    success = true;
                    answers = answers + letter + ", ";
                } else if (!(guess.length() == 1)){
                    System.out.println("Type exactly one char!");
                }
                else if (answers.contains(guess)){
                    System.out.println("You have already typed that symbol!");
                }

            }

            CheckingIfPasswordContainsLetter checker = new CheckingIfPasswordContainsLetter();
            if (checker.cointans(password, letter)) {
                for (int i = 0; i < password.length; i++) {
                    if (password[i] == letter) {
                        passwordView[i] = letter;
                    }
                }
            } else {
                badAnswers++;
                System.out.println("Bad");
            }

            if (!(Arrays.equals(password, passwordView))) {
                System.out.println("Already used symbols: " + answers);
                System.out.print("Current password is ");
                new PrintingArray(passwordView);
                System.out.print("\n");
            }


            if (Arrays.equals(password, passwordView)) {
                System.out.print("You won! the password was ");
                new PrintingArray(password);
                break;
            }

            if (badAnswers == 11) {
                System.out.print("You lose! Sorry!");
                break;
            }
        }

    }
}

PrintingArray.java

package com.company;

public class PrintingArray {

    public PrintingArray(char[] a){
        for (char z : a) {
            System.out.print(z);
        }
    }

}

CheckingIfPasswordContainsLetter.java

package com.company;


public class CheckingIfPasswordContainsLetter {
    public boolean cointans(char[] a, char b) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == b) {
                return true;
            }
        }
        return false;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
        for (int w = 0; w < passwordView.length; w++) {
            passwordView[w] = '_';
        }

There's a built-in for this

        Arrays.fill(passwordView, '_');

You don't have to do it manually.

        char letter = 'x';
        String guess;
        boolean success;

You could declare all these inside the loop instead by changing

            success = false;
            System.out.println("Type a char.");
            while (!success) {
                guess = input.nextLine();
                if (guess.length() == 1 && !(answers.contains(guess))) {
                    letter = guess.charAt(0);
                    success = true;
                    answers = answers + letter + ", ";

to

            char letter;
            boolean success = false;
            System.out.println("Type a char.");
            while (!success) {
                String guess = input.nextLine();
                if (guess.length() == 1 && !(answers.contains(guess))) {
                    letter = guess.charAt(0);
                    success = true;
                    answers = answers + letter + ", ";

or even better, get rid of success entirely.

            char letter;
            System.out.println("Type a char.");
            while (true) {
                String guess = input.nextLine();
                if (guess.length() == 1 && !(answers.contains(guess))) {
                    letter = guess.charAt(0);
                    answers = answers + letter + ", ";
                    break;

or consider something like

            char letter = inputLetter(input, answers);

with a properly defined method.

                new PrintingArray(passwordView);
                System.out.print("\n");

You don't need PrintingArray, but you could just say

                System.out.println(passwordView);

and get the same effect.

            CheckingIfPasswordContainsLetter checker = new CheckingIfPasswordContainsLetter();
            if (checker.cointans(password, letter)) {

You don't need this either. You could just say

            int firstOccurrence = word.indexOf(letter);
            if (firstOccurrence >= 0) {
                for (int i = firstOccurrence; i < password.length; i++) {

This also has the side effect of not duplicating the scan. Note that the original version started over at the beginning.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.