You are on page 1of 4

Tic-Tac-Toe: Creating Functions

1. Create a variable named board and set it equal to an empty list


2. Create a 3 x 3 gridline initialized to all __ and store it on board
Use range() to loop 3 times
Inside the loop, board.append([__] * 3)
3. Define a function called printBoard() with no arguments
4. Inside the function, write a for loop that iterates through each
row on board and prints it to the screen
To iterate) for row in board:
5. Call your function to make sure it prints
6. Inside the your function, inside the for loop, use as separator
to join the elements in each row
To join) print .join(row)
7. Define a function called player1play() that takes no argument
8. Ask user for row number in raw input and set it equal to variable
named row
9. Ask user for column number in raw input and set it equal to
variable named column
10.
Assign each row and column variable to an int version of
themselves.
To convert to type integer) row = int (row)
11.
Now to account for situation when user may enter a value
outside the 3 x 3 grid, create an if statement that checks to see if
row or column number entered is greater than 2 or less than 0
If row or column is greater than 2 or less than 0, print Not

valid!
On the next line, call the function player1play() to run the

function inside itself


Create an else statement on same indentation level as the

previous if statement
Inside the else statement, create an if statement to see if
board[row][column] does not equal to __ (if that spot
does not equal to __, that means the spot contains either

o or x)
Under the if statement, print That spot is already taken!
Try again!

On the next line, run the function player1play() inside itself


Create an else statement with the same indentation as the
previous if statement that sets board[row][column] equal
to o
Define a function called player2play() that takes no

12.

argument
13.
Ask user for row number in raw input and set it equal to
variable named row
14.
Ask user for column number in raw input and set it equal to
variable named column
15.
Assign each row and column variable to an int version of
themselves.
To convert to type integer) row = int (row)
16.
Now to account for situation when user may enter a value
outside the 3 x 3 grid, create an if statement that checks to see if
row or column number entered is greater than 2 or less than 0
If row or column is greater than 2 or less than 0, print Not

valid!
On the next line, call the function player2play()to run the

function inside itself


Create an else statement on same indentation level as the

previous if statement
Inside the else statement, create an if statement to see if
board[row][column] does not equal to __ (if that spot
does not equal to __, that means the spot contains either

o or x)
Under the if statement, print That spot is already taken!

Try again!
On the next line, run the function player2play() inside itself
Create an else statement with the same indentation as the
previous if statement that sets board[row][column] equal

17.
18.
19.

to o
Define a function called tie() that takes in no argument
Create a variable called count and set it equal to zero
Create a for loop that iterates through each row in the

variable board

20.

Create another for loop under the for loop that iterates

through each spot in the row


21.
Under the 2nd for loop, if the spot does not equal __, raise
the count by 1 (Use += operator)(this if statement checks to see
if each spot in the board is taken)
22.
After both for loops have been iterated (move back to
original indentation!), create an if statement to see if count
equals to 9
23.
If count equals 9, under the if statement, return True (This
means all 9 spots in the tic-tac-toe board has been filled up)
24.
Create an else statement with same indentation as the
previous if statement above and return False (This means all 9
spots are not filled up)
25.
Finally, copy and paste the functions inside the python file
given under the instructions into your file. It should contain
player1win() and player2win() functions.

Tic-Tac-Toe: Putting it all together


1. Create a while statement containing the following
while not(player1win()) or not(player2win()):
2. On the next line, call the player1play() function (player 1 will
make a move)
3. Create an if statement that calls aTie() function (checks to see
4.
5.
6.
7.

the game is tied)


Under the if statement, call printBoard() function
On the next line, print Its a tie
On the next line, type: break (this stops the game)
Indent back a level and write another if statement that calls

player1win() function (checks to see if player 1 has won)


8. Under the if statement, call printBoard() function
9. On the next line, print Player 1 wins!
10.
On the next line, type: break (this ends the game)
11.
Indent back a level (since we are done with this if
statement) and call the function printBoard()

12.

On the next line, call the player2play() function (player 2

will make a move)


13.
Create an if statement that calls aTie() function
Under the if statement, call printBoard() function
On the next line, print Its a tie
On the next line, type: break (this ends the game)
14.
Indent back a level and write another if statement that
calls player2win() function
Under the if statement, call printBoard() function
On the next line, print Player 2 wins!
On the next line, type: break
15.
Indent back a level (since we are done with this if
statement) and call the function printBoard() (this should still be
inside the while loop)
You are done! Kudos!

You might also like