Tiny Scrabble Game This problem to implement a small application which simulates
ID: 672980 • Letter: T
Question
Tiny Scrabble Game
This problem to implement a small application which simulates the game of Scrabble. Your specific task is to write two classes from scratch (LTile and RandomBag) and complete an application class (tinyScrGame).
The class should contain three instance data members -- a char for the letter, an int for the letter's value, and an int for ID.
The ID number of a tile should be determined/assigned by a class/static data member (which should be also be private) which keeps track of the number of LTile objects produced. Look at the example shown in the class, class Employee.
The class should contain the following instance methods:
Two constructors -- one with no parameter, and another with two parameters (a char and an int). The one with no parameter should initialize the letter with '?' and the value with 0, while the other constructor should initialize the data members with the parameters. In both constructors, the ID should be assigned by looking at the class/static member.
Three get methods -- one for all three instance members.
public boolean equals(Object obj) -- compares the ID's only (not letters or values) to determines the equality.
public String toString() -- returns a string of the form "[13: B,3]" (i.e., including [ : , ]).
If you like to test the class, I have written this main(). You can paste it in the code and compare your output with this output.
Class RandomBag -- a generic bag container which allows 'randomPick' of an element
This class should look like (only the top part shown):
The constructor with no parameter should initialize 'bag' with a new ArrayList<E> object and 'rand' with a new Random object with no parameter, while the constructor with an int parameter should initialize 'bag' with a new ArrayList<E> object but 'rand' with a new Random object with the seed value using the parameter.
In addition to constructors, the class should contain the following instance methods:
public int size() -- returns the size of the bag
public boolean isEmpty() -- returns true/false if the bag is/is not empty.
public void add(E element) -- adds the parameter element in the bag
public E randomPick() -- randomly selects an element using the random number generator 'rand' and removes that element from the bag, and returns the element.
(Hint: You can use the method 'remove(int k)' in ArrayList.)
public Iterator<E> iterator() -- obtains an iterator for the bag, and returns it. This method needs to be written because the class implements the Iterable<E> interface.
If you like to test the class, I have written this main(). You can paste it in the code and compare your output with this output.
Class tinyScrGame -- a small simulation of Scrabble, "tinyScrGame.java" (partially filled)
This game uses the real Scrabble dictionary and tiles. A game repeatedly selects a given number of tiles (say n) randomly and see if any permulation of the tiles (and the strings made from the tiles) makes a valid n-letter Scrabble word. At the end, it displays the average score of the words (but including those that were not a valid Scrabble word).
Each instance of the class is a game, and utilizes these instance variables:
''dictionary' is a set of strings, which are words in the (real) Scrabble dictionary (read in from a file "scrabble-ospd.txt" -- you should put in at an appropriate folder in your system).
'tilebag' is a RandomBag which contains 100 LTile objects corresponding to the Scrabble letter tiles (including 2 blank tiles). ''nletter' is the length of the words which is looked for in this particular game. For example, if this is set to 4, the game examines all 4-letter words (and no other words).
Your task is to fill in the method play(int k)
This method plays 'k' rounds of tile drawing. For each round, it randomly draws 'nletter' number of tiles from the tile bag, creates a string from the tiles), generates all permutations of the string, and prints those which are in the Scrabble dictionary. Finally the method prints the average word score -- the average of the k word scores.
You figure out the steps and schema by yourself.
One hint is that you can call the method 'permute' written in the partial code with a word string to obtain the permutations (public static ArrayList<String> permute(String s)).
Also you can use toUpperCase() method in Java String to obtain a string in which the characters in the original string were changed to upper-case.
Currently there is one game created in main():
As you notice, the constructor is specifying this game to look for 4-letter words (the second parameter) and the random seed of 5. Then the game is called to play 10 rounds. The output of the game is shown at the bottom.
You can modify those values and enjoy. Also if you like to extend the game in any way, please do so -- and document/write the changes you made at the top of the file. One idea is to utilize blank tiles.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Test {
public static int calculate(String word){ //calculate score
int score = 0;
String answerArr [] = new String[word.length()];
//calculation part here
for (int i = 0; i < word.length(); i++){
answerArr[i] = word.substring(i, i+1);
if (answerArr[i].equalsIgnoreCase("a")){
score += 1;
}
else if(answerArr[i].equalsIgnoreCase("c")){
score += 2;
}
else {
score += 0;
}
}
return score;
}
public static void main(String args[]){
int round = 1,totalScore=0,score;
File file = new File("scramble.txt");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String word = sc.nextLine();
score = calculate(word);
//calcualting score per each word and printing to console
totalScore = totalScore + score;
System.out.println("Round "+round+" ==>"+word+" score: "+score);
round++;
}
//prinitng average
System.out.println("Average score: "+totalScore/(round-1));
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.