Python hjälp
Hejsan
Jag är ny när det gäller programmering och nu har jag skrivit en kod i python som representerar spelet tic tac toe. Men den gör inte riktigt som jag vill. Jag vill att datorn ska försöka förhindra den andra från att vinna, och det ska dessutom kunna bli oavgjort och att det då printas ut "It's a tie" eller nåt liknande, men jag har inte lyckats. Finns det någon som skulle kunna hjälpa mig med koden? Jag har kämpst länge med det.
Tack
Kod:
--------------------------------------------------------------------------------
import random
Board = ['1 ', '2', '3 ', '4', '5 ', '6', '7 ', '8', '9 ']
EMPTY = "_"
X = "X"
O = "O"
TIE = "TIE"
NUM_SQUARES = 9
X = player
computer = O
def Playboard ():
XRange for rx in (0.3):
rx = r * 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, and )
(A, e, i), (g, e, c)]
winning = False
for the in winlist:
f in [0] == in [1] == in [2]:
winning = w [0]
winning return
playerwin = False
Run = True
while Run:
Playboard ()
choice = True
while choice:
play = raw_input ("Make a move, 1-9")
play.lower f () in ["quit", "exit", "close", "end"]:
choice = Run = False
try:
play = int (play)
int f (Board [play-1]) == play:
Board [play-1] = 'X'
choice = False
Except:
pass
Win f ():
playerwin = True
Playboard ()
print "You Win!"
Run = False
f playerwin == False:
Moves = []
XRange for n in (0.9):
if board [n] == str (n +1):
Moves.append (str (n))
Random.randint X = (0, len (Moves) -1)
X = Moves [X]
Board [int (X)] = "O"
Win f ():
#if all ([et al ['X', 'O'] for i in Board]):
# Print "REMI"
# Playerwin = True
# Run = False
Playboard ()
print "Computer Win!"
Run = False