Compare commits

..

2 Commits

Author SHA1 Message Date
fad15e84c5 update .gitignore 2020-04-10 17:03:20 -04:00
4f9f790086 add Player class 2020-04-10 17:02:28 -04:00
2 changed files with 88 additions and 1 deletions

4
.gitignore vendored
View File

@@ -636,7 +636,8 @@ DerivedData/
.LSOverride
# Icon must end with two \r
Icon
Icon
# Thumbnails
._*
@@ -683,3 +684,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk
.vscode/settings.json

View File

@@ -0,0 +1,85 @@
from collections.abc import Iterable
from functools import singledispatch
from ..cards.collections import Hand
from ..cards import Card
class Player(object):
valid_roles = {"dealer","host","guest"}
def __init__(self, name, roles={}):
self._name = name
self._hand = Hand()
self._score = 0
if not roles:
roles = {}
elif isinstance(roles, str):
if roles in self.valid_roles:
roles = {roles}
else:
raise ValueError(f"{roles} is not a valid role.")
elif isinstance(roles, Iterable):
if all([role in self.valid_roles for role in roles]):
self._roles = set(roles)
else:
raise ValueError(f"{', '.join(set(roles) - self.valid_roles)} are not valid roles")
else:
raise TypeError("Role should be a string or iterable")
@singledispatch
def __add__(self, other):
pass
@__add__.register
def _(self, other: int):
self.score += other
@__add__.register
def _(self, other: Card):
self._hand.append(other)
@property
def hand(self):
return self._hand
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def score(self):
return self._score
@score.setter
def score(self, value):
self._score = value
@property
def roles(self):
return self._roles
@roles.setter
def roles(self, value):
if not value in self.valid_roles:
raise ValueError(f"Invalid Player role {value}")
if (value == "host" or "host" in value) and "host" in self._roles:
raise RuntimeError("Once a player is the host, they are the host forever")
if isinstance(value, str):
self._roles ^= set((value,))
elif isinstance(Iterable, value):
self._roles ^= set(value)
else:
raise TypeError("Invalid role type")
@property
def cards_in_hand(self):
return len(self._hand)