def testCount(self): rs = River() rs.addPerson(1, self.redPlayer) self.assertEqual(1, rs.count(self.redPlayer)) with self.assertRaisesRegex(PlacementError, "Player %s already added person to the River" % self.redPlayer.getColor()): rs.addPerson(1, self.redPlayer) self.assertEqual(1, rs.count(self.redPlayer))
def testCountTwoPlayers(self): rs = River() rs.addPerson(1, self.redPlayer) self.assertEqual(1, rs.count(self.redPlayer)) self.assertEqual(6, rs.freeSlots()) rs.addPerson(3, self.bluePlayer) self.assertEqual(3, rs.count(self.bluePlayer)) self.assertEqual(3, rs.freeSlots())
class Board: """Class representing the gameboard """ def __init__(self, huts=None): self.huntingGrounds = HuntingGrounds() self.forest = Forest() self.clayPit = ClayPit() self.quarry = Quarry() self.river = River() self.farm = Farm() self.breedingHut = BreedingHut() self.toolSmith = ToolSmith() self.grounds = [self.huntingGrounds, self.forest, self.clayPit, self.quarry, self.river, self.toolSmith, self.farm, self.breedingHut] if not huts: huts = self._defaultHuts() shuffle(huts) div, mod = divmod(len(huts), 4) end1 = div + int(mod > 0) end2 = end1 + div + int(mod > 1) end3 = end2 + div + int(mod > 2) self.hutStacks = [huts[0 :end1], huts[end1:end2], huts[end2:end3], huts[end3: ]] self.cardPile = [SymbolCard(CardSymbol.weaving, CardAction.food, 3), SymbolCard(CardSymbol.weaving, CardAction.food, 1), SymbolCard(CardSymbol.time, CardAction.christmas, 0), SymbolCard(CardSymbol.time, CardAction.foodtrack, 1), SymbolCard(CardSymbol.healing, CardAction.food, 5), SymbolCard(CardSymbol.healing, CardAction.resource, 2), SymbolCard(CardSymbol.art, CardAction.tool, 1), SymbolCard(CardSymbol.art, CardAction.roll, 6), SymbolCard(CardSymbol.pottery, CardAction.food, 7), SymbolCard(CardSymbol.pottery, CardAction.christmas, 0), SymbolCard(CardSymbol.transport, CardAction.stone, 2), SymbolCard(CardSymbol.transport, CardAction.christmas, 0), SymbolCard(CardSymbol.music, CardAction.point, 3), SymbolCard(CardSymbol.music, CardAction.point, 3), SymbolCard(CardSymbol.writing, CardAction.extracard, 1), SymbolCard(CardSymbol.writing, CardAction.christmas, 0), MultiplierCard(CardMultiplier.hutcount, 1, CardAction.christmas, 0), MultiplierCard(CardMultiplier.hutcount, 1, CardAction.food, 4), MultiplierCard(CardMultiplier.hutcount, 2, CardAction.christmas, 0), MultiplierCard(CardMultiplier.hutcount, 2, CardAction.food, 2), MultiplierCard(CardMultiplier.hutcount, 3, CardAction.point, 3), MultiplierCard(CardMultiplier.foodtrack, 1, CardAction.christmas, 0), MultiplierCard(CardMultiplier.foodtrack, 1, CardAction.foodtrack, 1), MultiplierCard(CardMultiplier.foodtrack, 1, CardAction.stone, 1), MultiplierCard(CardMultiplier.foodtrack, 2, CardAction.food, 3), MultiplierCard(CardMultiplier.foodtrack, 2, CardAction.christmas, 0), MultiplierCard(CardMultiplier.toolsum, 2, CardAction.christmas, 0), MultiplierCard(CardMultiplier.toolsum, 2, CardAction.christmas, 0), MultiplierCard(CardMultiplier.toolsum, 1, CardAction.onetimetool, 3), MultiplierCard(CardMultiplier.toolsum, 1, CardAction.onetimetool, 4), MultiplierCard(CardMultiplier.toolsum, 2, CardAction.onetimetool, 2), MultiplierCard(CardMultiplier.personcount, 1, CardAction.stone, 1), MultiplierCard(CardMultiplier.personcount, 1, CardAction.gold, 1), MultiplierCard(CardMultiplier.personcount, 2, CardAction.clay, 1), MultiplierCard(CardMultiplier.personcount, 2, CardAction.roll, 3), MultiplierCard(CardMultiplier.personcount, 1, CardAction.roll, 5), ] shuffle(self.cardPile) def _defaultHuts(self): return [SimpleHut(Resource.wood, Resource.wood, Resource.clay), SimpleHut(Resource.wood, Resource.wood, Resource.stone), SimpleHut(Resource.wood, Resource.wood, Resource.gold), SimpleHut(Resource.wood, Resource.clay, Resource.stone), SimpleHut(Resource.wood, Resource.clay, Resource.clay), SimpleHut(Resource.wood, Resource.stone, Resource.stone), SimpleHut(Resource.wood, Resource.clay, Resource.gold), SimpleHut(Resource.wood, Resource.clay, Resource.gold), SimpleHut(Resource.wood, Resource.clay, Resource.stone), SimpleHut(Resource.wood, Resource.stone, Resource.gold), SimpleHut(Resource.wood, Resource.stone, Resource.gold), SimpleHut(Resource.clay, Resource.clay, Resource.stone), SimpleHut(Resource.clay, Resource.clay, Resource.gold), SimpleHut(Resource.clay, Resource.stone, Resource.stone), SimpleHut(Resource.clay, Resource.stone, Resource.gold), SimpleHut(Resource.clay, Resource.stone, Resource.gold), SimpleHut(Resource.stone, Resource.stone, Resource.gold), AnyHut(), AnyHut(), AnyHut(), CountHut(4,1), CountHut(4,2), CountHut(4,3), CountHut(4,4), CountHut(5,1), CountHut(5,2), CountHut(5,3), CountHut(5,4), ] def numberOfHutsLeft(self): return [len(stack) for stack in self.hutStacks] def upperHuts(self): return [stack[-1] for stack in self.hutStacks if len(stack) > 0] def availableHuts(self): return [hut for hut in self.upperHuts() if not hut.isOccupied()] def availableCards(self): """returns list of (card, card-price) tuples for non occupied cards""" return [(card, self.cardPile.index(card) + 1) for card in self.openCards() if not card.isOccupied()] def addHunters(self, count, player): self.huntingGrounds.addPerson(count, player) def addLumberjacks(self, count, player): self.forest.addPerson(count, player) def freeForestSlots(self): return self.forest.freeSlots() def addClayDiggers(self, count, player): self.clayPit.addPerson(count, player) def freeClayPitSlots(self): return self.clayPit.freeSlots() def addStoneDiggers(self, count, player): self.quarry.addPerson(count, player) def freeQuarrySlots(self): return self.quarry.freeSlots() def addGoldDiggers(self, count, player): self.river.addPerson(count, player) def freeRiverSlots(self): return self.river.freeSlots() def placeOnFarm(self, player): self.farm.addPerson(player) def farmOccupied(self): return self.farm.freeSlots() == 0 def placeOnBreedingHut(self, player): self.breedingHut.addPerson(player) def breedingHutOccupied(self): return self.breedingHut.freeSlots() == 0 def placeOnToolSmith(self, player): self.toolSmith.addPerson(player) def toolSmithOccupied(self): return self.toolSmith.freeSlots() == 0 def personsOnHuts(self, player): return [stack[-1].isOccupiedBy() for stack in self.hutStacks].count(player) def personsOnCards(self, player): return [card.isOccupiedBy() for card in self.openCards()].count(player) def personsOnGrounds(self, player): return sum([ground.count(player) for ground in self.grounds]) def person(self, player): return self.personsOnGrounds(player) + self.personsOnHuts(player) + self.personsOnCards(player) def resourceGrounds(self): return self.grounds[:-3] def villageGrounds(self): return self.grounds[-3:] def occupiedCards(self, player): return [(card, self.cardPile.index(card) + 1) for card in self.openCards() if card.isOccupiedBy() == player] def openCards(self): return self.cardPile[:4] def occupiedHuts(self, player): return [stack[-1] for stack in self.hutStacks if len(stack) > 0 and stack[-1].isOccupiedBy() == player] def numberOfCardsLeft(self): return len(self.cardPile) def reapResources(self, players): activePlayer = players[0] reapedResources = [] for ground in self.villageGrounds(): activePlayer.addResources(ground.reapResources(activePlayer)) # get occupied grounds occupiedGrounds = {} for ground in self.resourceGrounds(): if ground.count(activePlayer): occupiedGrounds[(ground.resource, ground.abreviation)] = ground while len(occupiedGrounds): resourceAbr = activePlayer.chooseReapingResource("".join([k[1] for k in sorted(occupiedGrounds.keys(), reverse=True)])) toPop = [k for k in occupiedGrounds.keys() if k[1] == resourceAbr][0] activePlayer.addResources(occupiedGrounds.pop(toPop).reapResources(activePlayer)) occupiedHuts = self.occupiedHuts(activePlayer) for hut in occupiedHuts: hut.removePerson() occupiedCards = self.occupiedCards(activePlayer) for card in occupiedCards: card[0].removePerson() return occupiedHuts, occupiedCards def popHuts(self, huts): for hut in huts: [stack.pop() for stack in self.hutStacks if len(stack) > 0 and stack[-1] == hut] def removeCards(self, cards): for card in cards: try: self.cardPile.remove(card) except ValueError: print("trying to remove %s from %s" % (card, self.cardPile)) def placeOnHutIndex(self, stackIndex, color): self.upperHuts()[stackIndex].placePerson(color) def placeOnCardWithPrice(self, price, color): self.cardPile[price - 1].placePerson(color) def placeOnHut(self, hut, player): hut.placePerson(player) def placeOnCard(self, card, player): card.placePerson(player) def isFinished(self): return [len(stack) for stack in self.hutStacks].count(0) > 0 def opencardStrings(self): padding6 = 6 * " " headingline = padding6.join(["%s" % s for s in ("%sc%d.%s%s" % (padding6, p, self.cardPile[p - 1].suffix(), padding6) for p in [4, 3, 2, 1])]) pilestrings = [card.outputStrings() for card in reversed(self.openCards())] result = [padding6.join(["%s" % s for s in (line[0] for line in pilestrings)]), padding6.join(["%s" % s for s in (line[1] for line in pilestrings)])] result.insert(0,headingline) return result def hutStacksString(self): stackstrings = ["%s" % ("|" * (len(stack)-1) + (str(stack[-1]))) for stack in self.hutStacks if len(stack) > 0] hutstacks = ["h%d: %-25s" % (idx + 1, s) for idx, s in enumerate(stackstrings)] return "%s\n%s" % (" ".join(hutstacks[:2]), " ".join(hutstacks[2:])) def __str__(self): return "\nHut Stacks: \n%s" % self.hutStacksString() + "\n\n" +\ "Cards: \n%s\n%s\n%s" % (self.opencardStrings()[0], self.opencardStrings()[1], self.opencardStrings()[2]) + "\n\n" +\ "\n".join(str(ground) for ground in self.grounds) + "\n"