PDA

View Full Version : BlackJack!



theplustwo
04-11-2005, 07:47 PM
CHECK IT OUT! (http://www.theplustwo.com/stuff/BlackJack.exe)

It's a crappy BlackJack game!

There's no graphics and there's some incorrect behavior, but it's under 600 lines. :)

Update: Now there are graphics! Ch-check it out:

http://www.theplustwo.com/stuff/blackjack.png

AtmaWeapon
04-11-2005, 08:25 PM
Countdown to whining about needing the .NET Framework installed....

Hey, Aces are supposed to be 11 or 1 :mad:

Otherwise pretty kewl theplustwo you are eventually going to motivate me into posting one of my projects :cool:

theplustwo
04-11-2005, 08:40 PM
Yeah, I have yet to make Aces behave properly, and also it's possible to get over 21 with your initial two cards and the system doesn't recognize that you've busted.

Edit: Atma, check your PMs.

Deviance
04-11-2005, 08:57 PM
Pretty nice application you made. What are you programming this in?

theplustwo
04-11-2005, 08:59 PM
C# .......

AtmaWeapon
04-11-2005, 09:05 PM
You've pretty much got the whole game here. Once the aces logic is in, you seem to have the display logic. From there it's just a matter of making a function to draw the cards and you can sell it to suckers for $$$ :)

Rainman
04-11-2005, 10:05 PM
I thought that I had done black jack in C, but apparently only did it in Matlab. I'll just post that. Maybe it'll give you an idea of how to do the aces. It's not that much different than C.
format compact
rand('state', sum(100*clock));
aces = 0;
daces = 0;
play='y';
while play=='y'
disp('-----------------------------------------------------------')
[cardstring, cardvalue]=getcardstring(get_card);
dealerhand=cardvalue;
disp(['Dealer has a ',cardstring,' showing.'])
% ----------------------------------------------
if cardvalue == 11
daces=daces + 1;
end
%-----------------------------------------------
%Player Action
[userstring1, uservalue1]=getcardstring(get_card);
[userstring2, uservalue2]=getcardstring(get_card);
disp('-----------------------------------------------------------')
disp(['You have a ',userstring1,' and a ', userstring2])
totalvalue=uservalue1+uservalue2;
% ----------------------------------------------
if uservalue1 == 11 & uservalue2 == 11
aces=aces + 1;
totalvalue = totalvalue - 10;
elseif uservalue1 == 11 | uservalue2 == 11
aces=aces + 1;
end
%-----------------------------------------------
disp(['Cards add up to ',num2str(totalvalue),' so far'])
choice=input('Do you want to hit or stand? (h,s) ','s');
while choice~='s'
if choice=='h'
[cardstringh, cardvalueh]=getcardstring(get_card);
% ----------------------------------------------
if cardvalueh == 11
aces=aces + 1;
end
%-----------------------------------------------
disp(['You got a ',cardstringh,'!'])
totalvalue=totalvalue+cardvalueh;
disp(['Cards now add up to ',num2str(totalvalue),' so far'])
if totalvalue > 21 & aces == 1
disp('An ace becomes a value "1"')
totalvalue=totalvalue-10;
aces=aces-1;
disp(['Cards now add up to ',num2str(totalvalue),' so far'])
elseif totalvalue > 21
break
end

else
disp('Bad Input!')
end
choice=input('Do you want to hit or stand? (h,s) ','s');
end
%Player Action ends
disp('-----------------------------------------------------------')
%Dealer begins
if totalvalue <= 21
[cardstring2, cardvalue2]=getcardstring(get_card);

