geekhack
geekhack Community => Other Geeky Stuff => Topic started by: Son of Gnome on Fri, 09 December 2011, 16:47:42
-
Hey guys I working on some python homework which I am completely lost on.
I am trying to make a card game, most of it works fine except the my moveCard function it doesnt work and I dont know why.
Here is my code:
# Note: cards are represented as 2-character strings, e.g.,
# "2C", "9S", "QH", "TD", "AC"
from random import randint, shuffle
class DeckOfCards:
def __init__(self):
"""Initializes a new deck of cards."""
ranks = "23456789TJQKA"
suits = "CDHS"
self.cards = [r+s for r in ranks for s in suits]
def shuffle(self):
"""Shuffles the cards into a random order."""
shuffle(self.cards)
def dealCard(self):
"""Removes the top/last card in the deck & returns it."""
if self.numCards() > 0:
return self.cards.pop()
else:
return ""
def addCard(self, card):
"""Adds the specified card to the bottom/front of the deck."""
self.cards.insert(0, card)
def numCards(self):
"""Returns the number of cards remaining in the deck."""
return len(self.cards)
def __str__(self):
"""Returns the string representation of the deck."""
return ' '.join(self.cards)
#########
class RowOfCards:
def __init__(self):
"""Initializes an empty row (list) of cards."""
self.cards = []
def addAtEnd(self, card):
"""Adds the specified card to the end of the row (list)."""
self.cards.append(card)
def moveCard(self, card, numSpots):
"""Moves the specified card numSpots to the left."""
self.cards.index(card)
(i for i in list if card == self)
print i
self.cards.remove(i)
self.cards.insert(i - numSpots, card)
def numCards(self):
"""Returns the number of cards in the row (list)."""
return len.self.cards
def __str__(self):
"""Returns the string representation of the deck (list)."""
return ' '.join(self.cards)
-
card == self ??
ps there is a code tag that would make this a lot more readable
def moveCard(self, card, numSpots):
"""Moves the specified card numSpots to the left."""
abc = self.cards.index(card)
if abc - numSpots >= 0
self.cards.insert(abc-numSpots, card)
self.cards.pop(abc+1)
-
Got that assignment done thank God lol. Stuck on this one now :(
from cards import DeckOfCards, RowOfCards
def skip3():
"""Basic framework for playing Skip3 Solitaire."""
print "Welcome to Skip3 Solitaire."
deck = DeckOfCards()
deck.shuffle()
row = RowOfCards()
response = ""
while response != "q":
print row, "\n"
response = raw_input("Action? ")
responseWords = response.split()
firstLetter = responseWords[0][0].lower()
if firstLetter == "d":
row.addAtEnd(deck.dealCard())
else print "ERROR"
elif firstLetter == "m":
row.moveCard(responseWords[1].upper(), 1)
elif firstLetter == "s":
row.moveCard(responseWords[1].upper(), 3)
elif firstLetter == "q":
print "Thanks for playing."
print DeckOfCards()
else:
print "Unknown command"
I am trying to make it so if the rules arent fulfilled the game presents an error message of some kind. I know I need to have the program read something I am just unsure of what and how to go about doing it.
-
If you do my VHDL. I'll do your Python. ; p.