Wednesday, February 6, 2013

Simple Blackjack Game

So here it is. I was messing around tonight and I decided that I would make a Blackjack game. I use comments to describe every step of the script. This is an interactive game to play Blackjack on the command line. The first part make a deck and shuffles it, second part uses get.chomp so you can actually write hit or stay in the command line to draw cards. As the cards are drawn the are eliminated from the array with the .pop method in order to draw a fresh card from the pile each time. After you say "stay" it moves on to the dealer section that plays by Casino rules. Afterwords I use boolean logic in order to evaluate the player vs. dealer and see the verdict of the game.

The limitations so far are that I have not written logic in order to turn an Ace into an 11 or 1 given the circumstance, which should not be too difficult but I have to be up at 4:45am tomorrow so I am running out of time. I also want if the player draws a 21 in the initial hand for the game to be immediately over, but I feel that I will have to put the whole thing in a while loop with a break if statement in order for this to work correctly. I will get back to this...Enjoy for now!

  1. puts ' '
  2. puts "So you want to play Blackjack?"
  3.  
  4. # Part 0__________________________________
  5. # Build array of cards to make a full deck
  6. one_suit = [2,3,4,5,6,7,8,9,10,10,10,10,11];
  7. full_deck = one_suit*4;
  8. deck = full_deck.dup;
  9.  
  10. # Part 1_____________________________________________
  11. # Shuffle: Where the deck is shuffled before starting
  12. new_deck = deck.shuffle
  13.  
  14. # Part 2_________________________________________________________________
  15. # Initial Deal: Where cards are initially dealt to both Player and Dealer
  16. player_initial_hand = new_deck[51] + new_deck[50]
  17. dealer_initial_hand = new_deck[49] + new_deck[48]
  18. puts ' '
  19. puts "Your hand: #{new_deck[51]}, #{new_deck[50]} = #{player_initial_hand}"
  20. new_deck.pop
  21. new_deck.pop
  22. new_deck.pop
  23. new_deck.pop
  24.  
  25. # Part 3_________________________________________________________
  26. # Player's turn: Decided to hit or stay via interactive get.chomp
  27. puts "Would you like to hit or stay?"
  28. until gets.chomp == "stay"
  29.         player_initial_hand += new_deck.last
  30.         new_deck.pop
  31.         puts "Hand = #{player_initial_hand}. Would you like to hit or stay?"
  32. end
  33.         puts ' '
  34.         puts "Players's final hand: #{player_initial_hand}"
  35.  
  36. # Part 4____________________________________________________________________________
  37. # Dealer's turn: Hits or stays based on casino rules of hitting on anything below 15
  38. until dealer_initial_hand >15
  39.         dealer_initial_hand += new_deck.last
  40.         new_deck.pop
  41. end
  42.         puts "Dealer's final hand: #{dealer_initial_hand}"
  43.         puts ' '
  44.  
  45. # Part 5______________________________________________________________________
  46. # Evaluation: This will use boolean logic to see if player or dealer wins game
  47.  
  48. if player_initial_hand == 21
  49.         puts "#{player_initial_hand}"
  50.         puts "BLACKJACK, You Win!"
  51. elsif player_initial_hand <21 && player_initial_hand > dealer_initial_hand && dealer_initial_hand <=21
  52.         puts "#{player_initial_hand}, vs. #{dealer_initial_hand}"
  53.         puts "You win!"
  54. elsif player_initial_hand == dealer_initial_hand
  55.         puts "#{player_initial_hand}, vs. #{dealer_initial_hand}"
  56.         puts "Draw"
  57. elsif player_initial_hand <21 && player_initial_hand < dealer_initial_hand && dealer_initial_hand <=21
  58.         puts "#{player_initial_hand}, vs. #{dealer_initial_hand}"
  59.         puts "You lose"
  60. elsif player_initial_hand <21 && dealer_initial_hand > 21
  61.         puts "#{player_initial_hand}, vs. #{dealer_initial_hand}"
  62.         puts "Dealer Bust, You Win!"
  63. else player_initial_hand > 21
  64.         puts "#{player_initial_hand}, vs. #{dealer_initial_hand}"
  65.         puts "BUST! You Lose."
  66. end
  67.  
  68. # The End

No comments:

Post a Comment