NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#CS 172 - Homework 1
#Purpose: Plays the mastermind game and prints out the number of white and blacks
#Author: Rohan Joshi
#Date: 4/5/21

#importing the random module
import random

'''
Purpose of Function: generates a list of r4 random colors
@param: colorlist (the list that has all the colors you want to randomize)
@return: secret_colors (the randomlly geenrated list)
'''
def generateSecretCode(colorList):
#lsit that will contain all the random colors geenrated by the computer
secret_colors = []
#seeding the random midule

#generating 4 random colors
for i in range(4):

secret_colors.append(colorList[random.randint(0,5)])

#returning the secret colors list
return secret_colors

'''
Purpose of Function: finds the number of exact matches
@param: computerColorList (the list randommlly generated by the computer), playerColorList (the list that contains the colors the user inputed)
@return: matches (amount of exact matches between both lists)
'''
def computeExactMatches(computerColorList, playerColorList):
#iterator variable
i = 0
#variable that will store the number of matches
matches = 0
#iterating through the entire list
while (i < len(playerColorList)):
#checking to see if the elements in both list with the same pointer are equal
if (playerColorList[i] == computerColorList[i]):
#incrementing the amount of matches
matches+=1

i+=1
#retuning the amount of matches
return matches

'''
Purpose of Function: finds the amount of partial matches
@param: computerColorList (the list randommlly generated by the computer), playerColorList (the list that contains the colors the user inputed)
@return: matches (the amount of partial matches between both lists)
'''
def computePartialMatches(computerColorList, playerColorList):
#variable that stores the amount of matches
matches = 0
#iterating through the user inputed list
for i in range(len(playerColorList)):
#checking to see if the elements are not the same and if the element is in the list
if (playerColorList[i] in computerColorList and computerColorList[i] != playerColorList[i]):
#increasing the amount of matches
matches += 1

#returning the amount of matches
return matches

#main function
if __name__ == "__main__":

#printing out welcome message and telling what the objective is
print("Mastermind Game")
print("The computer has generated a secret color code.")
print("Can you guess the code?")
print("These are the valid colors you can use: blue red white green yellow purple")

#all the colors the user can use
possible_colors = ["blue", "red", "white", "green", "yellow", "purple"]

#boolean that will tell if all 4 are black
correct_colors = False
#amount of turns that will be be used for the final output
num_turns = 0
#calling the generateSecretCode in order to generate a random list
random.seed(350)
computer_list = generateSecretCode(possible_colors)

#loop that will keep going until all 4 are black
while (correct_colors == False):

#placeholder variables
user_colors = []
#used while printing
color_number = 1
#stores the number of black and white
num_black = 0
num_white = 0

#loop that will keep on going until you finish the fourth valid color
while (color_number <= 4):

#asking user to input a color
color_input = input("Please enter color " + str(color_number) + " :")

#checking if the color is valid or not
if (possible_colors.count(color_input.lower()) == 0):
print("That is not a valid color. Try again.")

#at this point the color is valid
else:
user_colors.append(color_input.lower())
color_number+=1

#calculating the number of blacks by calling computeExactMatches()
num_black = computeExactMatches(computer_list,user_colors)

#will only calculate number of whites if the number of blacks is not equal to 4
if (num_black != 4):
num_white = computePartialMatches(computer_list,user_colors)


#printing the amount of blacks and whites
print(str(num_black) + " black")
print(str(num_white) + " white")

#incrementing the number of turns
num_turns+=1

#exiting the loop if the number of blacks is 4
if (num_black == 4):
correct_colors = True

#end print statement
print("Congratulations, you guessed the secret code!")
print("It took you " + str(num_turns) + " turn(s).")





     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.