The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
Bennjamin (27) [Avatar] Offline
#1
I am working on a project to make hearts with python!!! smilie i used the cards class from chapter 23 to make the game... i would appreciate suggestions on how to create the AI for the computer, thanks.
Here's the code so far:
import random
from card import Card
deck = []
for suit_id in range(1, 5):
for rank_id in range(1, 14):
deck.append(Card(suit_id, rank_id))
playerhand = []
computerhand = []
playertricks = []
computertricks = []
lastcardwasincomputer = True
for cards in range(0, 26):
a = random.choice(deck)
playerhand.append(a)
deck.remove(a)
for cards in range(0, 26):
a = random.choice(deck)
computerhand.append(a)
deck.remove(a)
yourlead = True
def playerturn():
hand = ""
for card in playerhand:
hand.__add__(card.short_name+", ")
print "Your lead. Your hand is: " + hand
leadcard = raw_input("Type in card to lead:")
for card in playerhand:
if leadcard = card.short_name:
if card.suit = "Hearts":
if Heartsplayed:
thecard = card
cardfound = True
else:
pass
else:
thecard = card
cardfound = True
if cardfound:
return thecard
def computerturn():
# insert code here to make computer choose card, as local variable card
for card in computerhand:
if leadcard = card.short_name:
if card.suit = "Hearts":
if Heartsplayed:
thecard = card
cardfound = True
else:
pass
else:
thecard = card
cardfound = True
if cardfound:
return thecard
carter.sande (32) [Avatar] Offline
#2
Re: Hearts
Hey Bennjamin,

Great project idea!

Just so you know, the code you posted on the forum isn't the same as the code you typed into IDLE. It's missing the indentation due to a bug with the forums. If you'd like other people to be able to use your code, you might want to try using a code sharing service such as Google Code or GitHub.

When you're designing an AI, you typically want to think about a strategy. What's a good strategy for playing Hearts? You can then turn that strategy into a set of rules for the computer.

Carter
Bennjamin (27) [Avatar] Offline
#3
Re: Hearts
good idea carter! thanks! i'll keep it in mind...
smilie