#### ## Noughts and Crosses game by Jude Hungerford. ## Started 19/6/2001. Last modified 25/7/2001. ## Modlist: ## 19/6/2001 - 20/6/2001 : writing the basic program... done. ## 0.01 - 0.02: start trying to implement AI. ## Sort of forgot about updating this for awhile. The AI is now there, but ## it's not very good. #### import ai import sys ## def Print_Board(board): sys.stdout.write("\n_______\n a b c") cx=0 while cx<=2: cy=0 sys.stdout.write("\n%d" % cx) while cy<=2: sys.stdout.write(" %s" % board[cx][cy]) cy=cy+1 cx=cx+1 sys.stdout.write("\n_______\n") return ## def Player_Turn(tog): if p[tog] == 'CPU': if tog==-1: symbol['me']='o' symbol['e']='x' else: symbol['me']='x' symbol['e']='o' move=ai.CPU_Turn(board,symbol['me'],symbol['e']) else: move=raw_input("Enter coordinates in the form letter:number : ") m1=move[:1] m2=move[-1:] if m1 == "a": m1=0 elif m1=="b": m1=1 elif m1=="c": m1=2 else: print "Invalid move - incorrect form. " return 0 if m2=="0": m2=0 elif m2=="1": m2=1 elif m2=="2": m2=2 else: print "Invalid move - incorrect form. " return 0 if board[m2][m1]!=".": print "Invalid move - space already occupied." return 0 else: if tog==1: board[m2][m1]="x" else: board[m2][m1]="o" return 1 ## def Check_Victory(board): if board[0][0]==board[0][1]==board[0][2]=="o": Win_O(tog) elif board[0][0]==board[0][1]==board[0][2]=="x": Win_X(tog) elif board[1][0]==board[1][1]==board[1][2]=="o": Win_O(tog) elif board[1][0]==board[1][1]==board[1][2]=="x": Win_X(tog) elif board[2][0]==board[2][1]==board[2][2]=="o": Win_O(tog) elif board[2][0]==board[2][1]==board[2][2]=="x": Win_X(tog) elif board[0][0]==board[1][0]==board[2][0]=="o": Win_O(tog) elif board[0][0]==board[1][0]==board[2][0]=="x": Win_X(tog) elif board[0][1]==board[1][1]==board[2][1]=="o": Win_O(tog) elif board[0][1]==board[1][1]==board[2][1]=="x": Win_X(tog) elif board[0][2]==board[1][2]==board[2][2]=="o": Win_O(tog) elif board[0][2]==board[1][2]==board[2][2]=="x": Win_X(tog) elif board[0][0]==board[1][1]==board[2][2]=="o": Win_O(tog) elif board[0][0]==board[1][1]==board[2][2]=="x": Win_X(tog) elif board[2][0]==board[1][1]==board[0][2]=="o": Win_O(tog) elif board[2][0]==board[1][1]==board[0][2]=="x": Win_X(tog) return ## def Init_Board(board): cx=0 cy=0 while cx<3: while cy<3: board[cx][cy]="." cy=cy+1 cx=cx+1 return ## def Win_O(tog): global endgame Print_Board(board) sys.stdout.write("\n * * * %s (o) won. * * *\n" % p[tog]) endgame=2 return ## def Win_X(tog): global endgame Print_Board(board) sys.stdout.write("\n * * * %s (x) won. * * *\n" % p[tog]) endgame=2 return ## board=[['.','.','.'],['.','.','.'],['.','.','.']] symbol={} maxturns=9 turns=0 endgame=0 p={} tog=-1 print "Noughts and Crosses Game by Jude Hungerford. This version is for" print "submission to the Useless Python Scripts page. I guess it counts as a" print '"game of any kind", and I suppose you could call its move selector an A.I.' print "If you want a player to be controlled by the move selector, name it CPU. " p[1]=raw_input("Enter name of the first player (x): ") p[-1]=raw_input("Enter name of the second player (o): ") Init_Board(board); while endgame==0: tog=tog*-1 sys.stdout.write("%s, it's your turn." % p[tog]) m=0 while(m==0): Print_Board(board) m=Player_Turn(tog) if p[tog]=='CPU': if m==0: print "A.I Error - switching to human." p[tog]='Human' Check_Victory(board) turns=turns+1 if turns>=9: endgame=1 Print_Board(board) if turns>=9: print "No player forced victory."