Skip to content

Commit daf5beb

Browse files
author
manasaa2811
committed
Added Card_game
1 parent a6bff7f commit daf5beb

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

Diff for: PyGamesScripts/Card_Game/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Card game
2+
## This is a simple card game built using object oriented programming with python.
3+
4+
# Instructions
5+
1. There are two players in this game. Each player gets half a deck(26 cards)
6+
2. In each round the players put out 1 card and the values of the cards are compared.
7+
3. the player with the higher value gets both the cards.
8+
4. Incase both the values are equal, there is a WAR.
9+
5. When at war compare the last card in the deck of each player and repeat steps 2 and 3.
10+
6. The first player to have less than 5 cards loses the game.
11+
NOTE: This is a simple non responsive game, hence both the players are computer bots.
12+
13+
# Setup
14+
This code can be run on any python IDE of jupyter notebook.
15+
16+
# The code is explained as comments wherever necassary.
17+
18+
Libraries imported-random
19+
20+
# Screenshot of output
21+
22+
Click here!!--> [Output](Screenshot.png)
23+
24+
# Author : Manasa, github: Manasa2811

Diff for: PyGamesScripts/Card_Game/Screenshot.png

97.8 KB
Loading

Diff for: PyGamesScripts/Card_Game/card_game.py

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import random
2+
3+
suits=('Hearts','Diamonds','Spades','Clubs')
4+
ranks=('Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace')
5+
#Values are assigned to make the comparison easier. A dictionary has been used.
6+
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}
7+
8+
#Object oriented programming is used.
9+
#Various classes are created.
10+
11+
#card class
12+
13+
class Card():
14+
15+
def __init__(self,suit,rank):
16+
self.suit=suit
17+
self.rank=rank
18+
self.value=values[rank]
19+
20+
def __str__(self):
21+
return self.rank + ' of ' + self.suit
22+
23+
#deck class
24+
25+
class Deck():
26+
def __init__(self):
27+
#Create a list to store all the cards of the deck
28+
self.allcards = []
29+
for suit in suits:
30+
for rank in ranks:
31+
self.allcards.append(Card(suit,rank))
32+
33+
def shuffle(self):
34+
#Shuffle the deck using "random" which was imported
35+
random.shuffle(self.allcards)
36+
37+
def deal_one(self):
38+
#Deal one card from the deck
39+
return self.allcards.pop()
40+
41+
#player class
42+
43+
class Player():
44+
def __init__(self,name):
45+
#define the player
46+
self.name = name
47+
self.allcards = []
48+
49+
def remove_one(self):
50+
#Remove one card
51+
return self.allcards.pop(0)
52+
53+
def add_cards(self,new_cards):
54+
#add cards
55+
if type(new_cards) == type([]):
56+
self.allcards.extend(new_cards)
57+
else:
58+
self.allcards.append(new_cards)
59+
60+
61+
def __str__(self):
62+
#Returns the number of cards the player has
63+
return f'Player {self.name} has {len(self.allcards)} cards.'
64+
65+
#Main logic of the game
66+
67+
#First define the names of both players
68+
player_one=Player("player1")
69+
player_two=Player("player2")
70+
71+
#Now, create a deck and shuffle it
72+
new_deck=Deck()
73+
new_deck.shuffle()
74+
75+
#divide the cards equally amongst both the players (a deck has 52 cards)
76+
for x in range(26):
77+
player_one.add_cards(new_deck.deal_one())
78+
player_two.add_cards(new_deck.deal_one())
79+
80+
import pdb
81+
82+
game_on=True
83+
84+
print("Welcome!!")
85+
86+
round_num = 0
87+
while game_on:
88+
89+
round_num += 1
90+
#print the round number to keep track.
91+
print(f"Round {round_num}")
92+
93+
94+
if len(player_one.allcards) == 0:
95+
print("Player One out of cards! Game Over")
96+
print("Player Two Wins!")
97+
game_on = False
98+
break
99+
100+
if len(player_two.allcards) == 0:
101+
print("Player Two out of cards! Game Over")
102+
print("Player One Wins!")
103+
game_on = False
104+
break
105+
106+
player_one_cards = []
107+
player_one_cards.append(player_one.remove_one())
108+
109+
player_two_cards = []
110+
player_two_cards.append(player_two.remove_one())
111+
112+
#check for a condition where both the players have an equal value on their card
113+
#This condition is called war
114+
at_war = True
115+
116+
while at_war:
117+
if player_one_cards[-1].value > player_two_cards[-1].value:
118+
119+
120+
player_one.add_cards(player_one_cards)
121+
player_one.add_cards(player_two_cards)
122+
123+
124+
125+
at_war = False
126+
127+
128+
elif player_one_cards[-1].value < player_two_cards[-1].value:
129+
130+
131+
player_two.add_cards(player_one_cards)
132+
player_two.add_cards(player_two_cards)
133+
134+
135+
at_war = False
136+
137+
else:
138+
print('WAR!')
139+
140+
if len(player_one.allcards) < 5: #It can be any number other than 5 as well, depends on game rules.
141+
print("Player One unable to play war! Game Over at War")
142+
print("Player Two Wins! Player One Loses!")
143+
game_on = False
144+
break
145+
146+
elif len(player_two.allcards) < 5:
147+
print("Player Two unable to play war! Game Over at War")
148+
print("Player One Wins! Player Two Loses!")
149+
game_on = False
150+
break
151+
152+
else:
153+
for num in range(5):
154+
player_one_cards.append(player_one.remove_one())
155+
player_two_cards.append(player_two.remove_one())
156+
157+
158+
159+
160+
161+
162+

0 commit comments

Comments
 (0)