frq 2
This question involves the WordMatch class, which stores a secret string and provides methods that compare other strings to the secret string. You will write two methods in the WordMatch class.
public class WordMatch
{
/** The secret string. */
private String secret;
/** Constructs a WordMatch object with the given secret string of lowercase letters. */
public WordMatch(String word)
{
/* implementation not shown */
}
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
public int scoreGuess(String guess)
{ /* to be implemented in part (a) */ }
/** Returns the better of two guesses, as determined by scoreGuess and the rules for a
* tie-breaker that are described in part (b).
* Precondition: guess1 and guess2 contain all lowercase letters.
* guess1 is not the same as guess2.
*/
public String findBetterGuess(String guess1, String guess2)
{ /* to be implemented in part (b) */ }
}
public class WordMatch {
private String secret = "mississippi"; // The secret string to be matched against.
// Calculate the score for a guess based on matching substrings.
public int scoreGuess(String guess) {
int count = 0; // Initialize a count for the number of matches.
String temp; // Declare a temporary variable to store substrings.
// Iterate through possible substrings of the secret string.
for (int x = 0; x <= secret.length() - guess.length(); x++) {
// Extract a substring of the same length as the guess.
temp = secret.substring(x, x + guess.length());
// Check if the extracted substring matches the guess, ignoring case.
if (temp.equalsIgnoreCase(guess)) {
count++;
}
}
// Calculate the score based on the number of matches and the length of the guess.
int score = count * guess.length() * guess.length();
return score;
}
// Find the better guess among two options by comparing their scores and lexicographical order.
public String findBetterGuess(String guess1, String guess2) {
int score1 = scoreGuess(guess1); // Calculate the score for guess1.
int score2 = scoreGuess(guess2); // Calculate the score for guess2.
if (score1 > score2) {
return guess1; // Return guess1 if it has a higher score.
} else if (score2 > score1) {
return guess2; // Return guess2 if it has a higher score.
} else {
// If both guesses have the same score, compare them lexicographically.
if (guess1.compareTo(guess2) > 0) {
return guess1;
}
return guess2;
}
}
public static void main(String[] args) {
WordMatch wordMatch = new WordMatch(); // Create an instance of the WordMatch class.
// Find and print the better guess between "i" and "a".
String betterGuess = wordMatch.findBetterGuess("i", "a");
System.out.println("Better Guess: " + betterGuess);
}
}
WordMatch.main(null);
Better Guess: i
import java.util.Scanner;
import java.util.Random;
public class WordMatch {
//New Variables
private String secret;
private int player1Score;
private int player2Score;
private Random random = new Random();
private Scanner scanner = new Scanner(System.in);
public WordMatch() {
// Initialize scores and get a random secret word
player1Score = 0;
player2Score = 0;
secret = generateRandomWord();
}
public int scoreGuess(String guess) {
int count = 0;
String temp;
for (int x = 0; x <= secret.length() - guess.length(); x++) {
temp = secret.substring(x, x + guess.length());
if (temp.equalsIgnoreCase(guess)) {
count++;
}
}
int score = count * guess.length() * guess.length();
return score;
}
public String findBetterGuess(String guess1, String guess2) {
int score1 = scoreGuess(guess1);
int score2 = scoreGuess(guess2);
if (score1 > score2) {
return guess1;
} else if (score2 > score1) {
return guess2;
} else {
if (guess1.compareTo(guess2) > 0) {
return guess1;
}
return guess2;
}
}
// Getting Random Word
public String generateRandomWord() {
String[] words = { "mississippi", "apple", "banana", "chocolate", "programming" };
return words[random.nextInt(words.length)];
}
//Play Game Method
public void playGame() {
System.out.println("Welcome to the Word Matching Game!");
System.out.println("Rule 1: No guessing the same letter (only applies to oneself)");
System.out.println("Rule 2: Hide your guesses from the other player");
System.out.println("Rule 3: Have fun");
for (int round = 1; round <= 5; round++) {
System.out.println("Round " + round);
// Player 1's turn
System.out.print("Player 1, enter your guess: ");
String player1Guess = scanner.next();
int player1ScoreRound = scoreGuess(player1Guess);
player1Score += player1ScoreRound;
System.out.println("Player 1 Score: " + player1Score);
// Player 2's turn
System.out.print("Player 2, enter your guess: ");
String player2Guess = scanner.next();
int player2ScoreRound = scoreGuess(player2Guess);
player2Score += player2ScoreRound;
System.out.println("Player 2 Score: " + player2Score);
}
System.out.println("Game Over!");
if (player1Score > player2Score) {
System.out.println("Player 1 wins!");
} else if (player2Score > player1Score) {
System.out.println("Player 2 wins!");
} else {
System.out.println("It's a tie!");
}
System.out.println("The secret word was: " + secret);
}
public static void main(String[] args) {
WordMatch wordMatch = new WordMatch();
wordMatch.playGame();
}
}
WordMatch.main(null);
Welcome to the Word Matching Game!
Rule 1: No guessing the same letter (only applies to oneself)
Rule 2: Hide your guesses from the other player
Rule 3: Have fun
Round 1
Player 1, enter your guess: Player 1 Score: 1
Player 2, enter your guess: Player 2 Score: 1
Round 2
Player 1, enter your guess: Player 1 Score: 1
Player 2, enter your guess: Player 2 Score: 3
Round 3
Player 1, enter your guess: Player 1 Score: 2
Player 2, enter your guess: Player 2 Score: 4
Round 4
Player 1, enter your guess: Player 1 Score: 3
Player 2, enter your guess: Player 2 Score: 5
Round 5
Player 1, enter your guess: Player 1 Score: 3
Player 2, enter your guess: Player 2 Score: 5
Game Over!
Player 2 wins!
The secret word was: apple