The famous Match&Hit game is played by a computer and a human player as follows. First, the com- puter selects a random 4-digit number N = a·103+b·102+c·10+d, where a,b,c,d are distinct non- zero digits — that is, a,b,c,d are distinct elements of the set {1,2,3,4,5,6,7,8,9}. Let us call num- bers like this valid. The human player then tries to deduce N from a sequence of queries to the com- puter. Each query consists of a valid 4-digit number M = x·103+ y·102+ z·10 + w. The computer responds to each query with the number of matches and the number of hits. A match is a digit of N that appears in M at the same position (thus each of x = a, y = b, z = c, or w = d counts as one match). A hit is a digit of N that appears in M, but not at the same position. For example, if N = 5167, then the queries 2934, 1687, 7165, 5167 will result in the following numbers of matches and hits: See image.
where matches are denoted by • and hits are denoted by ◦. The play continues until the human player either wins or loses the game, as follows:
WriteaCprogram, calledmatch and hit.c,thatimplements(thecomputerpartof)theMatch&Hit game. In this program, you are required to declare, define, and call the following functions:
Here is a sample run of this program, assuming that the executable file is called match and hit. This run illustrates a situation where the human player wins the game. User input is underlined.
/home/userXYZ/ECE15/Lab3> match and hit
***Welcome to the MATCH and HIT game***
The computer has selected a 4-digit number.
Try to deduce it in 12 rounds of queries.
Round #1
Please enter your query (4 digits): 5341
-> 2 matches and 1 hit
Round #2
Please enter your query (4 digits): 1235
-> 1 match and 2 hits
Round #3
Please enter your query (4 digits): 2345
-> 4 matches and 0 hits
**********************************
CONGRATULATIONS! You won the game!
**********************************
Notice that a singular form (match, hit) is used with the number 1. You can easily achieve this func- tionality with the ?: conditional expression, as shown in class. The next sample run illustrates a sit- uation where the human player loses the game. Again, user input is underlined.
/home/userXYZ/ECE15/Lab3> match and hit
***Welcome to the MATCH and HIT game***
The computer has selected a 4-digit number.
Try to deduce it in 12 rounds of queries.
Round #1
Please enter your query (4 digits): 1234
-> 0 matches and 3 hits
..
..
Round #12
Please enter your query (4 digits): 2435
-> 2 matches and 2 hits
*********************************
Sorry, out of queries. Game over!
*********************************
Notes:
/home/userXYZ/ECE15/Lab3> match and hit
***Welcome to the MATCH and HIT game***
The computer has selected a 4-digit number.
Try to deduce it in 12 rounds of queries.
Round #1
Please enter your query (4 digits): ##
Invalid query. Please try again!
Please enter your query (4 digits): 0##
Invalid number. Please try again!
Please enter your query (4 digits): -5341
Invalid number. Please try again!
Please enter your query (4 digits): 123456789
Invalid number. Please try again!
Please enter your query (4 digits): 5340
Invalid number. Please try again!
Please enter your query (4 digits): 5344
Invalid number. Please try again!
Please enter your query (4 digits): 5341#OK?
-> 2 matches and 1 hit
Round #2
Please enter your query (4 digits): 1235
-> 1 match and 2 hits
Round #3
Please enter your query (4 digits): 2345
-> 4 matches and 0 hits
**********************************
CONGRATULATIONS! You won the game!
**********************************