3 notes &
Cocoa Coding:BlackJack tutorial - PART 10
So the last part of magic, before I need to do some major tweaking, is done in the stand: method in the BlackJackController.
I’ve already promised that I would add some AI there and for this short part of the tutorial, all I’ll do is make the dealer either draw a card or stand.
After I’ll add that - it’s sort of finished.
However there will be some points open and ready to discuss later on (not limited to these):
- Doing most of the AI in the controller feels wrong, it should probably be in the model. (Though I have to read something first to decide upon it).
- If the game is finished there should be some kind of “You won”, “You lost”, thing.
- The ace. it’s now always 11. That’s just plain wrong.
- If table has 21 after being cards being dealt, table’s card should be showed immediately - and if player doesn’t also have 21. “You lost”
- …
Anyway let’s ignore all those points for now and open the BlackJackController.m
-(IBAction) stand:(id)sender{
[[(BlackJackView *)self.view hitBtn] setHidden:YES];
[[(BlackJackView *)self.view standBtn] setHidden:YES];
[blackJackModel openTableHand];
[(BlackJackView *)self.view showHand:blackJackModel.tableHand];
// We'll add some AI later
// if table should draw <=16
// then draw card
while ([blackJackModel.tableHand getPipValue]<=16) {
[blackJackModel.tableHand addCard:[blackJackModel.deck drawCard] open:YES];
[(BlackJackView *)self.view showHand:blackJackModel.tableHand];
}
// while table >16 and < player and > 21
// then draw card
while ([blackJackModel.tableHand getPipValue] < [blackJackModel.playerHand getPipValue] ) {
[blackJackModel.tableHand addCard:[blackJackModel.deck drawCard] open:YES];
[(BlackJackView *)self.view showHand:blackJackModel.tableHand];
}
[[(BlackJackView *)self.view restartBtn] setHidden:NO];
}
I’ve added some source comments to tell you what it does. And basically it is quite disrespectful to AI to call it AI.
Also you can see the code in both “while groups” is exactly the same. That suggest that we could do it prettier, by refactoring the code outside the while loops and just make it a (internal) method call: that draws a card from the deck, add it to the table’s hand and then shows the hand.
Further more, after doing a small “run” testdrive - I have to do somehing that resets everything. Otherwise old cards show after the Start button gets clicked at the end of the game.
So in the start:method in BlackJackController.m I’ve added this as the first two lines of the method:
for (NSImageView *timageview in [(BlackJackView *)self.view imageviews]) {
[timageview removeFromSuperview];
}
[[(BlackJackView *)self.view imageviews] removeAllObjects];
What this does is: loops through all the NSImageView (that’s the graphic interpretation of the card) that are in the view’s imageViews and remove them from the View that they are drawn on.
After removing them from that “superview”, we remove them all together.
But like I wrote earlier - I’ve decided to make this a very short part (because I am sleepy, because my 2yo didn’t sleep last night.)
The goal is to show you how easy it was from the part 9 to this. a fully working (slightly wrong blackjack).
More in the near future where I will fix the ace (at the very least). (But probably not tomorrow).
Please let me know if you liked this tutorial so far by either “liking” it, or by leaving a comment. Don’t be nasty in the comments please.
Next chapter: Blackjack tutorial - part 11.
Previous chapter: Blackjack tutorial - part 9.