forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classicIndianCardMatch.py
124 lines (106 loc) · 3.43 KB
/
classicIndianCardMatch.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import random
import time
SUITS = ("C", "S", "H", "D")
RANKS = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K")
VALUES = {
"A": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"T": 10,
"J": 10,
"Q": 10,
"K": 10,
}
class card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
self.suit = None
self.rank = None
print("Invalid card: ", suit, rank)
def __str__(self):
return self.suit + self.rank
def getRank(self):
return self.rank
def getSuit(self):
return self.suit
class deck:
def __init__(self):
self.deck = [card(suit, rank) for suit in SUITS for rank in RANKS]
def shuffle(self):
random.shuffle(self.deck)
def dealCard(self):
return random.choice(self.deck)
def __str__(self):
print(self.deck)
# Begin play
# create two decks, one for each player.
print("Gathering brand new two decks of cards............\n")
deck1 = deck()
deck2 = deck()
time.sleep(5)
print("..........decks ready!!!\n")
print("Combining and shuffling both the decks..")
time.sleep(10)
# Shuffle the decks
deck1.shuffle()
deck2.shuffle()
# combine both the shuffled decks
combinedDeck = deck1.deck + deck2.deck
# ReShuffle the combined deck, cut it and distribute to two players.
random.shuffle(combinedDeck)
print("....decks have been combined and shuffled...\n")
print("------------------------------------------\n")
input("Enter a key to cut the deck..\n")
player1 = combinedDeck[0:52]
player2 = combinedDeck[52:]
print(
"Deck has been split into two and Human get a half and computer gets the other...\n"
)
# Begin play:
print("------------------------------------------\n")
print("player1 == Human\n")
print("player2 == Computer\n")
print("------------------------------------------\n")
print("player1 goes first...hit any key to place the card on the pile..\n")
centerPile = []
currentPlayer2Card = None
while (
len(player1) != 0 and len(player2) != 0
): # this needs a fix as it goes on an infinite loop on a success.
switchPlayer = True
while switchPlayer == True:
for card in range(len(player1)):
input("Enter any key to place a card!!!\n")
currentPlayer1Card = player1[card].rank
print("Your current card's rank: {}".format(currentPlayer1Card))
centerPile.append(player1[card])
player1.pop(card)
switchPlayer = False
if currentPlayer2Card == currentPlayer1Card:
player1 = player1 + centerPile
print(
"The human got a match and takes all the cards from center pile.."
)
break
while switchPlayer == False:
for card in range(len(player2)):
currentPlayer2Card = player2[card].rank
print("Computer's current card's rank: {}".format(currentPlayer2Card))
centerPile.append(player2[card])
player2.pop(card)
switchPlayer = True
if currentPlayer1Card == currentPlayer2Card:
player2 = player2 + centerPile
print("Computer got a match and takes all the cards from center pile..")
break
print("GAME OVER!!!\n")
print("Human has {} cards and computer has {}..".format(len(player1), len(player2)))