Getting Started with Python

BEFORE YOU BEGIN: Please take a look at the Useful Links to refresh yourself on the basics of Python and review some of the concepts and strategies behind winning a game of tic tac toe

PLEASE NOTE: It is recommended that you use a simple text editor/code editor with Python markup enabled. Since your code cannot run unless it is uploaded to our app, using a full blown IDE or attempting to run your code on locally will not work. Check out the Useful Links page to see a list of recommend editors

Review the framework for using the functions in our framework. The most important functions are getGameState(), getTeam(), calcMove(), and submitMove(). Here’s an example of how you should use these functions to get the information your computer player needs to see the game board, make it’s decision on where to put its next move and submitting that move:

First, you’ll need to define the function called calcMove(). Basically, whenever it is your “player’s” turn, our framework will call this function to allow your player to calculate and determine its next move. All of your steps for determining the next move should be contained inside this function. Your code should now look something like this:

def calcMove():

Next, you’ll need some way for your computer player to see the game board, so they know what to do next! That’s where getGameState() comes in! This function is part of our framework, and when its called, will return a 2-D list of the characters on the game board. What is 2-D list, you ask? In Python, a list can be seen as well, a list of things. In this case, it’s a list of characters on the board. On our board, every tile is either an ‘X’ character, an ‘O’ character or a space character ‘ ‘. The syntax for a 1-D array is listName[index] where “listName” is the name of the list variable, and “index” is the position in the list. IMPORTANT NOTE: the first element of a list is ALWAYS at index [0], so the first element of the example listName array is listName[0].

But what is a 2-D list? Basically, a list of lists. But to put it more simply, it can be seen as a table. A 2-D list has 2 indices and they represent the row and column of our tic tac toe board. The board is laid out in the following way:

[0][0]

[0][1]

[0][2]

[1][0]

[1][1]

[1][2]

[2][0]

[2][1]

[2][2]

So, if you wanted to get the middle tile, you would use the index [1][1]

But first, you'll need to create your own list to hold the list returned by getGameState(). In Python, it would look something like this:

myList = getGameState()

This will get the game board list and store it in a variable called myList (you can name this variable whatever you’d like it doesn’t have to be myList, choose a name that makes sense to you).

Now you can get the value of any space on the board using the above chart!

The bottom right space would be myList[2][2], the top right space would be myList[0][2] etc.

So now, in your calcMove() function, you can create a variable to store the game board:

def calcMove():

mylist = getGameState()

Great! So now we have a way of looking at the board. You can also get your team (either ‘X’ or ‘O’) with the getTeam() function. It is important that you use this if you want your computer player to know what team it is on, as it can change from game to game. You can use it like this:

myTeam = getTeam()

As before, you can use whatever variable name you want instead of myTeam

Now your code should look something like this:

def calcMove():

mylist = getGameState()

myTeam = getTeam()

Here comes the fun part! Now you need to figure out how to make the best move based on the current state of the board. This is where your computer player needs to actually “think”. YOU need to create the algorithm to figuring out where you should place your ‘X’ or ‘O’. Remember, this is a difficult challenge. In a bit we’ll give you some tips on some tools you can use to help you with this.

Once your computer player has decided its move you must use the submitMove() function to send your move to the game. You would want to create two variables to hold onto these moves at the top of calcMove(), one for the row and one for the column that your move should be placed at. Your player should change the values of the row and column variables to whatever they need (they should be integers), and when your computer player figures out the perfect move, you can submit your move using:

submitMove(row, column)

Where row and column are integer values that correspond to tiles on the tic tac toe board (see the board chart above)

So, in the end your code should look like this:

def calcMove():

myRow = 0

myColumn = 0

mylist = getGameState()

myTeam = getTeam()

####### HERE"S WHERE YOUR ALGORITHM SHOULD GO #######

submitMove(myRow, myColumn)

Of course you need to replace the comment with your own algorithm, otherwise your player would always submit 0,0 and you can’t win that way.

Now since the nature of this competition is to create your own algorithm or set of steps to finding the next best move, we can only tell you so much, but here are some tips to creating your own algorithm:

Iterating through a 2D array:

You may or may not be familiar with python for loops, if not we suggest you look here:

Once you understand how for loops work, you can use them to go through each element in an array one by one.

Here’s and example with a 1-D list:

exampleList = [‘A’, ‘B’, ‘C’, ‘D’]

To go through all of the elements in the list and print them you could use this:

for index in range(exampleList.length)

&nbsp&nbspprint(exampleList[index])

Remember “index” here is a variable so you can name it what you want

Now we can apply this same process for a 2-D array, looping through each row while looping through each item in that row.

Here’s an example with the list we get from getGameState():

myList = getGameState()

We’ll call our list variable myList

Let’s say we wanted to go through all of the squares in the board and print them

Since we can look at a 2D list as like a table, we can go down through each row, and for each row go through each column in that row. Remember a 2D list is lists inside a list, so we need a loop inside a loop!

To go through the elements of this list and print them to the console you would do this:

for i in range(myList.length):

for j in range(myList[i].length)

print(myList[i][j]

Remember “i” and “j” are variables, so you can name them whatever you like, just don’t confuse them with other variables

Notice the myList[i].length in the second loop. Since each row is a list itself, we can use this to get the length of the current row

Using If Statements:

Let’s say you want our player to place their move on the center tile [1][1]

First we should check if that tile is empty or not (if it’s not empty, you shouldn’t place your move there, or our application will just skip your turn)

We could use an if statement to determine whether we should set our move there:

if myList[1][1] == ‘ ‘:

myRow = 1

myColumn = 1

Assuming that we are using the same variable names from when we set up our computer player, we test to see if the center tile is empty (represented by a space char ‘ ‘), if it is, set our move there. Notice that we use == instead of =. A single equal sign = means assignment in python, assigning a value to a variable. Double equal == means to check if the two values are equal and return true or false.

Chaining if Statements:

Maybe we want it to do something else if it the middle spot isn’t empty, in this case we could use an elif (else if):

if myList[1][1] == ‘ ‘:

myRow = 1

myColumn = 1

elif myList[1][2] == ‘ ‘:

myRow = 1

myColumn = 2

Here, we check if the middle space is empty, if it is , place our move there, otherwise, check if [1][2] is empty (the rightmost tile in the center row), if it is, place it there, otherwise, do nothing.

You can chain multiple else if’s to a single if, and it will go through each one until it finds the one that evaluates true.

Nested If:

Maybe you want to test one condition, and if it’s true, test another condition:

Let’s say if the middle tile is marked with our team character (depending on getTeam())

And the tile one to the right is empty and we want to place our tile there:

If myList[1][1] == myTeam:

If myList[1][1+1] == ‘ ‘:

myRow = 1

myColumn = 1 + 1

This can also be done more elegantly like this using the and operator: