Modify the Anagrams program to enforce the constraint that words must be at leas
ID: 3567809 • Letter: M
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 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<String> words;
/** Words scored by player 1. */
private Set<String> words1;
/** Words scored by player 2. */
private Set<String> words2;
/**
* 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<String>();
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<String>();
words2 = new OrderedList<String>();
}
/** 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<String> player, Set<String> opponent) {
pool.add(bag.draw());
System.out.println("Letters in pool: " + 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();
}
}
Explanation / Answer
Here put constarint
while (input.hasNextLine()) {
String check=input.nextLine();
if (check.length>=4){
words.add(input.nextLine());
}
else
{
System.out.println("Anagrams must contain atleast four characters");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.