Created
January 21, 2018 13:28
-
-
Save jmg/a16ea7a2506ca427fcb87fb413a926dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
import time | |
class Card(object): | |
def __init__(self, number, kind): | |
self.number = number | |
self.kind = kind | |
def get_value(self): | |
if self.number > 10: | |
value = 10 | |
else: | |
value = self.number | |
return value | |
def is_ace(self): | |
return self.number == 1 | |
def __str__(self): | |
number_figure = { | |
1: "Ace", | |
11: "Jack", | |
12: "Queen", | |
13: "King", | |
} | |
return "{} - {}".format(number_figure.get(self.number, self.number), self.kind) | |
class Deck(object): | |
def __init__(self): | |
self.cards = [] | |
for number in range(1,14): | |
for kind in ["Diamantes", "Treboles", "Picas", "Corazones"]: | |
card = Card(number, kind) | |
self.cards.append(card) | |
def get_random_card(self): | |
card = random.choice(self.cards) | |
self.cards.remove(card) | |
return card | |
class Player(object): | |
def __init__(self, deck, name): | |
self.deck = deck | |
self.name = name | |
self.cards_in_hand = [] | |
def draw_card(self): | |
card = self.deck.get_random_card() | |
print card | |
self.cards_in_hand.append(card) | |
def print_score(self, total_str): | |
print "Tiene en total: {}".format(total_str) | |
def get_total_score(self, print_results=True): | |
total = 0 | |
total_2 = 0 | |
ace = False | |
for card in self.cards_in_hand: | |
total += card.get_value() | |
if card.is_ace(): | |
ace = True | |
if ace: | |
total_2 = total + 10 | |
if print_results: | |
total_str = total | |
if ace and total_2 <= 21: | |
total_str = "{} / {}".format(total, total_2) | |
self.print_score(total_str) | |
if ace and total_2 <= 21: | |
total = total_2 | |
return total | |
def print_play(self): | |
print "*" * 80 | |
print "{} juega:".format(self.name) | |
print "*" * 80 | |
def play(self): | |
self.print_play() | |
self.draw_card() | |
self.draw_card() | |
self.get_total_score() | |
continue_playing = True | |
while continue_playing: | |
value = raw_input("Quiere una carta mas? s/n \n") | |
if value == "s": | |
self.draw_card() | |
score = self.get_total_score() | |
continue_playing = self.continue_playing(score) | |
elif value == "n": | |
self.get_total_score() | |
continue_playing = False | |
else: | |
print "Opcion incorrecta. Elija s o n \n" | |
return self.get_total_score(print_results=False) | |
def play_ia(self, score_to_stay): | |
self.print_play() | |
self.draw_card() | |
self.draw_card() | |
score = self.get_total_score(print_results=False) | |
while score < score_to_stay: | |
self.draw_card() | |
score = self.get_total_score(print_results=False) | |
print "*" * 80 | |
self.print_score(score) | |
return score | |
def continue_playing(self, score): | |
return score < 21 | |
def print_win(self): | |
print "*" * 80 | |
print "{} gana {}".format(self.name, ":(" if "banca" in self.name else ":)") | |
print "*" * 80 | |
return -1 if "banca" in self.name else 1 | |
def print_draw(self): | |
print "*" * 80 | |
print "Han empatado" | |
print "*" * 80 | |
return 0 | |
def black_jack(self): | |
score = self.get_total_score(print_results=False) | |
return score == 21 and len(self.cards_in_hand) == 2 | |
def print_backjack(self): | |
print "+" * 80 | |
print "BlackJack!" | |
print "{} gana {}".format(self.name, ":(" if "banca" in self.name else ":)") | |
print "+" * 80 | |
return -1 if "banca" in self.name else 1 | |
def busted(self): | |
score = self.get_total_score(print_results=False) | |
if score > 21: | |
print "Busted!" | |
return True | |
return False | |
class TwoPlayerGame(object): | |
player_plays_ia = False | |
def __init__(self): | |
self.deck = Deck() | |
self.player = Player(self.deck, "Jugador 1") | |
self.house = Player(self.deck, "La banca") | |
def play(self, score_to_stay=None): | |
if not self.player_plays_ia: | |
player_score = self.player.play() | |
else: | |
player_score = self.player.play_ia(score_to_stay=score_to_stay) | |
if self.player.busted(): | |
return self.house.print_win() | |
if self.player.black_jack(): | |
return self.player.print_backjack() | |
house_score = self.house.play_ia(player_score) | |
if self.house.busted(): | |
return self.player.print_win() | |
if self.house.black_jack(): | |
return self.house.print_backjack() | |
print "La banca tiene {}".format(house_score) | |
if player_score > house_score: | |
return self.player.print_win() | |
elif player_score < house_score: | |
return self.house.print_win() | |
else: | |
return self.house.print_draw() | |
class IAGame(TwoPlayerGame): | |
player_plays_ia = True | |
if __name__ == "__main__": | |
#TwoPlayerGame().play() | |
results_file = open("results.txt", "w") | |
for score in [14, 15, 16, 17, 18]: | |
banca_wins = 0 | |
player_wins = 0 | |
draws = 0 | |
for x in range(10000): | |
match_results = IAGame().play(score_to_stay=score) | |
if match_results == -1: | |
banca_wins += 1 | |
elif match_results == 1: | |
player_wins += 1 | |
else: | |
draws += 1 | |
print "" | |
results_file.write("Score to stay: {}\n".format(score)) | |
results_file.write("Cantidad de veces que gano el jugador 1: {}\n".format(player_wins)) | |
results_file.write("Cantidad de veces que gano la banca: {}\n".format(banca_wins)) | |
results_file.write("Cantidad de veces que empataron: {}\n".format(draws)) | |
results_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment