Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. package mini_Projekat_4;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Hangman {
  7.  
  8. public static void main(String[] args) {
  9. Scanner input = new Scanner(System.in);
  10.  
  11. // string array that contains words that are going to be randomized
  12. String[] words = { "otolaryngologist", "screw", "coin", "tube", "game",
  13. "pain", "victory", "defeat", "inspiration", "motive" };
  14. // user will guess the word which is selected randomly from the array
  15. String word = words[(int) (Math.random() * words.length)];
  16. // char array that will be shown to user
  17. char[] secretWord = new char[word.length()];
  18. // fill array at the begining with "*"
  19. Arrays.fill(secretWord, '*');
  20. // maximum number of mistakes
  21. int counter = 7;
  22. while (counter > 0) {
  23. printSecretWord(secretWord);
  24. System.out.println("Enter letter:");
  25. char letter = input.next().charAt(0);
  26. boolean letterFound = false;
  27. // if letter is found we replace * with that letter in char array
  28. for (int i = 0; i < secretWord.length; i++) {
  29. if (letter == word.charAt(i)) {
  30. secretWord[i] = letter;
  31. letterFound = true;
  32. }
  33. }
  34. // if the letter user entered is not part of the word we increment
  35. // counter used
  36. // for counting mistakes
  37. if (!letterFound) {
  38. System.out.println("Letter not found, try again:");
  39. counter++;
  40. }
  41. }
  42. //metod checks if there is * left in the word it's not finished yet
  43. public static boolean wordFinished(char[] array){
  44. int countStars = 0;
  45. for (int i = 0; i < secretWord.length; i++) {
  46. if (secretWord[i] == '*')
  47. countStars = 0;
  48.  
  49.  
  50. }
  51. return true;
  52. }
  53.  
  54. }
  55.  
  56. // metod to print secret word
  57. public static void printSecretWord(char[] secretWord) {
  58. for (int i = 0; i < secretWord.length; i++) {
  59. System.out.print(secretWord[i]);
  60. }
  61. System.out.println();
  62.  
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement