Reputation: 19
I managed to make 2 players take turn playing but it stops after the 2nd player entered her answer. I want to do the While loop however I seriously ran out of idea how to. I get it in my mind like I want the two players to keep playing until 3 position of 0s or Xs are filled but putting it in code I really need help.
Below is my condition but I want it to continue asking the player until these condition are met
if list1[0,1,2] == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [0] , list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [1], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list2 [0] , list2 [1], list2[2])== 'x':
print ('Congrats! x won!')
elif (list3 [0] , list3 [0], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [1] , list2 [1], list3[1]) == 'x':
print ('Congrats! x won!')
elif (list1 [2] , list2 [2], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [1], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1 [1] , list2 [1], list3[1]) == 'x':
print ('Congrats! x won!')
elif (list1 [2] , list2 [2], list3[2]) == 'x':
print ('Congrats! x won!')
elif (list1 [0] , list2 [0], list3[0]) == 'x':
print ('Congrats! x won!')
elif (list1[0,1,2] == 'o':)
print ('Congrats! o won!')
elif (list1 [0] , list2 [0] ,list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [1], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list2 [0] , list2 [1], list2[2]) == 'o':
print ('Congrats! o won!')
elif (list3 [0] , list3 [0], list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [1] , list2 [1], list3[1]) == 'o':
print ('Congrats! o won!')
elif (list1 [2] , list2 [2], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [1], list3[0]) == 'o':
print ('Congrats! o won!')
elif (list1 [1] , list2 [1], list3[1]) == 'o':
print ('Congrats! o won!')
elif (list1 [2] , list2 [2], list3[2]) == 'o':
print ('Congrats! o won!')
elif (list1 [0] , list2 [0], list3[0]) == 'o':
print ('Congrats! o won!')
else
Upvotes: 0
Views: 61
Reputation: 475
Try this:
def check_line(line):
if line[0]==line[1] and line[1]==line[2]:
return line[0]
return ' '
def check_win(board):
#check horizontals
for row in board:
res=check_line(row)
if res != ' ':
return res
#check verticals
x=0
for col in board[0]:
res=check_line([board[y][x] for y in range(0, 3)])
x+=1
if res != ' ':
return res
#check diagonals
for diagonal in ( [board[0][0], board[1][1], board[2][2]], [board[0][2], board[1][1], board[2][0]]):
res = check_line(diagonal)
if res != ' ':
return res
return ' '
def show_winner(list1, list2, list3):
winner=check_win([list1, list2, list3])
if winner!=" ":
print("Congrats! {0} won!".format(winner))
list1=['o', 'x', 'x']
list2=['o', 'x', ' ']
list3=['x', 'o', ' ']
show_winner(list1, list2, list3)
Upvotes: 0
Reputation: 54233
It would be easier to have your board as a list of 3 lists (with 3 elements each).
With all
and any
, you can avoid many unnecessary repetitions :
board = [['x', None, 'o'],
['o', 'x', None],
['o', None, 'x']
]
def three_in_row(board, player):
return any(all(board[j][i] == player for i in range(3)) for j in range(3))
def three_in_column(board, player):
return any(all(board[i][j] == player for i in range(3)) for j in range(3))
def three_in_diagonal(board, player):
return all(board[i][i] == player for i in range(3)) or\
all(board[i][2 - i] == player for i in range(3))
print three_in_row(board, 'x')
# False
print three_in_column(board, 'x')
# False
print three_in_diagonal(board, 'x')
# True
Upvotes: 3