CS project doubt
CS project doubt
I. Objective:
II. Problem:
1. Validating Player Input
Problem: Players might enter invalid guesses (e.g., words with incorrect length,
non-alphabetic characters, or repeated letters).
Solution:
Invalid guesses trigger clear error messages (e.g., "Invalid guess: Use 5
unique letters!")
Used Java’s Instant and Duration classes to calculate time elapsed during
gameplay.
3. Ensuring Replayability
CS Project 1
Problem: Repetitive gameplay reduces engagement.
Solution:
Used Random to randomly select a new secret word for each game
session.
1. Word Bank: it has a large collection of 200+ five-letter words for longer or
repeated gameplay.
3. Hints System: Feedback after each guess showing the number of hogs
(correct letters in correct spots) and pigs (correct letters in wrong spots).
The game should encourage the user to come and play again as it not only
recreational but also an educational game that helps you build your vocabulary
as well as your logical reasoning skills.
IV. Algorithm:
0. Run the program.
2. Load a dictionary of valid 5-letter words and randomly select a secret word.
4. If limited guesses, ask for the maximum number of attempts and validate
input.
CS Project 2
Get the player’s guess and validate it (5 letters, unique characters).
Break the loop if the word is guessed or max guesses are exceeded.
VI. Code:
import java.util.**;
import java.time.**;
CS Project 3
etWord) {
/* Calculates the number of pigs and hogs for a given g
uess and puts it in an array */
int pigs = 0;
int hogs = 0;
String[] dictionary = {
"about", "adorn", "amber", "angel", "angle", "appl
e", "arise", "atlas", "awake",
"badge", "balmy", "basin", "beach", "beast", "blots", "blus
h", "boast", "brace", "brave",
"bride", "brisk", "broad", "brush", "camel", "candy", "catc
h", "charm", "chill", "chime",
"clean", "climb", "clamp", "cloth", "cloud", "crane", "craf
CS Project 4
t", "crisp", "dance", "daisy",
"delta", "diver", "donor", "draft", "drain", "drift", "drow
n", "eagle", "early", "earth",
"elbow", "elite", "epoch", "equip", "erase", "event", "fair
y", "false", "fancy", "farce",
"fault", "feast", "flare", "flame", "flank", "flora", "flut
e", "frame", "fresh",
"frost", "fruit", "gamer", "giant", "glare", "glide", "glob
e", "glory", "grace", "grain",
"grand", "grape", "grill", "grind", "grove", "guest", "guil
t", "haiku", "hatch", "heart",
"hoist", "honey", "horse", "house", "hover", "human", "humo
r", "index", "inlet", "ionic",
"irate", "irony", "ivory", "jewel", "joker", "jolly", "jump
s", "jumbo", "kayak", "karma",
"kayak", "kneel", "knack", "knave", "knife", "knock", "latc
h", "laugh", "lemon", "liver",
"logic", "loyal", "lodge", "lunar", "lumen", "magic", "majo
r", "march", "marsh", "medal",
"minor", "mirth", "moist", "molar", "mover", "music", "naiv
e", "noble", "north", "novel",
"nudge", "nylon", "oasis", "ocean", "octal", "omega", "oper
a", "optic", "orbit", "ounce",
"parka", "pearl", "petal", "phase", "phone", "pivot", "plan
t", "plaza", "plead", "plume",
"plush", "poise", "pouch", "pride", "prism", "proud", "puls
e", "quack", "quail", "quake",
"queen", "quiet", "quirk", "quota", "quote", "radar", "ranc
h", "raise", "raven", "razor",
"reach", "relay", "ridge", "rider", "river", "robot", "rogu
e", "rough", "route", "royal",
"scale", "scary", "scene", "scent", "shear", "shine", "shoc
k", "shore", "short", "shout",
"slice", "solar", "sound", "space", "spice", "spike", "squa
d", "stack", "stage", "stain",
"stand", "stare", "storm", "stout", "strip", "table", "talo
n", "teach", "thorn", "tiger",
"tough", "trail", "trend", "tribe", "trial", "tulip", "ulce
CS Project 5
r", "ultra", "uncle", "under",
"unity", "urban", "valve", "vapor", "vault", "vigor", "vivi
d", "vixen", "vocal", "voice",
"waste", "water", "whale", "whirl", "widow", "widen", "wide
r", "windy", "woven", "worry",
"xenon", "xeric", "xerox", "xylon", "yield", "young", "yach
t", "youth", "zebra", "zesty",
"zonal", "zesty", "zonal"};
int gameMode = 0;
int maxGuesses = 0;
boolean validMode = false;
while (!validMode) {
try {
System.out.print("Enter 1 or 2: ");
gameMode = monkey.nextInt();
if (gameMode == 1 || gameMode == 2) {
validMode = true;
} else {
System.out.println("Invalid choice. Please
enter 1 or 2.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter
a number (1 or 2).");
monkey.next(); // Clear the invalid input
}
CS Project 6
}
if (gameMode == 2) {
boolean validGuesses = false;
while (!validGuesses) {
try {
System.out.print("Enter the maximum number
of guesses: ");
maxGuesses = monkey.nextInt();
if (maxGuesses > 0) {
validGuesses = true;
} else {
System.out.println("Please enter a posi
tive number.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please e
nter a positive number.");
monkey.next(); // Clear the invalid input
}
}
}
int GuessCount = 0;
boolean wordGuessed = false;
while (true) {
if (gameMode == 2 && GuessCount >= maxGuesses) {
System.out.println("You have reached the maximu
m number of guesses."+"The word was "+SecretWord+". Better
luck next time!");
break;
}
try {
CS Project 7
System.out.println("Enter your guess:");
String input = monkey.next().toLowerCase();
if (!isValidGuess(input)) {
throw new IllegalArgumentException("Invalid
guess. Enter a 5-letter word with unique letters.");
}
GuessCount++;
int[] actualPigsnHogs = pigznhogzcalc(input, Se
cretWord);
int pigs = actualPigsnHogs[0];
int hogs = actualPigsnHogs[1];
if (hogs == 5) {
wordGuessed = true;
break; // Exit the loop once the player gue
sses the right word (hogs == 5)
}
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println("An unexpected error occurre
d. Please try again.");
}
}
if (wordGuessed) {
System.out.println("Good job! You have guessed the
word \\"" + SecretWord + "\\" in " + GuessCount + " trie
CS Project 8
s.");
System.out.println("Time taken: " + timeElapsed.toM
inutes() + " minutes.");
} else if (gameMode == 1) {
System.out.println("Better luck next time!");
}
}
}
CS Project 9