Function: Algorithm – Tic Tac Toe

Classwork:
Open a new file, YI_PythonSyntax.py
Include the output and some comments from the following code:

# don't forget the header and comments

import random

# returning a list
x = 2
y = 3


def returnAList(a,b):
    a += 2
    b += 3
    return [a,b]

# Main
xyList = returnAList(x,y)

print(xyList)
print(xyList[0])
print(xyList[1])
# print either 0 or 1
for i in range(10):
    print(random.randint(0, 1),end = ' ')

# print the first letter
print()

print('yes'.lower().startswith('y'))

# adding items to any list
anyList = []
anyList.append(2)
anyList.append(3)

print(anyList)

sentence = 'it is a wonderful day'
sentence = sentence.split()
print(sentence)

sentence = ' '.join(sentence)
print(sentence)

# random choice
spaces = '1 2 3 4 5 6 7 8 9'.split()
print(spaces)
print(random.choice(spaces))
print(random.choice(spaces))
print(random.choice(spaces))

# what does this print?
print(None)


Screen Shot 2014-12-04 at 10.01.11 PM

Classwork:Tic-Tac-Toe.
Write an algorithm to play Tic-Tac-Toe. (Use pseudocode)
You are allowed to use the following functions from Al Sweigart’s program but your don’t have to:
isWinner(bo, le)
drawBoard(board)
isBoardFull(board)
getPlayerMove(board)
The rest must be implemented with your original and creative algorithm.

NOTE: this is a “what if” type of program. Think of all the moves ahead at each step. You can use as many “if’s” as you want!!
Keeping in mind the different locations in the board:
Screen Shot 2014-12-05 at 8.19.05 AM

Visit edmodo.com and answer these questions that could help you put together your algorithm. Remember it is your algorithm!

1. Before you commit to a move, what two things should your program check for?
2. What is the best spot on the board to start or to take the next safe move in the game?
3. What is the next best spot?
4. What is left when all the good spots are gone?
5. How many moves does it take to win, tie or lose a game of Tic Tac Toe? Best case, worse case and in an average case.

Make sure your algorithm addresses all these questions otherwise it would not be a good tool to help you write a smart AI program.

Programming Design:
1. What do you think is the best way to hold information about the space selected? Would you have 9 variables or a list?
2. Would you use while, for loop or both?

Homework:
Write first draft for TTT.py using pseudocode

NOTE: TO MAKE IT MORE INTERESTING AND INVITING TO THE PLAYER, MAKE THE FIRST COMPUTER MOVE RANDOM. However, it should be intelligent once the game is on.