This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.py
82 lines (77 loc) · 3.5 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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
# series number = 13 * m + n
# m: 0->Spades, 1->Hearts, 2->Clubs, 3->Diamonds
# n: 0~7->2~9, 8->10, 9~12->JQKA
from platform import system # system determines whether the program is run on a Windows, a Mac of a Linux computer.
from color import color # to make use of the colors
class card:
'Represents a digital version of a poker card'
__series = -1 # Initialize it by -1
__suites = ('♠♥♣♦', 'SHCD')
# __numbers = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
__numbers = tuple([number for number in
('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')])
@staticmethod
def mnToSeries(mnTuple): return 13 * mnTuple[0] + mnTuple[1]
@staticmethod
def seriesToMN(series): return series // 13, series % 13
def setMN(self, m, n):
'Sets the m, n values and return self'
self.setSeries(self.mnToSeries((m, n)))
return self
def getMN(self):
'Gets the m, n values'
return self.seriesToMN(self.__series)
def setSeries(self, series = -1):
'Sets the series number and return self'
# This method is only supposed to be used in initializing a deck of cards
self.__series = -1 if series < 0 else series
return self
def getSeries(self):
'Gets the series number'
return self.__series
def str(self, colored = True):
'Displays the card as text'
# For Windows, "colored" is deprecated and set always to be False
sysName = system() # obtain the OS's name
colored &= sysName != 'Windows'
mnToStr = lambda mnTuple, indexOfSuites: \
'Undefined card' if mnTuple[0] < 0 or mnTuple[1] < 0 else \
card.__suites[indexOfSuites][mnTuple[0]] + card.__numbers[mnTuple[1]]
#####################################################################################
# the current decision is that if windows, output SHCD and otherwise output unicode #
#####################################################################################
return ((color() if self.getMN()[0] % 2 == 0 else color('red')).text \
if colored else (lambda x: x))(mnToStr(self.getMN(), 'Windows' == sysName))
def printData(self, colored = True):
'For Windows, "colored" is deprecated and set always to be False'
print(self.str(colored))
################################
# The section for game playing #
################################
def higher(self, currGreat = None):
'Returns the higher card, according to currGreat'
'''compare whether self is the greater card according to the current suite
returns true if it is, false otherwise'''
if not currGreat: return self # if currGreat is None
return currGreat if \
self.getMN()[0] != currGreat.getMN()[0] or self.getMN()[1] <= currGreat.getMN()[1] \
else self
def __eq__(self, other):
return self.__series == other.__series
def sortKey(self):
'''When sorting hand, sort by the order:
suites as Clubs-Diamonds-Spades-Hearts
number from lowest to greatest
The easiest way to do this is to add
+series numbers of Spades and Hearts by 4 * 13
'''
return self.__series + (0 if self.getMN()[0] > 1 else 52)
def score(self):
if self.__series == 10:
return 13
if 13 <= self.__series <= 25:
return 1
return 0
pass