Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Creating the check for a winning game and other methods to our game class.
Accomplished in this Video
- Created a method to check if the cards at two given locations are a match
- Created a method to check if the game had been won by seeing if any card's matched attribute was still set to false
- Created a method to make sure the location the user gives exists in our grid
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
To recap,
we've put together the game class,
0:00
created cards and created the grid.
0:05
Now we need to check if two given
locations have matching cards.
0:11
Check if a game has been won.
0:17
And also check if the location of
player inputs is a valid input.
0:19
Let's start with checking for matches.
0:25
>> Let's call our method check match.
0:33
>> It'll take two locations.
0:38
These will be the location
guesses the player will input.
0:45
Let's create an empty list to hold
our two cards, since we'll need
0:49
to loop through our cards to find
the two that match the given locations.
0:54
For card and self dot cards
1:03
if card.location == loc1,
1:09
or card.location
1:15
== loc2. When the cards have
been found that match the locations,
1:20
we need to append it to our
list of cards.append(card).
1:27
Okay, now that we found the two cards,
we can check to see if they're a match.
1:34
If cards[0] == cards[1].
1:39
If they do, we need to set their matched
attribute to True and return True.
1:52
cards[0].matched = True.
1:59
cards[1].matched = True, return True.
2:11
If they don't match, then we need to
let the player know by printing out
2:19
the card and its location, since you
would be able to see the card and
2:24
where you got it when
playing the real game.
2:28
Then return False.
2:53
Nice work! Onwards, create a new
method named check_win.
3:00
It should loop through all of the cards.
3:15
If any cards' matched
attribute is still False.
3:23
We can return False because there's at
least one card who's matched attribute
3:32
is False, which means
the game hasn't been won.
3:39
Otherwise we can return True,
3:45
because none of the cards' matched
attributes are then False.
3:54
That method was a lot
smaller compared to the rest.
4:00
Nice work.
4:03
Now let's tackle player input to
make sure it's a correct location.
4:04
Let's call it check_location.
4:12
It'll take self and a time argument so we
know if this is the first or second guess.
4:19
Inside we're going to put a while loop, so
4:29
the player will continue to be
prompted for a location guess
4:32
until they provide one
that exists in our grid.
4:36
Then we need to get their input and
we can use the time
4:40
here to let the player
know what guess this is.
4:46
Now we can check if
the guess is in our locations.
5:10
I'm going to call upper on our guess
just to make sure we do get a match.
5:15
If it is a location, we can return it.
5:24
If not, then we need to let the player
know their guess isn't valid.
5:31
[BLANK AUDIO]
5:35
The only thing left is to build
the start_game method.
5:50
Take a minute to test out the methods
we've created on your own.
5:55
This is a great way to make sure you
know what the code is doing, and
5:59
to check that everything is
working the way it should.
6:03
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up