Question: The program need to be done in Java Problem 1 Consider a solitaire matching game in which you have a list of random integer values

The program need to be done in Java

Problem 1

Consider a solitaire 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 whose first or second digits match. If all values are removed, then you win.

For example, consider the following sequence of 10 integers:

10 82 43 23 89 12 43 84 23 32

The integers in the pair 10 and 82 do not match in either digit and so cannot be removed. However, the integers in the pair 43 and 23 match in the second digit and are removed, leaving the following sequence:

10 82 89 12 43 84 23 32

Continue checking for pairs from 89, the value after the removed pair. No other pairs have matching integers. Now return to the beginning of the list and check the pairs. The integers in the pair 82 and 89 match in the first digit and can be removed:

10 12 43 84 23 32

No other pairs can be removed, so we lose.

Write a program that simulates this game.

Implement the method

public static void initializeList(ArrayListWithListIterator theList)

which generates 40 random two-digit integers and place them in an instance of ArraListWithListIterator, using an instance of ListIterator. (20 points)

Implement the method

public static boolean scanAndRemovePairs(ArrayListWithListIterator theList)

which, using an iterator, scans the list and removes matching pairs of values (20 points).

To check whether two integers can be removed, implement the method

public static boolean removable(Integer x, Integer y)

which returns true if the 2-digit integers x and y share at least one digit in common (20 points).

Implement the method

public static void displayList(ArrayListWithListIterator theList)

which displays the contents of theList using an iterator (20 points).

Implement the method

public static void main(String args[])

to create and initialize a ArraListWithListIterator by calling initializeList and repeatedly calling scanAndRemovePairs until either the list is empty (print that the list is empty), or we cannot remove more pairs (print that no more pairs can be removed). After each pair is removed, call displayList to show the contents of the list. (20 points).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!