Deal of the Day

Home » Main » Manning Forums » 2008 » Hello World!

Thread: Hearts

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 2 - Pages: 1 - Last Post: Nov 8, 2011 5:52 PM by: Bennjamin
Bennjamin


Posts: 27
From: I live near Portland but in Washington.
Registered: 9/18/10
Hearts
Posted: Oct 16, 2011 1:24 PM
  Click to reply to this thread Reply

I am working on a project to make hearts with python!!! :D 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

Posts: 33
From: Ottawa, Canada
Registered: 1/27/09
Re: Hearts
Posted: Oct 21, 2011 7:28 PM   in response to: Bennjamin in response to: Bennjamin
  Click to reply to this thread Reply

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


Posts: 27
From: I live near Portland but in Washington.
Registered: 9/18/10
Re: Hearts
Posted: Nov 8, 2011 5:52 PM   in response to: carter.sande in response to: carter.sande
  Click to reply to this thread Reply

good idea carter! thanks! i'll keep it in mind...
:)

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor