Does anyone know of an easy way to tell if an a two dimensional array is a tic-tac-toe victory? Ie. if we let 1
s represent player 1, -1
s represent player 2 and 0
s are empty tiles then
1 -1 0
1 -1 -1
1 0 0
is a win for player 1. I’d basically want a simple, fast function that returns true if a configuration is a win and false if not. Right now, the only thing I could think of is
function winq(board::Array{Int, 2}, player::Int)
combos = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]];
bools = board .== player
for combo in combos
if [bools[i] for i in combo] == [true, true, true]
return true
end
end
return false
end
now if the above array is tile
, then winq(tile, +1) == true
and likewise, winq(tile, -1) == false
But I feel that there’s probably a way better way than explicitly writing out all the combos of indices that need to be checked for a victory. Does anyone have any suggestions using some array magic? I’d like this to be somewhat performant.