Issue
I'm trying to write a code for the Game of War, but I currently have two problems with my code.
The first problem is that, I can't get my game to go into WAR when both cards have the same value (in the dealWar()
function). It is determining a winner but I don't know how it is.
The second problem I have is that i'm trying to write a function that will determine the final score and determine the winner. The code I have written now finalScore()
doesn't seem to be counting all of the strings that say playerOne + " Wins!"
, it's stopping short.
import random
# deck of cards
frontOfCard = []
suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
royals = ["Ace", "Jack", "Queen", "King"]
oldDeck = []
for numbers in range(2,11):
frontOfCard.append(str(numbers))
for royal in range(4):
frontOfCard.append(royals[royal])
for suit in range(4):
for card in range(13):
cards = (frontOfCard[card] + " of " + suits[suit])
oldDeck.append(cards)
#create players
def nameOfPlayers():
print("Welcome to the Game of WAR! " + playerOne + " and " + playerTwo)
#explanation of game of war
def explanation():
print("Here are the rules!")
print("The deck is divided evenly to each player, with each player receiving 26 cards.")
print()
print("The cards will be flipped for the playeres and the player with the higher card takes both cards.")
print()
print("If the cards are the same rank, it is WAR. Each player turns up one card face down and one card face up. The player with the higher card takes both piles, if they are the same rank,each players places another card face down and turns the other face down card up, continue until the winner of that pile is determined.")
#Deal out cards
#Fix code to go into war if cards are the same
def dealWar():
random.shuffle(oldDeck)
hand1 = oldDeck[0:int(len(oldDeck)/2)]
hand2 = oldDeck[int(len(oldDeck)/2):len(oldDeck)]
for i in range(0, int(len(oldDeck)/2)):
handOne = hand1.pop()
handTwo = hand2.pop()
print(playerOne + " has played: " + handOne + " | " + playerTwo + " has played: " + handTwo)
if handOne > handTwo:
print(playerOne + " Wins!")
print()
elif handOne == handTwo:
print("WAR has BEGUN!")
handOne = hand1.pop()
handTwo = hand2.pop()
print(playerOne + " has played: " + handOne + " | " + playerTwo + " has played: " + handTwo)
if handOne > handTwo:
print(playerOne + " Wins!")
else:
print(playerTwo + " Wins!")
print()
else:
print(playerTwo + " Wins!")
print()
# fix to count every word that says player name and wins
def finalScore():
playerOneScore = len(playerOne + " Wins!")
playerTwoScore = len(playerTwo + " Wins!")
if playerOneScore > playerTwoScore:
print(playerOne + " ! ")
elif playerOneScore == playerTwoScore:
print("NO ONE, the game ended as a DRAW! ")
else:
print(playerTwo + " ! ")
#CODE
playerOne = input("What is your name? ")
playerTwo = input("What is your name? ")
print()
nameOfPlayers()
print()
explanation()
print()
userInput = input("Are both players ready to start? (y/n): ")
print()
if userInput == "y":
print("Here we go! ")
print()
elif userInput == "n":
print("Too bad we're starting anyways")
else:
print("Wrong Input, Try Again!")
print()
dealWar()
print("The winner is...........")
finalScore()
print()
playAgain = input("Would you like to play again? (y/n): ")
if playAgain == "y":
dealWar()
print()
finalScore()
print()
print(playAgain)
print()
print("The winnder is.......")
print()
finalScore()
else:
print("GOOD BYE")
Solution
For War, you could use list slicing to split the deck into two equal parts after you shuffle it.
hand1 = deck[0:int(len(deck)/2)]
hand2 = deck[int(len(deck)/2):len(deck)]
After those two lines, hand1
will contain the cards from index 0 to the middle of the deck (exclusive) and hand2
will contain the cards from the middle of the deck (inclusive) to the end.
If you wanted to simulate the act of dealing the cards from the deck, you could loop over the deck and append cards to each hand, while popping them from the deck.
for i in range(0, int(len(deck)/2)):
hand1.append(deck.pop())
hand2.append(deck.pop())
Answered By - Bill the Lizard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.