Question

In: Computer Science

using Java; In Scrabble, each player has a set of tiles with letters on them. The...

using Java;

In Scrabble, each player has a set of tiles with letters on them. The object of the game is to use those letters to spell words. The scoring system is complex, but longer words are usually worth more than shorter words. Imagine you are given your set of tiles as a string, like "quijibo", and you are given another string to test, like "jib". Write a method called canSpell that takes two strings and checks whether the set of tiles can spell the word. You might have more than one tile with the same letter, but you can use each tile only once.

Solutions

Expert Solution

  
   /**
   * Java method that takes as input 2 strings, representing the tiles and word and returns if the word can be spelled using the tiles
   * (each tile can be used only one)
   * @param tiles, letters currently the user have
   * @param word, letters of the word to check
   * @return, boolean, if word can be spelled using the tiles then return true else return false
   */
   public boolean canSpell(String tiles, String word)
   {
       // create an array of same size as number of letters in tiles, with all entries initialized to false
       boolean[] used = new boolean[tiles.length()];
       for(int i=0;i<used.length;i++)
           used[i] = false;
      
       boolean found;
      
       // loop over the word, character by character
       for(int i=0;i<word.length();i++)
       {
           found = false; // set found to false, as ith letter of word has not been found yet in tiles
          
           // loop over the tiles, to search for ith character of word in tiles
           for(int j=0;j<tiles.length();j++)
           {
               // jth letter of tiles = ith letter of word and jth letter has not been used
               if((word.toLowerCase().charAt(i) == tiles.toLowerCase().charAt(j)) && (!used[j]))
               {
                   // set jth letter used to true and set found to true and exit the loop
                   used[j] = true;
                   found = true;
                   break;
               }
           }
          
           if(!found) // if ith letter of word has not been found, return false as word cannot be spelled using the tiles
               return false;
       }
      
       return true; // all letters of word found in tiles, return true as word can be spelled using the tiles
   }


Related Solutions

Modify Java program to accept a telephone number with any numberof the letters, using the...
Modify Java program to accept a telephone number with any number of the letters, using the if else loop. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is -3, and the payoff for B is -2. Player B Strategy 1 Strategy 2 Player A Strategy 1 (-3,-2) (10,0) Strategy 2 (0,8) (0,0) How many pure strategy Nash equilibria does...
Below is a game between player A and player B. Each player has two possible strategies:...
Below is a game between player A and player B. Each player has two possible strategies: 1 or 2. The payoffs for each combination of strategies between A and B are in the bracket. For example, if A plays 1 and B plays 1, the payoff for A is 1, and the payoff for B is 0. Player B Strategy 1 Strategy 2 Player A Strategy 1 (1,0) (0,1) Strategy 2 (0,1) (1,0) How many pure strategy Nash equilibria does...
A set of strategies (one for each player) in which each player's strategy is the best option for that player, given the chosen strategy of the player's opponents
4. Game theory terminology Select the term that best describes each definition listed in the following table. DefinitionNash EquilibriumDominant Strategy CollusionPrisoners DilemmaPayoff MatrixPrisoners Dilemma GameA visual representation of a game showing all possible strategies for each player and all potential outcomes and payoffs A set of strategies (one for each player) in which each player's strategy is the best option for that player, given the chosen strategy of the player's opponents A player's best choice, if it exists, regardless of his or her opponent's strategyA...
Consider passwords of length 8 using symbols from the set of lower case letters: {a, b,...
Consider passwords of length 8 using symbols from the set of lower case letters: {a, b, c, ..., z}. (a) How many such passwords use the letter p exactly two times and the letter q exactly two times? To illustrate, both akpqbqop and quaqpoop satisfy this condition. Include a brief indication of your strategy. (b) How many such passwords use the letter p exactly once and the letter q exactly once and also have letters arranged so the p is...
Java OOP - how do I avoid using getter and setter method in player class for...
Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how Thank you ----------------------------------------------------------------------------------------------------------- package MainPackage; import java.util.ArrayList; public class SoccerTeam { private ArrayList<Player> list; public SoccerTeam(int maxSubmission) { list = new ArrayList<>(maxSubmission); } public boolean addPlayer(String newPlayer) { if(newPlayer == null || newPlayer.isEmpty() == true) { return false; } for(Player player : list) { if(player.getName() == newPlayer) { return...
Introduction to logic: Translate each argument using the letters provided and prove the argument valid using...
Introduction to logic: Translate each argument using the letters provided and prove the argument valid using all eight rules of implication. Sam will finish his taxes and Donna pay her property taxes or Sam will finish his taxes and Henry will go to the DMV. If Sam finishes his taxes, then his errands will be done and he will be stress-free for a time. Therefore, Sam will finish his taxes and he will be stress-free for a time. (S, D,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...
Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has...
1) A set of license plates has four letters followed by 4 digits. (a) In how...
1) A set of license plates has four letters followed by 4 digits. (a) In how many ways can a license plate be made with four letters followed by 4 digits? (Letters or digits may be repeated.) (b) In how many ways can a license plate be made with four different letters followed by 4 different digits? (c) If a license plate is made with four letters followed by 4 digits, what is the probability that it has no repeated...
A Scrabble game has identical copies of each letter tile numbering as follows: A-9, B-2, C-2,...
A Scrabble game has identical copies of each letter tile numbering as follows: A-9, B-2, C-2, D-4, E-12, F-2, G-3, H-2, I-9, J-1, K-1, L-4, M-2, N-6, O-8, P-2, Q-1, R-6, S-4, T-6, U-4, V-2, W-2, X-1, Y-2, Z-1 and Blanks-2. Seven tiles are randomly sampled without replacement from the above 100 tiles.If a letter appears exactly once in this seven-tile sample, we call it a singleton in this problem. a. Consider a letter that has n copies in total...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT