Posts tagged programming
Posts tagged programming
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];
}
3 notes &
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):
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.
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.
7 notes &
So here’s the 3rd part of the tutorial - and boy does it takes more time than planned.
Basically what I have from the first three parts (including part 0, that is) of the tutorial is now a Deck with playing cards, that can be initialized and print itself to the NSLog to prove it is somewhat working.
Hence we’ll now add the implementation of the shuffle and the drawCard methods.
The drawCard will draw the card from the deck. It is not going to draw a card on the screen. If you’re waiting for graphics in this tutorial, there will be some. But not just yet.