Thursday, November 25, 2010

TicTacToe game using Python

import sys

class TicTacToe:
    def __init__(self,usr_input=None,x=None,y=None):
        self.matrix=([0,0,0],[0,0,0],[0,0,0])
        self.usr_input=usr_input
        self.x=x
        self.y=y
# Function to display the tictactoe grid
    def show_grid(self):
        """ Display the grid layout in the standard output
        There is no arguments and no return types"""
        for i in (0,1,2):
            for j in (0,1,2):
                if self.matrix[i][j]==0:
                    sys.stdout.write("   ")
                elif self.matrix[i][j]==4:
                    sys.stdout.write(" X ")
                else:
                    sys.stdout.write(" 0 ")
                if j!=2:
                    sys.stdout.write("|")
            if i!=2:
                print " "
                print "---|---|---"
        print " "
# Function to get the input from the user
    def get_input(self):
        while True:
            try:
                self.usr_input=str(raw_input("Enter the x and y axis:"))
                if (len(self.usr_input)>3):
                    print ("invalid input!")
                    continue
                else:
                    self.x,self.y=self.usr_input.split(",")
                    self.x=int(self.x)
                    self.y=int(self.y)
                    break
            except:
                print "invalid input!"
                continue
    def user_primary_input(self,mark):
        while True:
            self.get_input()
            try:
                if ((self.matrix[self.x-1][self.y-1]==4) or (self.matrix[self.x-1][self.y-1]==3)):
                    print "invalid move!"
                    return 0
                else:
                    self.matrix[self.x-1][self.y-1]=mark
                    return 1
            except:
                print "out of range"
                continue
# Fuction to check the winning status
    def check_win(self):
        for j in (0,1,2):
            if (self.matrix[j][0] + self.matrix[j][1] + self.matrix[j][2]==12):
                return 0
            elif (self.matrix[j][0] + self.matrix[j][1] + self.matrix[j][2]==9):
                return 1
            if (self.matrix[0][j] + self.matrix[1][j] + self.matrix[2][j]==12):
                return 0
            elif (self.matrix[0][j] + self.matrix[1][j] + self.matrix[2][j]==9):
                return 1
        if (self.matrix[0][0] + self.matrix[1][1] + self.matrix[2][2]==12):
            return 0
        elif (self.matrix[0][0] + self.matrix[1][1] + self.matrix[2][2]==9):
            return 1
        if (self.matrix[2][0] + self.matrix[1][1] + self.matrix[0][2]==12):
            return 0
        elif (self.matrix[2][0] + self.matrix[1][1] + self.matrix[0][2]==9):
            return 1
# Function to check the draw status
    def check_draw(self):
        count=0
        for i in (0,1,2):
            for j in (0,1,2):
                if self.matrix[i][j]==0:
                    count=count+1
        if count==0:
            return 0
        return 1
# Initial movement of the player
    def start_game(self,mark,player,player_win):
        win=0
        while True:
            print "player " +str( player) + ":"
            valid=self.user_primary_input(mark)
            if valid==0:
                continue
            win=self.check_win()
            if win==player_win:
                self.show_grid()
                print "player " +str(player) + " won the game"
                return 1
            x=self.check_draw()
            if x==0:
                self.show_grid()
                return 2
            self.show_grid()
            break
        return 0
def main():
    t=TicTacToe()
    status=0
    first_player_mark=4
    second_player_mark=3
    player_one=1
    player_two=2
    print "your game starts:"
    t.show_grid()
    while True:
        status=t.start_game(first_player_mark,player_one,0)
        if status==1 or status==2:
            break
        status=t.start_game(second_player_mark,player_two,1)
        if status==1 or status==2:
            break
    if status!=1:
        print "Game draw!"
    return

   
if __name__=='__main__':
    main()


No comments:

Post a Comment