blackjack help
-
Thursday, August 30, 2012 10:42 PMI have been working on a (very very) basic blackjack game and it all works except for if you deciede to get a new card more than once, it will only store the most recent value for when the player gets a new card. To put it in a simplier way, if you get another new card it will delete the value of the last card that was given to the player and because i am very new to small basic i cant think of a way to fix this problem. Any help would be much appreciated
All Replies
-
Thursday, August 30, 2012 10:59 PMWelcome to the Small Basic Forums! Perhaps you could store all the card's values in an array? Small Basic's array's can be quite slow, but they get the job done. I could provide more help, if you post some code. Or perhaps you could use multiple variables to store that card's data, or you could write values to a .TXT and use the File.Readline function to get the value. That would be my choice.
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
- Edited by Noah Buscher Thursday, August 30, 2012 11:04 PM
-
Thursday, August 30, 2012 11:19 PM
Sub twist
twistrannum = Math.GetRandomNumber(11)
TextWindow.WriteLine(twistrannum)
finalnum = twistrannum + randnum3
If twistrannum + randnum3 <22 Then
stickortwist()
ElseIf twistrannum + randnum3 >22 then
TextWindow.WriteLine ("Oh no, your bust, better luck next time ")
EndIfEndSub
thats the code for when the player twists (gets a new card)
Sub stick
CPUnum = Math.GetRandomNumber(4) + 17
TextWindow.WriteLine (" Your number was " + finalnum)
TextWindow.WriteLine (" The computers number was " + CPUnum)
If CPUnum < finalnum Then
TextWindow.WriteLine ("Congratulations ! you won ")
Elseif CPUnum > finalnum then
TextWindow.WriteLine ("The computer had a higher number than you, better luck next time")'
elseif CPUnum = finalnum then
TextWindow.WriteLine ("It's a draw !")
EndIf
EndSuband thats the code for figuring out if the player has won or lost
im not to confident in using arrays as im still learning, but i could try and do that i suppose
-
Thursday, August 30, 2012 11:29 PMWould there be any reason you would need to keep the old card's value if the player picked a new one? But if you need to store two card values, you would be best off storing the card's values in a .txt file. Please let me know if you have any more questions. :)
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
-
Thursday, August 30, 2012 11:37 PMyes, because each cards is ment to be kept to go towards the ultimate goal of getting as close to 21 as possible and how would i store them in a text file and retrieve them ?
-
Thursday, August 30, 2012 11:44 PM
OK... Save As the program in a Folder, and run this program:
card1 = Math.GetRandomNumber(10) card2 = Math.GetRandomNumber(10) File.WriteLine(Program.Directory + "\CardValues.txt", 1, card1) File.WriteLine(Program.Directory + "\CardValues.txt", 2 card2)
It will get a random number for card 1 and 2 and store them in a .txt file called "CardValues.txt" that it creates. To read the values:
card1 = File.ReadLine(Program.Directory + "\CardValues.txt", 1) card2 = File.ReadLine(Program.Directory + "\CardValues.txt", 2) TextWindow.WriteLine(card1) TextWindow.WriteLine(card2)
To over-write the variables, just use the file.writeline command with the same line # and the program will over-write the value. Hope it helps! :)
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
- Proposed As Answer by Noah Buscher Friday, August 31, 2012 3:15 AM
- Marked As Answer by Ed Price - MSFTMicrosoft Employee, Owner Thursday, October 18, 2012 3:02 AM
-
Friday, August 31, 2012 12:11 AMbecause there is no way of knowing how many times the user may want a new card would there be any way of putting that operation in a loop ?
-
Friday, August 31, 2012 12:14 AM
You could do a While Loop:
while endGame = "False" checkForNewCard endWhileor you could use a GoTo:
Loop: 'CODE GOES HERE Goto Loop
Please let me know if you need any more help. :)
-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
- Edited by Noah Buscher Friday, August 31, 2012 12:15 AM
-
Friday, August 31, 2012 7:04 AMAnswerer
Heyya samualsock!
One of the secrets to design a good logical program is to know how to make subroutines w/ very specific tasks.
For your BlackJack game, we can have a Sub to draw 1 card, check total drawn, ask user what to do, check bust hand, compare results, etc.
And then, after initializing all necessary variables, make a game loop which will call each Sub orderly.
Below is what I got. Hope you like it. ^_^
'#############################################' 'BlackJack (v1.1) 'SamualSock & GoToLoop (2012/Aug/31) 'http://social.msdn.microsoft.com/Forums/en-US/smallbasic '/thread/8520042c-fc42-49a4-ba97-f6b0eda8b0ee '#############################################' '________________________________________________________________________' TextWindow.BackgroundColor = "DarkGreen" TextWindow.ForegroundColor = "Yellow" LF = Text.GetCharacter(10) ESC = Text.GetCharacter(27) '________________________________________________________________________' GameLoop: TextWindow.Clear() ResetVars() While isTwisting DrawACard() GetTotalSum() If total < 22 Then StickOrTwist() Else isTwisting = "False" EndIf If key = "S" Then isTwisting = "False" EndIf EndWhile Stick() AskExit() Goto GameLoop '________________________________________________________________________' Sub ResetVars cardsInHand = "" cards = "" isTwisting = "True" EndSub '________________________________________________________________________' Sub StickOrTwist TextWindow.WriteLine("<S>tick or <T>wist?") key = "" While key <> "S" And key <> "T" key = Text.ConvertToUpperCase( TextWindow.ReadKey() ) Sound.PlayClick() EndWhile TextWindow.WriteLine(LF) EndSub '________________________________________________________________________' Sub Stick cpuTotal = Math.GetRandomNumber(4) + 17 TextWindow.WriteLine("CPU got a hand total of " + cpuTotal + LF) If total > 21 Then TextWindow.WriteLine("Oh no, you're bust!!! Better luck next time!" + LF) Sound.PlayBellRing() ElseIf cpuTotal < total Then TextWindow.WriteLine("Congratulations ! You won!!!" + LF) Sound.PlayChime() ElseIf cpuTotal > total Then TextWindow.WriteLine("It's higher than yours! Better luck next time!" + LF) Sound.PlayBellRing() Else TextWindow.WriteLine("It's a draw!" + LF) Sound.PlayChimes() EndIf EndSub '________________________________________________________________________' Sub DrawACard cardsInHand = cardsInHand + 1 cards[cardsInHand] = Math.GetRandomNumber(11) TextWindow.WriteLine("You got a/an " + cards[cardsInHand] + LF) EndSub '________________________________________________________________________' Sub GetTotalSum total = "" For cardNum=1 To cardsInHand total = total + cards[cardNum] EndFor TextWindow.WriteLine("Current total is " + total + LF) EndSub '________________________________________________________________________' Sub AskExit TextWindow.WriteLine ("Hit 'Q' ou 'Esc' to quit or any other to restart..." + LF) key= Text.ConvertToUpperCase ( TextWindow.ReadKey() ) If key = "Q" Or key = ESC Then Sound.PlayClickAndWait() Program.End() EndIf EndSub '________________________________________________________________________'Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
-
Friday, August 31, 2012 8:58 AMAnswerer
Well, the version above uses an array variable called cards[]. However, its usage there is totally unnecessary! I've just wanted to introduce you how arrays worked! :P
Anywayz, the only thing we need is to keep feeding the var total with all of the drawn card's values. Nothing complex, so no need to use an array to keep track of all cards drawn!
To remove the array var cards[] and its auxiliary index var cardsInHand, just replace the old code w/ the following modified Subs below:
Sub ResetVars total = "" isTwisting = "True" EndSub
Sub DrawACard randNum = Math.GetRandomNumber(11) total = total + randNum TextWindow.WriteLine("You got a/an " + randNum) TextWindow.WriteLine("Current total is " + total + LF) EndSubAnd remove Sub GetTotalSum from whole code, since Sub DrawACard unified both tasks now.
And that's it!
Still, there is much more you can do to better your BlackJack game.
For example, you can add score & hiscore; a money bet mechanism in which both player & CPU have to pay for each card drawing; and also display a number/letter or figure representing the card just drawn.
But for the bet mechanism to work, you have to make an AI for CPU to draw 1 card each time and to decide to keep drawing or not, like a player is obliged to go through; rather than having CPU toss a safe 21-ranged random number!
And of course, in the future, convert this TextWindow console mode into a GraphicsWindow full-fledged game!
Well, cya laterz!
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
- Edited by GoToLoopEditor Friday, August 31, 2012 9:16 AM
- Proposed As Answer by Ed Price - MSFTMicrosoft Employee, Owner Tuesday, October 09, 2012 12:50 AM
- Marked As Answer by Ed Price - MSFTMicrosoft Employee, Owner Thursday, October 18, 2012 3:01 AM
-
Friday, August 31, 2012 10:11 AM
Thank you for the response, i havnt had time to look through it thoroughly but ive already got a few ideas of how to use what you said :P however i dont quite understand whatLF = Text.GetCharacter(10)
does ? and it is used quite alot through out the code -
Friday, August 31, 2012 12:30 PM
GetCharacter
Text.GetCharacter(characterCode)Given the unicode character code, gets the corresponding character, which can then be used with regular text.
characterCode
The character code (Unicode based) for the required character.Returns
A unicode character that corresponds to the code specified.-Noah J. Buscher "Nothing is Impossible Until Proven Impossible."
-
Friday, August 31, 2012 5:06 PMAnswerer
The ASCII code 10 means Line Feed. It is a special non-printable character which makes the cursor to jump to next line.
With LF = Text.GetCharacter(10), I can use the var LF in various places as a shortcut replacement of the longer TextWindow.WriteLine("")! :P
Here's a picture to illustrate the concept better:
Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)
- Edited by GoToLoopEditor Friday, August 31, 2012 5:24 PM
- Proposed As Answer by Ed Price - MSFTMicrosoft Employee, Owner Tuesday, October 09, 2012 12:50 AM
- Marked As Answer by Ed Price - MSFTMicrosoft Employee, Owner Thursday, October 18, 2012 3:01 AM

