⭐ Let's code a HANGMAN GAME with Java! 🕺

Поделиться
HTML-код
  • Опубликовано: 13 янв 2025

Комментарии • 20

  • @BroCodez
    @BroCodez  18 дней назад +3

    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 -> "";
    };
    }
    }

    • @brandx1232
      @brandx1232 17 дней назад

      not working :

    • @funspot9520
      @funspot9520 14 дней назад

      A while loop is missing in this code

  • @Hnxzxvr
    @Hnxzxvr 18 дней назад +2

    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

  • @lamaXD
    @lamaXD 18 дней назад +2

    Best RUclipsr out there

  • @NebaDan-ug3kp
    @NebaDan-ug3kp 18 дней назад +1

    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❤❤❤🎉🎉

    • @Hnxzxvr
      @Hnxzxvr 18 дней назад +1

      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

  • @chandugupta9990
    @chandugupta9990 17 дней назад

    the constant videos he is uploading for java its looking like java playlist is coming soon!!!!!!!!

  • @thelowfer1016
    @thelowfer1016 18 дней назад +1

    Bro, when will you update the JS course?

  • @zhadrn
    @zhadrn 11 дней назад

    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.

  • @garagebandofficial
    @garagebandofficial 18 дней назад

    please make a video explaining Dijkstra's algorithm.

  • @Seif285
    @Seif285 18 дней назад

    Thank you bro ❤

  • @MassiveKittenFire-vw1cw
    @MassiveKittenFire-vw1cw 2 дня назад

    BroCode's perfect date is a pizza dinner and the Spongebob Movie hand to god

  • @LSTIMEGO
    @LSTIMEGO 17 дней назад

    Hello teacher can you make us a lesson about Linux terminal

  • @officialdreamplayz
    @officialdreamplayz 18 дней назад

    👌👍

  • @maxymusgamer9900
    @maxymusgamer9900 18 дней назад

    well damn this is goated

  • @Kingsglory2123
    @Kingsglory2123 13 дней назад

    What about flutter and react native

  • @mmarcaroni
    @mmarcaroni 18 дней назад

    what software are you using to code? i’m using eclipse looking for the name of this one