0% found this document useful (0 votes)
785 views

Java Programming IT105 STI College Q&A Laboratory 6A

This document provides instructions for a lab exercise to create a simple guessing game in Java. Students will generate a random word from a text file, display it with missing letters, and allow the user to guess letters until the full word is revealed. The program checks if guesses are correct, updates the displayed word, and notifies the user until they solve the puzzle and the game ends.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
785 views

Java Programming IT105 STI College Q&A Laboratory 6A

This document provides instructions for a lab exercise to create a simple guessing game in Java. Students will generate a random word from a text file, display it with missing letters, and allow the user to guess letters until the full word is revealed. The program checks if guesses are correct, updates the displayed word, and notifies the user until they solve the puzzle and the game ends.

Uploaded by

Marvin Cinco
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Programming | IT105 STI College

Q&A Laboratory 6A

Objective:

At the end of the exercise, the students should be able to:

 Create a simple game that exhibits file input and output.

Software Requirements:

 Latest version of NetBeans IDE


 Java Development Kit (JDK) 8

Procedure:

1. Create a folder named LastName_FirstName (ex. Reyes_Mark) in your local drive.


2. Create a new project named LabExer6A. Set the project location to your own folder.
3. Create a simple guessing game (similar to Hangman or Hangaroo). In this game, the user guesses a letter and then
attempts to guess the word.
4. Create a Notepad file named words.txt which will store any number of words each written per line.
5. The Java program shall:
 randomly select a word from the list saved in words.txt;
 display a letter in which some of the letters are replaced by?; for example, ED??A??ON (for EDUCATION);
 place the letter in the correct spot (or spots) in the word each time the user guesses a letter correctly;
 inform the user if the guessed letter is not in the word; and
 display a congratulatory message when the entire correct word has been deduced.

CODES:

package labexer6a;

import java.util.*;
import java.io.*;

public class LabExer6A{


public static void main(String[] args) throws Exception {
char guess;

File file = new File("words.txt");


Scanner sc = new Scanner(file);

String words[] = sc.nextLine().split(",");

Random r1 = new Random();

int value =r1.nextInt(words.length);


String phraseSolution = words[value];

StringBuilder phrase = new StringBuilder("");

for(int i=0;i<words[value].length(); i++){


if(i%2==0 && words[value].charAt(i) != ' '){
phrase.append("?");
}

else{
phrase.append(words[value].charAt(i));
}
}

Scanner input = new Scanner(System.in);

System.out.print(phrase+ "\nEnter a letter you suspect is in this phrase\n--------------");

while (!phraseSolution.equals(phrase.toString())) {

System.out.println("\nEnter a Letter you suspect is in the phrase");


guess = Character.toUpperCase(input.nextLine().charAt(0));
boolean check = check(guess, phraseSolution);

if (check) {
int spot = placeLocator(guess, phraseSolution);
phrase.setCharAt(spot, guess);

System.out.println(phrase);
System.out.println("Correct! Enter another Letter you suspect is in the phrase");
}

else{
System.out.println("Sorry! This letter is not in the phrase please guess another!");
}
}

System.out.print("\nCongrats! You found all letters and finished the game!");


}

public static int placeLocator(char input, String phraseSolution){


int place;
int x = 0;

while (input != phraseSolution.charAt(x)){


x++;
}

place = x;

return place;
}

public static boolean check(char input, String phraseSolution){


boolean validation = false;
int letter = 0;

for (int x = 0; x < phraseSolution.length(); x++){


if (input != phraseSolution.charAt(x)){
validation = false;
}

else{
validation = true;
letter++;
}
}

if (letter >= 1){


validation = true;
}

return validation;
}
}
OUTPUTS:

You might also like