Question: Let s start with Fisher - Yates. Write a function ( shuffle ( ) ) that takes the encryption key ( an array of 2
Lets start with FisherYates. Write a function shuffle that takes the encryption key an array of char as a parameter. This function will implement FisherYates to change the order of the letters in the key.
We will need to make two arrays of characters. One for the real encryption key and one to hold what the player has entered so far. Since these are fixed in size and small, we dont need to use malloc for them we can make regular old arrays.
Now lets take a look at initialization We will get the puzzle like before but now we will generate the encrypted string. To do this:
Generate the ordered encryption key use a loop and call shuffle on it
Populate the players key with the NUL character
Build the encrypted string:
a Allocate memory for it
b Loop over the original string.
i If the current character is a letter, get the upper case, look it up in the encryption key and append to the encrypted string the encrypted value
ii Otherwise, output the original this preserves spaces, commas, etc
c NUL terminate the encrypted string
We will change updateState to accept a pair. If the input is not NULL and is not quit then if it is characters long, we will populate the players key with this new mapping. The first character is the distance, the second is the replacement. If the player entered something else, output a reasonable error message.
We will change displayWorld. It will output the encrypted string and the players progress:
Encrypted: QZZ ZQZ
Decrypted:
The rule for decrypted:
if the encrypted character is AZ
look that character up in the players key. If the key is output
otherwise output the key.
Else
Output the encrypted character spaces punctuation, etc.
Finally, in teardown, make sure that you free the memory that you allocated for the encrypted string.
With this work done, we can actually sort of play the game. We can enter pairs and see the decoded string.
Some useful C library functions you might need:
random time srandom
strlen isalpha toupper
malloc free
Your program must run through Valgrind without any memory leaks.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
