import java.util.Scanner;
public class HiddenWord {
private String hiddenWord; //Hidden Word Field
public HiddenWord(String hiddenWord) { //contrsuctor for the hidden word
this.hiddenWord = hiddenWord;
}
public String getHint (String guess) {
StringBuilder hint = new StringBuilder(); //Builder for making the hint
for (int i = 0; i < guess.length(); i++) {
char guessChar = guess.charAt(i); // Character from the guess
char hiddenChar = hiddenWord.charAt(i); // Corresponding character from the hidden word
if (guessChar == hiddenChar) {
hint.append(guessChar); // If characters match, add the character to the hint
} else if (hiddenWord.contains(String.valueOf(guessChar))) {
hint.append("+"); // If the guess character is in the hidden word but in a different position
} else {
hint.append("*"); // If the guess character is not in the hidden word
}
}
return hint.toString(); //Convert StringBuilder to String and return hint
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
HiddenWord puzzle = new HiddenWord("HARPS");
System.out.println("Guess the hidden word: ");
while (true) {
System.out.println("Enter your guess (or type 'exit' to quit): ");
String userGuess = scanner.nextLine(); // Read user input
if (userGuess.equalsIgnoreCase("exit")) {
break; // Exit the loop if user types 'exit'
}
System.out.println("Your guess: " + userGuess);
String hint = puzzle.getHint(userGuess);
System.out.println("Hint: " + hint);
if (hint.equals(userGuess)) {
System.out.println("Congratulations! You guessed the hidden word correctly!");
break; // Break the loop if the guess is correct
}
}
scanner.close(); // Close the scanner
}
}
HiddenWord.main(null);
Guess the hidden word:
Enter your guess (or type 'exit' to quit):
Your guess: HASHE
Hint: HA++*
Enter your guess (or type 'exit' to quit):
Your guess: HACKS
Hint: HA**S
Enter your guess (or type 'exit' to quit):
Your guess: HARMS
Hint: HAR*S
Enter your guess (or type 'exit' to quit):
Your guess: HARPS
Hint: HARPS
Congratulations! You guessed the hidden word correctly!
In developing the HiddenWord
class, I focused on encapsulating both the data and the logic required for the game within a single, cohesive class structure.
The HiddenWord
class centers around a private instance variable HiddenWord
, which stores the word to be guessed. This encapsulation of the hidden word within the class ensures that the integrity of the game’s core data is maintained, as it can’t be accessed or modified directly from outside the class. In the constructor of HiddenWord
, I initialized the HiddenWord
variable. This constructor is a fundamental aspect of class design, as it sets up the initial state of the object when it’s created.
The getHint
method is where the primary logic of the game resides. This method compares the user’s guess with the hidden word and constructs a hint based on specific rules. By iterating over each character of the guess and comparing it with the corresponding character in the hidden word, the method effectively uses control structures to build the hint.
In the initial development of the HiddenWord
class, I utilized a simple String
to create the hint in the getHint
method. However, I soon realized that this approach was not the most efficient, particularly in terms of memory management. Every time a new character was added to the hint, using a String
would create a new String
object in memory. After some research, I came across the StringBuilder
class, a mutable sequence of characters. StringBuilder
allows for the dynamic modification of its contents without creating new objects each time something is appended to it. Now, instead of creating a new string each time a character is added, I simply modify the existing StringBuilder
object. This change not only optimizes the memory usage but also makes the code cleaner.
Finally, the main method transforms this class into an interactive game. By incorporating a Scanner to take user inputs and using a loop to allow continuous guessing, I created an engaging user experience. The loop checks for correct guesses and congratulates the user upon success.
package com.nighthawk.spring_portfolio.nbapredictor.monte_carlo;
public class PlayerStats {
private int points;
private int rebounds;
private int assists;
// Getters and setters
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public int getRebounds() {
return rebounds;
}
public void setRebounds(int rebounds) {
this.rebounds = rebounds;
}
public int getAssists() {
return assists;
}
public void setAssists(int assists) {
this.assists = assists;
}
}
The PlayerStats
class is a great example of class desgin from our project, and a perfect example of a Plain Old Java Object (POJO). The context of PlayerStats
is to hold individual basketball players’ statistical data, such as points, rebounds, and assists. This class forms the base layer in our application’s hierarchy, as it’s the most granular level of data representation.
The fields for the statistical data are kept private, ensuring that the internal representation of a player’s stats is hidden from other parts of the program, safeguarding the integrity of the data.For each private field in the PlayerStats
class, there getter and setter methods. The getters allow other parts of the program to read the player’s statistics, while the setters enable the modification of these stats under specific conditions defined in the program.
The PlayerStats
class plays a critical role in the broader structure of our project. It serves as the building block for more complex classes like Player
and GameTeam
. In the Player
class, PlayerStats
is used to represent the individual stats of a player. The GameTeam
class leverages uses Player
class to form a complete team.