Tic-tac-toe winner
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Tic-tac-toe winner
Decide a game of noughts and crosses from the board.
There are eight lines to check: three rows, three columns and two diagonals. board[r][c] is the square in row r and column c, so a column is board[0][c] + board[1][c] + board[2][c]. Collect all eight lines as three-letter strings, then look for "XXX" or "OOO".
Write winner(board). board is a list of three strings, one per row, using X, O, and . for an empty square. Return "X" or "O" if that player has three in a row, column or diagonal, otherwise "".
Click Run to see the output here.