Is it possible to check if multiple string indexes match a variable?
Solution 1:
def endgame():
print('game end!')
board = ['PLACEHOLDER', '-', '-', '-', '-', '-', '-', '-', '-', '-']
tag = 'X'
idx = [[7, 4, 1], [8, 5, 2], [9, 6, 3], [7, 8, 9], [4, 5, 6], [1, 2, 3], [7, 5, 3], [1, 5, 9]]
for cond in idx:
if board[cond[0]] == tag and board[cond[1]] == tag and board[cond[2]] == tag:
endgame()
You could do something like this, which keeps all the win conditions in a list, and iterates over each one, checking if it's true and ending the game if it's true.
As khelwood points out, a more clean version of this takes advantage of variable unpacking/destructuring.
for i, j, k in idx:
if i == tag and j == tag and k == tag:
endgame()
As Olvin points out, generalizing to dynamic board sizes, with the all() function, we can write this as
for cond in idx:
if all(board[i] == tag for i in cond):
endgame()