Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Challenge2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import random
RANKS =["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]

Player1Counter=0
Player2Counter=0
print("Welcome to WAR!")
print("You and a friend will player war until one of the two of you get ten points!")
print("Before we begin, Can you please enter our names.")
Player1Name=input("Player 1 Name: ")
Player2Name=input("Player 2 Name: ")
while Player1Counter < 10 and Player2Counter < 10:
Player1CardIndex=random.randrange(len(RANKS))
Player2CardIndex=random.randrange(len(RANKS))
Player1Card=RANKS[Player1CardIndex]
Player2Card=RANKS[Player2CardIndex]
if Player1CardIndex > 8:
Player1CardIndex = 10
if Player2CardIndex > 8:
Player2CardIndex = 10
Player1Suit=random.choice(SUITS)
Player2Suit=random.choice(SUITS)
Player1Card =Player1Card + Player1Suit
Player2Card = Player2Card + Player2Suit
print(Player1Name,":",Player1Card)
print(Player2Name,":",Player2Card)
if Player1CardIndex > Player2CardIndex:
print(Player1Name,"has won this round and has gained one point.")
Player1Counter += 1
elif Player1CardIndex < Player2CardIndex:
print(Player2Name,"has won this round and has gained one point.")
Player2Counter += 1
else:
print("Oh No! No one has won this round1")
print(Player1Name,":",Player1Counter)
print(Player2Name,":",Player2Counter)
input("Please press the <Enter> key.")
if Player1Counter < Player2Counter:
print(Player1Name,"has won and is the ultimate champion!")
else:
print(Player2Name,"has won and is the ulimate champion!")
print("Thanks for playing.")
input("Please press <Enter> to exit.")

Binary file added __pycache__/cards.cpython-32.pyc
Binary file not shown.
Binary file added __pycache__/games.cpython-32.pyc
Binary file not shown.
109 changes: 89 additions & 20 deletions blackjack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Blackjack
# From 1 to 7 players compete against a dealer
#Help from interweb tutor
#and tom

import cards, games

Expand Down Expand Up @@ -66,26 +68,60 @@ def is_busted(self):
return self.total > 21







class BJ_Player(BJ_Hand):
""" A Blackjack Player. """
def betting(stash):
try:
if stash>0:
wager=int(input("\n How mush do you want to wager?: "))
if wager > bet.stash:
int(input("\n You can only wager what you have. How much?: "))
elif wager <0:
int(input("\nYou can only wager what you have. How much?: "))
except ValuError:
int(input("\n Thats not valid choose a number: "))


def is_hitting(self):
response = games.ask_yes_no("\n" + self.name + ", do you want a hit? (Y/N): ")
return response == "y"

def bust(self):
def bust(self,stash,wager):
print(self.name, "busts.")
self.lose()
self.lose(self,stash,wager)

def lose(self):
def lose(self,stash,wager):
print(self.name, "loses.")
stash = stash - wager
print("Your stash is: ",stash)
return stash

def win(self):
def win(self,stash,wager):
print(self.name, "wins.")
stash = stash + wager
print("Your stash is: ",stash)
return stash

def push(self):
print(self.name, "pushes.")


def betting(bet,stash):
try:
if stash > 0:
wager = int(input("\nHow much do you want to wager?: "))
if wager > stash:
int(input("\n You can only wager what you have. How much?: "))
elif wager < 0:
int(input("\n You can only wager what you have. How much?: "))
except ValueError:
int(input("\n That's not valid! Choose a number: "))


class BJ_Dealer(BJ_Hand):
""" A Blackjack Dealer. """
def is_hitting(self):
Expand All @@ -104,15 +140,43 @@ class BJ_Game(object):
def __init__(self, names):
self.players = []
for name in names:
stash=100
player = BJ_Player(name)
bet=BJ_Player(name).betting(stash)

playerbet=bet(stash).betting(stash)
self.players.append(player)


def betting(bet,stash):
try:
if stash > 0:
wager = int(input("\nHow much do you want to wager?: "))
if wager > stash:
int(input("\n You can only wager what you have. How much?: "))
elif wager < 0:
int(input("\n You can only wager what you have. How much?: "))
except ValueError:
int(input("\n That's not valid! Choose a number: "))

self.dealer = BJ_Dealer("Dealer")

self.deck = BJ_Deck()
self.deck.populate()
self.deck.shuffle()

def populate(self):
for suit in BJ_Card.SUITS:
for rank in BJ_Card.RANKS:
self.cards.append(BJ_Card(rank, suit))

def gamble(bet):
if bet.stash <= 0:
print("\nYou are out of money! You're out of the game!")

def __init__(bet, money = 10):
stash = money

@property
def still_playing(self):
sp = []
Expand All @@ -121,14 +185,14 @@ def still_playing(self):
sp.append(player)
return sp

def __additional_cards(self, player):
def __additional_cards(self, player,stash,wager):
while not player.is_busted() and player.is_hitting():
self.deck.deal([player])
print(player)
if player.is_busted():
player.bust()
player.bust(self,stash,wager)

def play(self):
def play(self,stash,wager):
# deal initial 2 cards to everyone
self.deck.deal(self.players + [self.dealer], per_hand = 2)
self.dealer.flip_first_card() # hide dealer's first card
Expand All @@ -138,58 +202,63 @@ def play(self):

# deal additional cards to players
for player in self.players:
self.__additional_cards(player)
self.__additional_cards(player,stash,wager)

self.dealer.flip_first_card() # reveal dealer's first
self.dealer.flip_first_card() # reveal dealer's first

if not self.still_playing:
# since all players have busted, just show the dealer's hand
print(self.dealer)
else:
# deal additional cards to dealer
print(self.dealer)
self.__additional_cards(self.dealer)
self.__additional_cards(self.dealer,stash,wager)

if self.dealer.is_busted():
# everyone still playing wins
for player in self.still_playing:
player.win()
player.win(stash,wager)
else:
# compare each player still playing to dealer
for player in self.still_playing:
if player.total > self.dealer.total:
player.win()
player.win(stash,wager)
elif player.total < self.dealer.total:
player.lose()
player.lose(stash,wager)
else:
player.push()

# remove everyone's cards
for player in self.players:
player.clear()
self.dealer.clear()


def main():
print("\t\tWelcome to Blackjack!\n")

stash = 0
wager = 0
pari=10
names = []
number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8)
number = games.ask_number("How many players? (1 - 7): ", low = 1, high =8)

for i in range(number):
name = input("Enter player name: ")
bet=BJ_Player(name).betting(pari)
names.append(name)
print()

game = BJ_Game(names)



again = None
while again != "n":
game.play()
game.play(stash,wager)
again = games.ask_yes_no("\nDo you want to play again?: ")


main()
input("\n\nPress the enter key to exit.")



45 changes: 45 additions & 0 deletions challeng2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import random
RANKS =["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]

Player1Counter=0
Player2Counter=0
print("Welcome to WAR!")
print("You and a friend will player war until one of the two of you get ten points!")
print("Before we begin, Can you please enter our names.")
Player1Name=input("Player 1 Name: ")
Player2Name=input("Player 2 Name: ")
while Player1Counter < 10 or Player2Counter < 10:
Player1CardIndex=random.randint(len(RANKS))
Player2CardIndex=random.randint(len(RANKS))
Player1Card=RANKS[Player1CardIndex]
Player2Card=RANKS[Player2CardIndex]
if Player1CardIndex < 9:
Player1CardIndex = 10
if Player2CardIndex < 9:
Player2CardIndex = 10
Player1Suit=random.choice(SUITS)
Player2Suit=random.choice(SUITS)
Player1Card =Player1Card + Player1Suit
Player2Card = Player2Card + Player2Suit
print(Player1Name,":",Player1Card)
print(Player2Name,":",Player2Card)
if Player1CardIndex < Player2CardIndex:
print(Player1Name,"has won this round and has gained one point.")
Player1Counter += 1
elif Player1CardIndex > Player2CardIndex:
print(PlayerName,"has won this round and has gained one point.")
Player2Counter += 1
else:
print("Oh No! No one has won this round1")
print(Player1Name,":",Player1Counter)
print(Player2Name,":",Player2Counter)
input("Please press the <Enter> key.")
if Player1Counter < Player2Counter:
print(Player1Name,"has won and is the ultimate champion!")
else:
print(Player2Name,"has won and is the ulimate champion!")
print("Thanks for playing.")
input("Please press <Enter> to exit.")

13 changes: 13 additions & 0 deletions challeng2.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RANKS =["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = ["c", "d", "h", "s"]

Player1Counter=0
Player2Counter=0
print("Welcome to WAR!")
print("You and a friend will player war until one of the two of you get ten points!")
print("Before we begin, Can you please enter our names.")
Player1Name=input("Player 1 Name: ")
Player2Name=input("Player 2 Name: ")
while Player1Counter < 10 or Player2Counter < 10: