-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from relatko/simple-building
Add SimpleBuilding
- Loading branch information
Showing
4 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
# pylint: disable=unused-argument, duplicate-code | ||
from typing import Iterable, Optional | ||
from stone_age.simple_types import Effect | ||
|
||
|
||
class Building: | ||
def build(self, resources: Iterable[Effect]) -> Optional[int]: | ||
assert False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Iterable, Optional | ||
from stone_age.simple_types import Effect | ||
from stone_age.game_board. interfaces import Building | ||
|
||
|
||
class SimpleBuilding(Building): | ||
_required_resources: list[Effect] | ||
|
||
def __init__(self, resources: Iterable[Effect]): | ||
assert all(Effect.is_resource(x) for x in resources) | ||
self._required_resources = list(resources) | ||
|
||
def build(self, resources: Iterable[Effect]) -> Optional[int]: | ||
if not all(Effect.is_resource(x) for x in resources): | ||
return None | ||
if sorted(self._required_resources) != sorted(resources): | ||
return None | ||
return sum(Effect.points(x) for x in resources) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import unittest | ||
from stone_age.simple_types import Effect | ||
from stone_age.game_board.simple_building import SimpleBuilding | ||
|
||
class TestSimpleBuilding(unittest.TestCase): | ||
def test_non_resource_use(self) -> None: | ||
self.assertRaises(AssertionError, SimpleBuilding, [Effect.WOOD, Effect.FOOD]) | ||
|
||
def test_resources_used_once(self) -> None: | ||
building = SimpleBuilding([Effect.WOOD, Effect.CLAY]) | ||
self.assertEqual(building.build([Effect.WOOD, Effect.CLAY]), 7) | ||
self.assertIsNone(building.build([Effect.WOOD, Effect.CLAY, Effect.CLAY])) | ||
self.assertIsNone(building.build([Effect.WOOD])) | ||
self.assertIsNone(building.build([Effect.WOOD, Effect.GOLD])) | ||
|
||
def test_resources_multiset(self) -> None: | ||
building = SimpleBuilding([Effect.WOOD, Effect.WOOD, Effect.CLAY]) | ||
self.assertEqual(building.build([Effect.WOOD, Effect.WOOD, Effect.CLAY]), 10) | ||
self.assertIsNone(building.build([Effect.WOOD, Effect.CLAY, Effect.CLAY])) | ||
self.assertIsNone(building.build([Effect.WOOD, Effect.CLAY])) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
|
||
|