Programming Games with AI

by Manikantan

So far, I have talked about flash games based on logic, their design, code modeling and given tips in Flash Lite. As some might know, I have been reading Python and J2ME for quite some time now. And in this post, I shall discuss how to program a simple Tic-Tac-Toe game in Python. I shall not discuss the depth involved in the UI design, but shall provide simple inputs.

Let us take the case of a 1 player game, where user competes with the CPU. The gameflow is quite trivial, and hence I won’t peak into it. Let us assume we are programming for a touch phone like 5800. This is done to reduce the simplicity in UI work. Hence, where ever the user touches, it simply means that he intends to place his coin in that square. Provided no coin is placed at that square earlier, the user can place it there, and internally we need to update our data structures.

Have a simple list of 9 elements. This will model our board. Each item in the list can take any of the three states – not  filled, filled by User’s coin and filled by CPU’s coin. So, whenever user presses on an empty square or CPU makes a move, we need to update this list.

Let sq be the square where the user presses. After verifying its emptiness, update tables as follows.

Board[sq] = user_coin

We also have the duty to perform checks if either of the players have won the game. This also can be checked by looping construct.


This function is dedicated to check if on a board a player(referenced via coin) is a winner. This function is written separately to improve the code- reuse. In this function, perform a test that comprises of checking on the 3 rows, 3 columns and 2 diagonals. If the user wins, declare the result. If not the next move should be the CPU’s move.


This is where bulk of the AI coding comes into picture.

First, intention from the CPU must be to make the winning move. To code this part, use the same isWinner() function previously written and check through all empty squares if by CPU placing a coin there, can win the game.


If a winning move is not possible, the CPU should predict the presence of a winning move from the user’s end? Check if the user can win the game in the next move. If that’s the case, the CPU must place a coin in a spot to block the user’s winning move.

The next prospect from the CPU must be to place a coin in one of the corners, centers. Corners and centers are of more importance as they are involved in multiple sequences. If either of the corners or center is free, try to place the CPU coin there. If none of them are empty place it on a side square. This can be randomized, but once again a smarter move would involve more coding.


Other than this, after completion of every move we need to check if either player has won and check if the Board is full. Checking if the board is full is another for loop iteration.

This is a brute force approach to the computer move. Smarter moves can be made by coded by taking an approach that looks into future moves and then decides the best move.


Invent your own computer games with Python by Albert Sweigart. This is a simple book for learning Python with game development in mind. Target audience : Beginner.

Related posts:

  1. Creating Simple Car games As a sequel to my previous post on Gaming Tutorial,...
  2. PyJumble v1.0.0 I have been poking my nose into PyS60 since few...
  3. Announce: Py-Hangman Game Greetings! Very long time with hectic schedule I have written...
  4. Games on Symbian – Scope and dimensions I have been doing a random literature survey on...
  5. Jomtris 1.10 Jomtris development release v1.10: Jomtris is a bit more complicated...

Related posts brought to you by Yet Another Related Posts Plugin.