Game

java

School

Arizona State University *

*We aren’t endorsed by this school

Course

222

Subject

Computer Science

Date

Feb 20, 2024

Type

java

Pages

6

Uploaded by BaronGuineaPig1709

Report
import java.util.ArrayList; import java.util.HashSet; import java.util.Set; /** * Class for handling some game logic for hangman game. * Every game starts with a score of 10 and the points are reduced based on the description of * "makeGuess". Points holds the current score for one game. Game is lost when the user made 10 * guesses and did not guess the word. */ public class Game { /** Holds the points for the game. */ protected int points; /** Holds the round of the game. */ //SER316 TASK 2 SPOTBUGS FIX removed round /** Holds the player name for the game. */ private String name; /** Holds the answer for the current game. */ protected String answer; /** The path to the file holding the leaderboard.*/ //SER316 TASK 2 SPOTBUGS FIX removed leaderboard /** The status of the game. {0 - In progress, 1 - Game won, 2 - game over}*/ protected int gameStatus = 0; // all things that were already guessed, needs to be cleared for each game protected ArrayList<String> guesses = new ArrayList<String>(); // all answers from makeGuess that were already returned protected ArrayList<Double> answers = new ArrayList<Double>(); /** * Gets the name for the game. * @return String The name. */ public String getName() { return this.name; } /** * Gets the answer for the game. * @return String The Answer. */ public String getAnswer() { return this.answer.toLowerCase(); } /** * Gets the current status of the game.
* @return int The game status. */ public int getGameStatus() { return this.gameStatus; } /** * Sets the score for the game. * @param points the points to set for the game5w */ public void setPoints(int points) { this.points = points; } /** * Gets the score for the game. * @return int The game score. */ public int getPoints() { return this.points; } /** * Counts the number of letters that have been guessed correctly during the game. * The number of correct letter guesses will be returned. * @return int The count of correct letters guessed. */ public int countCorrectLetters() { Set<Character> countedLetters = new HashSet<>(); int result = 0; if (!guesses.isEmpty()) { for (int i = 0; i < this.answer.length(); i++) { char currentChar = this.answer.charAt(i); // Check if the letter is correct and hasn't been counted yet if (guesses.contains(String.valueOf(currentChar)) && ! countedLetters.contains(currentChar)) { result += 1; // Increment result by 1 for each correct letter countedLetters.add(currentChar); } } } return result; } /** * Counts how often a letter occurs in the answer word. * @param letter the letter to be counted * @return int The count of occurrence of the letter. */ public int countLetters(char letter) { int count = 0; int i = 0; while (this.getAnswer().indexOf(letter, i) >= 0) {
i = this.getAnswer().indexOf(letter, i) + 1; count++; } return count; } /** * Constructs a new game with a random word. * @param name the name of the player */ public Game(String name) { this.name = name; setRandomWord(); setPoints(5); } /** * Constructs a new game with a given word and given name. * @param fixedWord the fixed word for the game * @param name the name of the player */ public Game(String fixedWord, String name) { this.name = "Anna"; this.answer = fixedWord; setPoints(10); } /** * Constructs a new game with no arguments, empty name and answer. */ public Game() { this.name = ""; this.answer = ""; setPoints(10); } /** * Initializes a game with the provided answer and player name. * @param answer the answer word for the game * @param name the name of the player */ public void initGame(String answer, String name) { this.name = name; this.answer = answer; this.gameStatus = 0; this.guesses.clear(); this.answers.clear(); setPoints(10); } /** * Checks if the guess made is correct, should ignore upper/lower case. * Should give points based on made guess. * Method returns a double, number of the double has different meanings * 0 Correct guess * 1.x Letter is in the word, x represents the number of times the letter is in the word * 2.0 Guess has correct length
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
* 2.1 Guess is too long (only if it was a word) * 2.2 Guess is too short (only if it was a word) * 3.0 Guess is partially included in the word (only if it was a word), * given instead of 2.2 if it is a partial word * 4.0 This guess was already used * 4.1 Guess includes symbols, numbers (not just letters or one letter) * 5. After 10 guesses the game ends and is set to game over * 5.1 If the player keeps guessing even though the status is not InProgress * The returned answer and the guess needs to be * added to the respective lists for tracking. * If letter: * Return 1.NumOfOccurrence, 1.0 for the letter not being in the word, * 1.1 for being in there once etc. * Add points according to NumOfOccurance * If word (go by this order of checks): * If word is correct return 0.0 and add points based on the * length of the word (e.g. dog - 3 points, horse - 5 points), set game status to won * If word is incorrect but has correct length return 2.0 and add 1 point * If word is incorrect and is partially included in the word return 3.0 and add 2 points * If word is too long or too short return 2.1, 2.2 accordingly and * reduce points based on how off the word is (e.g. how many letters off) * For either: * Guess was already used, reduce points by 2 and return 4.0 (checked before 4.1 error). * Guess still counts toward made guesses. * Guess includes numbers/symbols etc. (so more than just letters) * reduce points by 3 and return 4.1, * the guess is still added to the list of guesses * Score can also be negative, that is no problem. * When the player guessed 10 times and did not guess the word * set the game to game over (status) and * return 5.0 (no matter if there was another error). * If the player guesses again, even though game status * is won or game over return 5.1. * @param guess the users guess * @return double returns the appropriate number */ public double makeGuess(String guess) { if (gameStatus == 1 || gameStatus == 2) { // Game is already won or over, return 5.1 return 5.1; } if (guess == null || guess.isEmpty() || !guess.matches("[a-zA-Z]+")) { // Guess includes symbols, numbers (not just letters or one letter) // Reduce points by 3 and return 4.1 points -= 3; guesses.add(guess); // Add the guess to the list return 4.1; } guess = guess.toLowerCase(); // Convert guess to lowercase if (guesses.contains(guess)) { // Guess was already used, reduce points by 2 and return 4.0 points -= 2; guesses.add(guess); // Add the guess to the list
return 4.0; } // Convert answer to lowercase for case-insensitive comparison String lowerCaseAnswer = answer.toLowerCase(); if (guess.equals(lowerCaseAnswer)) { // Word is correct, return 0.0 and add points based on the length of the word gameStatus = 1; // Game won points += guess.length(); guesses.add(guess); // Add the guess to the list return 0.0; } if (guess.length() == 1) { // Check if the guessed letter is in the word if (lowerCaseAnswer.contains(guess)) { // Letter is in the word, return 1.NumOfOccurrence int numOfOccurrences = countLetters(guess.charAt(0)); points += numOfOccurrences; // Add points according to NumOfOccurrence guesses.add(guess); // Add the guess to the list return 1.0 + numOfOccurrences * 0.1; } else { // Letter is not in the word, return 1.0 and add the guess to the list points -= 1; guesses.add(guess); return 1.0; } } // Incorrect word guess if (lowerCaseAnswer.contains(guess)) { // Partial word is included in the target word, return 3.0 and add 2 points int partialWordPoints = 2; points -= partialWordPoints; guesses.add(guess); // Add the guess to the list // Check if this is the 10th incorrect guess if (guesses.size() == 10) { gameStatus = 2; // Game over } return 3.0; } else if (guess.length() > lowerCaseAnswer.length()) { // Word is too long, return 2.2 and reduce points based on how off the word is points -= (guess.length() - lowerCaseAnswer.length()) * 2; guesses.add(guess); // Add the guess to the list return 2.2; } else { // Incorrect word with correct length, return 2.0 and add the guess to the list points -= 2; // Deduct points for incorrect word // Check if this is the 10th incorrect guess if (guesses.size() == 10) {
gameStatus = 2; // Game over } return 2.0; } } /** * Pulls out a random animal and sets it as answer. */ public void setRandomWord() { String[] animals = {"dog", "horse", "pony", "cat", "lion", "bear","lioncub" }; int randomNum = 0; randomNum = (int) (Math.floor(Math.random() * (100 - 2 + 1) + 2) % animals.length); this.answer = animals[randomNum]; } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help