Learning Objectives: By the end of this assignment you should be able to: - Desi
ID: 3710773 • Letter: L
Question
Learning Objectives: By the end of this assignment you should be able to: - Design and code a variety of functions - Decide what parameters are needed in a given situation - Decide if a return statement is needed in a given situation Use Lists (and their corresponding built-in functionality) to solve a problem Take a written description and create the corresponding Python code to solve that problem Design a small Python program Mastermind This game is traditional a two-player code breaking game. One player (i.e., the codemaker) creates a secret code of four colored pegs. The secret code can be made up of any combination of six options: Red, Green, Yellow, Purple, Black, or Orange and can include multiples of any color (e.g., Orange, Orange, Orange, and Black). The codebreaker attempts to guess the pattern (both in color and order) within 10 "turns"/guesses. Instructions: Create a python program that simulates the codebreaker side of the game. The "codemaker" player side will be stored within the program. The program will interact with the user by asking the user to enter a guess. The program will provide feedback on the guess. The feedback is summarized in the able below: Situation Feedback Indicate it is correct by replicating the peg color and location Peg of correct color placed in correct location Peg of correct color but incorrect location Use a white pin Wrong color peg Left blank (i.e., _) The program should also calculate a "score". The codemaker gets one point for each guess a codebreaker makes. The score for the player, the codebreaker, should be shown as a negative number Color Pegs Notation Color Peg Notation in Program RedExplanation / Answer
ScreenShot
-------------------------------------------------------------------------------------------------------------
Program
#Random file reading package
import random
#exit package
import sys
#Random line read of a file function return line
def random_line(afile):
lines = open(afile).read().splitlines()
return random.choice(lines)
#guess function
def guess(codeVal):
#variables for calculation
counter=0
i=5
score=0
feedback='----'
#guess prompt repeatation until 5 guess
while i>=0:
#user guess prompt
userGuess=input("Guess: ")
#if user entered data length >4 generate error
if(len(userGuess)>4):
print("Error!!!")
sys.exit(0)
#if user entered data length not R,G,Y,P generate error
for c in range(0, len(userGuess)):
if(c!='R' and c!='G' and c!='Y' and c!='P'):
counter+=1
#error message
if(counter>0):
print("Error!!!")
counter=0
#otherwise generate score and feedBack
else:
i=i-1
score=score-1
#loop through user input and codebreaker
for c in range(0, len(userGuess)):
for ch in range(0,len(codeVal)):
#If both equal then next
if(c==ch):
#generate feed back string properly
if(userInput.find(c)==codeVal.find(ch)):
pos=userInput.find(c)
feedback[pos].replace('-',c)
print(feedback)
feedback="----"
#generate feed back if not the exact position
else:
pos=userInput.find(c)
feedback[pos].replace('-','W')
print('FeedBack:',feedback)
feedback="----"
else:
print('FeedBack:',feedback)
#print score in negative
print('Score:',score)
#Main function definition
def main():
code=random_line('C:/Users/deept/Desktop/input.txt')
guess(code)
#main function
if __name__== "__main__":
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.