VI. Matching Game Consider a matching game in which you have a list of random in
ID: 3827531 • Letter: V
Question
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
# -*- coding: utf-8 -*-
"""
Output added in the end of the program
Created on Sat Apr 29 17:25:30 2017
@author: raska_000
"""
from random import randint
import random
#Function to remove pair as per criteria
def removeItem(start,randomList):
#x1y1 x2y2
flag=1
i=0
print()
while(flag==1):
x1=randomList[i][0]
y1=randomList[i][1]
x2=randomList[i+1][0]
y2=randomList[i+1][1]
if x1==x2 or x1==y2 or y1==x2 or y1==y2:
print(" Removed:" + randomList[i] + " " +randomList[i+1])
del randomList[i]
del randomList[(i)]
else:
i=i+1
if(i>=len(randomList)-1):
flag=0
return randomList
#check winner by taking into consideration - size of a list
def checkWinner(randomList):
if len(randomList)==0:
print("The list is empty")
print(" No more paires to remove.")
print("*** Winner! ***")
return 1
else:
return 0
#print list after each pass
def printList(randomList):
for i in randomList:
print(i,end= " ")
#function to shuffle
def shuffleList(randomList,shuffleCnt):
print("No more pairs to remove. Shuffling the numbers.")
random.shuffle(randomList)
print("The list after shuffling #" + str(shuffleCnt+1))
printList(randomList)
shuffleCnt=shuffleCnt+1
return randomList,shuffleCnt
def main():
shuffleCnt=0
PassNo=1
TotalNumbers=int(input("How many numbers(no less than 10)?"))
print("Starting with:")
randomList=[str(randint(10,99)) for i in range(TotalNumbers)]
printList(randomList)
while(shuffleCnt<5):
Cnt=len(randomList)
randomList=removeItem(0,randomList)
if(Cnt==len(randomList)):
randomList,shuffleCnt=shuffleList(randomList,shuffleCnt)
randomList=removeItem(0,randomList)
print("The list after Pass" + str(PassNo))
printList(randomList)
print(" " + "-"*20)
PassNo=PassNo+1
print()
else:
print("The list after Pass" + str(PassNo))
printList(randomList)
print(" " + "-"*20)
PassNo=PassNo+1
print()
if checkWinner(randomList)==0:
if shuffleCnt==5:
print("No more pairs to remove. *** Better luck next time! ***")
break
else:
break
if __name__=="__main__":
main()
Output:
"""Output
How many numbers(no less than 10)?10
Starting with:
12 12 42 51 30 10 18 82 90 24
Removed:12 12
Removed:30 10
Removed:18 82
The list after Pass1
42 51 90 24
--------------------
No more pairs to remove.
Shuffling the numbers.
The list after shuffling #1
90 24 42 51
Removed:24 42
The list after Pass2
90 51
--------------------
No more pairs to remove.
Shuffling the numbers.
The list after shuffling #2
90 51
The list after Pass3
90 51
--------------------
No more pairs to remove.
Shuffling the numbers.
The list after shuffling #3
51 90
The list after Pass4
51 90
--------------------
No more pairs to remove.
Shuffling the numbers.
The list after shuffling #4
51 90
The list after Pass5
51 90
--------------------
No more pairs to remove.
Shuffling the numbers.
The list after shuffling #5
51 90
The list after Pass6
51 90
--------------------
No more pairs to remove.
*** Better luck next time! ***
"""
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.