Tuesday, February 23, 2010

Bagels

The name of the game I have created for this post, is the game Bagels. Bagels is a game where the user guesses a three digit number, and the computer responds with "Fermi" for every digit that is in the right place that you guessed, and "Pico" for every digit that you guessed which is in the wrong place, but is still a digit in the secret number.

The concepts that this game goes over are hardcoding and nested loops, the chapter is this one: http://inventwithpython.com/chapter10.html.

A nested loop is simply a loop within a loop. This is useful for when you want to iterate something x times, for every time y appears, or something similar.

Hardcoding is a coding practice where the coder codes the program so that changing of simple variables requires changing lots of code. This program was programmed without hardcoding, where a couple constants (variables that do not change while the program is running, which are represented with caps, although, are actually just normal variables in Python) can be changed to change how the game is played.

import random

def getSecretNumbers(numDigits):

    numbers = list(range(10))
    random.shuffle(numbers)

    secretNum = ''
    for i in range(numDigits):
        secretNum += str(numbers[i])

    return secretNum

def getClues(guess, secretNum):

    if guess == secretNum:
        return 'You have guessed the correct number!'

    clue = []

    for i in range(len(guess)):
        if guess[i] == secretNum[i]:
            clue.append('Fermi')
        elif guess[i] in secretNum:
            clue.append('Pico')

    if len(clue) == 0:
        return 'Bagels'

    clue.sort()

    return ' '.join(clue)

def isOnlyDigits(num):

    if num == '':
        return False

    for i in num:
        if i not in '1 2 3 4 5 6 7 8 9'.split():
            return False

    return True

def playAgain():

    print('Do you want to play again? Yes or no.')
    return raw_input().lower().startswith('y')

NUMDIGITS = 3
MAXGUESS = 10

print('I am thinking of a %s-digit number. Try to guess what it is.' % (NUMDIGITS))
print('Here are some clues:')
print('When I say:    That means:')
print('  Pico         One digit is correct but in the wrong position.')
print('  Fermi        One digit is correct and in the right position.')
print('  Bagels       No digit is correct.')

while True:
    secretNum = getSecretNumbers(NUMDIGITS)
    print('I have thought up a number. You have %s guesses to get it.' % (MAXGUESS))

    numGuesses = 1
    while numGuesses <= MAXGUESS:
        guess = ''
        while len(guess) != NUMDIGITS or not isOnlyDigits(guess):
            print('Guess #%s: ' % (numGuesses))
            guess = raw_input()

        clue = getClues(guess, secretNum)
        print(clue)
        numGuesses += 1

        if guess == secretNum:
            break
        if numGuesses > MAXGUESS:
            print('You ran out of guesses. The answer was %s.' % (secretNum))

    if not playAgain():
        break


My code should be pretty much identicle to the code in the example program, as there aren't really any easier ways to code this program; this is a really rather cut and draw program to create.

This part of the program confused me for a moment:

def getSecretNumbers(numDigits):

    numbers = list(range(10))
    random.shuffle(numbers)


These lines create a list of numbers and then shuffle them. When I first saw these lines, the first one confused me: numbers = list(range(10)). I wasn't sure why the auther was changing the range() to list() inside of the range. Although, in the book it explained that if I wanted to store a list of integers, they needed to be converted from an iterator (value of range) into a list. This made sense, although the author didn't go into detail about exactly what the value of a range was. I assume it's just an amount of iterations to perform, which isn't what I would want if I wanted a list to store variables in.

I plan to run through chapter eleven of this book for my next post. This should be a relatively easy chapter, as it just goes through the basics of a Cartesian coordinate system.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.