Done. All that's left is an option to let the computer take the first turn, and a better interface. But it's very much playable.
# A simple tic-tac-toe game, with a "human" artificial intelligence opponent.
# For reference purposes both the computer and user is referred to as players.
from random import randint
# The fields. A "0" means empty, a "1" means X, a "2" means O.
field = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0}
# The fields, visualised
vfield = " \n \n "
# The user's unit type(X by default).
usertype = 1
# The computer's unit type(O by default).
comtype = 2
# The redrawing of the visualised fields
def redrawfields():
global field
global vfield
vfield = ""
count = 0
for entry in field:
if count == 3:
vfield += "\n"
count = 0
if field[entry] == 0:
vfield += " "
elif field[entry] == 1:
vfield += "X"
else:
vfield += "O"
count += 1
# The computer's movement.
def computermove():
global field
global usertype
global comtype
# All possible rows.
row1 = [1, 2, 3]
row2 = [4, 5, 6]
row3 = [7, 8, 9]
row4 = [1, 4, 7]
row5 = [2, 5, 8]
row6 = [3, 6, 9]
row7 = [1, 5, 9]
row8 = [3, 5, 7]
rows = [row1, row2, row3, row4, row5, row6, row7, row8]
# Finds out if the opponent is about to win, or if the computer is about to win. The "danger" and "win" variables also act as a positioners.
danger = 0
win = 0
for row in rows:
totalopponent = 0
for entry in row:
if field[entry] == usertype:
totalopponent += 1
elif field[entry] == comtype:
totalopponent -= 1
if totalopponent == 2:
for entry in row:
if field[entry] == 0:
danger = entry
break
break
elif totalopponent == -2:
for entry in row:
if field[entry] == 0:
win = entry
break
break
# Checks if all the fields are taken
takenfields = 0
for entry in field:
if field[entry] != 0:
takenfields += 1
# Finds out what to do
if takenfields == 9:
return 0
elif win != 0:
field[win] = comtype
elif danger == 0:
if field[5] == 0:
field[5] = comtype
else:
while True:
a = randint(1, 9)
if field[a] == 0:
field[a] = comtype
break
else:
field[danger] = comtype
# Finds out if someone has won.
def win():
# Borrows the code from computermove
global field
global usertype
global comtype
row1 = [1, 2, 3]
row2 = [4, 5, 6]
row3 = [7, 8, 9]
row4 = [1, 4, 7]
row5 = [2, 5, 8]
row6 = [3, 6, 9]
row7 = [1, 5, 9]
row8 = [3, 5, 7]
rows = [row1, row2, row3, row4, row5, row6, row7, row8]
for row in rows:
totalopponent = 0
for entry in row:
if field[entry] == usertype:
totalopponent += 1
elif field[entry] == comtype:
totalopponent -= 1
if totalopponent == 3:
return 1
break
elif totalopponent == -3:
return 2
break
takenfields = 0
for entry in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
if field[entry] != 0:
takenfields += 1
if takenfields == 9:
return 3
return 0
# Here the script starts.
#Prints the intro.
print "This is a simple game of Tic-Tac-Toe."
print "You'll be playing against a computer opponent."
print "You choose which field to fill by entering the corresponding number."
print "The fields in the first row are 1, 2, and 3, and so on."
print " "
# Finds out which unit type the user wants.
while True:
print "Would you like to play as X or O?"
choice = raw_input("> ")
if choice == "X" or choice == "x":
usertype = 1
comtype = 2
break
elif choice == "O" or choice == "o":
usertype = 2
comtype = 1
break
else:
"That's not a valid entry. Only X/x or O/o, please."
print " "
print "Game start!"
#Here the game of tic-tac-toe starts. It checks whether anyone has one or if it's become a tie.
while win() == 0:
print " "
print vfield
print " "
choice = input("Which field do you choose? ")
if field[choice] != 0:
print "That field's already taken."
else:
field[choice] = usertype
if win() == 1:
break
computermove()
redrawfields()
redrawfields()
print " "
print vfield
print " "
if win() == 1:
print "You won!"
elif win() == 2:
print "The computer won!"
else:
print "It's a tie!"
Excuse me while I go off to bed.