Program.in

By Santosh Wadghule

TicTacToe in Python

with 2 comments

Finally, today I have done TicTacToe game in Python. From last few years I really wanted to do TicTiacToe game. Now here is code of game.

#----------------------------------------------
# TicTacToe game in Python
# author: [Sanosh Wadghule, santosh.wadghule@gmail.com]
# copyright: (c) 2010-2011 Santosh Wadghule
#----------------------------------------------

import sys
import random
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']

print "--------------------------"
print "Powerful TicTacToe Game"
print "--------------------------"
print 

def displayPos():
    print "Postion of board: "
    print "0 | 1 | 2"
    print "3 | 4 | 5"
    print "6 | 7 | 8"

player = random.randrange(1, 3)     #choose random player

def displayBoard(board):
    print "========="
    print board[0] + " | " + board[1] + " | " + board[2]
    print board[3] + " | " + board[4] + " | " + board[5]
    print board[6] + " | " + board[7] + " | " + board[8]
    print "========="

def checkwin(a,b,c,player,board):
    if board[a] == 'X' and board[b] == 'X' and board[c] == 'X':
        print       #newline
        print "Player - " + str(player) + " is *** WINNER ***!!!"
        displayBoard(board)
        sys.exit()

    elif board[a] == 'O' and board[b] == 'O' and board[c] == 'O':
        print   #newline
        print "Machine is *** WINNER ***!!!"
        displayBoard(board)
        sys.exit()

def winner(board,player):
    checkwin(0,1,2,player,board)
    checkwin(3,4,5,player,board)
    checkwin(6,7,8,player,board)
    checkwin(0,3,6,player,board)
    checkwin(1,4,7,player,board)
    checkwin(2,5,8,player,board)
    checkwin(0,4,8,player,board)
    checkwin(2,4,6,player,board)

def play(board,x,player):
    if board[x] != ' ':
        print "--You have chosen wrong position--"
        sys.exit()
    else:
        if player == 1:
            board[x] = 'X'
            winner(board,player)
        else:
            board[x] = 'O'
            winner(board,player)

def checkPriority(a,b,c,board):
    if (board[a] == 'X' and board[b] == 'X') or (board[a] == 'O' and board[b] == 'O'):
	if board[c] == ' ':
	    return c
    elif (board[a] == 'X' and board[c] == 'X') or (board[a] == 'O' and board[c] == 'O'):
    	if board[b] == ' ':
            return b
    elif (board[b] == 'X' and board[c] == 'X') or (board[b] == 'O' and board[c] == 'O'):
    	if board[a] == ' ':
           return a

def Priority(board):
	y = checkPriority(0,1,2,board)
	if y == None:
	    y = checkPriority(3,4,5,board)
	if y == None:
	    y = checkPriority(6,7,8,board)
	if y == None:
	    y = checkPriority(0,3,6,board)
	if y == None:
	    y = checkPriority(1,4,7,board)
	if y == None:
	    y = checkPriority(2,5,8,board)
	if y == None:
	    y = checkPriority(0,4,8,board)
	if y == None:
	    y = checkPriority(2,4,6,board)

	if y != None:
	    return y
	else:
	    while True:
		x = random.randrange(0, 9)
		if board[x] != 'X' and board[x] != 'O':
		    break
		else:
		    continue
            return x

for i in range(0,9):
    if player == 1:
        print "Player " + str(player) + " >> Its your turn"
        displayPos()
        x = input("--Enter the position number: ")
        play(board,x,player)
        displayBoard(board)
        print       #newline
        player = 2
    else:
        print "Machine >> Its your turn"
        displayPos()
        if i == 0:
	    x = random.randrange(0, 9)
	    if board[x] != 'X' and board[x] != 'O':
	        play(board,x,player)

        else:
	    x = Priority(board)
	    play(board,x,player)

        displayBoard(board)
        print       #newline
        player = 1

print "--Match Draw--"
Advertisement

Written by Santosh Wadghule

March 9, 2010 at 5:39 pm

Posted in Python

2 Responses

Subscribe to comments with RSS.

  1. how do i run it

    hisham Alruwaih

    May 17, 2011 at 7:25 am

  2. Hey, this is fantastic, but how can you do this in python GUI? i really need to know ASAP if you can really tell me how can i transform this into Graphical User Interface code to actually play the game by mouse clicks.

    Thank you

    P.S.: I use Graphics.py library

    Namo Kaftan

    May 22, 2011 at 8:29 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.