Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Artificial Intelligence Problem: Please answer ALL PARTS OF THE QUESTION with FU

ID: 3597683 • Letter: A

Question

Artificial Intelligence Problem: Please answer ALL PARTS OF THE QUESTION with FULL EXPLANATIONS. Thanks!

Problem 4 (30 points) Mastermind In this problem, you will write a program to play as the "code-guesser" in the game of Masetermind. For simplicity, this game of Mastermind has four colors (not six, as usual) and three positions (not four, as usual). The colors are Red, Blue, Orange, and White. Thi:s means that there are only 64 possible codes. Your program should allow you (as user) to think of a "secret" code. The program will then make successive guesses at the code, and you answer with X and O responses as in the standard MasterMind game. The program should simply keep track of the current set of "open" guesses consistent with the answers so far; you can then decide how the next guess can be selected. When the game starts, the program has 64 "open" possibilities; each answered guess should narrow the possibilities. Here's an example. Suppose you think of the code: R R W. The program might begin by asking:

Explanation / Answer

import random

def main():

    print '>> New game started. >> Good luck! '

    answer = generateAnswer()

    while True:

        userGuess = getUserGuess()

        if userGuess == answer:

            print '>> Congratulations, you won!'

            return

        print '>> The answer you provided is incorrect. >> Perhaps this hint will help you: '

        giveFeedback(answer, userGuess)

def generateAnswer():

    digits = [str(x) for x in range(10)]

    answer = ''

    for i in range(4):

        digit = random.sample(digits, 1)[0]

        digits.remove(digit)

        answer += digit

    return answer

def getUserGuess():

    while True:

        guess = raw_input('>> Please enter a 4-digit number: ').strip()

        if len(guess) != 4:

            continue

        guessIsValid = True

        for x in guess:

            if guess.count(x) != 1 or ord(x) not in range(48, 58):

                guessIsValid = False

                break

        if guessIsValid:

            return guess

def giveFeedback(answer, guess):

    for i in range(4):

        if guess[i] == answer[i]:

            print 'X',

            continue

        if guess[i] in answer:

            print 'O',

            continue

        print '-',

    print ' '

if __name__ == '__main__':

    try:

        main()

    except Exception, e:

        print '>> Fatal error: %s' % e

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote