Posts tagged blackjack
Posts tagged blackjack
2 notes &
Added the source code of the blackjack tutorial for download.
click (t)here for the: sourcecode
2 notes &
Looking back I have made a working blackjack. But there are still a few open ends. So let’s start with the cleaning of the view. This removes all the drawn cards (of both hands) from the window’s NSView. Currently it was done in the BlackJackController’s startGame: method and while I do feel it should be initiated from there, the code actually doing the work should be moved to the BlackJackView class.
Again this is to keep inline with the MVC-model. The V in MVC, stands for the View, and it should do the stuff that’s needed for displaying the information (or erasing some of it, in this case).
So the startGame: method looked liked this:
-(IBAction) startGame:(id)sender{
for (NSImageView *timageview in [(BlackJackView *)self.view imageviews]) {
[timageview removeFromSuperview];
}
[[(BlackJackView *)self.view imageviews] removeAllObjects];
blackJackModel = [[BlackJackModel alloc] initWithController:self];
[blackJackModel setupGame];
}
And after the moving of the actual functionality of removing it will be turned into this:
-(IBAction) startGame:(id)sender{
[(BlackJackView *)self.view cleanView];
blackJackModel = [[BlackJackModel alloc] initWithController:self];
[blackJackModel setupGame];
}
0 notes &
And here it is. The (near) final part of the blackjack tutorial. In which i change nearly all the files, except the xibs, the AppDelegates and the deck class. The rest is changed - so this is one heck of an episode in the tutorial.
The main thing I’ve done now, is implement a real MVC-model. That means that the BlackJackModel class needs to do everything that has to do with blackjack. The controller needs only to receive the notifications from the view (a click on a button) and needs to set the view, based on signals from the BlackJackModel.
The BlackJackView is the least changed. All I did here is add a method that can set the HiddenStatus of the three button. I figured these buttons’ hidden status are kind of always set together, so i made one method:
-(void) hideHitBtn:(BOOL)hideHit standBtn:(BOOL)hideStand restartBtn:(BOOL)hideRestart
{
[self.hitBtn setHidden:hideHit];
[self.standBtn setHidden:hideStand];
[self.restartBtn setHidden:hideRestart];
}
7 notes &
Well. That looks really smart. But how come after clicking I still see a start button and not the hit and stand button. Obviously, it is because I haven’t said anything in the code about it yet.
And that’s what I will do now.
First I’ll declare the hitCard: and stand: methods in the BlackJackController.h - They look the same as the existing startGame: method.
2 notes &
In this part I’ll finally show those images of the cards in the application.
Of course since everything is already in it’s place - in the last part I’ve dealt the cards into the appropriate hands- all I need to do now, is add a way to show the images that belong to the cards in the hands.
The blackJackController knows the blackJackView and knows the blackJackModel that the hands are part of. So stuff needs to be done in the blackJackController.
First though, I decided it would be handy if a hand instance would “know” what type it was. Either a player’s hand or a table’s hand. So that means I have to add a property HandType (as an enumeration of Player or Table) and have it set through a new init, namely initWithHandType.
2 notes &
Looking at what I have now: a couple of classes for the Hand, Deck and Card. Then there is the BlackJackView and the BlackJackController and of course the tutorial21AppDelegate.
So let’s pretend I know what the Model-View-Controller (MVC) model is, and since i see a View and a Controller, the only thing missing is the Model. So let’s create a BlackJackModel file. If nothing else, it will look really smart.
Our model will have to know all about Blackjack. The model needs to be aware there are two hands: a playerhand and a tablehand. Also it has to be aware of the deck (of cards). And It has be smart enough to setup a game.
So, add a new class to the files, and choose it’s template to be “Objective-C class”. Be sure to make it a subclass of NSObject.
2 notes &
In this part I will set up the MainMenu.xib, containing the “window” of the application. That’s the one I’ve ignored up to now, by going straight to the Log Console.
Basically in the MainMenu.xib I only changed the window title to “Blackjack”.
That’s it so far.
Now I am going to add a BlackJackView Class. In the Classes directory add new file, choose as template: Mac OS X | Cocoa | Objective-C class. After clicking Next, the popup will ask you what Subclass it should be. Change this into: NSView.
0 notes &
So there’s a couple of things I want do to the card class:
1) add a property that will set the card open or close.
2) calculating the pipvalue differently if the property is close.
3) Add an NSImage to the card, that will either return the image of the card or a closed card, again depending on the property.
Also we’re gonna need some cards images. Of course you can draw them yourself in your almost-legal-if-you-actually-were-a-student-still-photoshop or whatever kind of drawing program you have. But for this tutorial, i am going to be lazy (again). So let’s use google to look for a nice deck. The first hit when typing: “deck of cards image” links to www.jfitz.com/cards/ and what would you know, it has a deck of playing cards as images in a zip. I choose the classic set and the .png format. Because PNG is how we roll these days on the interwebs.
5 notes &
So i’ve moved up to Xcode 4 and imported the subversion blackjack into Xcode 4. It worked quite well, though it did have an issue with the following line in my code, specifically in the initWithCardId: suit: in the card.m
if (self = [super init])
There are two solutions proposed by the warning either 1)make it a == if you actually want it to be a compare. or 2. place parentheses around the assignment to silence the warning.
It happens to not be a comparison, but a declaration. And if the declaration returns something other then nil, it will do the stuff in the if-statement. So. Double click on the warning on the left and click “fix-it : adding parenthesis…”
it turned the code into:
if ((self = [super init]))
And we can move along.
Next for our tutorial, we are going to add the class Hand.