Hi people. I'm trying to create a tictactoe program in python but i'm having some troubles. It's the player against the computer. Now the computer should go on random but still block/win if possible. Right now i'm kinda stuck so feel free to help. I'd prefer it as simple as possible so that i can understand the code. This is what i got so far...
import random
def print_board(board):
board = board.replace("_"," ")
print "." + "---." * 3
for bound in [0,3,6]:
print "|",
for sym in board[bound:bound+3]:
print sym, "|",
if bound < 6:
print "\n|" + "---|" * 3
print "\n’" + "---´" * 3
coordinate = ["0,0", "0,1", "0,2", "1,0", "1,1", "1,2", "2,0", "2,1", "2,2"]
usedcoord = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
def percentchance():
import random
return int(100*random.random())
running = False
while running:
while used == True:
pos = random.randrange(0,8)
coordinate[pos]
if coordinate[pos] in usedcoord:
used = True
else:
usedcoord.append(coordinate[pos])
board[pos] = "o"
print "My turn. I put my ring in square", coordinate[pos]
print_board(board)
used = False
while used == True:
user = raw_input("Your turn. Input the coordinate for your move on the form x,y: ")
if user in usedcoord:
used = True
else:
idx = coordinate.index(user)
usedcoord.append(user)
board[idx] = "x"
print_board(board)
used = False
def randoms(list):
import random
return random.choice(list)
usedcoord += number