Permalink
Browse files

Initial commit with start-of-game support

  • Loading branch information...
jleclanche committed Aug 25, 2014
0 parents commit c144d1b37e2872383217946c94d713cec358cfec
Showing with 131 additions and 0 deletions.
  1. +1 −0 data/basic.json
  2. +78 −0 fireplace/__init__.py
  3. +23 −0 fireplace/cards.py
  4. 0 fireplace/game/__init__.py
  5. +9 −0 fireplace/heroes.py
  6. +20 −0 tests/main.py

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,78 @@
import random
from . import heroes
from .cards import cardsForHero, THE_COIN
class Deck(object):
MAX_CARDS = 30
MAX_UNIQUE_CARDS = 2
MAX_UNIQUE_LEGENDARIES = 1
@classmethod
def randomDraft(cls, hero):
"""
Return a deck of 30 random cards from the \a hero's collection
"""
deck = []
collection = cardsForHero(hero)
while len(deck) < cls.MAX_CARDS:
card = random.choice(collection)
if deck.count(card) < cls.MAX_UNIQUE_CARDS: # this probably doesnt work right because dicts
# todo legendary check too
deck.append(card)
return Deck(deck)
def __init__(self, cards):
self.cards = cards
def shuffle(self):
random.shuffle(self.cards)
class Player(object):
MAX_HAND = 10
def __init__(self, name, deck):
self.name = name
self.deck = deck
self.hand = []
def addToHand(self, card):
if len(self.hand) >= self.MAX_HAND:
return
self.hand.append(card)
def draw(self, count=1):
while count:
card = self.deck.cards.pop()
self.addToHand(card)
count -= 1
class Game(object):
def __init__(self, players):
self.players = players
self.player1 = self.players[0]
self.player2 = self.players[1]
self.turn = 0
self.playerTurn = None
def tossCoin(self):
outcome = random.randint(0, 1)
# player who wins the outcome is the index
winner = self.players[outcome]
# T_T
loser = [p for p in self.players if p != winner][0]
return winner, loser
def start(self):
for player in self.players:
player.deck.shuffle()
player.draw(3)
winner, loser = self.tossCoin()
loser.draw()
# TODO mulligan phase
loser.addToHand(THE_COIN)
self.beginTurn(winner)
def beginTurn(self, player):
self.turn += 1
self.playerTurn = player
@@ -0,0 +1,23 @@
import json
import os
_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, "data", "basic.json")
f = open(_path, "r")
CARDS = json.load(f)
f.close()
THE_COIN = "GAME_005"
class Card(object):
def __init__(self, json):
self.json = json
def __str__(self):
return self.json["name"]
def __repr__(self):
return "<%s %s (%r)>" % (self.__class__.__name__, self.json["id"], self.json["name"])
def cardsForHero(hero):
return [Card(card) for card in CARDS if card.get("playerClass") == hero or "playerClass" not in card]
No changes.
@@ -0,0 +1,9 @@
MAGE = "Mage"
WARRIOR = "Warrior"
ROGUE = "Rogue"
DRUID = "Druid"
WARLOCK = "Warlock"
PRIEST = "Priest"
SHAMAN = "Shaman"
PALADIN = "Paladin"
HUNTER = "Hunter"
@@ -0,0 +1,20 @@
#!/usr/bin/env python
import sys; sys.path.append("..")
import fireplace
def main():
deck1 = fireplace.Deck.randomDraft(hero=fireplace.heroes.MAGE)
deck2 = fireplace.Deck.randomDraft(hero=fireplace.heroes.WARRIOR)
player1 = fireplace.Player(name="Player1", deck=deck1)
player2 = fireplace.Player(name="Player2", deck=deck2)
game = fireplace.Game(players=(player1, player2))
game.start()
print(game.player1.hand)
print(game.player2.hand)
if __name__ == "__main__":
main()

0 comments on commit c144d1b

Please sign in to comment.