Computer Science Canada

A very crude Blackjack.

Author:  Mackie [ Sun Apr 06, 2008 9:49 pm ]
Post subject:  A very crude Blackjack.

Here is a very crude blackjack, I'm just trying to learn ruby, so I thought it would be a fun project to try out. It's pretty crude, I'm actually making a really cool deck class for super-accurate shuffling, and probability.

I'd love feed back/things I did wrong. Trying to get out of the Turing mindset proves difficult.

Ruby:
# Mackie Drew
# Black Jack
# April 6th, 2008

# Set defaults
dealerCards = [rand(10),rand(10)]
playerCards = [rand(10),rand(10)]
playAgain = "1"

# Pushes Screen Down
def cls
        puts "\n" * 22
end

def total(hand)
        total = 0
        0.upto(hand.size){|i| total += hand[i].to_i}
        total.to_i
end

def end_of_round(playerHand, dealerHand)
        puts "Dealer's Total: #{total(dealerHand)}"
        puts "Your Total: #{total(playerHand)}\n\n"

        if total(playerHand) > 21 :
                puts "Bust!\nYou Lose. :("
                playAgain = "2"
        elsif total(dealerHand) > 21 :
                puts "Dealer Bust!\nYou Win!"
                playAgain = "2"
        else
                if total(playerHand) > total(dealerHand) :
                        puts "You Win!"
                else
                        puts "Dealer Wins. :("
                end
        end
end

#Title Menu
cls
puts "-----Mackie's Black Jack-----"
puts "1: New Game"
puts "2: Exit"

menuChoice = gets

while menuChoice.include?("1") :

        while total(dealerCards) < 16 :
                dealerCards += [rand(10)]
        end

        while playAgain.include?("1") :
               
                cls
                puts "Dealers Hand: #{dealerCards[0]}, #{dealerCards.collect{|x| if x > 3 : "??, " end}}\n"
            puts "Your Hand: #{playerCards.collect{|x| x.to_s + ", "}}"
                puts "Your Total: #{total(playerCards)} \n\n\n\n\n\n\n"
               
                puts "1: Hit"
                puts "2: Stay"
               
                turnChoice = gets
               
                if turnChoice.include?("1") :
                        playerCards += [rand(10)]
                        if total(playerCards) > 21 :
                                playAgain = "2"
                        end
                elsif turnChoice.include?("2") :
                        playAgain = "2"
                end
        end
       
        cls
       
        end_of_round(playerCards, dealerCards)
       
        puts "\n1: Play Again"
        puts "2: Exit"
        playAgain = gets
        if playAgain == "2\n" :
                menuChoice = "2"
                playAgain = "2"
        else
                dealerCards = [rand(10),rand(10)]
                playerCards = [rand(10),rand(10)]
                playAgain = "1"
        end
       
end

Author:  wtd [ Mon Apr 07, 2008 12:42 am ]
Post subject:  RE:A very crude Blackjack.

code:
def cls
        puts "\n" * 22
end


code:
def clear_screen
   22.times { puts }
end

Author:  wtd [ Mon Apr 07, 2008 11:03 am ]
Post subject:  RE:A very crude Blackjack.

code:
                if turnChoice.include?("1") :
                        playerCards += [rand(10)]
                        if total(playerCards) > 21 :
                                playAgain = "2"
                        end
                elsif turnChoice.include?("2") :
                        playAgain = "2"
                end


Try making use of regular expressions instead of include? Smile

Author:  Mackie [ Mon Apr 07, 2008 1:16 pm ]
Post subject:  Re: RE:A very crude Blackjack.

wtd @ Mon Apr 07, 2008 12:42 am wrote:
code:
def cls
        puts "\n" * 22
end


code:
def clear_screen
   22.times { puts }
end


Thanks wtd, I changed it, I just don't know if there is any advantage to the change. Is it just common practice to do it your way?

wtd @ Mon Apr 07, 2008 11:03 am wrote:
code:
                if turnChoice.include?("1") :
                        playerCards += [rand(10)]
                        if total(playerCards) > 21 :
                                playAgain = "2"
                        end
                elsif turnChoice.include?("2") :
                        playAgain = "2"
                end


Try making use of regular expressions instead of include? Smile


I had a feeling someone was going to mention that. Razz
I wasn't sure why regular expressions weren't working until later on, when i realized it added the eol character.

Author:  wtd [ Mon Apr 07, 2008 2:09 pm ]
Post subject:  Re: RE:A very crude Blackjack.

Mackie @ Tue Apr 08, 2008 2:16 am wrote:
wtd @ Mon Apr 07, 2008 12:42 am wrote:
code:
def cls
        puts "\n" * 22
end


code:
def clear_screen
   22.times { puts }
end


Thanks wtd, I changed it, I just don't know if there is any advantage to the change. Is it just common practice to do it your way?


Which is more self-documenting?

Author:  ambok [ Tue Nov 16, 2010 7:48 am ]
Post subject:  Re: A very crude Blackjack.

if turnChoice.include?("1") :
playerCards += [rand(10)]
if total(playerCards) > 21 :
playAgain = "2"
end
elsif turnChoice.include?("2") :
playAgain = "2"
end

Author:  Insectoid [ Tue Nov 16, 2010 10:31 am ]
Post subject:  RE:A very crude Blackjack.

Check your date stamps bro.


: