Question
We will play a simple two-player logic game and your task is to write a function that simulates this game. In the game, the first
We will play a simple two-player logic game and your task is to write a function that simulates this game.
In the game, the first player is the Code Maker who creates an d-digit numerical code using numbers from 1 to n (n<=9). NO NUMBERS MAY BE REPEATED (and the number zero is excluded). For example: - A three digit code (d=3) using numbers from 1-6 (n=6) could be 413 - A four digit code (d=4) using numbers from 1-9 (n=9) could be 8295 - Etc.
The second player is the Code Breaker, who is trying to crack the code set by the Code Maker. In each round, the Code Breaker makes a guess and the Code Maker returns information regarding the guess. The Code Maker returns the number of correct numbers that are in the correct spot, but does not reveal which numbers are in the right spot. The Code Breaker then guesses again. The game continues until the Code Breaker guesses the right code or until 8 guesses have been made.
Heres an example: Code Maker sets code 413 Round 1: Code Breaker guesses 253; Code Breaker responds with 1 (3 is in the right spot) Round 2: Code Breaker guesses 613; Code Breaker responds with 2 (1 &3 are in the right spots) Etc.
Your function must have two inputs: number of digits (d) and upper limit (n<=9) Your function must have one output: number of rounds of guesses (rounds) to crack the code
1) Use the provided function named gen_vector.m to create a vector called guess_vector that stores all possible color combinations given the digits. Do not alter the gen_vector.m function!! The inputs to gen_vector are n and d , and the output is a vector with all possible codes given the values of n and d. Call this function as: guess_vector=gen_vector(n,d) For example, if n=6 and d=4, then there are 360 possible codes (6x5x4x3).
2) Randomly select a single value from guess_vector, and assign it to the variable code. This is the code your function must guess correctly.
3) Now randomly select an initial guess from guess_vector. Call this variable guess.
4) Initialize a logical vector called TF. It should be the same size as guess_vector, and should be initialized as all true. This will help when eliminating guesses later.
5) Initialize rounds with a value of 1
6) Now its time to play the game. Use a while loop that runs while guess and answer are NOT equal and while the number of rounds is less than or equal to 8. For each iteration of the while loop:
a. Use a subfunction called check_matches with two inputs (n1,n2) and one output (N_match). The inputs are just two integer numerical values, and the output is the number of matches in the digits between the two numbers. For example, if you call check_matches(8295,6275), then the output should be N_match=2. HINT: You can break a number up into its individual digits using the following line of code: dig = dec2base(num,10)-'0'; num is a number, and dig is a vector in which each element is a digit of num. So if num=1234, then dig = [1 2 3 4].
b. Given the output of check_matches, you can eliminate many possibilities from your guess_vector. You should eliminate any number in the guess_vector that does not have the same number of matches with guess as the matches returned from check_matches. For example, if you guess 1234 and check_matches returns 1, you will eliminate any number that doesnt have at exactly one match with 1234 (ex: 2341 [no matches], 1243 [2 matches], 1235 [3 matches], etc.) Use a for loop to compare each value in guess_vector to guess and change the corresponding element in TF to false if the number of matches is not correct.
c. Use your TF vector to eliminate guesses from guess_vector. guess_vector will shrink in size with each round.
d. Now, select a new guess from the updated guess_vector. Increment the value of rounds by 1, and update TF so that it is the same size as guess_vector and has all true values once again. Repeat the above steps to continue the game, which will end once guess is equal to answer or if rounds exceeds 8.
Create a script that will examine the changes that occur with different combinations of n and d.
1) Clear all variables and shuffle the seed.
2) We will test three combinations of inputs: n = 6, d = 4; n = 8, d = 5; and n = 9, d = 6. So, create a 3x2 matrix of inputs, with n in the first column and d in the second column.
3) Use two nested for loops to test each combination N times. Store the output of your function in a 3xN matrix called rounds_all, with each row corresponding to each of the three input combinations, and each column being one realization of playing the game. Remember the output of your function is the number of rounds played to guess the answer. When your code is done, set N=20. When testing though, make it a smaller value otherwise it will take too long. 3
4) Create three separate histograms for the values of rounds_all for each combination of inputs. Use subplot to put them on one figure, and use titles and labels to make it clear which is which.
5) Calculate the win percentage for each combination of n and d and display this in the command window. Reminder: a WIN is when the Code Breaker guesses the code in 8 or fewer rounds. Anything greater is a LOSS.
6) Compute the average number of rounds it takes for each combination of n and d and display this in the command window.
The funtion given
function [M] = gen_vector(n,d) % Inputs: n<=9 -upper bound for natural numbers in code % d - number of digits in code % Output: M - vector of all possible numbers; no repetition (or 0)
m1 = 0; mf = 0;
for j=1:d m1 = m1 + 10^(j-1)*1; end
for j=1:d mf = mf + 10^(j-1)*n; end
M = [m1:mf];
L = length(M); TF = M>1;
for j=1:L dig = M(j); dig_digits = zeros(1,d); dig_track = dig; for k=d:-1:1 dig_digits(1,k) = rem(dig_track, 10); dig_track = floor(dig_track/10); end for k=1:d if dig_digits(k) > n || dig_digits(k) == 0 || sum(dig_digits(k) == dig_digits) > 1 TF(j) = false; break end end end
M = M(TF);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started