+ 1
How can I improve this program?
2 odpowiedzi
+ 2
My instinct would be to do as Airree has said. I would do something like:
winning_combos = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}}
x, o = {}, {}
Then for all 'x' moves, add the index to x's set. Same for o's moves. Comparison using
if any(i < x for i in winning_combos):
Also, your program accepts 9 as an input, but the highest index is 8.
+ 1
(array is roughly the same as list)
When I made my Tic Tac Toe game, I used 3 arrays instead of one: one for x, one for o, and one for the winning scenarios. I pushed the places index to the x array when x placed somewhere. Every turn, I checked if one of the winning scenarios is the subarray of one of the teams' array
This way, the code was shorter