Sunday 27 July 2014

Tic Tac Toe - Game Program with Python

Hello Friends.
Today I have another program to share.

This is a game - Tic Tac Toe

You are going to love it if you have memories of playing it on paper with your friends. Here, in this program, you cannot play it with your friends. Instead, You play with the computer itself!

I've created a function in the program which uses mere selections to block the user if he/she is going to win. The computer is not very easy to defeat and at the same time it is not impossible to defeat.

While starting, the program asks the user for choosing a symbol, decides who goes first - the user or the computer through a simple toss and then prints the pattern of the board for the user to make it easy for him to play. The pattern is like this:

 0  | 1  | 2
___|___|___
 3  | 4  | 5
___|___|___
 6  | 7  | 8
     |     |

The user has to enter the number of the cell where he wants to put his symbol.
For instance if the user has chosen the symbol "O" and wants his symbol to be placed in the middle(4th) cell then he must give 4 as the input when asked for his choice and the board will then look like this:

     |     | 
___|___|___
     | O | 
___|___|___
     |     | 
     |     |

Then the computer will tackle the user.

Enjoy.

The source code (the program) can be downloaded here: Datafilehost

Thanks for visiting. Keep Coming... and don't forget to join this site and explore more.

--------------------------------------- * UPDATE * ---------------------------------------
Fixed a bug - The program was raising error when the user entered wrong number(>8 or <0) or an alphabet. It gave an error and then closed itself. Now, It checks for valid input and functions only if the input is valid and asks for input again otherwise.

I also improved the A.I. which was earlier only preventing the user from winning. Now, It tries to win itself too.

Download update: Datafilehost


See the code:








The Game in action:




I have commented the program line by line and it is easy to understand for everyone. But if you have any problem, Comment or E-Mail me here.

And, don't forget to join this site.

By Gurpreet Singh

No comments:

Post a Comment