Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, we will create a memory game called \"Mind Game in this game,

ID: 3708380 • Letter: I

Question

In this exercise, we will create a memory game called "Mind Game in this game, even number of tiles are placed face-down in rows on a "Mind Game Board. Each tile contains an image. For each tile, there is exactly one other tile with the same image. The player is to pick a tile, see which image it has, and try to find its match from the remaining face-down tiles. If the player fails to turn over its match, both tiles are turned face-down and the player attempts to pick a matching pair again. The game is over when all tiles on the board are turned face-up. In this implementation of a simplified version of Mind Game, Images will be strings chosen from a FixdLenStringList. (Use the FixdLenStringList class from exercise 1 The mind game board will contain even number of Tiles. The board will be implemented as a one-dimensional array of Tiles . . For example, if the size of the mind game board is requested to be 4, then the board will have 16 tilles. The one dimension array, gameBoard, can be viewed as a 4-by-4 mind game board as ollows: gameBoa gameBoard ganeBoard[6 gameBoard[7] gameBoard8) gameBoard[9 gameBoard 10] gameBoard[11] An incomplete implementation of the Tile and Board dasses are provided with this exercise Tie java, Boord java). You should examine these classes and complete them with the folowing methods. To mplement the methods, see the specifications provided in the comments of the methods a) Write the implementation of the Board method fi11Board, which will randomly fill the mind game board with Tiles whose images (strings) are randomly chosen from the strings contained in the FixdLenStringList. A tile image that appears on the board appears exactly twice on the board b] write the implementation for the Board method lookAtTi le. Tis method will call the appropriate method of the Tile class to turn the Tile face-up. c) Write the implementation for the Board's method checkMatch. This method will check whether the tiles in its two integer parameter positions on gameBoard have the same image. If they do, the tilles will remain face-up. If they have different images, then tiles will be turned face-down (See the comments that were given in the problem specification). d) Complete the implementation for print Board sothat the Mind Game board is printed as described in the comment. For example, you should call the Board method format to right-justify the printing of the Tile image or the printing of the Tile position. An example of printBoard for a 4 by 4 mind game board that is partially solved is shown below after 4 matches have been found. The F?dLenStr ingLi t passed as a parameter to Board constructor contains strings of length 3 fox dog 10 12 13 15

Explanation / Answer

Code

import java.util.Arrays;

public class FixdLenStringList {

private String[] possibleStrings;
   private int strLength;

public FixdLenStringList(int len) {
       this.strLength = len;
       this.possibleStrings = new String[0];
   }

public int getStringLength() {
       return this.strLength;
   }
  
public String getString(int x) {
       if ((0 <= x) && (x < this.possibleStrings.length))
           return this.possibleStrings[x];
       else
           return null;
    }

public boolean found(String key) {
       for (String str : this.possibleStrings) {
           if (str.equals(key)) // Compare key with the strings in the list
               return true;
       }

       return false;
   }

public void addString(String entry) {
if ((entry.length() == this.strLength) && !(found(entry))) {
           int len = this.possibleStrings.length;
this.possibleStrings = Arrays.copyOf(this.possibleStrings, len + 1);

this.possibleStrings[len] = entry;
       }
   }

public String removeRandomString() {
       int len = this.possibleStrings.length;

int randomNum = ((int) (Math.random() * 10)) % len;

String str = this.possibleStrings[randomNum];

for (int i = randomNum; i < (len - 1); i++) {
           this.possibleStrings[i] = this.possibleStrings[i + 1];
       }

this.possibleStrings = Arrays.copyOf(this.possibleStrings, len - 1);

return str;
   }

@Override
   public String toString() {
       if (this.possibleStrings.length> 0) {
           StringBuffer sb = new StringBuffer();
           sb.append(this.possibleStrings[0]);

           for (int i = 1; i < this.possibleStrings.length; i++) {
               sb.append(", " + this.possibleStrings[i]);
           }

           return sb.toString();
       } else
           return "";
   }
}

public class Tile {

   private String image;
   private boolean faceUP;

public Tile(String word) {
       this.image = word;
       this.faceUP = false;
   }

public String showFace() {
       if (this.faceUP)
           return this.image;
       else
           return "";
   }

public boolean isFaceUp() {
       return this.faceUP;
   }

public String getImage() {
       return image;
   }

@Override
   public boolean equals(Object other) {
if (this == other)
           return true;
      
       // null check
       if (other == null)
           return false;

       // type check and cast
       if (other instanceof Tile) {
           Tile another = (Tile)other;
           // field comparison
          
           return (this.getImage().equals(another.getImage()));
       }
       return false;
   }

public void turnFaceUp() {
       this.faceUP = true;
   }

public void turnFaceDown() {
       this.faceUP = false;
   }
}

import java.util.Random;

public class Board {

   // Instance variables
   private Tile[] gameBoard; // Mind Game board of Tiles
   private int size; // Number of Tiles on board
   private int rowLength; // Number of Tiles printed in a row
   private int numberOfTileFaceUp; // Number of Tiles face-up
   private FixdLenStringList possibleTileValues; // Possible Tile values

public Board(int n, FixdLenStringList list) {
       this.rowLength = n;
       this.size = n * n;
       this.gameBoard = new Tile[this.size];
       this.possibleTileValues = list;
       this.numberOfTileFaceUp = 0;

       // Fill the board
       fillBoard();
   }

private int getRandomPosition() {
       // Create random object to generate random numbers
       Random r = new Random();
       int pos = -1;
      
       do {
           pos = r.nextInt(this.size);
       } while (this.gameBoard[pos] != null);
      
       return pos;
   }

