Created
November 17, 2015 09:46
-
-
Save cubarco/20374b8f3987bc640953 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf8 | |
import socket | |
import re | |
import sys | |
def debug(string): | |
print '\033[92m' + '[DEBUG] ' + '\033[0m' + string | |
def judge(mine, dealers): | |
if mine >= dealers and dealers >= 17: | |
return False | |
if mine >= 20: | |
return False | |
return True | |
if __name__ == '__main__': | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(('127.0.0.1', 9009)) | |
sf = s.makefile() | |
cash = 0 | |
mine = 0 | |
dealers = 0 | |
s.send('y\n1\n') | |
while True: | |
line = s.recv(10240) | |
if cash == 1: | |
print 'You lose.' | |
sys.exit(1) | |
if line.find('Cash:') >= 0: | |
cash = int(re.findall(r'Cash: \$([0-9]*)', line)[0]) | |
debug('Cash: %d' % cash) | |
if cash >= 1e9: | |
print line.split()[0] # flag here | |
sys.exit(0) | |
if line.find('Your Total is') >= 0: | |
mine = int(re.findall(r'Your Total is ([0-9]*)', line)[0]) | |
if line.find('The Dealer Has a Total of ') >= 0: | |
dealers = int(re.findall(r'The Dealer Has a Total of ([0-9]*)', | |
line)[0]) | |
if line.find('Enter Bet:') >= 0: | |
s.send('%d\n' % (cash / 2)) | |
if line.find('Please Enter H to Hit or S to Stay') >= 0: | |
hos = 'H' if judge(mine, dealers) else 'S' | |
s.send(hos + '\n') | |
if line.find('Would You Like To Play Again?') >= 0: | |
s.send('y\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment