IN PYTHON Guessing Game1 Part 1 Listed below is code to play a guessing game. In
ID: 3694320 • Letter: I
Question
IN PYTHON
Guessing Game1
Part 1
Listed below is code to play a guessing game. In the game two players attempt to guess a number. Your task is
to extend the program with objects that represent either a human player or a computer player.
def guessingGame(player1, player2):
answer = random.randint(0,100)
while(True):
print(player1.getName()+"'s turn to guess: ", end="")
guess = player1.getGuess()
if checkForWin(player1,guess,answer):
return
print(player2.getName()+"'s turn to guess: ", end="")
guess = player2.getGuess()
if checkForWin(player2,guess,answer):
return
def checkForWin(player,guess,answer):
print(player.getName(),"guesses", guess)
if answer == guess:
print("You're right! You win!")
return True
elif answer < guess:
print("Your guess is too high.")
else:
print("Your guess is too low.")
return
First, define a Player class with a single attribute for the player's name, an accessor method: getName that will return the player's name and a second accessor method: getGuess that will simply return zero.
Next, define a class named HumanPlayer derived from Player. Override the base-class getGuess
method to prompt the user to enter a number, then return the entered value as an integer.
Next, define a class named ComputerPlayer derived from Player. Override the base-class getGuess
method to return a number between 0 and 100.
Finally, construct a function named main that will do the following:
• invoke guessingGame() using two instances of a HumanPlayer (human versus human)
• invoke guessingGame() using an instance of a HumanPlayer and ComputerPlayer (human
versus computer)
• invoke guessingGame() using two instances of ComputerPlayer (computer versus computer).
You will need to include the random module and the code listed above.
Part 2
The computer player in Part 1 does not play very well in the number guessing game since it only makes random
guesses. Modify the program so that the computer plays a more sophisticated game. The specific strategy is up
to you, but you will need to add method(s) to the Player and ComputerPlayer classes so that the
guessingGame function can provide the results of a guess back to the player. In other words, the computer
must be told if its last guess was too high or too low. The computer can then use this information to revise its
next guess.
Explanation / Answer
Solution:
1. Part-1: See the code below:-
----------------------------------------------------------------
#imports random module
import random
#Player class
class Player:
def __init__(self,name):
self.name=name
def getName(self):
return self.name
def getGuess(self):
return 0
#HumanPlayer class derived from Player class
class HumanPlayer(Player):
def getGuess(self):
num=input("Enter a number")
return int(num)
#ComputerPlayer class derived from Player class
class ComputerPlayer(Player):
def getGuess(self):
return random.randint(0,100)
#function for guessing game
def guessingGame(player1, player2):
answer = random.randint(0,100)
while(True):
print(player1.getName()+"'s turn to guess: ", end="")
guess = player1.getGuess()
if checkForWin(player1,guess,answer):
return
print(player2.getName()+"'s turn to guess: ", end="")
guess = player2.getGuess()
if checkForWin(player2,guess,answer):
return
#function to check for win
def checkForWin(player,guess,answer):
print(player.getName(),"guesses", guess)
if answer == guess:
print("You're right! You win!")
return True
elif answer < guess:
print("Your guess is too high.")
else:
print("Your guess is too low.")
#driver function
def main():
humanPlayer1=HumanPlayer("John")
humanPlayer2=HumanPlayer("Martin")
computerPlayer1=ComputerPlayer("Computer1")
computerPlayer2=ComputerPlayer("Computer2")
guessingGame(humanPlayer1,humanPlayer2) #invoking with two human players
guessingGame(humanPlayer1,computerPlayer1) #invoking with one human player and one computer player
guessingGame(computerPlayer1,computerPlayer2) #invoking with two computer players
return
#calling driver function
main()
-------------------------------------------------------------
2. Part-2: See the code below:
--------------------------------------------------------
#imports random module
import random
#Player class
class Player:
def __init__(self,name, previousGuess):
self.name=name
self.previousGuess=0
def getName(self):
return self.name
def getGuess(self):
return 0
def getPreviousGuess(self):
return 0
def setPreviousGuess(self,previousGuess):
self.previousGuess=previousGuess
#HumanPlayer class derived from Player class
class HumanPlayer(Player):
def getGuess(self):
num=input("Enter a number")
return int(num)
#ComputerPlayer class derived from Player class
class ComputerPlayer(Player):
def getGuess(self):
previousGuess=self.getPreviousGuess()
if(previousGuess!=0):
return random.randint(0,previousGuess)
else:
return random.randint(0,100)
def getPreviousGuess(self):
return self.previousGuess
#function for guessing game
def guessingGame(player1, player2):
answer = random.randint(0,100)
while(True):
print(player1.getName()+"'s turn to guess: ", end="")
guess = player1.getGuess()
player1.setPreviousGuess(guess)
if checkForWin(player1,guess,answer):
return
print(player2.getName()+"'s turn to guess: ", end="")
guess = player2.getGuess()
player2.setPreviousGuess(guess)
if checkForWin(player2,guess,answer):
return
#function to check for win
def checkForWin(player,guess,answer):
print(player.getName(),"guesses", guess)
if answer == guess:
print("You're right! You win!")
return True
elif answer < guess:
print("Your guess is too high.")
else:
print("Your guess is too low.")
#driver function
def main():
humanPlayer1=HumanPlayer("John",0)
humanPlayer2=HumanPlayer("Martin",0)
computerPlayer1=ComputerPlayer("Computer1",0)
computerPlayer2=ComputerPlayer("Computer2",0)
#guessingGame(humanPlayer1,humanPlayer2) #invoking with two human players
guessingGame(humanPlayer1,computerPlayer1) #invoking with one human player and one computer player
#guessingGame(computerPlayer1,computerPlayer2) #invoking with two computer players
return
#calling driver function
main()
-------------------------------------------------------------