mtg/mtg.py
2010-06-17 12:49:25 -04:00

81 lines
2.1 KiB
Python

import copy
import random
import cards
from observable import Observable
class Game:
def __init__(self):
self.players = []
class Player:
def __init__(self, name, deck=None):
self.name = name
self.deck = None
self.setDeck(deck)
self.hand = CardList()
self.graveyard = CardList()
self.battlefield = CardList()
def __repr__(self):
return 'Player: {0} [Deck:{1}]'.format(self.name, len(self.deck))
def setDeck(self, deck):
if not deck:
return
self.deck = copy.copy(deck)
for card in deck:
card.owner = self
def draw(self):
card = self.deck.pop()
self.hand.append(card)
card.moved.emit('deck', 'hand')
def cast(self, card):
hand.remove(card)
self.battlefield.append(card)
card.moved.emit('hand', 'battlefield')
class Card:
def __init__(self, name, attributes, cost, power, toughness, owner=None):
self.name = name
self.attributes = [a.lower() for a in attributes]
self.cost = cost
self.power = power
self.toughness = toughness
self.owner = owner
# Events
self.moved = Observable()
self.tapped = Observable()
self.attacked = Observable()
self.store()
def __repr__(self):
return 'Card: [{2}] {0}: {1} [{3}/{4}]'.format(self.name, ' '.join([a.capitalize() for a in self.attributes]), self.cost, self.power, self.toughness)
def store(self):
self.__stored = copy.copy(self)
def restore(self):
self = copy.copy(self.__stored)
def tap(self):
self.tapped.emit()
class Ability:
def __init__(self, target):
self.target = target
self.init()
def init(self):
pass
class CardList(list):
def __init__(self, game):
list.__init__(self)
self.game = game
def append(self, item):
item.list = self
list.append(self, item)
class Deck(CardList):
def __init__(self):
pass
def shuffle(self):
random.shuffle(self)
def cards(self):
return self.__cards