-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPart3_OOP_Project_SOLUTIONS.py
170 lines (141 loc) · 5.07 KB
/
Part3_OOP_Project_SOLUTIONS.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#####################################
### WELCOME TO YOUR OOP PROJECT #####
#####################################
# For this project you will be using OOP to create a card game. This card game will
# be the card game "War" for two players, you an the computer. If you don't know
# how to play "War" here are the basic rules:
#
# The deck is divided evenly, with each player receiving 26 cards, dealt one at a time,
# face down. Anyone may deal first. Each player places his stack of cards face down,
# in front of him.
#
# The Play:
#
# Each player turns up a card at the same time and the player with the higher card
# takes both cards and puts them, face down, on the bottom of his stack.
#
# If the cards are the same rank, it is War. Each player turns up three cards face
# down and one card face up. The player with the higher cards takes both piles
# (six cards). If the turned-up cards are again the same rank, each player places
# another card face down and turns another card face up. The player with the
# higher card takes all 10 cards, and so on.
#
# There are some more variations on this but we will keep it simple for now.
# Ignore "double" wars
#
# https://en.wikipedia.org/wiki/War_(card_game)
from random import shuffle
# Two useful variables for creating Cards.
SUITE = 'H D S C'.split()
RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
class Deck:
"""
This is the Deck Class. This object will create a deck of cards to initiate
play. You can then use this Deck list of cards to split in half and give to
the players.
"""
def __init__(self):
print("Creating New Ordered Deck")
self.allcards = [(s,r) for s in SUITE for r in RANKS ]
def shuffle(self):
print("Shuffling Deck")
shuffle(self.allcards)
def split_in_half(self):
return (self.allcards[:26],self.allcards[26:])
class Hand:
'''
This is the Hand class. Each player has a hand, and can add or remove
cards from that hand.
'''
def __init__(self,cards):
self.cards = cards
def __str__(self):
return "Contains {} cards".format(len(self.cards))
def add(self,added_cards):
self.cards.extend(added_cards)
def remove_card(self):
return self.cards.pop()
class Player:
def __init__(self,name,hand):
self.name = name
self.hand = hand
def play_card(self):
drawn_card = self.hand.remove_card()
print("{} has placed: {}".format(self.name,drawn_card))
print('\n')
return drawn_card
def remove_war_cards(self):
war_cards = []
if len(self.hand.cards) < 3:
return war_cards
else:
for x in range(3):
war_cards.append(self.hand.cards.pop(0))
return war_cards
def still_has_cards(self):
"""
Returns True if player still has cards
"""
return len(self.hand.cards) != 0
######################
#### GAME PLAY #######
######################
print("Welcome to War, let's begin...")
# Create New Deck and split in half
d = Deck()
d.shuffle()
half1,half2 = d.split_in_half()
# Create Both Players
comp = Player("computer",Hand(half1))
name = input("What is your name player? ")
user = Player(name,Hand(half2))
# Set Round Count
total_rounds = 0
war_count = 0
# Let's play
while user.still_has_cards() and comp.still_has_cards():
total_rounds += 1
print("It is time for a new round!")
print("Here are the current standings: ")
print(user.name+" count: "+str(len(user.hand.cards)))
print(comp.name+" count: "+str(len(comp.hand.cards)))
print("Both players play a card!")
print('\n')
# Cards on Table represented by list
table_cards = []
# Play cards
c_card = comp.play_card()
p_card = user.play_card()
# Add to table_cards
table_cards.append(c_card)
table_cards.append(p_card)
# Check for War!
if c_card[1] == p_card[1]:
war_count +=1
print("We have a match, time for war!")
print("Each player removes 3 cards 'face down' and then one card face up")
table_cards.extend(user.remove_war_cards())
table_cards.extend(comp.remove_war_cards())
# Play cards
c_card = comp.play_card()
p_card = user.play_card()
# Add to table_cards
table_cards.append(c_card)
table_cards.append(p_card)
# Check to see who had higher rank
if RANKS.index(c_card[1]) < RANKS.index(p_card[1]):
print(user.name+" has the higher card, adding to hand.")
user.hand.add(table_cards)
else:
print(comp.name+" has the higher card, adding to hand.")
comp.hand.add(table_cards)
else:
# Check to see who had higher rank
if RANKS.index(c_card[1]) < RANKS.index(p_card[1]):
print(user.name+" has the higher card, adding to hand.")
user.hand.add(table_cards)
else:
print(comp.name+" has the higher card, adding to hand.")
comp.hand.add(table_cards)
print("Great Game, it lasted: "+str(total_rounds))
print("A war occured "+str(war_count)+" times.")