My question is:
I have written a program to take input in the format e.g.
0 0 0
X 0 X
X X 0
the program is supposed to simulate "noughts and crosses" or "Tic Tac Toe" ,depending where you are from, and decide who is the winner i.e. "X" or "0" or if there is no winner, "draw". I am at the point where i can output for each 8 possibilities either "True" if X won or "False" if 0 won in the form of a list. What i don't know how to do is iterate over the elements in the list and get the program
to evaluate if there are both True and False in list to output "draw", if there is only True, to output "X is the winner", or if there is only False, 0 is the winner.
This is my code
Thanks you for any help and also please suggest ways to neaten and shorten my code with more productive functions. Thank you!!!
I have written a program to take input in the format e.g.
0 0 0
X 0 X
X X 0
the program is supposed to simulate "noughts and crosses" or "Tic Tac Toe" ,depending where you are from, and decide who is the winner i.e. "X" or "0" or if there is no winner, "draw". I am at the point where i can output for each 8 possibilities either "True" if X won or "False" if 0 won in the form of a list. What i don't know how to do is iterate over the elements in the list and get the program
to evaluate if there are both True and False in list to output "draw", if there is only True, to output "X is the winner", or if there is only False, 0 is the winner.
This is my code
Code:
def EvalC(A,B,C,V,K):
#Evaluates each row or column to determine True or False
if A== V:
i = 1
if B == V:
i = 2
if C == V:
i = 3
return True
elif A == K:
n = 1
if B == K:
n = 2
if C ==K:
n = 3
return False
Matrix = []
while True:
Input = raw_input()
if Input:
Input = Matrix.append(Input.split())
else:
break
#takes the input an put it into a list
#e.g.[['X', '0', '0'], ['X', '-', '-'], ['X', '0', '0']]
TRow = Matrix[0][0] + Matrix[0][1] + Matrix[0][2]
MRow = Matrix[1][0] + Matrix[1][1] + Matrix[1][2]
BRow = Matrix[2][0] + Matrix[2][1] + Matrix[2][2]
LCol = TRow[0] + MRow[0] + BRow[0]
MCol = TRow[1] + MRow[1] + BRow[1]
RCol = TRow[2] + MRow[2] + BRow[2]
VertR = TRow[0] + MRow[1] + BRow[2]
VertL = TRow[2] + MRow[1] + BRow[0]
#above are all the possible combos for wins
List1 =[(EvalC(TRow[0],MRow[0],BRow[0],"X","0")),(EvalC(TRow[1],MRow[1],BRow[1],"X","0")),(EvalC(TRow[2],MRow[2],BRow[2],"X","0")),
(EvalC(TRow[0],MRow[1],BRow[2],"X","0")),(EvalC(TRow[2],MRow[1],BRow[0],"X","0")),
(EvalC(Matrix[0][0],Matrix[0][1],Matrix[0][2],"X","0")),(EvalC(Matrix[1][0],Matrix[1][1],Matrix[1][2],"X","0")),
(EvalC(Matrix[2][0],Matrix[2][1],Matrix[2][2],"X","0"))]
#the function applied to each possible win combo
A possible output at this level would be:
List1=[True, None, None, None, None, None, None, None]
Comment