   /**
   * Randomly fills this Mind Game Board with tiles. The number of distinct
   * tiles used on the board is size/2. Any one tile image appears exactly
   * twice. Precondition: number of position on board is even,
   * FixdLenStringList contains at least size / 2 elements.
   */
   private void fillBoard() {
       // For each string in FixdLenStringList get two random position in the
       // Board
       int i = 0;
       while (this.possibleTileValues.getString(i) != null) {
           // Create Tile with image
           Tile newTile = new Tile(this.possibleTileValues.getString(i));

           // Generate two random positions which are not already occupied
           // and set newTile at that position
           int pos = getRandomPosition();
           this.gameBoard[pos] = newTile;
          
           pos = getRandomPosition();
           this.gameBoard[pos] = newTile;

           i += 1;
       }
   }

   /**
   * Precondition: 0 <= p < gameBoard.length. Precondition: Tile in position p
   * is face-down. Postcondition: After execution, Tile in position p is
   * face-up @param p the index of the tile to turn face-up
   */
   public void lookAtTile(int p) {
       this.gameBoard[p].turnFaceUp();
   }

   /**
   * Checks whether the Tiles in pos1 and pos2 have the same image. If they
   * do, the Tiles are turned face-up. If not, the Tiles are turned face-down.
   * Precondition: gameBoard[pos1] is face-up, gameBoard[pos2] is face-up
   *
   * @param pos1
   *            index in gameBoard, 0 <= pos1 < gameBoard. length @param pos2
   *            index in gameBoard, 0 <= pos1 < gameBoard. length
   */
   public void checkMatch(int pos1, int pos2) {
       Tile t1 = pickTile(pos1);
       Tile t2 = pickTile(pos2);

       if (t1.equals(t2)) {
           lookAtTile(pos1);
           lookAtTile(pos2);
           this.numberOfTileFaceUp += 2;
       } else {
           t1.turnFaceDown();
           t2.turnFaceDown();
       }
   }

   /**
   * Board is printed for the Player. If the Tile is turned face-up, the image
   * is printed. If the Tile is turned face-down, the Tile position is
   * printed.
   */
   public void printBoard() {

       final int PADDING = 3;
       int spacing = possibleTileValues.getStringLength() + PADDING;
       for (int i = 0; i < size; i++) {
           if ((i % rowLength) == 0)
               System.out.println();

           String display = this.gameBoard[i].showFace();
           if (!display.equals("")) // Tile is face-up
               System.out.print(format(display, spacing));
           else
               System.out.print(format(i, spacing));
       }
   }

   /**
   * Returns Tile in position pos. Precondition: 0 <= pos < gameBoard.length.
   *
   * @param pos
   *
   *            is index in gameBoard
   * @return tile in position pos, null otherwise
   */
   public Tile pickTile(int pos) {
       if (pos < 0 || pos >= gameBoard.length) {
           return null;
       }

       return gameBoard[pos];

   }

   /**
   * Right-justifies a number to a given number of places.
   *
   * @param number
   *            an integer to be formatted
   * @param p
   *            total number of characters in returned string
   * @return right-justified number with p places as a string
   */
   private String format(int number, int p) {
       String str = String.format("%1$" + p + "s", (number + ""));
       return str;
   }

   /**
   * Right-justifies a string to a given number of places.
   *
   * @param word
   *            a string to be formatted
   * @param p
   *            total number of characters in returned string
   * @return right-justified word with p places as a string
   */
   private String format(String word, int p) {
       String str = String.format("%1$" + p + "s", word);
       return str;
   }

   /**
   * Checks whether all tiles are face-up.
   *
   * @return true if all tiles are face-up; false otherwise
   */
   public boolean allTilesUp() {
       return (this.numberOfTileFaceUp == this.size);
   }
}

import java.util.Scanner;

/**
* This client class play starts the mind game and controls user input.
*/

public class MindGameClient {

   public static void main(String[] args) {

       // Create fixed length strings
       FixdLenStringList fl = new FixdLenStringList(3);
       fl.addString("cat");
       fl.addString("dog");
       fl.addString("pig");
       fl.addString("eel");
       fl.addString("pet");
       fl.addString("net");
       fl.addString("ted");
       fl.addString("car");

       // Create board
       Board b = new Board(4, fl);

       // Scanner to get user input
       Scanner kb = new Scanner(System.in);
       while (!b.allTilesUp()) {
           // Display board
           b.printBoard();
           System.out.print(" Choose positions:");

           // Get positions from the user
           int n1 = kb.nextInt();
           int n2 = kb.nextInt();
          
           // Get tiles at n1, n2
           Tile t1 = b.pickTile(n1);
           Tile t2 = b.pickTile(n2);
          
           boolean isValidSelection = true;
          
           // Check if n1 and n2 are not same
           if (n1 != n2) {
               // Check if n1, n2 is a valid selection
               if (t1 != null && t2 != null) {
                  
                   // Check if tiles at n1 and n2 are not already face up
                   if (!t1.isFaceUp() && !t2.isFaceUp()) {
                       System.out.print(t1.getImage() + " , ");
                       System.out.println(t2.getImage());
                       System.out.println("");
                      
                       // Check if both tiles match
                       b.checkMatch(n1, n2);
                      
                   } else
                       isValidSelection = false;
               } else
                   isValidSelection = false;
           } else
               isValidSelection = false;
          
           if (!isValidSelection)
               System.out.println("Invalid Selection!");
       }
       // Print the final board
       b.printBoard();
       // Close scanner
       kb.close();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote