import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { // JAVA HANGMAN GAME String filePath = "words.txt"; // Put your file path here ArrayList words = new ArrayList(); try(BufferedReader reader = new BufferedReader(new FileReader(filePath))){ String line; while((line = reader.readLine()) != null){ words.add(line.trim()); } } catch(FileNotFoundException e){ System.out.println("Could not find file"); } catch(IOException e){ System.out.println("Something went wrong"); } Random random = new Random(); String word = words.get(random.nextInt(words.size())); Scanner scanner = new Scanner(System.in); ArrayList wordState = new ArrayList(); int wrongGuesses = 0; for(int i = 0; i < word.length(); i++){ wordState.add('_'); } System.out.println("************************"); System.out.println("Welcome to Java Hangman!"); System.out.println("************************"); while(wrongGuesses < 6){ System.out.print(getHangmanArt(wrongGuesses)); System.out.print("Word: "); for(char c : wordState){ System.out.print(c + " "); } System.out.println(); System.out.print("Guess a letter: "); char guess = scanner.next().toLowerCase().charAt(0); if(word.indexOf(guess) >= 0){ System.out.println("Correct guess!"); for(int i = 0; i < word.length(); i++){ if(word.charAt(i) == guess){ wordState.set(i, guess); } } if(!wordState.contains('_')){ System.out.print(getHangmanArt(wrongGuesses)); System.out.println("YOU WIN!"); System.out.println("The word was: " + word); break; } } else{ wrongGuesses++; System.out.println("Wrong guess!"); } } if(wrongGuesses >= 6){ System.out.print(getHangmanArt(wrongGuesses)); System.out.println("GAME OVER!"); System.out.println("The word was: " + word); } scanner.close(); } static String getHangmanArt(int wrongGuesses){ return switch(wrongGuesses){ case 0 -> """ """; case 1 -> """ o """; case 2 -> """ o | """; case 3 -> """ o /| """; case 4 -> """ o /|\\ """; case 5 -> """ o /|\\ / """; case 6 -> """ o /|\\ / \\ """; default -> ""; }; } }
@brandx123226 күн бұрын
not working :
@funspot952023 күн бұрын
A while loop is missing in this code
@Hnxzxvr27 күн бұрын
I remember using your vids to make stuff in python I never cared about what you were saying and I never learned anything but now I’m coding in java all the time without needing tutorials Tysm dude
@fortyquinn130127 күн бұрын
Ok
@lamaXD27 күн бұрын
Best KZbinr out there
@NebaDan-ug3kp27 күн бұрын
the Legend is here 🎉🎉🎉🎉🎉🎉 can you make us a flutter serious .......because any body can be software engineer with out going to college just by watching your amazing videos❤❤❤🎉🎉
@Hnxzxvr27 күн бұрын
Software engineer without going to collage may be a little far but he can definitely help anyone get any coding job whether you go to collage or not
@chandugupta999026 күн бұрын
the constant videos he is uploading for java its looking like java playlist is coming soon!!!!!!!!
@zhadrn20 күн бұрын
5:55 what do i need to press in order for the next line to start after the arrow? mine just starts at the beginning which is why the hangman then appears wrong when i run the code.
@thelowfer101627 күн бұрын
Bro, when will you update the JS course?
@MassiveKittenFire-vw1cw11 күн бұрын
BroCode's perfect date is a pizza dinner and the Spongebob Movie hand to god
@Seif28526 күн бұрын
Thank you bro ❤
@garagebandofficial27 күн бұрын
please make a video explaining Dijkstra's algorithm.
@LSTIMEGO26 күн бұрын
Hello teacher can you make us a lesson about Linux terminal
@maxymusgamer990027 күн бұрын
well damn this is goated
@Kingsglory212321 күн бұрын
What about flutter and react native
@officialdreamplayz27 күн бұрын
👌👍
@mmarcaroni27 күн бұрын
what software are you using to code? i’m using eclipse looking for the name of this one