Author Topic: [Python]Tic Tac Toe  (Read 1812 times)

0 Members and 1 Guest are viewing this topic.

Offline DeXtreme

  • Peasant
  • *
  • Posts: 95
  • Cookies: 8
  • I was there and you never knew.
    • View Profile
    • My Designs
[Python]Tic Tac Toe
« on: September 08, 2012, 03:54:30 pm »
I'm relatively new to python and as a beginner's project i made a simple tic tac toe game that runs in python shell.The script is a bit lengthy and the AI is fairly good but it's too easy to beat.Any comments and suggestions are welcome. ;D
 
 
Code: [Select]
import string
 import random
 
 
 game=True #game variable
 board=[]  #board array
 
 
 print """       Python Shell Tic-Tac-Toe--DeXtreme
       Enter the column number-space-row number
            to play.Four in a row to win"""
 print "\n"
 
 #make a 2D list
 def makeboard():
     for i in range(0,4):
         board.append([])
         for x in range(0,4):
             board[i].append(" ")
     printboard()
         
 
 #computer AI
 def complay(a=0,b=0):
     choice=random.choice([0,1])
     if choice==0:
         play=True
         while play==True:
             n=random.choice([0,1,2,3])
             m=random.choice([0,1,2,3])
             if board[n][m]==" ":
                 board[n][m]="O"
                 print "Computer Has Played"
                 print "\n"
                 play=False
     if choice==1:
         play=True
         while play==True:
             m=random.choice([0,1,2,3])
             if board[a][m]==" ":
                 board[a][m]="O"
                 print "Computer Has Played"
                 print "\n"
                 play=False
             elif board[m][b]==" ":
                 board[m][b]="O"
                 print "Computer Has Played"
                 print "\n"
                 play=False
             else:
                 complay()
 
 #check for win           
 def checkwin():
     for i in range(0,4):
         if board[i][0]=="X" and board[i][1]=="X" and board[i][2]=="X" and board[i][3]=="X":
             print "You Won"
             return "win"
     for i in range(0,4):
         if board[0][i]=="X" and board[1][i]=="X" and board[2][i]=="X" and board[3][i]=="X":
             print "You Won"
             return "win"
     for i in range(0,4):
         if board[i][0]=="O" and board[i][1]=="O" and board[i][2]=="O" and board[i][3]=="O":
             print "Computer Won"
             return "lose"
     for i in range(0,4):
         if board[0][i]=="O" and board[1][i]=="O" and board[2][i]=="O" and board[3][i]=="O":
             print "Computer Won"
             return "lose"
 
     if board[0][0]=="X" and board[1][1]=="X" and board[2][2]=="X" and board[3][3]=="X":
             print "You Won"
             return "win"
 
     if board[3][0]=="X" and board[2][1]=="X" and board[1][2]=="X" and board[0][3]=="X":
             print "You Won"
             return "win"
     if board[0][0]=="O" and board[1][1]=="O" and board[2][2]=="O" and board[3][3]=="O":
             print "Computer Won"
             return "lose"
 
     if board[3][0]=="O" and board[2][1]=="O" and board[1][2]=="O" and board[0][3]=="O":
             print "Computer Won"
             return "lose"
 
 #print board
 def printboard():
     print "---------"
     print "|%s|%s|%s|%s|" %(board[0][0],board[1][0],board[2][0],board[3][0])
     print "---------"
     print "|%s|%s|%s|%s|" %(board[0][1],board[1][1],board[2][1],board[3][1])
     print "---------"
     print "|%s|%s|%s|%s|" %(board[0][2],board[1][2],board[2][2],board[3][2])
     print "---------"
     print "|%s|%s|%s|%s|" %(board[0][3],board[1][3],board[2][3],board[3][3])
     print "---------"
     print "\n"
 
 
 makeboard()   
 
 
 while game==True:
     player=raw_input("Play where: ")
     try:
         pcol=string.atoi(string.split(player)[0])
         prow=string.atoi(string.split(player)[1])
         if board[pcol-1][prow-1]==" ":
             board[pcol-1][prow-1]="X"
             printboard()
             if checkwin()=="win" or checkwin()=="lose":
                 break
             complay(pcol-1,prow-1)
             checkwin()
             printboard()
         else:
             print "Cannot play there"
     except:
         print "Cannot play that"