Question: Modify the Anagrams program to enforce the constraint that words must be at least four letters long. (This is not necessary if all shorter words
Modify the Anagrams program to enforce the constraint that words must be at least four letters long. (This is not necessary if all shorter words are removed from the file words.txt.)
import java.util.Scanner;
/** The game of Anagrams. */
public class Anagrams {
/** For reading from the console. */
public static final Scanner INPUT = new Scanner(System.in);
/** Letters in the bag. */
private LetterCollection bag;
/** Letters in the pool. */
private LetterCollection pool;
/** Large set of legal words. */
private Set
/** Words scored by player 1. */
private Set
/** Words scored by player 2. */
private Set
/**
* Read in the dictionary from the file "anagram-words" and create
* the letters.
*/
public Anagrams() {
bag = new LetterCollection(true);
pool = new LetterCollection(false);
words = new OrderedList
try {
Scanner input = new Scanner(new java.io.File("words.txt"));
while (input.hasNextLine()) {
words.add(input.nextLine());
}
} catch (java.io.IOException e) {
e.printStackTrace();
System.exit(1);
}
words1 = new OrderedList
words2 = new OrderedList
}
/** Play until someone gets five words. */
public void play() {
while (true) {
System.out.println("PLAYER 1 TO PLAY");
playTurn(words1, words2);
if (words1.size() == 5) {
System.out.println("Player 1 wins!");
return;
}
System.out.println("PLAYER 2 TO PLAY");
playTurn(words2, words1);
if (words2.size() == 5) {
System.out.println("Player 2 wins!");
return;
}
}
}
/** Play one turn for the specified player. */
public void playTurn(Set
pool.add(bag.draw());
System.out.println("Letters in pool:\n" + pool);
System.out.println("Player 1's words: " + words1);
System.out.println("Player 2's words: " + words2);
System.out.print("Your play: ");
String play = INPUT.nextLine();
int spaceIndex = play.indexOf(' ');
if (spaceIndex != -1) { // Stolen word
String word = play.substring(0, spaceIndex);
if (!(words.contains(word))) {
System.out.println("Sorry, " + word + " is not a word.");
} else {
String stolen = play.substring(spaceIndex + 1, play.length());
player.add(word);
opponent.remove(stolen);
pool.add(stolen);
pool.remove(word);
}
} else if (!(play.equals(""))) { // Regular play
if (!(words.contains(play))) {
System.out.println("Sorry, " + play + " is not a word.");
} else {
player.add(play);
pool.remove(play);
}
}
System.out.println();
}
/** Create and play the game. */
public static void main(String[] args) {
Anagrams game = new Anagrams();
System.out.println("Welcome to Anagrams.");
System.out.println();
System.out.println("To make a word from the pool, enter it.");
System.out.println("To steal a word, enter the new word, a"
+ " space, and the word being stolen.");
System.out.println("To pass, just hit return.");
System.out.println();
game.play();
}
}
Step by Step Solution
3.38 Rating (170 Votes )
There are 3 Steps involved in it
Here put constarint while inputhasN... View full answer
Get step-by-step solutions from verified subject matter experts
Document Format (1 attachment)
1019-B-C-A-C-P-A(4740).docx
120 KBs Word File
