-
-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"war" card game #371
Open
vedpawar2254
wants to merge
2
commits into
DhanushNehru:master
Choose a base branch
from
vedpawar2254:"WAR"-card-game
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "\"WAR\"-card-game"
Open
"war" card game #371
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from random import shuffle | ||
|
||
class Card: | ||
suits = ["spades", "hearts", "diamonds", "clubs"] | ||
values = [None, None, "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"] | ||
|
||
def __init__(self, v, s): | ||
"""suit + value are ints""" | ||
self.value = v | ||
self.suit = s | ||
|
||
def __lt__(self, c2): | ||
if self.value < c2.value: | ||
return True | ||
if self.value == c2.value: | ||
if self.suit < c2.suit: | ||
return True | ||
else: | ||
return False | ||
return False | ||
|
||
def __gt__(self, c2): | ||
if self.value > c2.value: | ||
return True | ||
if self.value == c2.value: | ||
if self.suit > c2.suit: | ||
return True | ||
else: | ||
return False | ||
return False | ||
|
||
def __repr__(self): | ||
v = self.values[self.value] + " of " + self.suits[self.suit] | ||
return v | ||
|
||
class Deck: | ||
def __init__(self): | ||
self.cards = [] | ||
for i in range(2, 15): | ||
for j in range(4): | ||
self.cards.append(Card(i, j)) | ||
shuffle(self.cards) | ||
|
||
def rm_card(self): | ||
if len(self.cards) == 0: | ||
return | ||
return self.cards.pop() | ||
|
||
class Player: | ||
def __init__(self, name): | ||
self.wins = 0 | ||
self.card = None | ||
self.name = name | ||
|
||
class Game: | ||
def __init__(self): | ||
name1 = input("Player 1 name: ") | ||
name2 = input("Player 2 name: ") | ||
self.deck = Deck() | ||
self.p1 = Player(name1) | ||
self.p2 = Player(name2) | ||
|
||
def wins(self, winner): | ||
w = "{} wins this round".format(winner) | ||
print(w) | ||
|
||
def draw(self, p1n, p1c, p2n, p2c): | ||
d = "{} drew {} and {} drew {}".format(p1n, p1c, p2n, p2c) | ||
print(d) | ||
|
||
def play_game(self): | ||
cards = self.deck.cards | ||
print("Beginning War!") | ||
while len(cards) >= 2: | ||
m = "Press 'q' to quit. Any other key to play: " | ||
response = input(m) | ||
if response == 'q': | ||
break | ||
p1c = self.deck.rm_card() | ||
p2c = self.deck.rm_card() | ||
p1n = self.p1.name | ||
p2n = self.p2.name | ||
self.draw(p1n, p1c, p2n, p2c) | ||
if p1c > p2c: | ||
self.p1.wins += 1 | ||
self.wins(self.p1.name) | ||
else: | ||
self.p2.wins += 1 | ||
self.wins(self.p2.name) | ||
|
||
win = self.winner(self.p1, self.p2) | ||
print("War is over. {} wins".format(win)) | ||
|
||
def winner(self, p1, p2): | ||
if p1.wins > p2.wins: | ||
return p1.name | ||
if p1.wins < p2.wins: | ||
return p2.name | ||
return "It was a tie!" | ||
|
||
game = Game() | ||
game.play_game() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you consider to add a readme instruction for thsi script?