Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 BlackJack Game Help
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
l0tt0




PostPosted: Sat Oct 18, 2003 11:34 pm   Post subject: BlackJack Game Help

Ok, I just started learning Turing this year in school. So far we have learned if/elseif, variables, and yesterday we just learned basic loops and how to exit them. My teacher hasn't taught us arrays yet but I went forward and read a couple tutorials on them. Today I tried making a BlackJack Game which is really basic but the problem I'm having is:

- The code will only finish when you bust

Here's my code

code:


    % BlackJack Game
   
    var card: array 1..10 of int
    var input: array 1..10 of string
    var total: int
    var dealer: int
    var counter: int
   
   
    total:= Rand.Int(1,10)   
    counter:= 1
   
   
    loop
   
        put "You have ", total, ", would you like to Hit or Stay? <H/S>   "..
        get input(counter)
       
        if input(counter) = "H" or input(counter) = "h" then
            card(counter + 1):= Rand.Int(1,10)
        end if
       
        total:= card(counter + 1) + total
       
        exit when input(counter) = "S" or input(counter) = "s" or total > 21
       
        counter:= counter + 1
               
    end loop
   
    dealer:= Rand.Int(1,21)
   
   
    put ""
    put "------------------------"
    put "The dealer has ", dealer, "!"
    put "You have ", total, "!"
    put "------------------------"
   
       if total > 21 then
            put "You busted, dealer wins!"
    elsif total > dealer then
            put "You win!"
    elsif total = dealer then
            put "You and the dealer tied!"
    else
            put "The dealer was better than you!"
    end if


Thanks in advance to anyone that can help me out! Smile
Sponsor
Sponsor
Sponsor
sponsor
l0tt0




PostPosted: Sat Oct 18, 2003 11:43 pm   Post subject: Figured It Out

I figured it out, it wasn't hard at all. But I haven't learned about alot of this stuff yet so it was sort of hard for me. But reading posts on here is really helpful Smile.


Here's my finished code (for now, as I learn more I will make it better).
Could some people still give me some comments/suggestions on this though please? Thanks!

code:


    % BlackJack Game
   
    var card: array 1..10 of int
    var input: array 1..10 of string
    var total: int
    var dealer: int
    var counter: int
   
   
    total:= Rand.Int(1,10)   
    counter:= 1
   
   
    loop
   
        put "You have ", total, ", would you like to Hit or Stay? <H/S>   "..
        get input(counter)
       
        if input(counter) = "H" or input(counter) = "h" then
            card(counter + 1):= Rand.Int(1,10)
        else
            exit
        end if
       
        total:= card(counter + 1) + total
       
        exit when total > 21
       
        counter:= counter + 1
               
    end loop
   
    dealer:= Rand.Int(1,21)
   
   
    put ""
    put "------------------------"
    put "The dealer has ", dealer, "!"
    put "You have ", total, "!"
    put "------------------------"
   
       if total > 21 then
            put "You busted, dealer wins!"
    elsif total > dealer then
            put "You win!"
    elsif total = dealer then
            put "You and the dealer tied!"
    else
            put "The dealer was better than you!"
    end if
   
   
Tony




PostPosted: Sat Oct 18, 2003 11:46 pm   Post subject: (No subject)

I'm confused Confused

I dont see the point in the use of array the way you have your game set up.

you would just enclose everything from "total:=" line to the end in another loop/endloop to run your game indefinatly.

Thing is... thats not how blackjack is played. Dealer can also bust. And this way you dont display what cards one gets. And if one starts figuring cards out by change in total, then one can get 5 of a kind Laughing

I donno how accurate your game has to be.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
l0tt0




PostPosted: Sun Oct 19, 2003 12:18 am   Post subject: (No subject)

tony wrote:

And if one starts figuring cards out by change in total, then one can get 5 of a kind


i see what you mean by the array thing thanks alot. I know thats not how blackjack is played i was just trying to keep it simple for this point. I didnt really understand about what you mean by what i quoted...could you explain this a bit more? I have just spent a bit more time making it better, and tomorrow I am going to try and make it so the dealer can bust. This is the updated version I have.

code:


    % BlackJack Game
   
    var card:    int
    var input:   string
    var plays:   string
    var total:   int
    var dealer:  int
    var counter: int
   
   
   
   
   
    loop
   
        total:= Rand.Int(1,10)   
           
        loop
   
            put "You have ", total:2, ", would you like to Hit or Stay? <H/S>   "..
            get input
       
            if input = "H" or input = "h" then
            card:= Rand.Int(1,10)
            else
                exit
            end if
       
            total:= card + total
       
            exit when total > 21
       
           
               
        end loop
   
        dealer:= Rand.Int(1,21)
   
   
        put ""
        put "-----------------------------------------------------"
        put "The dealer has ", dealer, "!"
        put "You have ", total, "!"
        put "-----------------------------------------------------"
   
        if total > 21 then
            put "You busted, dealer wins!"
        elsif total > dealer then
            put "You win!"
        elsif total = dealer then
            put "You and the dealer tied!"
        else
            put "The dealer was better than you!"
        end if
       
             
        put "-----------------------------------------------------"
        put "Would you like to play again? <Y/N>    "..
        get plays
       
        if plays = "N" or plays = "n" then
            exit
        else
            cls
        end if

    end loop       



btw. is there a function to close the run window screen if they choose N?
Thanks
Tony




PostPosted: Sun Oct 19, 2003 12:31 am   Post subject: (No subject)

what I ment is that since you randomly chouse the value of the card instead of card itself, same card can be picked out more then once. You'd understand what I'm talking about when you deside to draw which cards are in play.

Also thing might come in useful - Dealer's AI:

hit untill total of 16 or more.

I mean come on... Dealer will not stop at 1 as it shows in some of your cases (for one, there're 2 cards) Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
l0tt0




PostPosted: Sun Oct 19, 2003 12:24 pm   Post subject: (No subject)

ok i understand what you meant. And ya i was gonna make something like the dealer hits until 16.

btw. is there a way to close the run window at the end fo it?
l0tt0




PostPosted: Sun Oct 19, 2003 12:38 pm   Post subject: (No subject)

Here is the updated version with dealer AI.

code:


    % BlackJack Game
   
    var card:    int
    var input:   string
    var plays:   string
    var total:   int
    var dealer:  int
    var dcard:   int
   
   
   
   
   
    loop
   
        total:= Rand.Int(1,10)   
           
        loop
   
            put "You have ", total:2, ", would you like to Hit or Stay? <H/S>   "..
            get input
       
            if input = "H" or input = "h" then
            card:= Rand.Int(1,10)
            else
                exit
            end if
       
            total:= card + total
       
            exit when total > 21
       
           
               
        end loop
   
        loop
            dealer:= Rand.Int(1,11)
           
            if dealer < 16 then
                dcard:= Rand.Int(1,11)
                dealer:= dealer + dcard
            end if
           
            exit when dealer >= 16
           
        end loop
           
       
       
   
   
        put ""
        put "-----------------------------------------------------"
        put "The dealer has ", dealer, "!"
        put "You have ", total, "!"
        put "-----------------------------------------------------"
   
        if total > 21 and dealer > 21 then
            put "You and the dealer busted!"
        elsif total > 21 then
            put "You busted, dealer wins!"
        elsif total = dealer then
            put "You and the dealer tied!"
        elsif total > dealer then
            put "You win!"
        elsif dealer > 21 then
            put "The dealer busted, you win!"
        else
            put "The dealer wins!"
        end if
       
             
        put "-----------------------------------------------------"
        put "Would you like to play again? <Y/N>    "..
        get plays
       
        if plays = "N" or plays = "n" then
            exit
        else
            cls
        end if

    end loop       
   
   
Tony




PostPosted: Sun Oct 19, 2003 1:13 pm   Post subject: (No subject)

ok, thats better

to close the program, you call
code:
quit


oddly enough, it quits the program.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
l0tt0




PostPosted: Sun Oct 19, 2003 1:47 pm   Post subject: (No subject)

ok thanks alot for all your help Very Happy
Mazer




PostPosted: Mon Oct 20, 2003 8:03 am   Post subject: (No subject)

if you want to close the run window, you'll first have to open it (manually)

code:

var win := Window.Open ("graphics:640;480") % opens the window, put this at beginning

Window.Close (win) % closes the window put this... in the middle (haha, just kidding)
morgoth




PostPosted: Fri Nov 21, 2003 8:03 pm   Post subject: black jack eh?

I also finished a blackjack game for my school I think it's very good but I think I will upload it later hopefully I can get some bits for it lol Surprised
morgoth




PostPosted: Fri Nov 21, 2003 8:08 pm   Post subject: (No subject)

for your deck randomizer you should make an array deck :1..52 create a for loop that will place the numbers 1 to 13 four times then have another for loop that will swap the different numbers, if you use this idea you can never get more that four of the same card.
Tony




PostPosted: Fri Nov 21, 2003 8:17 pm   Post subject: (No subject)

well you just need to fill the array with numbers in order. Then you can use my random number list function to "shuffle" the deck
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
thoughtful




PostPosted: Fri Nov 21, 2003 11:18 pm   Post subject: (No subject)

hmm...if you want to make something which invloves graphics and stuff you should make 2d array for example
code:

var cards:array 1..4,1..13 of int

now you can load the pictures in it. 1..4 represents the four suits, where as 1..13 represents the the cards. so if you choose 1 to be you ACES than if you have card(1,13) it is the king of ACES.
You can also check if the card has been played before or not. this is not that hard to make. You just need all the pictures of cards and then tell computer that cards from 10-13 are worth 10, 1 is worth 1 or 11 and the rest are the same.
You can even have like the picture of a deck where u click to get hit or you can knock on the table to stay.
If you wana take this to the next level you can have the first card face down as in real black jack. Thats simple also you just need the program to draw a face down card picture for any first card and add its value to the total and then the rest are all gonna be face up cards.

There might be an easier way of making this if you wana go ahead with thie graphical version maybe u can let the forum know and somebody might tell you an easier way.
Tony_200004




PostPosted: Thu Dec 04, 2003 9:22 pm   Post subject: (No subject)

One thing... when you program is quitting instead of
if reply = "n" then
exit
do
if reply = "n" then
quit
the exit doesnt work as well as the quit... the exit is almost like a crash when you use it to close the program and the quit just is a straight out quit
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: