I don’t think you want to do that Dave…



In this session we are going to begin thinking about AI by adding a computer opponent to our number guessing game. Along the way we are going to introduce two new important concepts – logical combinations when making decisions and extending the available commands by adding a function.

The new version of the game is going to introduce a competitive element to the game by having the computer guess the code number each time the player does. Instead of the simple ‘higher’ ‘lower’ feedback we provided we will instead indicate who is closer to the answer each round. This is adds two elements to our game – a competitive tension and a clue that allows the player to improve there skill at the game with practice. We’ll discuss what makes a ‘good’ game many times over the coming sessions I am sure!

Functions

So far we have coded using commands, or functions, that already form part of the Python language and libraries. A function is a self-contained mini program that completes a calculation or task in its own right. They are very important for structuring more complicated programs and allowing you to re-use your code effectively. Functions often receive the data on which they will operate in the form of an ‘argument list’, which is passed to the function inside a pair of brackets () that follow the function name. A good example of a function is print() where you would normally pass the string of text you want to print as an argument (inside quotes and between the brackets) and the print function just does its thing without you having to worry about the ‘mini-program’ inside.

Our function is going to compare two values and return the absolute difference between them for us to use to give a clue back to the player as to their next guess. At the top of our program we start by defining the function we are going to use as follows:

def difference(x, y):
if x >= y:
result = x - y
else:
result = y - x
return result

As you can see we start with the Def keyword, our function’s name and the arguments it is going to use. The rest of the code is just a mini-program that does its stuff and returns the answer.

Once defined, we can use our function just like any pre-existing one – we’ve effectively *extended* the language, how cool is that?

I’m never going to not do the opposite…

The other new concept to introduce logical combinations in our decisions. In our new game design, we need to check if either the player *or* the computer has one.’Or’ is the first of several ways of combining questions to make a decision the other really important one is ‘and’. If you check to see if one thing is true ‘or’ another thing is true – then the decision will be triggered if either one is true. If we only want to trigger the decision if both things are true we can use ‘and’ instead.

Here is the code in its entirety, see if you can locate all the logical decisions being made and follow the flow of the program:

import random


def difference(x, y):
if x >= y:
result = x - y
else:
result = y - x
return result


answer = random.randint(1, 100)

print('I have selected a number between 1 and 100.')


# Main game loop, only exits when there is a winner using 'break'
while True:

# Lets get the player and computer guesses
player_guess = int(input('Your guess? '))
computer_guess = random.randint(1,100)
print('Computer guess is: ' + str(computer_guess))


# Has either one won?
if computer_guess == answer or player_guess == answer:
break


# See who is closest?
player_guess_difference = difference(answer, player_guess)
computer_guess_difference = difference(answer, computer_guess)


# Print a clue
if player_guess_difference > computer_guess_difference:
print('The Computer is closer...')
elif player_guess_difference == computer_guess_difference:
print('You are both neck and neck')
else:
print('You are closer. Nearly there...')


# We get here once we break from the loop, but who has won?
if computer_guess == answer:
print('You LOSE. Sorry, it seems your computer is smarter than you.')
else:
print('You WIN. Phew, you are still the boss!')

 

Don’t forget: half-term next week, so no computer club during the holiday!

Comments


Comments powered by Disqus