-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
157 lines (126 loc) · 4.42 KB
/
index.py
File metadata and controls
157 lines (126 loc) · 4.42 KB
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
import random
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8,
'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14}
class Card():
def __init__(self,suit,rank):
self.suit=suit
self.rank=rank
self.value=values[rank]
def __str__(self):
return self.rank+" of "+self.suit
# twoheart=Card(suits,ranks)
# print(twoheart)
# print(values[twoheart.rank] )
#deck class
class Deck:
def __init__(self):
self.allcards=[]
for suit in suits:
for rank in ranks:
created_card=Card(suit,rank)
self.allcards.append(created_card)
def shuffle(self):
random.shuffle(self.allcards)
def dealone(self):
return self.allcards.pop()
newdeck=Deck()
print(newdeck.allcards[-1])
newdeck.shuffle()
for cardobject in newdeck.allcards:
print(cardobject)
mycard=newdeck.dealone()
print(mycard)
# player card
class Player:
def __init__(self,name):
self.name=name
self.allcards=[]
def removeone(self):
return self.allcards.pop(0)
def addcards(self,newcard):
if type(newcard)==type([]):
self.allcards.extend(newcard)
else:
self.allcards.append(newcard)
def __str__(self):
return f"Player {self.name} has {len(self.allcards)} cards."
newplayer=Player("Jose")
# print(newplayer)
newplayer.addcards(mycard)
print(newplayer)
print(newplayer.allcards[0])
newplayer.addcards([mycard,mycard])
print(newplayer)
newplayer.removeone()
print(newplayer)
# print(mycard)
#game setup
#while game
#indented while
player_one=Player("one")
player_two=Player("two")
newdeck=Deck()
newdeck.shuffle()
for x in range(26):
player_one.addcards(newdeck.dealone())
player_two.addcards(newdeck.dealone())
game_on=True
round_num = 0
while game_on:
round_num += 1
print(f"Round {round_num}")
# Check to see if a player is out of cards:
if len(player_one.allcards) == 0:
print("Player One out of cards! Game Over")
print("Player Two Wins!")
game_on = False
break
if len(player_two.allcards) == 0:
print("Player Two out of cards! Game Over")
print("Player One Wins!")
game_on = False
break
# Otherwise, the game is still on!
# Start a new round and reset current cards "on the table"
player_one_cards = []
player_one_cards.append(player_one.removeone())
player_two_cards = []
player_two_cards.append(player_two.removeone())
at_war = True
while at_war:
if player_one_cards[-1].value > player_two_cards[-1].value:
# Player One gets the cards
player_one.addcards(player_one_cards)
player_one.addcards(player_two_cards)
# No Longer at "war" , time for next round
at_war = False
# Player Two Has higher Card
elif player_one_cards[-1].value < player_two_cards[-1].value:
# Player Two gets the cards
player_two.addcards(player_one_cards)
player_two.addcards(player_two_cards)
# No Longer at "war" , time for next round
at_war = False
else:
print('WAR!')
# This occurs when the cards are equal.
# We'll grab another card each and continue the current war.
# First check to see if player has enough cards
# Check to see if a player is out of cards:
if len(player_one.allcards) < 5:
print("Player One unable to play war! Game Over at War")
print("Player Two Wins! Player One Loses!")
game_on = False
break
elif len(player_two.allcards) < 5:
print("Player Two unable to play war! Game Over at War")
print("Player One Wins! Player One Loses!")
game_on = False
break
# Otherwise, we're still at war, so we'll add the next cards
else:
for num in range(5):
player_one_cards.append(player_one.removeone())
player_two_cards.append(player_two.removeone())