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)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\n*** Winner!!! ***");
        }
        else
        {
            System.out.println("\n*** 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();
    }
}

Add a comment
Know the answer?
Add Answer to:
Java - Data structures and algorithms VI. Matching Game Consider a matching game in which you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game si...

    Project 2 – Memory Match Game Purpose This Windows Classic Desktop application plays a simple matching game. The game simulates a card game where the cards a placed face down and the player flips over pairs of cards in an attempt to find matching cards.   Program Procedure Display a 4x4 grid of “face down” cards. Assign the letters A through H randomly to the cards in pairs. Allow the user to click on a card to “flip” it over and...

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • java In this project you will implement a trivia game. It will ask random trivia questions,...

    java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • 2 A Game of UNO You are to develop an interactive game of UNO between a...

    2 A Game of UNO You are to develop an interactive game of UNO between a number of players. The gameplay for UNO is described at https://www.unorules.com/. Your program should operate as follows. 2.1 Setup 1. UNO cards are represented as variables of the following type: typedef struct card_s { char suit[7]; int value; char action[15]; struct card_s *pt; } card; You are allowed to add attributes to this definition, but not to remove any. You can represent colors by...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT