
-----------------------------------
l0tt0
Sat Oct 18, 2003 11:34 pm

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 



    % 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?    "..
        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! :)

-----------------------------------
l0tt0
Sat Oct 18, 2003 11:43 pm

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 :).


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!



    % 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?    "..
        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
Sat Oct 18, 2003 11:46 pm


-----------------------------------
I'm 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  :lol:

I donno how accurate your game has to be.

-----------------------------------
l0tt0
Sun Oct 19, 2003 12:18 am


-----------------------------------

 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.



    % 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?    "..
            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?     "..
        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
Sun Oct 19, 2003 12:31 am


-----------------------------------
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)  :lol:

-----------------------------------
l0tt0
Sun Oct 19, 2003 12:24 pm


-----------------------------------
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
Sun Oct 19, 2003 12:38 pm


-----------------------------------
Here is the updated version with dealer AI.



    % 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?    "..
            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?     "..
        get plays
        
        if plays = "N" or plays = "n" then
            exit
        else
            cls
        end if

    end loop        
    
    


-----------------------------------
Tony
Sun Oct 19, 2003 1:13 pm


-----------------------------------
ok, thats better

to close the program, you call quit

oddly enough, it quits the program.

-----------------------------------
l0tt0
Sun Oct 19, 2003 1:47 pm


-----------------------------------
ok thanks alot for all your help :D

-----------------------------------
Mazer
Mon Oct 20, 2003 8:03 am


-----------------------------------
if you want to close the run window, you'll first have to open it (manually)


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
Fri Nov 21, 2003 8:03 pm

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 :o

-----------------------------------
morgoth
Fri Nov 21, 2003 8:08 pm


-----------------------------------
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
Fri Nov 21, 2003 8:17 pm


-----------------------------------
well you just need to fill the array with numbers in order. Then you can use my [url=http://www.compsci.ca/v2/viewtopic.php?t=2184]random number list function to "shuffle" the deck

-----------------------------------
thoughtful
Fri Nov 21, 2003 11:18 pm


-----------------------------------
hmm...if you want to make something which invloves graphics and stuff you should make  2d array for example

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
Thu Dec 04, 2003 9:22 pm


-----------------------------------
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

-----------------------------------
poly
Thu Dec 04, 2003 9:52 pm


-----------------------------------
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 quitEXIT's are used to exit loops or for statements, QUIT is to quit programs
