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

Java - Data structures and algorithms VI. Matching Game Consider a matching game

ID: 3843592 • Letter: J

Question

Java - Data structures and algorithms

VI. Matching Game Consider a matching game in which you have a list of random integer values between 10 and 99. You remove from the list any pair of consecutive integers that match. If first integer has digits xly1 and the second integer has digits x2y2 the match is found if any ofthe following is true xi is the same as x2 x1 is the same as y2 yl is the same as x2 yl is the same as y2 If all integer values are removed, you win. You are allowed to shuffle the integer values up to 5 times to increase the probability of finding more matches For example consider the following sequence of integers 70 82 43 23 89 12 43 84 93 17 The pair 70 and 82 does not match in either digit and so cannot be removed, next check 82 and 43, no match either. Next check 43 and 23, there is a match, so both values are removed. Continue checking for pairs from 89 which is the value after the removed pair. Once you finish the first pass the following sequence remains 70 82 89 12 93 17 Now return to the beginning of the list and check the pairs again. After the second pass the following sequence remains 70 12 93 17 Now return to the beginning of the list and check for the pairs again. This time no matches were found, so shuffle the list and try again. You are allowed to shuffle maximum 5 times Your Task: 1. Write a program that simulates this game (the skeleton is provided for you) a. initializeList generates numbers two-digit integers between 10 and 99 inclusively. The generated integers must be stored in ArrayList theNumbers using an instance of Listlterator b. Then using another instance of Listlterator, scan the list and remove matching pairs of values c. After each pass use an instance of Iterator to display the remaining content of the list 2. A sample run of the program and a UML diagram are provided 3. Make sure that the output is correct (see Sample Runs below)

Explanation / Answer

import java.util.*;


public class MatchingGame
{
    private ArrayList<Integer> theNumbers;
    final int MAX_NUMBER_OF_SHUFFLES = 5;
    final int MIN_NUMBER = 10;
    final int MAX_NUMBER = 99;

    public MatchingGame(int numberAmount)
    {
        this.theNumbers = new ArrayList<>();
        initializeList(numberAmount);
    }

    /**
     * Initialize the list with the count of random 2 digit numbers.
     *
     */
    private void initializeList(int numberAmount)
    {
        Random generator = new Random();
        ListIterator<Integer> iter = this.theNumbers.listIterator();

        // STEP 1
        // generate the numbers and add them to theNumbers using iterator
        while (iter.hasNext())
        {

        }
    }

    /**
     * See whether two numbers are removable.
     * @param first the first 2 digit integer value
     * @param second the second 2 digit integer value
     * @return true if the first and second match
     */
    private boolean removablePair(Integer first, Integer second)
    {
        // STEP3
        // implement this method
        return false;
    }

    /**
     * Implements one pass when called by play method
     * Scans over the list and removes any pairs of values that are removable.
     * @return true if any pair of integers was removed
     */
    private boolean scanAndRemovePairs()
    {
        boolean removedAPair = false;
        ListIterator<Integer> scan = this.theNumbers.listIterator();

        Integer first = null;
        Integer second = null;

        // STEP4
        // implement the method
        // this method calls helper method removablePair to see if there is a match
        return removedAPair;
    }

    private void displayTheNumbers()
    {
        // STEP2
        // using an instance of Iterator display the content of theNumbers
        // notify the user if the list is empty
    }

    public void play()
    {
        int pass = 0;
        int numberOfShuffles = 0;
        boolean repeat;

        System.out.println("Starting with: ");
        displayTheNumbers();

        do
        {
            repeat = false;
            while (scanAndRemovePairs())
            {
                pass++;
                System.out.println("The list after pass #" + pass);
                displayTheNumbers();
            }
            System.out.println("No more pairs to remove.");
            // do we have at least 3 numbers in the list?
            if (this.theNumbers.size() > 2)
            {
                if (numberOfShuffles < MAX_NUMBER_OF_SHUFFLES)
                {
                    numberOfShuffles++;
                    System.out.println("Shuffling the numbers.");
                    Collections.shuffle(this.theNumbers);
                    System.out.println("The list after shuffling #" + numberOfShuffles);
                    displayTheNumbers();
                    repeat = true;
                }
            }
        }while(repeat);

        if (this.theNumbers.isEmpty())
        {
            System.out.println(" *** Winner!!! ***");
        }
        else
        {
            System.out.println(" *** Better luck next time! ***");
        }
    }

    public static void main(String[] args)
    {
        final int MIN_NUMBER_OF_ELEMENTS = 10;
        Scanner scan = new Scanner(System.in);
        int numberAmount;

        do
        {
            System.out.println("How many numbers (no less than " + MIN_NUMBER_OF_ELEMENTS + ")?");
            numberAmount = scan.nextInt();
        }while(numberAmount < MIN_NUMBER_OF_ELEMENTS);

        MatchingGame game = new MatchingGame(numberAmount);
        game.play();
    }
}

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