0% found this document useful (0 votes)
5 views1 page

Guess The Word Game - GuessTheWord Class

The document contains a Java class named 'GuessTheWord' that implements a word guessing game. Players have 10 attempts to guess letters of a randomly selected word, and the game continues until the word is guessed or attempts run out. The class includes methods for starting the game, showing the current word status, getting user input, checking guesses, and ending the game.

Uploaded by

benbernou.sabri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Guess The Word Game - GuessTheWord Class

The document contains a Java class named 'GuessTheWord' that implements a word guessing game. Players have 10 attempts to guess letters of a randomly selected word, and the game continues until the word is guessed or attempts run out. The class includes methods for starting the game, showing the current word status, getting user input, checking guesses, and ending the game.

Uploaded by

benbernou.sabri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

package game;

import java.util.Scanner;

public class GuessTheWord {

private boolean play = true;


private Words randomWord = new Words();
private Scanner scanner = new Scanner(System.in);
private int rounds = 10;
private char lastRound;

public void start() {

do {
showWord();
getInput();
checkInput();
} while (play);
}

void showWord() {
System.out.println("You have " + rounds + " tries left.");
System.out.println(randomWord);
}

void getInput() {

System.out.print("Enter a letter to guess the word: ");


String userGuess = scanner.nextLine();
lastRound = userGuess.charAt(0);

void checkInput() {

boolean isGuessedRight = randomWord.guess(lastRound);

if (isGuessedRight) {
if (randomWord.isGuessedRight()) {
System.out.println("Congrats, you won!");
System.out.println("The word is: " + randomWord);
play = false;
}
} else {
rounds--;

if (rounds == 0) {
System.out.println("Game Over!");
play = false;
}
}
}

public void end() {


scanner.close();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy