-
-
Save Celeo/6df2ea81a0c06d206947250cbc24628a to your computer and use it in GitHub Desktop.
Tic-tac-toe simple solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tictactoe import check | |
TESTCASES = { | |
'#########': '#', | |
'XXX######': 'X', | |
'###XXX###': 'X', | |
'######XXX': 'X', | |
'X##X##X##': 'X', | |
'#X##X##X#': 'X', | |
'##X##X##X': 'X', | |
'X###X###X': 'X', | |
'##X#X#X##': 'X', | |
'OOO######': 'O', | |
'###OOO###': 'O', | |
'######OOO': 'O', | |
'O##O##O##': 'O', | |
'#O##O##O#': 'O', | |
'##O##O##O': 'O', | |
'O###O###O': 'O', | |
'##O#O#O##': 'O' | |
} | |
def test_check(): | |
for data, expected in TESTCASES.items(): | |
assert check(data) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Layout, in terms of array indexes: | |
# 012 | |
# 345 | |
# 678 | |
VALID_WINS = [ | |
'012', | |
'345', | |
'678', | |
'036', | |
'147', | |
'258', | |
'048', | |
'246' | |
] | |
def check(data): | |
for condition in VALID_WINS: | |
indexes = [int(e) for e in condition] | |
if data[indexes[0]] == data[indexes[1]] == data[indexes[2]] and data[indexes[0]] != '#': | |
return data[indexes[0]] | |
return '#' | |
if __name__ == '__main__': | |
test_data = sys.stdin.readline().strip() | |
print(check(test_data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires pytest to run the tests, or a bit of manual work.