I have changed my Tic Tac Toe to include the computer playing against you:
#String for X wins
xWin = "\t _______\n\t/ \\\n\t|VICTORY|\n\t| FOR |\n\t|PLAYER |\n\t| O |\n\t\\_______/"
#String for Y wins
yWin = "\t _______\n\t/ \\\n\t|VICTORY|\n\t| FOR |\n\t|PLAYER |\n\t| Y |\n\t\\_______/"
#String for draw
draw = "\t _______\n\t/ \\\n\t| DRAW |\n\t\\_______/"
#Create Empty Board
matrice = [[ '-' for i in range(3) ] for j in range(3)]
#Check if bot can win from a row
def BotWinRow():
for i in range(3):
if matrice[0][i] == matrice[1][i]:
if matrice[0][i] == 'X' and matrice[2][i] == '-':
matrice[2][i] = 'X'
return False
if matrice[0][i] == matrice[2][i]:
if matrice[0][i] == 'X' and matrice[1][i] == '-':
matrice[1][i] = 'X'
return False
if matrice[1][i] == matrice[2][i]:
if matrice[1][i] == 'X' and matrice[o][i] == '-':
matrice[0][i] = 'X'
return False
return True
#Check if bot can win from a collumn
def BotWinColumn():
for i in range(3):
if matrice[i][0] == matrice[i][1]:
if matrice[i][0] == 'X' and matrice[i][2] == '-':
matrice[i][2] = 'X'
return False
if matrice[i][0] == matrice[i][2]:
if matrice[i][0] == 'X' and matrice[i][1] == '-':
matrice[i][1] = 'X'
return False
if matrice[i][1] == matrice[i][2]:
if matrice[i][1] == 'X' and matrice[i][0] == '-':
matrice[i][0] = 'X'
return False
return True
#Check if bot can win from a diagonal
def BotWinDiagonal():
#Top Left to Bottom Right
if matrice[0][0] == matrice[1][1]:
if matrice[0][0] == 'X' and matrice[2][2] == '-':
matrice[2][2] = 'X'
return False
if matrice[0][0] == matrice[2][2]:
if matrice[0][0] == 'X' and matrice[1][1] == '-':
matrice[1][1] = 'X'
return False
if matrice[1][1] == matrice[2][2]:
if matrice[1][1] == 'X' and matrice[0][0] == '-':
matrice[0][0] = 'X'
return False
#Top Right to Bottom Left
if matrice[2][0] == matrice[1][1]:
if matrice[2][0] == 'X' and matrice[0][2] == '-':
matrice[0][2] = 'X'
return False
if matrice[2][0] == matrice[0][2]:
if matrice[2][0] == 'X' and matrice[1][1] == '-':
matrice[1][1] = 'X'
return False
if matrice[1][1] == matrice[0][2]:
if matrice[1][1] == 'X' and matrice[2][0] == '-':
matrice[2][0] = 'X'
return False
return True
#Check if opponent can win from a row
def OpponentWinRow():
for i in range(3):
if matrice[0][i] == matrice[1][i]:
if matrice[0][i] == 'O' and matrice[2][i] == '-':
matrice[2][i] = 'X'
return False
if matrice[0][i] == matrice[2][i]:
if matrice[0][i] == 'O' and matrice[1][i] == '-':
matrice[1][i] = 'X'
return False
if matrice[1][i] == matrice[2][i]:
if matrice[1][i] == 'O' and matrice[0][i] == '-':
matrice[0][i] = 'X'
return False
return True
#Check if opponents can win from a collumn
def OpponentWinColumn():
for i in range(3):
if matrice[i][0] == matrice[i][1]:
if matrice[i][0] == 'O' and matrice[i][2] == '-':
matrice[i][2] = 'X'
return False
if matrice[i][0] == matrice[i][2]:
if matrice[i][0] == 'O' and matrice[i][1] == '-':
matrice[i][1] = 'X'
return False
if matrice[i][1] == matrice[i][2]:
if matrice[i][1] == 'O' and matrice[i][0] == '-':
matrice[i][0] = 'X'
return False
return True
#Check if opponents can win from a Diagonal
def OpponentWinDiagonal():
#Top Left to Bottom Right
if matrice[0][0] == matrice[1][1]:
if matrice[0][0] == 'O' and matrice[2][2] == 'O':
matrice[2][2] = 'X'
return False
if matrice[0][0] == matrice[2][2]:
if matrice[0][0] == 'O' and matrice[1][1] == 'O':
matrice[1][1] = 'X'
return False
if matrice[1][1] == matrice[2][2]:
if matrice[1][1] == 'O' and matrice[0][0] == 'O':
matrice[0][0] = 'X'
return False
#Top Right to Bottom Left
if matrice[2][0] == matrice[1][1]:
if matrice[2][0] == 'O' and matrice[0][2] == 'O':
matrice[0][2] = 'X'
return False
if matrice[2][0] == matrice[0][2]:
if matrice[2][0] == 'O' and matrice[1][1] == 'O':
matrice[1][1] = 'X'
return False
if matrice[1][1] == matrice[0][2]:
if matrice[1][1] == 'O' and matrice[2][0] == 'O':
matrice[2][0] = 'X'
return False
return True
#Defence against human tactic
def TacticalDefense():
#Top Left
if matrice[0][0] == 'O' and matrice[2][2] == '-':
matrice[2][2] = 'X'
return False
#Top Right
if matrice[2][0] == 'O' and matrice[0][2] == '-':
matrice[0][2] = 'X'
return False
#Bottom Right
if matrice[2][2] == 'O' and matrice[0][0] == '-':
matrice[0][0] = 'X'
return False
#Bottom Left
if matrice[0][2] == 'O' and matrice[2][0] == '-':
matrice[2][0] = 'X'
return False
return True
#Select next move
def OtherNextMove():
#Middle
if matrice[1][1] == '-':
matrice[1][1] = 'X'
return False
#Top Left
if matrice[0][0] == '-':
matrice[0][0] = 'X'
return False
#Top Right
if matrice[2][0] == '-':
matrice[2][0] = 'X'
return False
#Bottom Right
if matrice[2][2] == '-':
matrice[2][2] = 'X'
return False
#Bottom Left
if matrice[0][2] == '-':
matrice[0][2] = 'X'
return False
#Up
if matrice[1][0] == '-':
matrice[1][0] = 'X'
return False
#Right
if matrice[2][1] == '-':
matrice[2][1] = 'X'
return False
#Down
if matrice[1][2] == '-':
matrice[1][2] = 'X'
return False
#Left
if matrice[0][1] == '-':
matrice[0][1] = 'X'
return False
return True
#Bot takes a turn
def BotShot():
MyTurn = True
if MyTurn:
MyTurn = BotWinRow()
if MyTurn:
MyTurn = BotWinColumn()
if MyTurn:
MyTurn = BotWinDiagonal()
if MyTurn:
MyTurn = OpponentWinRow()
if MyTurn:
MyTurn = OpponentWinColumn()
if MyTurn:
MyTurn = OpponentWinDiagonal()
if MyTurn:
MyTurn = TacticalDefense()
if MyTurn:
MyTurn = OtherNextMove()
if MyTurn:
print "I dont know"
print "Your turn."
#Print Matrice
def printMatrix():
print "\n\n"
for i in range(3):
print " | ",
for j in range(3):
print matrice[i][j],
print " | ",
print "\n",
print"\n\n"
#Take player one's turn
def TakeTurn():
printMatrix()
x = int(input("Enter X co-ordinate: ")) - 1
y = int(input("Enter Y co-ordinate: ")) - 1
if matrice[y][x] == '-':
matrice[y][x] = 'O'
else:
print "Please choose another square\n\n"
playerOneTurn()
#Test if Game Has Come to End
def gameStatus():
over = '='
for i in range(3):
for j in range(3):
if matrice[i][j] == '-':
over = '-'
break
for i in range(3):
if matrice[0][i] == matrice[1][i] == matrice[2][i]:
victory(matrice[1][i])
if matrice[i][0] == matrice[i][1] == matrice[i][2]:
victory(matrice[i][1])
if matrice[0][0] == matrice[1][1] == matrice[2][2]:
victory(matrice[1][1])
if matrice[2][0] == matrice[1][1] == matrice[0][2]:
victory(matrice[1][1])
if over == '=':
victory(over)
#Victory Print
def victory(c):
if c == 'O':
print yWin
printMatrix()
raise SystemExit
if c == 'X':
print xWin
printMatrix()
raise SystemExit
if c == '=':
print draw
printMatrix()
raise SystemExit
#main logic
def main():
while True:
TakeTurn()
gameStatus()
BotShot()
gameStatus()
#Set to run main
if __name__ == "__main__":
main()