Skip to content

Instantly share code, notes, and snippets.

@Mononofu
Created June 15, 2013 11:08
Show Gist options
  • Save Mononofu/5787771 to your computer and use it in GitHub Desktop.
Save Mononofu/5787771 to your computer and use it in GitHub Desktop.
import socket
def clean_cards(line):
return line.split(":")[-1].strip().split(" ")
def decide(player_worth):
if player_worth < 18:
s.send("H\n")
else:
s.send("S\n")
def parse_cash(line):
return int(line.split("$")[-1])
s = socket.socket(socket.AF_INET)
s.connect(("blackjack.shallweplayaga.me", 6789))
print s.recv(1024)
s.send("myname\n")
cash = parse_cash(s.recv(1024))
bet = 1
# ask for bet
print s.recv(1024)
while True:
print "I have %d $" % cash
#make bet
print "Betting %d $" % bet
s.send("%d\n" % bet)
# first round get values from dealer too
cards = s.recv(1024) + s.recv(1024)
dealer, player, _ = map(clean_cards, cards.split("\n"))
player_worth = int(player[-1][1:-1])
player = player[:-1]
print "Dealer has %s, player has %s worth %d" % (dealer, player, player_worth)
decide(player_worth)
while True:
cards = s.recv(1024) + s.recv(1024)
if "You" in cards:
cash = parse_cash(next((line for line in cards.split("\n") if "$" in line), None))
if "win" in cards:
print "I won!"
bet = 1
elif "draw" in cards:
print "It's a draw :-/"
else:
print "I lost :("
bet = bet*2
break
player = clean_cards(cards.split("\n")[0])
player_worth = int(player[-1][1:-1])
player = player[:-1]
print "player has %s worth %d" % (player, player_worth)
decide(player_worth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment