Function: Algorithm – Tic-Tac-Toe by Al Sweigart


Screen Shot 2014-12-04 at 10.01.11 PM

Classwork:Tic-Tac-Toe.
Write an algorithm to play Tic-Tac-Toe. (Use pseudocode)
Keeping in mind the different locations in the board:
Screen Shot 2014-12-05 at 8.19.05 AM

A pseudocode you could follow:

start and reset the board

choose who goes first

start the while loop to play the game 

     if it is the player's turn, 
        draw the board
    prompt the user for move
    check if it is a valid move
    if the player wins, draw the board, and print a message.
    if player doesn't win, if all the spaces are taken, it is a tie, 
                               if not all spaces are taken, let the computer make a move
    if it is the computer's turn, choose a space
    if the computer wins, draw the board and print a message
    if the computer doesn't win, if all spaces are taken, it is a tie,
                     if not all spaces are taken, let the player make a move
    
    

Advice:
Write small parts of the program and test them before you move on to the next one.

# Name: G. Elia
# Description: This program demonstrates definitions, variables scopes
#              and how to implement a good exit from a loop
# Version: 3.2.5
# Date: 12/8

import random

# returning a list
x = 2
y = 3


def returnAList(a,b):
    a += 2
    b += 3
    return [a,b] # This returns a list

#  Main - This is where the execution of the program starts

playAgain = "y"

while playAgain.lower().startswith('y'):
    print("Before call to returnAlist def: x =",x,"y =",y)
    xyList = returnAList(x,y)
    print("This list is coming from returnAList def: ",xyList)
    print("Before update: x =",x,"y =",y)
    x = xyList[0]
    y = xyList[1]
    print("After update: x =",x,"y =",y)
    playAgain = input("Would you like to run this program again? yes/no ")

print("Good bye")


##>>> 
##Before call to returnAlist def: x = 2 y = 3
##This list is coming from returnAList def:  [4, 6]
##Before update: x = 2 y = 3
##After update: x = 4 y = 6
##Would you like to run this program again? yes/no y
##Before call to returnAlist def: x = 4 y = 6
##This list is coming from returnAList def:  [6, 9]
##Before update: x = 4 y = 6
##After update: x = 6 y = 9
##Would you like to run this program again? yes/no n
##Good bye
##>>> 

Reminder:

Turn in your friend’s name for Thursday’s Hour of Code activity.
One activity you might want to show your friend:

Screen Shot 2014-12-07 at 12.04.44 PM