Hello everyone and welcome to the simple game project solutions lecture will be finishing off my follow
one with this lecture.
Are we going to be coding through the solutions for the game project.
Let's get started.
OK.
Whenever we begin a project it's always a good idea to map out the steps we need to take to actually
complete the tasks.
For one thing I'm going to need to get the actual player guess.
So that's one of the steps I need to do.
The other thing I need to do is generate the computer code that's the three digit number.
Then I also want to generate the clues.
So that's going to be comparing the guest to the computer code that I generate.
And then what I want to do is actually have some sort of logic that runs the game.
So we'll call this run game logic OK.
Let's get started with the simpler function of getting the guess sort of going to do here is say D E
F get underscore guess and I won't take any actual parameters for this function.
Instead what I'm going to do is return the input of what is your guess.
So let's actually hear at the bottom where it says run game logic.
Test this out by saying Get guess and then I'm going to save this as some variable called X and then
let's say print TX.
I will save this and let's run this.
I will see Python.
No it's not pi enter and I see a function.
So actually Whoops forgot to call the function.
And now let's run this.
OK so what is your guess.
I'm going to input let's say 1 2 3 enter and it types 1 to 3 out.
Now let's check to see what type that is going to save this run this again.
I'll put one two three is my guess.
And it says it's a string.
So I can see here that any input is a string.
Now you can definitely work for string and you can index off of strings so I could say something like
X 0 and then do stuff with it.
But a string is also immutable.
And I may want to play around with this object more so I will cast it to a list after the input.
Again it's totally up to you.
There's many ways you could have created this project but I'll just do that for safekeeping and now
my guess function is complete.
The next step is to generate some computer code.
So there's the three digit number that the computer is going to generate.
Then when I say generate code and then let's do it this way I'm going to create an object called digits
and I will use a list comprehension to do it again you couldn't just use the for loop here but I will
say string for
an range 10.
So here what I'm doing is I'm creating a list of every number from 0 to 9 and I'm going to cast it as
a string.
And then what I need to do is generate a code.
So I will say this shuffle this digit.
So I'm going to shuffle the digits then grab the first three after the shuffle and this is given to
as a hint in the hint lecture.
So I'll say import random and we'll talk about importing random and making your own modules later on.
But this will allow you to say random shuffle and pass in our digits list and then when I grab the first
three I'm going to do here say return digits whoops colon three.
So I've created digits shuffle them and then return the first three.
So that should give me a random three digit code that is completely unique.
So all the numbers are unique because I used range 0 tonight.
Next I want to generate the clues and this probably the harder part both a little bit logic and control
flow.
We should be able to manage it I'm going to create a function called Let's just call it generate Kloos
and it will take in the code that the computer did and then the user
gets so the user is going to make some guess.
So this again is going to take any user guess in some code and I compared the numbers in a loop and
it's going to create a list of clues according to the matching parameters.
So first off I'm going to say if the user guess is equal to the computer code I'm just going to return
code cracked because if the guess is the same as the computer Curt Weldon we're all done here.
So that's a very first thing I should check next.
If that has happened and going to create a list an empty list right now called clues and I will keep
adding to it as I go through the user gets.
So something I can do here is use enumerate and let me show you what enumerate actually does and going
to say for I N.Z. numb in enumerate.
And you don't have to use enumerate but you can.
Basically what it does is it allows you to instead of having to do something like Count equals zero
or to make this a more realistic sample.
I Endy is equal to zero and then come in here and say something like I N D is equal to I n the plus
1.
If I just want the actual index I can use enumerate which is going to enumerate starting at zero for
me.
And then I'm going to put in the user guess
and then I will say if number is equal to code I and the I'm going to append
match.
So what does this actually mean.
Well I'm going to with enumerate go through the user guess and using tuple and packing and unpacking
two things the actual index location and then the number itself.
And I'm saying if the number is equal to the code index locations remember I'm going through user guess.
So I'm saying if the current number and user guesses equal to the number of the code at the same index
location that I know I have a match.
So append the word match to my list of clues.
Then I'm going to say Elif numb is in code.
So what does that mean.
Well I know the name is somewhere in the code.
Then I will say Kloos append close.
So I'm saying hey you didn't get a match but you're pretty close.
And pay attention to the ordering here.
I'm saying if there's a match then check that first.
Then if the name is in the code a the close.
I can't have this go in another order so I can't check for close first.
Otherwise I would always skip overmatch.
So pay attention to the ordering.
Here it's kind of important for the specific case.
Otherwise if I didn't actually have a match or a close one I'm going to do is start off with another
if statement where I say if Kloos is empty.
So after going for the for loop.
If the clues is empty then all I'm going to do is return a list of a single item that says nope.
And then I will say else
return clues.
So what does this actually mean.
Well let's break it down.
And I'm going to zoom out a little bit so he can see the entire function.
Will you break it down.
OK first off I just want to check if the user gets matches a code.
If it does I've cracked it and we're ready to go.
Otherwise what I'm going to do is to find an empty list called clues.
And then using the special enumerate function I'm going to go through the index and numbers again.
You could have done this without enumerate by just saying indexical 0 and then adding to index for every
iteration.
This for loop.
But this is kind of just say convenience function.
Then I will say if the number the user guess is equal to the code at the specific index location that
is going to append the word.
Else if the number is in the code then I append close and note the order here.
I write the check for the match first before I check if it's close or not.
Then if I go through this entire for loop includes a still empty meaning there was no match or there
was no close and willing to return no else I'll just return clues.
Next it's time to add in some game logic so I'll show you how we can do this.
I'm going to print something like
welcome codebreaker and then I will generate a secret code so I'll make some variable called secret
code.
And said equal to generate code remember generate code returns the computer's code.
So that's the secret code and then I'm going to also say make some very well called Clear report and
set that equal to an empty list.
Well let's try to follow snake case here and then I want to keep asking this until the code is cracked.
So while clu report is not equal to the string code cracked.
Now let's make sure that matches exactly appear.
OK so while it's not this here's one I'm going to do.
I'm going to set my guess equal to
guess close princes and then what I want to do is say
clear report is equal to generate Kloos of the user guess and the secret code.
Then I'm going to print.
Here's the result of your guess.
And actually save for clue inside the clear.
Poor print the clue.
And this keeps going over and over and over again until I actually have a clue report that says code
cracked because remember generate Kloos returns.
This string could crack once have actually gotten the correct code.
So let's run this and see if we can actually play again.
When say Python notes stop PI enter.
Looks like an invalid syntax right now so I say it's Kloos equal to whoops that should be two equal
signs so let's check that that's happening at line 29.
Scroll back up we'll save clues double equal sign.
Save this then you can always reference the core solution notes to get the actual correct code.
Now I get Python notes that pi perfect.
Welcome Codebreaker what is your guess I'm going to guess.
One two three enter and here is the result to be a guess.
It says match match.
So let's confirm this by typing something like five six seven and we get close.
So that's interesting means.
One of the extra numbers is 1 5 6 or 7.
So that means if I say 8 9 0 it should give me none.
Nope perfect.
So looks like my game is working so far.
So now it's just time of guessing 3 to 6 6 3 to 6 to 3.
All right.
Looks like the code is cracked with 6 to 3.
Great.
All right.
So if you have any questions on how to do that project or what was going on.
Feel free to post the Q&A forums and make sure you walk through the solutions code yourself.
This is just one simple example of how you could have done this problem.
There are many many ways you could have done this project and you could have added more to it.
It's really up to you how far you want to go.
But again if you have any questions feel free to post the Kewney forums.
Thanks everyone and I'll see you at the next lecture.