disp(['Dealer shows a ',cardstring2,'!'])
dealerhand=dealerhand+cardvalue2;
% ----------------------------------------------
if cardvalue2 == 11 & dealerhand == 22
dealerhand=dealerhand-10;
elseif cardvalue2 == 11
daces=daces + 1;
end
%-----------------------------------------------
disp(['Dealer cards now add up to ',num2str(dealerhand),'.'])
pause
while dealerhand < 17
disp('Dealer has to hit!')
pause
[cardstring2, cardvalue2]=getcardstring(get_card);
disp(['Dealer shows a ',cardstring2,'!'])
dealerhand=dealerhand+cardvalue2;
disp(['Dealer cards now add up to ',num2str(dealerhand),'.'])
if cardvalue2 == 11
daces=daces + 1;
end
if dealerhand > 21 & daces == 1
disp('An ace becomes a value "1"')
dealerhand=dealerhand-10;
daces=daces-1;
disp(['Cards now add up to ',num2str(totalvalue),' so far'])
elseif dealerhand > 21
break
end
end
end
disp('Dealer is done.')
%Dealer ends
disp('-----------------------------------------------------------')
winlose(dealerhand,totalvalue)
play=input('Play again? ','s');
aces = 0;
daces = 0;
rand('state', sum(100*clock));
end


PS. I hate Matlab.

theplustwo
04-11-2005, 10:07 PM
Yeah, I was going to include card graphics, but after about five minutes of futal Google image searching I gave up. One of the other guys in my class has some cool ones, I may just ask him if I can borrow them.

Atma, how do you include images in a project so that you don't have to distribute the actual .jpg (or whatever) files?

Edit: Thanks for the help, Rainman. It should actually be fairly simple to get the Aces working properly, since each card is an instance of a Card class, I just have to modify the Value property's Get routine to return either a one or an eleven based on what the total value of the hand currently is.

AtmaWeapon
04-11-2005, 11:40 PM
OK this gets kind of hairy but it's not as hard as some dopes on forums will make it out to be.

Their solution
hey u need 2 buy this prorpietary resores editter and add em up!

My solution
In Visual Studio, add all of the image files to the project. Next, click on the image files and look at the property page for the files. Change the "Build Action" from "content" to "Embedded Resource". Now, download the file I've attached and look at the example for the code it takes to extract these bad boys.

This is not the best way to implement this a form that has a background image, but it's a good illustration of the guts of getting an embedded resource. For your card game, I'd personally have a startup loop that loads any image resources into an array of either Bitmap or Image. Oh, and the resize logic should NOT be called from within OnPaint, you get some crazy behavior if you resize the form ;). The lazier C# programmer could use Bitmap.FromResource to skip some of the steps I did. Watch your return types. Bitmap.FromStream does not return a Bitmap but an Image, because returning Bitmap would make too much sense.

Anyway, you can download the sample project here (http://www.atmaweapon.org/files/AGN/sample.zip) and those of you without Visual Studio can download the program here (http://www.atmaweapon.org/files/AGN/sample.exe) for a treat :)

BTW I used C# for this since it would be more useful to you but I must say, the C# IDE blows. It kept screwing up the indentation, forgetting to display Intellisense, forgetting to tell me the arguments to the function, forgetting to pop up enumeration values for me, etc. I like C# but man, you ought to take VB for a spin and see what a real IDE feels like :cool:

However, there are a few features of C# that I really wish were in VB; the ability to hook events through the Properties page is extremely useful, though you can do it from the code window in VB and not C#. It'd be nice to put the intelligence of VB's IDE with the features of C#'s IDE. From what I've seen, 2005 does this but who knows when we'll see that?

The application is roughly 400KB because I used a rather large image as the embedded resource but it will remind you off good times, theplustwo :)

P.S. Where did you go? You were on AIM and then you went AWOL.

theplustwo
04-18-2005, 11:08 AM
Just bumping this to say I implemented Atma's image embedding sample, and now it's GA-RAPHICAL!

Also: Aces work as they should and face cards are now all valued at 10, as they should be.

Glitch
04-18-2005, 12:47 PM
I see a error, that I can tell from your screenshot. The dealers are supposed to stand on 17, not hit.

War Lord
04-18-2005, 03:09 PM
If you're going to use Poker Stars' card graphics, at least get rid of the 4 color deck bs, that thing is hideous. Use a regular deck color scheme.