-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
90 lines (69 loc) · 2.28 KB
/
card.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
#-*- coding: utf-8 -*-
from suit import *
from bcolor import *
class Card:
"""
Card for the Hanabi game.
"""
def __init__(self, suit=Suit.unknown, value=0):
self.value = value
self.suit = suit
def getSuit(self):
return self.suit
@classmethod
def getSuitFromInt(cardID):
return Suit(int((cardID - 1) / 5) + 1)
def getValue(self):
return self.value
@classmethod
def getValueFromInt(cls, cardID):
return ((cardID - 1) % 5) + 1
def setSuit(self, suit):
self.suit = suit
def setValue(self, value):
self.value = value
def toString(self):
return Suit.toColor(self.suit) + " " + Suit.toString(self.suit) + " " + str(self.value) + Bcolor.END
def toBinary(self):
return pad([int(i) for i in str(bin(Suit.toInt(self.getSuit())))[2:]], 3) + pad([int(i) for i in str(bin(self.getValue()))[2:]], 3)
def __str__(self):
return self.toString()
def __int__(self):
""" overload of int() for easy value comparisons
1 <= Suit.toInt(self.suit) <= 6
0 <= (Suit.toInt(self.suit) - 1) <= 5
0 <= (Suit.toInt(self.suit) - 1) * 5 <= 25
1 <= (Suit.toInt(self.suit) - 1) * 5 + self.value <= 30
this stretches our nColors*nValues = 6*5 = 30 different types
of cards over 30 different values, which allows us to order them
"""
return (Suit.toInt(self.suit) - 1) * 5 + self.value
def __lt__(self, other):
""" Overload of the < operator for sorting
"""
return int(self) < int(other)
def __le__(self, other):
""" Overload of the <= operator
"""
return int(self) <= int(other)
def __gt__(self, other):
""" Overload of the > operator
"""
return int(self) > int(other)
def __ge__(self, other):
""" Overload of the >= operator
"""
return int(self) >= int(other)
def __eq__(self, other):
""" Overload of the == operator
"""
return int(self) == int(other)
def __ne__(self, other):
""" Overload of the != operator
"""
return int(self) != int(other)
def pad(x, size):
while len(x) < size:
x = [0] + x
return x