Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.24 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) { new Main().run(); }
  8.  
  9.     Scanner sc = new Scanner(System.in);
  10.  
  11.     public void run(){
  12.         String[] wordList = {"water", "koffie", "bril", "stekkerdoos", "beamer", "telefoon"};
  13.         Random random = new Random();
  14.         String word = wordList[random.nextInt(wordList.length)];
  15.         boolean[] guessed = new boolean[word.length()];
  16.         char[] alreadyGuessed = {};
  17.  
  18.         System.out.println("---------------------------------------------------");
  19.         System.out.println("| Het woord dat je moet raden heeft " + word.length() + " letters.");
  20.         System.out.println("---------------------------------------------------");
  21.  
  22.         while(alreadyGuessed.length < 10){
  23.             printBoard(word, guessed, alreadyGuessed);
  24.             System.out.print("Raad een letter: ");
  25.             char input = getValidInputFromUser();
  26.             guessed = checkIfInputMatchesWord(input, word, guessed);
  27.             if(checkIfWon(guessed)){
  28.                 System.out.println("Je hebt gewonnen!");
  29.                 break;
  30.             }
  31.             alreadyGuessed = addCharToGuessedList(input, alreadyGuessed, word);
  32.         }
  33.         if(alreadyGuessed.length == 10) {
  34.             System.out.println("Je hebt verloren!");
  35.         }
  36.     }
  37.  
  38.     public void printBoard(String word, boolean[] guessed, char[] alreadyGuessed){
  39.         for (int i = 0; i < word.length(); i++) {
  40.             if(guessed[i]){
  41.                 System.out.print(word.charAt(i) + " ");
  42.             } else {
  43.                 System.out.print("_ ");
  44.             }
  45.         }
  46.         System.out.println();
  47.         System.out.print("Je hebt de volgende letters al geprobeerd: [ ");
  48.         for (int i = 0; i < alreadyGuessed.length; i++) {
  49.             System.out.print(alreadyGuessed[i] + " ");
  50.         }
  51.         System.out.println("]");
  52.     }
  53.  
  54.     public char getValidInputFromUser(){
  55.         return sc.next().charAt(0);
  56.     }
  57.  
  58.     public boolean[] checkIfInputMatchesWord(char input, String word, boolean[] guessed){
  59.         for (int i = 0; i < word.length(); i++) {
  60.             if(input == word.charAt(i)){
  61.                 guessed[i] = true;
  62.             }
  63.         }
  64.         return guessed;
  65.     }
  66.  
  67.     public char[] addCharToGuessedList(char input, char[] alreadyGuessed, String word){
  68.         boolean correctInput = false;
  69.         for (int i = 0; i < word.length(); i++) {
  70.             if(input == word.charAt(i)){
  71.                 correctInput = true;
  72.             }
  73.         }
  74.         if(!correctInput){
  75.             char[] newAlreadyGuessed = new char[alreadyGuessed.length+1];
  76.             for (int i = 0; i < alreadyGuessed.length; i++) {
  77.                 newAlreadyGuessed[i] = alreadyGuessed[i];
  78.             }
  79.             newAlreadyGuessed[alreadyGuessed.length] = input;
  80.             return newAlreadyGuessed;
  81.         }
  82.         return alreadyGuessed;
  83.     }
  84.  
  85.     public boolean checkIfWon(boolean[] guessed){
  86.         boolean hasWon = true;
  87.         for (int i = 0; i < guessed.length; i++) {
  88.             if(!guessed[i]){
  89.                 hasWon = false;
  90.             }
  91.         }
  92.         return hasWon;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement