Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mini_Projekat_4;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Hangman {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- // string array that contains words that are going to be randomized
- String[] words = { "otolaryngologist", "screw", "coin", "tube", "game",
- "pain", "victory", "defeat", "inspiration", "motive" };
- // user will guess the word which is selected randomly from the array
- String word = words[(int) (Math.random() * words.length)];
- // char array that will be shown to user
- char[] secretWord = new char[word.length()];
- // fill array at the begining with "*"
- Arrays.fill(secretWord, '*');
- // maximum number of mistakes
- int counter = 7;
- while (counter > 0) {
- printSecretWord(secretWord);
- System.out.println("Enter letter:");
- char letter = input.next().charAt(0);
- boolean letterFound = false;
- // if letter is found we replace * with that letter in char array
- for (int i = 0; i < secretWord.length; i++) {
- if (letter == word.charAt(i)) {
- secretWord[i] = letter;
- letterFound = true;
- }
- }
- // if the letter user entered is not part of the word we increment
- // counter used
- // for counting mistakes
- if (!letterFound) {
- System.out.println("Letter not found, try again:");
- counter++;
- }
- }
- //metod checks if there is * left in the word it's not finished yet
- public static boolean wordFinished(char[] array){
- int countStars = 0;
- for (int i = 0; i < secretWord.length; i++) {
- if (secretWord[i] == '*')
- countStars = 0;
- }
- return true;
- }
- }
- // metod to print secret word
- public static void printSecretWord(char[] secretWord) {
- for (int i = 0; i < secretWord.length; i++) {
- System.out.print(secretWord[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement