. JAVA PROGRAMMING VI Matching Game Consider a matching game in which you have a
ID: 3859397 • Letter: #
Question
.
JAVA PROGRAMMING
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 x1v1 and the second integer has digits x2v2 the match is found if any of the following is true: * xl is the same as x2 * xl is the same as v2 * vl is the same as x2 * y1 is the same as y2 If all integer values are removed, vou 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 8 9, 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 vou): initializeList - generates numbers - two-digit integers between 10 and 99 inclusivelv. The generated integers must be stored in ArrayList theNumbers using an instance of Listlterator. Then using another instance of Listlterator, scan the list and remove matching pairs of values. After each pass use an instance of Iterator to display the remaining content of the list. a. b. c. A sample run of the program and a UML diagram are provided. Make sure that the output is correct (see Sample Runs below) 2. 3.Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.july;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
/**
*
* @author Sam
*/
public class MatchingGame {
ArrayList<Integer> list;
public MatchingGame() {
list = new ArrayList<>(10);
}
private void initializeList(int n) {
ListIterator<Integer> iterator = list.listIterator();
for (int i = 0; i < n; i ++)
iterator.add((int) (Math.random() * Integer.MAX_VALUE)% 90 + 10);
System.out.println("Starting with");
printList();
}
private boolean removePairs() {
ListIterator<Integer> iterator = list.listIterator();
int prev = iterator.next();
int curr;
boolean removed = false;
while (iterator.hasNext()) {
curr = iterator.next();
if (validPair(prev,curr)) {
iterator.remove();
iterator.previous();
iterator.remove();
System.out.println("removed " + prev + " " + curr);
if (iterator.hasNext())
curr = iterator.next();
removed = true;
}
prev = curr;
}
return removed;
}
private void shuffle() {
Collections.shuffle(list);
}
private void printList() {
System.out.println(list.toString());
}
private static boolean validPair(int prev, int curr) {
if (prev%10 == curr%10)
return true;
if (prev%10 == curr/10)
return true;
if (prev/10 == curr%10)
return true;
return prev/10 == curr/10;
}
public static void main(String[] args) throws IOException {
MatchingGame game = new MatchingGame();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
do {
System.out.println("How many numbers? (no less than 10)?");
n = Integer.parseInt(br.readLine());
}while (n < 10);
game.initializeList(n);
int pass = 0;
int shuffled = 0;
while (!game.list.isEmpty() && shuffled < 5) {
pass ++;
boolean flag = game.removePairs();
if (flag){
System.out.println("The list after pass #" + pass);
game.printList();
}
else {
System.out.println("No more pairs to remove");
game.shuffle();
System.out.println("Shuffling list");
System.out.println("");
shuffled++;
}
}
if (game.list.isEmpty())
System.out.println("*** WINNER ***");
else
System.out.println("*** BETTER LUCK NEXT TIME ***");
}
}
Output:
run:
How many numbers? (no less than 10)?
10
Starting with
[12, 18, 24, 55, 96, 15, 46, 25, 47, 59]
removed 12 18
The list after pass #1
[24, 55, 96, 15, 46, 25, 47, 59]
No more pairs to remove
Shuffling list
removed 25 55
removed 15 59
removed 46 96
The list after pass #3
[47, 24]
removed 47 24
The list after pass #4
[]
*** WINNER ***
I hope you like the answer!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.