I'm pretty new in programming with python and I've written a tic tac toe game but there are 2 problems I haven't been able to fix. It's a game between human and the comuter, and I wan't the computer to try to prevent the human from winning. Right now the comp is pretty slow. And then If noone wins I wan't it to print out "it's a tie" or something.
Anyone able to help me??
thanks :)
# Tic Tac Toe
import random
Board = ["1","2","3","4","5","6","7","8","9"]
EMPTY = "_"
X = "X"
O = "O"
TIE = "TIE"
NUM_SQUARES = 9
player = X
computer = O
def playboard():
for rx in xrange(0,3):
r = rx * 3
print '| %s | %s | %s |' %(Board[r+0],Board[r+1],Board[r+2])
print
def Win():
a,b,c,d,e,f,g,h,i = Board
winlist = [(a,b,c), (d,e,f), (g,h,i), (a,d,g,), (b,e,h), (c,f,i),
(a,e,i), (g,e,c)]
winning = False
for w in winlist:
if w[0]==w[1]==w[2]:
winning = w[0]
return winning
playerwin = False
Run = True
while Run:
playboard()
choice = True
while choice:
play = raw_input("Make a move, 1-9: ")
if play.lower() in ["quit","exit","close","end"]:
choice = Run = False
try:
play = int(play)
if int(Board[play-1]) == play:
Board[play-1] = "X"
choice = False
except:
pass
if Win():
playerwin = True
playboard()
print "You Win!"
Run = False
if playerwin == False:
Moves = []
for n in xrange(0,9):
if Board[n] == str(n+1):
Moves.append(str(n))
#if(len(Moves)>0):
X = random.randint(0,len(Moves)-1)
X = Moves[X]
Board[int(X)] = "O"
if Win():
#if all([i in ['X','O'] for i in Board]):
# print "REMI!"
# playerwin = True
# Run = False
playboard()
print "Computer Win!"
#Run = False
raw_input("Remi")
#else:
# print "Remi"
# Run= False
Edited by ZekeDragon, 14 April 2010 - 02:18 AM.
Do not post duplicate threads.


Sign In
Create Account

Back to top









