I'm working on a Discord bot that can be used to play blackjack (Python). The following code is supposed to calculate the values of the respective hands:
class Player(object):
def __init__(self, id=''):
self.id = id
self.hand = None
self.status_text = ''
self.bet = 0
self.value = 0
self.score = 0
self.wins = 0
self.no_response = 0
self.request_leave = False
self.playing = False
def calculate_value(self):
"""Calculates value of player's hand"""
if not self.hand:
return 0
num_aces = 0
total_value = 0
for card in self.hand:
if pydealer.const.DEFAULT_RANKS['values'][card.value] == 13:
num_aces += 1
total_value += 11
elif pydealer.const.DEFAULT_RANKS['values'][card.value] >= 10:
total_value += 10
else:
total_value += int(card.value)
while num_aces > 0 and total_value > 21:
total_value -= 10
num_aces -= 1
return total_value
Unfortunately I get at
for card in self.hand:
if pydealer.const.DEFAULT_RANKS['values'][card.value] == 13:
num_aces += 1
total_value += 11
the error Non-iterable value self.hand is used in an iterating context is displayed.
I hope someone can help! Greetings and thank you very much.
Your self.hand is initialized to be None.
So do make sure your self.hand gets set to an iterable object before you run stuff with it.
Related
All code
import random
import time
class Enemy():
def __init__(self):
self.health = 100
self.power = random.randint(10,20)
def hit(self, player):
player.health -= self.power
class player():
def __init__(self):
self.health = 300
self.power = 50
def hit(self, Enemy):
Enemy.health -= self.power
player1 = player()
enemies = []
for i in range(5): # create 5 enemy
enemies.append(Enemy())
print("Play - Help - Quit")
action1 = input("Type 'hit' for enemies\n>>>> ")
while action1 != 'q':
print("---------------")
for i in range(len(enemies)):
print("#{}.enemy heatlh-->{}".format(i, enemies[i].health))
print("----------------")
random_enemy = random.randint(1, 5)
action = input(">>>> ")
if action == 'hit':
which = int(input("Which enemy? there are {} enemies\n>>>>".format(len(enemies))))
if enemies[which].health == 0:
enemies[which].health = 0
print("\nThis is a death enemy")
else:
player1.hit(enemies[which])
damage_enemy = random.randint(1,5)
if enemies[random_enemy].health == 0:
continue
else:
if damage_enemy == 1 or damage_enemy == 3 or damage_enemy == 4:
if enemies[which].health == 0 or enemies[which].health <= 0:
enemies[which].health = 0
print("{}. enemy death HP: {} ".format(which, enemies[which].health))
else:
enemies[random_enemy].hit(player1)
print("{}. enemy hit you {} damage, your HP: {} ".format(random_enemy,enemies[random_enemy].power,player1.health))
elif enemies[which].health != 0 or enemies[which].health >= 0:
print("{}. enemy HP: {} ".format(which, enemies[which].health))
elif action == 'q':
break
I get this error at random time, my list size is 5, enemies die and their healths are 0. They stay there, but for some reason sometimes I get this error.
#0.enemy heatlh-->100
#1.enemy heatlh-->100
#2.enemy heatlh-->0
#3.enemy heatlh-->0
#4.enemy heatlh-->100
if enemies[random_enemy].health == 0:
continue
The random.randint(1, 5) can return 5. Your list has 5 elements, but the indices are from 0 to 4.
P.S. Also, the minimum value that you get is 1. Not 0.
I am trying to create a blackjack simulation so I can test various strategies and simulate them indefinitely. The biggest problem I have encountered is how to score an ace card. Ideally if you were dealt an ace, it would look at your score and it will give you 1 or 11 accordingly however I can't avoid if you were dealt a Five(5), Ace(11) and then a Six(6) to giving you a bust when you should have 12.
class Person:
wins = 0
def __init__(self, call):
self.call = call
self.hand = []
def deal(self, cards_dealt):
cards_in_deck = len(deck)
for card in range(0, cards_dealt):
card_position = (random.randint(0, cards_in_deck) - 1)
self.hand.append(deck[card_position])
deck.pop(card_position)
cards_in_deck -= 1
def total(self):
my_total = 0
for x in range(0, len(self.hand)):
if self.hand[x][0] > 10:
my_total += 10
if self.hand[x][0] == 1:
if self.total < 11:
my_total += 11
else:
my_total += 1
else:
my_total += self.hand[x][0]
return my_total
numbers_in_each_suit = 13
suits = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
deck = []
for number in range(0, numbers_in_each_suit):
for suit in suits:
deck.append([(number + 1), suit])
cpu = Person(10)
cpu.deal(2)
cpu.total()
Just use 1 for number for creating ace, add 11 for every ace and count each ace, and then subtract 10 if you bust until you don’t or until you run out of aces to decrement.
We are making this Black Jack program to test card counting methods. We are trying to get the auto play function working, and it does, but when we run it in a while loop the loop never finishes and exits.
"""
Eli Byers
Josh Rondash
Black_Jack.py
"""
import random
#---------- CLASSES -----------------------------------------------------------
class Card(object):
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.value = 0
if self.rank is "Ace":
self.value = 11
if self.rank.isdigit():
self.value = int(self.rank)
if self.rank in ["Jack", "Queen", "King"]:
self.value = 10
def __str__(self):
return "["+str(self.rank)+" "+str(self.suit)+"]"
class Deck(object):
def __init__(self, numofdecks):
self.deck = []
self.suit = [" Clubs", " Hearts", " Spades", " Diamonds"]
self.rank = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
self.numofdecks = numofdecks
for i in range(self.numofdecks):
for r in self.rank:
for s in self.suit:
self.deck.append(Card(r,s))
def __str__(self):
deck_str = ""
for card in self.deck:
deck_str += str(card)+" "
deck_str = deck_str[:-1]
return deck_str
def __len__(self):
return len(self.deck)
def __getitem__(self,i):
return self.deck[i]
def __delitem__(self, i):
del self.deck[i]
def draw(self):
top_card = self.deck[0]
del self.deck[0]
return top_card
def addcard(self,card):
self.deck.append(card)
def shuffle(self): #Random shuffle function
a = len(self.deck)
b = a-1
for d in range(b,0,-1):
e = random.randint(0,d)
if e == d:
continue
self.deck[d],self.deck[e] = self.deck[e],self.deck[d]
return self.deck
class Player(object):
def __init__(self, bankroll):
self.hand = []
self.bankroll = bankroll
self.score = 0
self.bet = 0
self.count = 0
self.aces = 0
self.dealer_hand = []
def __str__(self):
hand = ""
for card in self.hand:
hand += str(card)+" "
return "Hand: "+hand+" Bank: "+str(self.bankroll)+" Bet: "+str(self.bet)+" Ct: "+str(self.count)+" A: "+str(self.aces)
def __getitem__(self, i):
return self.hand[i]
def getcard(self,Card):
self.hand.append(Card)
self.score = 0
ace = 0
for card in self.hand:
if card.rank == "Ace":
ace += 1
self.score += 1
else:
self.score += card.value
for a in range(ace):
if (self.score + 10) <= 21:
self.score += 10
self.updateCount(Card,"P")
def placebet(self, b=0):
if b != 0:
self.bankroll -= b
self.bet += b
else:
self.bet += input("Bankroll: "+str(self.bankroll)+" Ct: "+str(self.count)+" A: "+str(self.aces)+" Place bet: ")
self.bankroll -= self.bet
def updateCount(self, card, player):
if card.value in range(2,6):
self.count += 1
elif card.value is 10:
self.count -= 1
elif card.rank is "Ace":
self.aces += 1
if player == "D":
self.dealer_hand.append(card)
def makeBet(self):
bet = 0.1*self.bankroll
if self.count > 3:
c = 0
for i in range(self.count):
c += 1
if c == 3:
bet += 0.5 * bet
c = 0
elif self.count < -3:
bet -= 0.5 * bet
return bet
def Play(self):
if self.score < 17:
choice = 1 #hit
else:
choice = 2 #stand
return choice
class Dealer(object):
def __init__(self, Deck, discardpile, Player):
self.deck = Deck
self.discardpile = discardpile
self.player = Player
self.hand = []
self.score = 0
def __str__(self):
hand = ""
for card in self.hand:
hand += str(card)+" "
return "Dealer Hand: "+hand
def __getitem__(self, i):
return self.deck[i]
def draw(self):
cardval = self.deck.draw()
self.hand.append(cardval)
self.score = 0
ace = 0
for card in self.hand:
if card.rank == "Ace":
ace += 1
self.score += 1
else:
self.score += card.value
for a in range(ace):
if (self.score + 10) <= 21:
self.score += 10
player.updateCount(cardval,"D")
def deal(self, Player):
for i in range(2):
self.player.getcard(self.deck.draw())
self.draw()
def burn(self):
self.discardpile.addcard(self.deck.draw())
def blackjack(self):
if self.score == 21:
return True
else:
return False
class Table(Dealer, Player):
def __init__(self, Dealer, Player, Deck , discardpile):
self.dealer = Dealer
self.player = Player
self.deck = Deck
self.discardpile = discardpile
self.betplaced = 0
def initGame(self):
self.clearTable()
Deck.shuffle(self.deck)
self.dealer.burn()
def clearTable(self):
for card in self.player.hand:
self.discardpile.addcard(card)
for card in self.dealer.hand:
self.discardpile.addcard(card)
self.player.hand = []
self.dealer.hand = []
def playGame(self):
self.betplaced = self.player.placebet()
self.dealer.deal(self.player)
print self.player
print self.dealer
if self.dealer.blackjack():
print("Dealer Black Jack!")
elif self.player.score <= 21:
stand = 0
while self.player.score < 21 and stand == 0:
print("Use number Keys> Hit: 1 Stand: 2")
choice = input()
if choice == 1: # Hit
self.player.getcard(self.deck.draw())
elif choice == 2: # Stand
stand = 1
print self.player
print ("Your score is "+str(self.player.score))
while self.dealer.score <= 17 and self.player.score <= 21:
if self.dealer.score == 17:
for card in self.dealer.hand:
if card.rank == "Ace":
self.dealer.draw()
else:
self.dealer.draw()
print self.dealer
print ("Dealer score is "+str(self.dealer.score))
if self.dealer.score <= 21:
if (self.player.score > self.dealer.score) and (self.player.score <= 21) :
if self.player.score == 21:
self.player.bankroll += self.player.bet*2.5
else:
self.player.bankroll += self.player.bet*2
print ("Win")
elif self.player.score == self.dealer.score:
self.player.bankroll += self.player.bet
print("Push")
else:
print("You Lose")
elif (self.dealer.score > 21) and (self.player.score <= 21):
if self.player.score == 21:
self.player.bankroll += self.player.bet*2.5
else:
self.player.bankroll += self.player.bet*2
print ("Win")
else:
print("You Lose.")
self.player.bet = 0
self.player.dealer_hand = []
print
def autoPlay(self):
self.betplaced = self.player.placebet(int(self.player.makeBet()))
self.dealer.deal(self.player)
if (self.dealer.blackjack() == False) and (self.player.score <= 21):
stand = 0
while self.player.score < 21 and stand == 0:
choice = player.Play()
if choice == 1: # Hit
self.player.getcard(self.deck.draw())
elif choice == 2: # Stand
stand = 1
while self.dealer.score <= 17 and self.player.score <= 21:
if self.dealer.score == 17:
for card in self.dealer.hand:
if card.rank == "Ace":
self.dealer.draw()
else:
self.dealer.draw()
if self.dealer.score <= 21:
if (self.player.score > self.dealer.score) and (self.player.score <= 21):
if self.player.score == 21:
self.player.bankroll += self.player.bet*2.5
else:
self.player.bankroll += self.player.bet*2
print ("Win")
elif self.player.score == self.dealer.score:
self.player.bankroll += self.player.bet
print("Push")
else:
print("Lose")
elif (self.dealer.score > 21) and (self.player.score <= 21):
if self.player.score == 21:
self.player.bankroll += self.player.bet*2.5
else:
self.player.bankroll += self.player.bet*2
print ("Win")
else:
print("Lose")
self.player.bet = 0
self.player.dealer_hand = []
print self.player.bankroll
#----------- MAIN -----------------------------------
deck = Deck(6)
player = Player(500)
discardpile = Deck(0)
dealer = Dealer(deck, discardpile, player)
table = Table(dealer, player, deck, discardpile)
table.initGame()
while (player.bankroll > 0) and (player.bankroll < 1000):
table.autoPlay()
table.clearTable()
print "Game Over."
Just add some debug statements. You have multiple while loops in your methods. I'm sure a simple print statement will catch the errors in your logic.
DUDE, I found the infinite loop within 1 minute. lucky i had python installed on my box.
LINE 251
if (self.dealer.blackjack() == False) and (self.player.score <= 21):
stand = 0
while self.player.score < 21 and stand == 0:
print "in player score"
choice = player.Play()
if choice == 1: # Hit
self.player.getcard(self.deck.draw())
elif choice == 2: # Stand
stand = 1
while self.dealer.score <= 17 and self.player.score <= 21:
print "in dealer score"
print self.dealer.score
if self.dealer.score == 17:
for card in self.dealer.hand:
if card.rank == "Ace":
self.dealer.draw()
else:
self.dealer.draw()
With the following sample code containing #property decorators:
class Grade_Calculator(object):
def __init__(self,score):
self.score = score
#property
def grade(self):
if self.score in range(60,70):
grade = 'D'
elif self.score in range(70,80):
grade = 'C'
elif self.score in range(80,90):
grade = 'B'
elif self.score in range(90,101):
grade = 'A'
return grade
#property
def failure(self):
if self.score < 60:
print 'See me'
grade = 'F'
return grade
and an instance:
g = Grade_Calculator(28)
g.grade returns an UnboundLocalError. I would like to call failure() within grade() to avoid this error.
With an instance:
g = Grade_Calculator(89)
g.failure() fails silently. I would like to call grade() within failure() to act as a fail-safe in this situation.
I've seen a lot of references to just doing self.method() calls, but they're not working for me and I think the presence of the decorator is screwing me up somehow:
#property
def grade(self):
if self.score < 60:
self.failure()
elif self.score in range(60,70):
grade = 'D'
elif self.score in range(70,80):
grade = 'C'
elif self.score in range(80,90):
grade = 'B'
elif self.score in range(90,101):
grade = 'A'
return grade
g = Grade_Calculator(28)
g.grade
See me
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-667-fb10e6cf27d4> in <module>()
----> 1 g.grade
./sample.py in grade(self)
6 def grade(self):
7 if self.score < 60:
----> 8 self.failure()
9 elif self.score in range(60,70):
10 grade = 'D'
TypeError: 'str' object is not callable
What am I not understanding?
You have defined failure as a property. Therefore, you can't call it using () notation; in fact, not doing that is kinda the whole point of using a property. self.failure returns 'F' and you then try to call it, which is why you get the error about not being able to call a string. It's as if you wrote 'F'() which is an obvious mistake.
Also, you're throwing away the value you get from failure and you're only returning a value in the case of failure (which means you get None otherwise). Of course you only access failure if you have a failing grade to begin with... in which case you don't need to check that condition inside failure, now do you?
Also, printing stuff in properties is pretty bad style. You don't expect something to be printed when you access an attribute. Separation of concerns: something outside of the class should probably be doing the printing, or you should have a separate method to call to print whatever is returned by grade.
I'd rewrite your class as follows:
class Grade_Calculator(object):
def __init__(self, score):
self.score = score
#property
def grade(self):
if self.failure:
return 'F'
if 60 <= self.score < 70:
return 'D'
if 70 <= self.score < 80:
return 'C'
if 80 <= self.score < 90:
return 'B'
if 90 <= self.score < 100:
return 'A'
#property
def failure(self):
return self.score < 60 # returns True or False
def print_grade(self):
print self.grade, "- see me" * self.failure
if __name__ == '__main__':
c = Grade_Calculator(71)
c.print_grade()
class Grade_Calculator(object):
def __init__(self, score):
self.score = score
#property
def grade(self):
grade = self.failure
if 60 <= self.score < 70:
grade = 'D'
elif 70 <= self.score < 80:
grade = 'C'
elif 80 <= self.score < 90:
grade = 'B'
elif 90 <= self.score < 100:
grade = 'A'
return grade
#property
def failure(self):
if self.score < 60:
print 'See me'
grade = 'F'
return grade
if __name__ == '__main__':
a = Grade_Calculator(71)
print a.grade
You're basically creating a new attribute to your object with the failure method. And you're never trying to get its value, that is why you'll never see 'See me'
By the way, you're overriding the value of grade which was initially an integer. You converted it to a string ('A', 'B', 'C', ...)
The code I provided works, but I changed a few things. There is no need to call range (this is expensive). You can use value < variable < other_value in Python
(Cards numbered 2-10 should be valued from 2-10, respectively. J,Q, and K should be 10, and A should be either 1 or 11, depending on the value of the hand).
How do I assign the deck these values? Also, the game needs to be 3 rounds. The way I did it is only one round. How do I make the game go three times, while keeping track of the players wins/losses?
Could someone please explain how I can do this a simple way?
here is a complete implementation for you
look at hand.Score
import random
class Card:
def __init__(self,rank,suite):
self.rank = rank
self.suite = suite
def Rank(self):
return "Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King".split()[self.rank]
def Suite(self):
return "Hearts Spades Clubs Diamonds".split()[self.suite]
def __str__(self):
#print "Get Self:",type(self)
#print "Dir:",dir(self)
#return "OF"
return self.Rank()+" of "+ self.Suite()
class Hand:
def __init__(self):
self.cards = []
def Score(self):
aces_ct = 0
score = 0
for c in self.cards:
if c.rank == 0:
aces_ct += 1
score += 11
if 0 < c.rank < 9:
score += c.rank+1
else:
score += 10
while score > 21 and aces_ct > 0:
score -= 10
aces_ct -= 1
return score
def add(self,card):
self.cards.append(card)
def Show(self,show_only=None):
if not show_only:
for k in self.cards:
print "%s"%k
else:
if isinstance(show_only,int):
print "%s"%self.cards[show_only]
elif isinstance(show_only,(list,tuple)):
for idx in show_only:
print "%s"%self.cards[idx]
class deck:
def __init__(self):
self.cards = []
for i in range(4):
for j in range(13):
self.cards.append(Card(j,i))
random.shuffle(self.cards)
def shuffle(self):
random.shuffle(self.cards)
def pop(self):
return self.cards.pop()
if __name__ == "__main__":
d = deck()
player_hand = Hand()
dealer_hand = Hand()
player_hand.add(d.pop())
dealer_hand.add(d.pop())
player_hand.add(d.pop())
dealer_hand.add(d.pop())
print "Player Score :",player_hand.Score()
player_hand.Show()
print "\n\nDealer Score :",dealer_hand.Score()
dealer_hand.Show()
You can represent cars as tuples: a character indicating suit, and a number indicating value. I would also add a third value to the tuple, its blackjack value (e.g. a king and a queen both count as a 10).
card = ('D', 11, 10) # card is queen of diamonds
You could also make your own class to represent cards.
class Card(object):
def __init__(self, suit, number):
self.suit = suit
self.number = number
self.value = (number if 2 <= number <= 9 else 10)
You will have to special-case the value of the ace, of course.
You should probably calculate the total dynamically. Also, you need some kind of way to store the money of each individual player. Right now, there is no way of knowing the distribution of money since you only have one total.