Using Python develop a program in modular format (def Main, call Main, etc.) for
ID: 3735599 • Letter: U
Question
Using Python develop a program in modular format (def Main, call Main, etc.) for the following question: The child's card game, War, consists of two players each having a deck of cards. For each play each person turns over the top card in hi or her deck. The higher card wins that round of play and the winner takes both cards. The game continues until one person has all the cards and the other has none. create a program that stimulates a modified game of War. The computer will play both hands, as PlayerOne and PlayerTwo score is increased by 1. if there is a tie, no score is incremented. when one "Player" reaches a score of 10, that player should be deemed the winner and the game ends. the range of numbers should be 1-13 to stimulate the values of cards in a deck.
Explanation / Answer
here is the code for the above stated program import itertools import random class Cards: def __init__(self): self.suits = ['Spades','Hearts','Diamonds','Clubs'] self.values = range(1,14) self.ActualCards = [] #Empty List to Append for Card in itertools.product(self.suits,self.values): self.ActualCards.append(Card) #Cartesian Product to Create Deck def GetRandomCard(self): RandomNumber = random.randint(0,51) CardToBeReturned = self.ActualCards[RandomNumber] #Generates Random Card return(CardToBeReturned) class Player: def __init__(self,ID,Card): self.PlayerID = ID self.CardForPlayer = Card class Game: def __init__(self,NameOfGame): self.name = NameOfGame class SimpleWar(Game): def __init__(self,NumberOfPlayers): self.NumberOfPlayers = NumberOfPlayers self.PlayerList = [] def StartGame(self): DeckOfCards = Cards() for playerID in range(0,self.NumberOfPlayers): CardForPlayer = DeckOfCards.GetRandomCard() #Deal Card to Player NewPlayer = Player(playerID,CardForPlayer) #Save Player ID and Card self.PlayerList.append(NewPlayer) self.DecideWinner() def DecideWinner(self): WinningID = self.PlayerList[0] #Choose Player 0 as Potential Winner for playerID in self.PlayerList: if(playerID.CardForPlayer[1]>WinningID.CardForPlayer[1]): WinningID = playerID #Find the Player with Highest Card print "Winner is Player "+str(WinningID.PlayerID) print "Her Card was "+ str(WinningID.CardForPlayer[1]) + " of " + str(WinningID.CardForPlayer[0]) if __name__=="__main__": NewGame = SimpleWar(2) NewGame.StartGame()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.