Advertisement
English transcript
=
[ Music ]
>> Stanford University.
>> Okay, well welcome to Lecture number 3
of CS193p for fall of 2013/14.
Today I'm going to jump right into a demo and then
after that I have some slides, time permitting,
assuming the demo doesn't take [pause] the whole time here,
and after, the demo I'm going to do is I'm going
to take the little card thing we did last week and turn it
into a real card matching game
where we're actually matching cards, okay,
rather than just flipping them over.
And, like I say, if I have the time I'll talk a little bit more
about objective-C and we're really going to go
into objective-C on Wednesday in quite a bit of detail.
I also included on the stuff I posted some review slides
at the end just kind
of reviewing what we will have learned
in the first three lectures, just so you can kind of look
through it real quick and say oh yeah, do I know that?
Yeah I do, and if not, than you can obviously post
and ask us questions about it.
Okay? So, let's do this demo.
So for the demo [background sounds],
I'm going to start this demo actually
by giving you the solution to the homework,
I told you I was going to do this, this is the only time
that I'm going to do this this quarter.
Normally I don't go over the solution, but the solution's
so simple and because obviously we need the solution of this
to move on to what I'm going to do today.
We'll do the solution first.
So here I'm just launching XCode, you can see now
in my recents I have this machismo from last time.
So, we'll bring that up.
There it is.
Make sure that we're using
as much screen real estate as we can here.
Get these wires out of my way.
Okay. So, from where we left off, hopefully you remember
because you did your homework.
We just have this single, this card, single card,
and it flips back and forth between the ace and the clubs,
and your homework was to use the model that we worked
on in lecture to make it flip through an entire deck.
So, the solution to that is quite straightforward.
We obviously need a deck so I'm just going
to add a property here, strong property, should be strong
because we need that deck to, to stay around, and it's going
to be a deck and I'm going to call it deck.
Okay? Now we have an error here because of deck,
so we have to go up here and import deck.
Alright? To make that error go away.
So now we got this property that's cool, obviously we want
to lose lazy instantiation to instantiate that property,
so I'm going to do that, that's a deck [inaudible] deck,
and I'm just going to see if my instance variable is null
or nil, rather, the 0, basically.
Then I'm going to say, sorry, underbar, underbar deck, equals,
and I'm actually going to create deck using another method here,
you'll see why in a second and then I'm going
to return underbar deck.
Okay? And create deck is just going to create a deck.
Create deck and I'm going to create a playing card deck,
because that's what you were asked to do so I'm just going
to do [inaudible] to do that.
I got an error because not only do I need to import deck,
but playing card deck, and it's actually kind
of unfortunate here that in this nice, generic card game
that I'm building, that I'm importing a playing card deck.
Why is that unfortunate?
Well, I'm going to try and build a card game here
that has nothing to do with playing cards.
Okay? It's going to be a generic,
should work with any kind of deck, okay, a deck of any kind
of cards and, in fact, in your homework, you're going
to be asked to enhanced the game next week
to play a different deck.
Okay? So it's unfortunate that I have to do this and in lecture,
either on Wednesday or probably on Monday, we will talk
about how we can get rid of this.
Okay, get this import playing card deck
and this create deck method to be more generic, okay,
not be so specific to playing cards.
But anyway, so now I have this deck
and it's lazily instantiated so now that I have a deck,
I can use it to get rid of this little ace
of clubs thing right here, okay?
And, I'm going to do that by saying here card star card
equals self dot deck, draw random card.
So I'm going to draw a random card out of the deck
and then instead of putting ace of clubs here,
I'm going to put the cards contents.
Okay? And that's really your entire homework assignment,
there's one other minor thing that I'm going to do here,
actually two minor things.
Because we don't want just a solution that works,
this will work if we run this.
We're going to see it work, but it's kind of not very elegant
and not really, it's not really a really very good solution
to the problem, even though it functions, you see, it's,
it's going through the random cards here.
But it was kind of annoying, a couple of things.
One, it started out with the ace of clubs, that was kind
of gross, okay, because if I keep clicking here,
eventually I'm going to get the ace of clubs,
there it was back again, so that's kind of bad.
The other thing is when this deck is empty, this is not going
to particularly elegantly, you know, finish off,
and we want to have some elegant solution and I said
in the hints come up with an elegant solution.
Also, this flips thing here,
once I flip through the whole deck,
it's going to keep flipping.
You know, it's going to be 102, 103, 104, 105,
that's not very elegant either.
So let's fix all three of those things with simple,
elegant solutions, okay?
So my simple, elegant solution to the ace of clubs
which I hinted at for you is simply to start it face down.
So I'm going to get the card back here, get rid of the ace
of clubs, and now this card starts out facedown.
So I didn't have to write any code and I just use my brain
over my brawn to make sure that this works, right?
So, that's kind of the elegant way of thinking
about solving that problem.
So we'll just start it face down, there's no,
there was no required task that said you had to start
with the ace of clubs face up, so that's a good solution.
When it comes to running out of cards,
I think an elegant solution is it just will stop flipping
at that point.
I mean, you could make arguments, load up another deck,
other stuff, but I think a simple solution is just once you
run out of cards, you keep clicking, and it stops flipping.
Okay? So how are we going to do that?
Well, I know some of you are going to say something like,
if number of flips equals 102, then stop flipping,
and that's really, really bad.
That's a very bad design, because that 102 is
like a magic number you just put into your program
and there's completely no reason for that bad design either.
All you need to do is just say if card, then flip it, okay,
so I'm going to flip it here,
I'll make some more space so you can see.
Otherwise, I'm not going to flip it.
Okay? So I'm just, this is a super elegant solution to,
you know, not continuing to flip this thing
over when the deck runs out,
and we know that draw a random card returns nil,
if you remember from our implementation it returns nil
when the deck is empty, so, perfect!
Once it's nil, it's just not going to flip back
over to the contents sides,
it's going to always stay on the backside.
Question? [Inaudible background question]
And flip count is the third thing that was kind
of not very elegant, so how can we fix that?
Well, you know, there's a number of ways to go at that,
but I think a simple way is to only flip, sorry,
when we actually do the flipping over.
Now, this is kind of not a great solution
because here we're not actually flipping over, you know, if,
if we're on the card back, then we're flipping over here,
so this one's kind of not so good, because we kind of gonna,
once we flip over to the back, we'll be okay
because when we start to do [pause],
we'll be executing this code over and over right here
because we'll always be on the back.
So I think this'll work, right?
Is that a good solution?
The reason I don't like it is I don't like having this line
of code in two different places, kind of eh.
I could have done a lot of other things here,
we could have factored out the image and the string
into separate local variables and had our if,
apply those, and stuff like that.
I think all of that would have been a little bit of overkill.
So, let's see if this works.
So here's our thing here, so it started facedown.
Let's click through the deck here,
I'll click through it quickly.
So I'm clicking so quickly through, you'll notice,
that I'm not giving time for the thing to finish animating, okay?
And we're going to start talking about animation
in the week after next.
And [pause] one thing about animation is it's going
to happen a little bit out of band
with what's actually happening.
So I am actually flipping here and all
that flipping is happening, but the animation is kind of slow
and it's staying behind, and that's kind of just,
unfortunately, the way it is,
the animation will eventually catch up, but here we go
to our last card, then we get facedown, and now we click,
and it's not clicking over and it's stopped at 104.
Okay? Any questions about this solution?
Pretty straightforward.
Again, elegance, simplicity, that's what you're shooting
for in your homework, okay?
Big, overwrought solutions where you're adding a bunch of API
and checking a lot of things are generally not preferred
over more simple solutions, so like this,
even if you have something where you kind of have these two lines
of code that are doing the same thing,
maybe shouldn't be replicated like this, it's a close call
as to the really perfect elegance, but I was going
for simplicity here too because I only have
so much time [laughing].
Okay?
>> Alright.
So now, we're going to go into doing the next phase
of our card matching game,
which is to make the game actually play.
Now the playing of the game is part of our model.
It's really important to understand, it's not part
of our controller, its part of our model.
Because I told you that the model is the what your game is.
Well, what is this?
It's a card matching game, so the what better be in the model.
So we're going to create a class in our model that is going
to encapsulate the logic of the game playing and it's not going
to know anything about user interface, okay?
It's not going to have any things in it that have anything
to do with user interface
and that's a really important distinction to make.
So, let's create that new class,
remember that we always create new class the same way you did
with your assignment today by saying new file.
Okay? And we want an objective-C class and I'm going
to call it card matching [pause] game.
Okay? And it's going to inherit from NSObject as all of our kind
of base classes do, and I'm going to put this thing
in the same directory as the rest of my model
and I'm also going to put it in the same group
than the navigator as my model, as well.
Okay? So it goes with all these guys and it goes in this group.
It's a nice thing to remember to do.
Alright? So we create it.
Here it is.
Okay? Here's its header file right here and I'm going
to use this counterparts, automatic counterparts,
to show the implementation, okay?
So there is its header file and its implementation,
and I always, when I design a new class, I always try
to give my first crack at its public API first.
Okay? I'll go do its implementation later,
but I want to kind of think of how are people going
to use my class first?
Just to drive my overall design, okay?
And that's for a simple class like this,
if you get into a more complicated class,
you might be having significant design meetings which are teamed
to understand what's this object, how does it fit
into the world, but if you're just doing a simple
straightforward class,
especially if you're doing its first iteration,
it sometimes nice to just think what is it's API, we call it,
okay, API is the Public Programming Interface
for a class.
So, I need to think of what this card matching game needs to do.
Well, one thing I know it needs is an initializer.
Okay? And int.
But when I think about my card gap matching game,
there's a couple of things it just has to know to initialize.
It can't just initialize itself like a playing card deck can.
A playing card deck knows everything it needs to know
about a playing card deck when it's started,
but this doesn't really know that, so,
I need a couple of things.
One thing I need is how many cards are we playing with?
Okay? How many cards are, is the person allowed to match total?
Right? What's the total amount of cards?
So we need that.
So I'm going to say, int with card count,
and we'll make an NSUInteger.
Really, that count has got to be at least two, you would think,
so I'd probably, in my initializer, should check
to make sure it's greater than one
and if it's not I'll probably return nil from self.
In other words, I won't initialize,
my [inaudible] will return nil.
Okay? And we'll, actually, we'll pick one way
where it does return nil, but we should probably check that too.
The second thing that I need is a deck, a deck of cards,
because I got to deal these cards out, right?
I'm going to deal them out and then you're going to be able
to pick some of them and do it.
So, I'm going to say using deck.
Deck star deck.
Okay, so there's the initializer, and, of course,
to do that I need to import deck, alright?
So that's pretty good.
Start there.
What else do I need for my game?
Well it's a game, so I think it should have a score.
[Pause] Eh, we'll say NSInteger score.
And, again, NSInteger,
NSUInteger versus unsigned int is kind of a style thing,
I used NSUInteger above, I'm going to use NSInteger
to be consistent here and my score can be negative possibly.
So that's why it's not an NSUInteger, it can be negative.
And notice I made this property read only.
It's the first time we've seen that.
And why is that?
Well that's because I'm the game logic,
I get to determine what the score is,
nobody can set the score, I tell you what the score is.
Therefore, there should be no setter for this property.
At least not publicly.
You're going to see in a moment, privately,
we're going to make this rewrite
so that we can set it privately, right?
Because we obviously need to be updating our score all the time
as people flip these cards over and get matches,
they get points, we need to update it.
But publicly, we want it to be a read only.
Okay? The only other thing I need to do is someone's going
to be clicking on these cards; they're going to be flipping
over and being chosen and possibly matching,
so I need some sort of method to let somebody choose a card.
So, I'm going to call this choose card at index,
NSUInteger, again, index.
So, there's a lot of ways in API you could have
for letting someone choose a card.
The person who creates this card matching game, not the person,
but the other object, like a controller
that creates this model class, knows the count,
because it specified the counter cards.
So, it's pretty reasonable to have them specify an index,
which means 0, in this count minus 1,
anytime someone chooses a card.
Okay?
So this is just a simple way of specifying
which card did the user choose?
And similarly, I actually need to be able to return a card
at a given index, and why do I need this?
Well, I need to be able to find out the state of the game
at any given time, I mean, how is my controller going
to display a UI for this game if it can't, you know,
if it can't find out what the cards, what the state
of the cards is, so this is just a little way for it
to get the cards, and it could iterate through all the cards
and get them all, and update them all, or it can get one
in a particular index and could kind of,
whatever fits best to what we're doing.
Alright, so, that's my public API.
Now before I go and do my implementation over here,
you can see that I got this warning right here
and what's this warning saying,
it's saying there's actually three of them, you can click
on this little three by the way,
it's saying you have not implemented these [pause],
these three methods that you made public, so that,
that's a good warning.
I need to go do that.
But before I do that, I'm going to go back to my controller,
I want to show you how we're going to,
what kind of UI we're going to have over here so that
as we're doing our implementation, you can kind
of imagine how they're going to work together,
but I actually wouldn't necessarily have
to do the UI before I do the card matching game
and one could argue I should do the card matching game first
and not be influenced by the UI so much
when I'm designing my model.
Okay? But, you know, this model is going to be served by some UI
so we're going to do them at the same time
so you can imagine it a little better.
So what do we need for this UI?
Well one thing is we need a lot more cards, okay, this one card,
it can't be matched against anything else,
so let's make more cards, this is very easy to do.
I'm just going to move this card up into the corner here
and I'm just going to copy and paste it, so I can select it
and copy and paste, okay?
Place it, use the nice blue guidelines there.
I can even copy two and paste them.
Okay? Or copy four, and paste them.
Okay? So now I'm going to have 12 cards,
that's a good number of cards.
I actually don't need this flips thing anymore, I'm not going
to be just flipping cards, I'm going to be matching cards,
I'm probably going to need something for the score
which we'll do a little bit later.
But that's pretty much my entire UI, I'm just going
to have these cards and I'm going to click on them,
it's going to show a card and then I'm going,
I could either click on it again to turn it back facedown,
un-choose it, or I could click on another card
and if it matches I should get some points
and if it doesn't match, the other one,
the last one I had is going to flip facedown.
Okay? Now those of you who are used to a game
like Concentration, it's a little different,
usually you pick a card, then you pick another card,
and if they match you get points,
if they don't match they usually both turn back over, okay?
Which would be a cool UI, but I haven't taught you how
to do animation and that's what you would need there
because that second card, when it comes up, would need to be
on screen for a little bit before they both went
down because you got to at least get to see this one.
So since you don't have animation, when you click
on that second one, it's going
to facedown the previous one if they don't match.
Okay? And then you just keep clicking around trying
to find matches and you get points, and we'll show you,
kind of the point system that I'm going to use.
So this is my entire UI.
Very, it's a very content-central UI, right?
The UI is really focused on these cards, there's not a lot
of adornments and other stuff around it.
And we're really not going to have much other stuff except
for the score and you're going to add like a redeal button
and maybe a little bit of stuff about some status
about what's going on in your homework,
but this is the fundamental basis of the UI.
Okay? So now let's go back to our card matching game,
and let's talk about how we're going to implement this thing.
I'm going to go back to automatic here.
Get this thing back.
Just to make this look nicer, I'm going to go like that.
And I'm going to leave this header file up while we go
and do the implementation so you can kind of see
where we're shooting, this is what we're shooting for.
[Pause] So, the first thing I want
to do is make this be readwrite in my implementation only.
And I do that with this little, private interface,
remember I showed you about this, that you can have one
of these with the little open parentheses, close parentheses
like that, and now you can declare your own things.
Well, I'm going to redeclare this thing
to be nonatomic readwrite, NSInteger score.
Okay?
Now this readwrite, we don't use that very much
because it's the default, right?
Is chosen, remember the properties chosen
and match that are in card.
Those didn't say readwrite
…
[ Music ]
>> Stanford University.
>> Okay, well welcome to Lecture number 3
of CS193p for fall of 2013/14.
Today I'm going to jump right into a demo and then
after that I have some slides, time permitting,
assuming the demo doesn't take [pause] the whole time here,
and after, the demo I'm going to do is I'm going
to take the little card thing we did last week and turn it
into a real card matching game
where we're actually matching cards, okay,
rather than just flipping them over.
And, like I say, if I have the time I'll talk a little bit more
about objective-C and we're really going to go
into objective-C on Wednesday in quite a bit of detail.
I also included on the stuff I posted some review slides
at the end just kind
of reviewing what we will have learned
in the first three lectures, just so you can kind of look
through it real quick and say oh yeah, do I know that?
Yeah I do, and if not, than you can obviously post
and ask us questions about it.
Okay? So, let's do this demo.
So for the demo [background sounds],
I'm going to start this demo actually
by giving you the solution to the homework,
I told you I was going to do this, this is the only time
that I'm going to do this this quarter.
Normally I don't go over the solution, but the solution's
so simple and because obviously we need the solution of this
to move on to what I'm going to do today.
We'll do the solution first.
So here I'm just launching XCode, you can see now
in my recents I have this machismo from last time.
So, we'll bring that up.
There it is.
Make sure that we're using
as much screen real estate as we can here.
Get these wires out of my way.
Okay. So, from where we left off, hopefully you remember
because you did your homework.
We just have this single, this card, single card,
and it flips back and forth between the ace and the clubs,
and your homework was to use the model that we worked
on in lecture to make it flip through an entire deck.
So, the solution to that is quite straightforward.
We obviously need a deck so I'm just going
to add a property here, strong property, should be strong
because we need that deck to, to stay around, and it's going
to be a deck and I'm going to call it deck.
Okay? Now we have an error here because of deck,
so we have to go up here and import deck.
Alright? To make that error go away.
So now we got this property that's cool, obviously we want
to lose lazy instantiation to instantiate that property,
so I'm going to do that, that's a deck [inaudible] deck,
and I'm just going to see if my instance variable is null
or nil, rather, the 0, basically.
Then I'm going to say, sorry, underbar, underbar deck, equals,
and I'm actually going to create deck using another method here,
you'll see why in a second and then I'm going
to return underbar deck.
Okay? And create deck is just going to create a deck.
Create deck and I'm going to create a playing card deck,
because that's what you were asked to do so I'm just going
to do [inaudible] to do that.
I got an error because not only do I need to import deck,
but playing card deck, and it's actually kind
of unfortunate here that in this nice, generic card game
that I'm building, that I'm importing a playing card deck.
Why is that unfortunate?
Well, I'm going to try and build a card game here
that has nothing to do with playing cards.
Okay? It's going to be a generic,
should work with any kind of deck, okay, a deck of any kind
of cards and, in fact, in your homework, you're going
to be asked to enhanced the game next week
to play a different deck.
Okay? So it's unfortunate that I have to do this and in lecture,
either on Wednesday or probably on Monday, we will talk
about how we can get rid of this.
Okay, get this import playing card deck
and this create deck method to be more generic, okay,
not be so specific to playing cards.
But anyway, so now I have this deck
and it's lazily instantiated so now that I have a deck,
I can use it to get rid of this little ace
of clubs thing right here, okay?
And, I'm going to do that by saying here card star card
equals self dot deck, draw random card.
So I'm going to draw a random card out of the deck
and then instead of putting ace of clubs here,
I'm going to put the cards contents.
Okay? And that's really your entire homework assignment,
there's one other minor thing that I'm going to do here,
actually two minor things.
Because we don't want just a solution that works,
this will work if we run this.
We're going to see it work, but it's kind of not very elegant
and not really, it's not really a really very good solution
to the problem, even though it functions, you see, it's,
it's going through the random cards here.
But it was kind of annoying, a couple of things.
One, it started out with the ace of clubs, that was kind
of gross, okay, because if I keep clicking here,
eventually I'm going to get the ace of clubs,
there it was back again, so that's kind of bad.
The other thing is when this deck is empty, this is not going
to particularly elegantly, you know, finish off,
and we want to have some elegant solution and I said
in the hints come up with an elegant solution.
Also, this flips thing here,
once I flip through the whole deck,
it's going to keep flipping.
You know, it's going to be 102, 103, 104, 105,
that's not very elegant either.
So let's fix all three of those things with simple,
elegant solutions, okay?
So my simple, elegant solution to the ace of clubs
which I hinted at for you is simply to start it face down.
So I'm going to get the card back here, get rid of the ace
of clubs, and now this card starts out facedown.
So I didn't have to write any code and I just use my brain
over my brawn to make sure that this works, right?
So, that's kind of the elegant way of thinking
about solving that problem.
So we'll just start it face down, there's no,
there was no required task that said you had to start
with the ace of clubs face up, so that's a good solution.
When it comes to running out of cards,
I think an elegant solution is it just will stop flipping
at that point.
I mean, you could make arguments, load up another deck,
other stuff, but I think a simple solution is just once you
run out of cards, you keep clicking, and it stops flipping.
Okay? So how are we going to do that?
Well, I know some of you are going to say something like,
if number of flips equals 102, then stop flipping,
and that's really, really bad.
That's a very bad design, because that 102 is
like a magic number you just put into your program
and there's completely no reason for that bad design either.
All you need to do is just say if card, then flip it, okay,
so I'm going to flip it here,
I'll make some more space so you can see.
Otherwise, I'm not going to flip it.
Okay? So I'm just, this is a super elegant solution to,
you know, not continuing to flip this thing
over when the deck runs out,
and we know that draw a random card returns nil,
if you remember from our implementation it returns nil
when the deck is empty, so, perfect!
Once it's nil, it's just not going to flip back
over to the contents sides,
it's going to always stay on the backside.
Question? [Inaudible background question]
And flip count is the third thing that was kind
of not very elegant, so how can we fix that?
Well, you know, there's a number of ways to go at that,
but I think a simple way is to only flip, sorry,
when we actually do the flipping over.
Now, this is kind of not a great solution
because here we're not actually flipping over, you know, if,
if we're on the card back, then we're flipping over here,
so this one's kind of not so good, because we kind of gonna,
once we flip over to the back, we'll be okay
because when we start to do [pause],
we'll be executing this code over and over right here
because we'll always be on the back.
So I think this'll work, right?
Is that a good solution?
The reason I don't like it is I don't like having this line
of code in two different places, kind of eh.
I could have done a lot of other things here,
we could have factored out the image and the string
into separate local variables and had our if,
apply those, and stuff like that.
I think all of that would have been a little bit of overkill.
So, let's see if this works.
So here's our thing here, so it started facedown.
Let's click through the deck here,
I'll click through it quickly.
So I'm clicking so quickly through, you'll notice,
that I'm not giving time for the thing to finish animating, okay?
And we're going to start talking about animation
in the week after next.
And [pause] one thing about animation is it's going
to happen a little bit out of band
with what's actually happening.
So I am actually flipping here and all
that flipping is happening, but the animation is kind of slow
and it's staying behind, and that's kind of just,
unfortunately, the way it is,
the animation will eventually catch up, but here we go
to our last card, then we get facedown, and now we click,
and it's not clicking over and it's stopped at 104.
Okay? Any questions about this solution?
Pretty straightforward.
Again, elegance, simplicity, that's what you're shooting
for in your homework, okay?
Big, overwrought solutions where you're adding a bunch of API
and checking a lot of things are generally not preferred
over more simple solutions, so like this,
even if you have something where you kind of have these two lines
of code that are doing the same thing,
maybe shouldn't be replicated like this, it's a close call
as to the really perfect elegance, but I was going
for simplicity here too because I only have
so much time [laughing].
Okay?
>> Alright.
So now, we're going to go into doing the next phase
of our card matching game,
which is to make the game actually play.
Now the playing of the game is part of our model.
It's really important to understand, it's not part
of our controller, its part of our model.
Because I told you that the model is the what your game is.
Well, what is this?
It's a card matching game, so the what better be in the model.
So we're going to create a class in our model that is going
to encapsulate the logic of the game playing and it's not going
to know anything about user interface, okay?
It's not going to have any things in it that have anything
to do with user interface
and that's a really important distinction to make.
So, let's create that new class,
remember that we always create new class the same way you did
with your assignment today by saying new file.
Okay? And we want an objective-C class and I'm going
to call it card matching [pause] game.
Okay? And it's going to inherit from NSObject as all of our kind
of base classes do, and I'm going to put this thing
in the same directory as the rest of my model
and I'm also going to put it in the same group
than the navigator as my model, as well.
Okay? So it goes with all these guys and it goes in this group.
It's a nice thing to remember to do.
Alright? So we create it.
Here it is.
Okay? Here's its header file right here and I'm going
to use this counterparts, automatic counterparts,
to show the implementation, okay?
So there is its header file and its implementation,
and I always, when I design a new class, I always try
to give my first crack at its public API first.
Okay? I'll go do its implementation later,
but I want to kind of think of how are people going
to use my class first?
Just to drive my overall design, okay?
And that's for a simple class like this,
if you get into a more complicated class,
you might be having significant design meetings which are teamed
to understand what's this object, how does it fit
into the world, but if you're just doing a simple
straightforward class,
especially if you're doing its first iteration,
it sometimes nice to just think what is it's API, we call it,
okay, API is the Public Programming Interface
for a class.
So, I need to think of what this card matching game needs to do.
Well, one thing I know it needs is an initializer.
Okay? And int.
But when I think about my card gap matching game,
there's a couple of things it just has to know to initialize.
It can't just initialize itself like a playing card deck can.
A playing card deck knows everything it needs to know
about a playing card deck when it's started,
but this doesn't really know that, so,
I need a couple of things.
One thing I need is how many cards are we playing with?
Okay? How many cards are, is the person allowed to match total?
Right? What's the total amount of cards?
So we need that.
So I'm going to say, int with card count,
and we'll make an NSUInteger.
Really, that count has got to be at least two, you would think,
so I'd probably, in my initializer, should check
to make sure it's greater than one
and if it's not I'll probably return nil from self.
In other words, I won't initialize,
my [inaudible] will return nil.
Okay? And we'll, actually, we'll pick one way
where it does return nil, but we should probably check that too.
The second thing that I need is a deck, a deck of cards,
because I got to deal these cards out, right?
I'm going to deal them out and then you're going to be able
to pick some of them and do it.
So, I'm going to say using deck.
Deck star deck.
Okay, so there's the initializer, and, of course,
to do that I need to import deck, alright?
So that's pretty good.
Start there.
What else do I need for my game?
Well it's a game, so I think it should have a score.
[Pause] Eh, we'll say NSInteger score.
And, again, NSInteger,
NSUInteger versus unsigned int is kind of a style thing,
I used NSUInteger above, I'm going to use NSInteger
to be consistent here and my score can be negative possibly.
So that's why it's not an NSUInteger, it can be negative.
And notice I made this property read only.
It's the first time we've seen that.
And why is that?
Well that's because I'm the game logic,
I get to determine what the score is,
nobody can set the score, I tell you what the score is.
Therefore, there should be no setter for this property.
At least not publicly.
You're going to see in a moment, privately,
we're going to make this rewrite
so that we can set it privately, right?
Because we obviously need to be updating our score all the time
as people flip these cards over and get matches,
they get points, we need to update it.
But publicly, we want it to be a read only.
Okay? The only other thing I need to do is someone's going
to be clicking on these cards; they're going to be flipping
over and being chosen and possibly matching,
so I need some sort of method to let somebody choose a card.
So, I'm going to call this choose card at index,
NSUInteger, again, index.
So, there's a lot of ways in API you could have
for letting someone choose a card.
The person who creates this card matching game, not the person,
but the other object, like a controller
that creates this model class, knows the count,
because it specified the counter cards.
So, it's pretty reasonable to have them specify an index,
which means 0, in this count minus 1,
anytime someone chooses a card.
Okay?
So this is just a simple way of specifying
which card did the user choose?
And similarly, I actually need to be able to return a card
at a given index, and why do I need this?
Well, I need to be able to find out the state of the game
at any given time, I mean, how is my controller going
to display a UI for this game if it can't, you know,
if it can't find out what the cards, what the state
of the cards is, so this is just a little way for it
to get the cards, and it could iterate through all the cards
and get them all, and update them all, or it can get one
in a particular index and could kind of,
whatever fits best to what we're doing.
Alright, so, that's my public API.
Now before I go and do my implementation over here,
you can see that I got this warning right here
and what's this warning saying,
it's saying there's actually three of them, you can click
on this little three by the way,
it's saying you have not implemented these [pause],
these three methods that you made public, so that,
that's a good warning.
I need to go do that.
But before I do that, I'm going to go back to my controller,
I want to show you how we're going to,
what kind of UI we're going to have over here so that
as we're doing our implementation, you can kind
of imagine how they're going to work together,
but I actually wouldn't necessarily have
to do the UI before I do the card matching game
and one could argue I should do the card matching game first
and not be influenced by the UI so much
when I'm designing my model.
Okay? But, you know, this model is going to be served by some UI
so we're going to do them at the same time
so you can imagine it a little better.
So what do we need for this UI?
Well one thing is we need a lot more cards, okay, this one card,
it can't be matched against anything else,
so let's make more cards, this is very easy to do.
I'm just going to move this card up into the corner here
and I'm just going to copy and paste it, so I can select it
and copy and paste, okay?
Place it, use the nice blue guidelines there.
I can even copy two and paste them.
Okay? Or copy four, and paste them.
Okay? So now I'm going to have 12 cards,
that's a good number of cards.
I actually don't need this flips thing anymore, I'm not going
to be just flipping cards, I'm going to be matching cards,
I'm probably going to need something for the score
which we'll do a little bit later.
But that's pretty much my entire UI, I'm just going
to have these cards and I'm going to click on them,
it's going to show a card and then I'm going,
I could either click on it again to turn it back facedown,
un-choose it, or I could click on another card
and if it matches I should get some points
and if it doesn't match, the other one,
the last one I had is going to flip facedown.
Okay? Now those of you who are used to a game
like Concentration, it's a little different,
usually you pick a card, then you pick another card,
and if they match you get points,
if they don't match they usually both turn back over, okay?
Which would be a cool UI, but I haven't taught you how
to do animation and that's what you would need there
because that second card, when it comes up, would need to be
on screen for a little bit before they both went
down because you got to at least get to see this one.
So since you don't have animation, when you click
on that second one, it's going
to facedown the previous one if they don't match.
Okay? And then you just keep clicking around trying
to find matches and you get points, and we'll show you,
kind of the point system that I'm going to use.
So this is my entire UI.
Very, it's a very content-central UI, right?
The UI is really focused on these cards, there's not a lot
of adornments and other stuff around it.
And we're really not going to have much other stuff except
for the score and you're going to add like a redeal button
and maybe a little bit of stuff about some status
about what's going on in your homework,
but this is the fundamental basis of the UI.
Okay? So now let's go back to our card matching game,
and let's talk about how we're going to implement this thing.
I'm going to go back to automatic here.
Get this thing back.
Just to make this look nicer, I'm going to go like that.
And I'm going to leave this header file up while we go
and do the implementation so you can kind of see
where we're shooting, this is what we're shooting for.
[Pause] So, the first thing I want
to do is make this be readwrite in my implementation only.
And I do that with this little, private interface,
remember I showed you about this, that you can have one
of these with the little open parentheses, close parentheses
like that, and now you can declare your own things.
Well, I'm going to redeclare this thing
to be nonatomic readwrite, NSInteger score.
Okay?
Now this readwrite, we don't use that very much
because it's the default, right?
Is chosen, remember the properties chosen
and match that are in card.
Those didn't say readwrite
…