Week one was all about getting your programming up to par.
Class would start at 930 with a mini quiz. Basically a warm up to get yourself in the data mood. Then we would move on to the morning lecture. Introducing a new idea/frame work of how to do things. After that we moved onto the indiviudal assignment for the day. Here we explore the idea in the first lecture and get our feet wet in that area. Time absolutely flew by this first week. After the individual assignment and lunch (free mini food at galvanize btw) it was on to the afternoon lecture. This time we take the topic that we learned in the morning and go more in depth. The lectures are not necessarily meant to teach you all the little details, but rather show you what is possible, get the ideas in your head, and prepare you enough to go out and try/fail/succeed repeat on your own. Once the afternoon lecture is done we move on to the pair programming assignment. This is usually a much larger task and one that requires a ton of pre planning. I've been fortunate to have good partners and learn a ton from other people.
I spent the weekend preping for week 2 which is all about statistics. Although week 1 was tiring, it wasn't conceptually difficult to grasp the topics. I know stats however, is going to be a little more difficult. I'm exicted to see what the best practices are in the work place when it comes to the hardcore math
Material:
Lets get into a little code from the week. Here is a large portion of a blackjack game, GScott and I did. The goal of the game was to get your OOP up to speed.
#from deck2 import Deck
#from blackjack_player import Player
import sys
class Blackjack(object):
def __init__(self):
self.player1 = self.create_player(init_name = "Player 1")
self.dealer = self.create_dealer()
self.deal()
def create_player(self, init_name):
name = raw_input("Enter %s's name: " % init_name)
wallet = int(raw_input("Enter %s's wallet: " % name))
return Player(name, wallet)
def create_dealer(self):
return Player("Dealer", 0)
def deal(self):
#start new game by asking for bets
self.reset_game()
bet_size = self.ask_bet()
#Initialize players hands
self.player1.hand = self.draw_cards(2)
print "%s, you have %s, %s" % (self.player1.name, self.player1.hand, self.player1.hand_value())
self.dealer.hand = self.draw_cards(2)
print "%s, you have %s, %s" % (self.dealer.name, self.dealer.hand, self.dealer.hand_value())
#run through player 1's turn
player_didnt_bust = self.player_round(self.player1)
if player_didnt_bust:
print "%s has %d." % (self.player1.name, self.player1.hand_value())
else:
print "%s has busted with %d..." % (self.player1.name, self.player1.hand_value())
self.ask_new_game()
#run through the dealer's turn if the player didn't bust
dealer_bust = not(self.dealer_round())
if dealer_bust:
print "Dealer busted with %d" % (self.dealer.hand_value())
print "Awesome %s, you won" % (self.player1.name)
self.add_to_wallet(bet_size*3.0/2)
self.ask_new_game()
#compare results if neither busted
self.compare_hands(self.player1, self.dealer, bet_size)
#prompt for a new game
self.ask_new_game()
def add_to_wallet(self, amount):
self.player1.wallet += amount
print "%s now has %d in their wallet\n" % (self.player1.name, self.player1.wallet)
def ask_new_game(self):
#check to make sure player1 isn't broke!
if self.player1.wallet <= 0:
print "SORRY, BRAH! You are broke!... Goodbye!\n"
sys.exit()
#check if the player wants to play again
play_again = raw_input("\n\n\n%s, do you want to play again (y/n)? " % (self.player1.name))
if play_again == "Y" or play_again == "y":
print "\n\n\n"
self.deal()
elif play_again == "N" or play_again == "n":
print "Thank you for playing, %s!\n\n\n" % (self.player1.name)
sys.exit()
else:
print "command not accepted, please make sure to enter a legal character...\n"
self.ask_new_game()
def player_round(self, player):
hand_continue = True
hand_value = self.player1.hand_value()
while hand_continue and hand_value <=21:
hand_continue = self.hit_or_not()
hand_value = self.player1.hand_value()
print self.player1.hand, hand_value
return hand_value <=21
def dealer_round(self):
hand_value = self.dealer.hand_value()
while hand_value <= 16:
self.dealer.hand.append(self.deck.draw_card())
hand_value = self.dealer.hand_value()
print "Dealer now has %s, %s" % (self.dealer.hand, self.dealer.hand_value())
return hand_value <= 21
def reset_game(self):
self.dealer.reset()
self.player1.reset()
self.deck = Deck()
self.deck.shuffle()
def ask_bet(self):
print "%s has %d in their wallet" % (self.player1.name, self.player1.wallet)
bet_size = int(raw_input("%s, what is your bet?: " % (self.player1.name)))
if self.player1.has_funds(bet_size):
self.player1.wallet -= bet_size
print "%s bet size: %d, wallet balance: %d" % (self.player1.name, bet_size, self.player1.wallet)
else:
self.ask_bet()
return bet_size
def draw_cards(self, n):
cards = []
for i in xrange(n):
cards.append(self.deck.draw_card())
return cards
def hit_or_not(self):
prompt = raw_input("%s, do you want to hit (y/n)? " % (self.player1.name))
if prompt == "Y" or prompt == "y":
self.player1.hand.append(self.deck.draw_card())
return True
elif prompt == "N" or prompt == "n":
return False
else:
print "command not accepted, please make sure to enter a legal character...\n"
self.hit_or_not()
def compare_hands(self, player, dealer, bet_size):
if dealer.hand_value() > player.hand_value():
print "Sorry %s, you lost" % (player.name)
elif dealer.hand_value() < player.hand_value():
print "Awesome %s, you won" % (player.name)
self.add_to_wallet(bet_size*3.0/2)
else:
print "You guys tied, you get your money back"
self.add_to_wallet(bet_size)
if __name__ == '__main__':
pass
#game = Blackjack()
#game.play_game()