Tic-Tac-Toe How To

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
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, .append() a list containing 3 __ s to
board
3. Define a function called printBoard() with a single argument,
board
4. Inside the function, write a for loop that iterates through each
row on board and prints it to the screen
5. Call your function with board to make sure it works
6. Inside the your function, inside the for loop, use as separator
to to .join the elements in each row
Example: 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.
Example: 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

12.

valid!
On the next line, run the function player1play() in order to
reiterate through the function
Create an else statement to check and see if the specific

spot is available and not taken.


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() in order to

reiterate through the function


Create an else statement and under the statement, set
board[row][column] equal to o
Define a function called player2play() that takes no

13.

argument
14.
Ask user for row number in raw input and set it equal to
variable named row
15.
Ask user for column number in raw input and set it equal to
variable named column
16.
Assign each row and column variable to an int version of
themselves.
Example: row = int (row)
17.
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
(only numbers 0,1, and 2 are valid row and column numbers)
If row or column is greater than 2 or less than 0, print Not

valid!
On the next line, run the function player2play() in order to
reiterate through the function
Create an else statement to check and see if the specific

18.

spot is available and not taken.


Under the else statement, create an if statement to see if

19.
20.

board[row][column] does not equal to __


Under the if statement, print That spot is already taken!

Try again!
On the next line, run the function player2play() in order to

reiterate through the function


Create an else statement and under the statement, set
board[row][column] equal to x
Define a function called tie() that takes in no argument
Create a variable called count and set it equal to zero

21.

Create a for loop that iterates through each row in the

variable board
22.
Create another for loop under the for loop that iterates
through each spot in the row
23.
Under the 2nd for loop, if the spot does not equal __, raise
the count by 1 (Use += operator)
24.
After both for loops have been iterated (move back to
original indentation!), create an if statement to see if count
equals to 9
25.
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)
26.
Create an else statement and under the else statement,
return False

Tic-Tac-Toe: Putting it all together


1. Create a while statement containing the following
while not(player1win()) and not(player2win()):
2. On the next line, call the player1play() function
3. Create an if statement that calls aTie() function
4. Under the if statement, call printBoard() function
5. On the next line, print Its a tie
6. On the next line, type: break (this stops the game)
7. Indent back a level and write another if statement that calls
player1win() function
8. Under the if statement, call printBoard() function
9. On the next line, print Player 1 wins!
10.
On the next line, type: break
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
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 stops the game)
14.
Indent back a level and write another if statement that
calls player2win() function
Under the if statement, call printBoard() function

15.

On the next line, print Player 1 wins!


On the next line, type: break
Indent back a level (since we are done with this if

statement) and call the function printBoard()

You might also like