Created
October 7, 2020 05:11
-
-
Save charlieInDen/4203c49dde01e5ddb040e2a38dbf59ab to your computer and use it in GitHub Desktop.
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
class TicTacToe { | |
var rows: [Int]! | |
var cols: [Int]! | |
var diagonal: Int! | |
var antiDiagonal: Int! | |
/** Initialize your data structure here. */ | |
init(_ n: Int) { | |
rows = [] | |
cols = [] | |
for i in 0..<n { | |
rows.append(0) | |
cols.append(0) | |
} | |
diagonal = 0 | |
antiDiagonal = 0 | |
} | |
/** Player {player} makes a move at ({row}, {col}). | |
@param row The row of the board. | |
@param col The column of the board. | |
@param player The player, can be either 1 or 2. | |
@return The current winning condition, can be either: | |
0: No one wins. | |
1: Player 1 wins. | |
2: Player 2 wins. */ | |
func move(_ row: Int, _ col: Int, _ player: Int) -> Int { | |
let toAdd = player == 1 ? 1 : -1 | |
rows[row] = rows[row] + toAdd | |
cols[col] = cols[col] + toAdd | |
if row == col { | |
diagonal += toAdd | |
} | |
if col == (cols.count - row - 1){ | |
antiDiagonal += toAdd | |
} | |
let size = rows.count | |
if abs(rows[row]) == size || | |
abs(cols[col]) == size || | |
abs(diagonal) == size || | |
abs(antiDiagonal) == size { | |
return player; | |
} | |
return 0 | |
} | |
} | |
/** | |
* Your TicTacToe object will be instantiated and called as such: | |
* let obj = TicTacToe(n) | |
* let ret_1: Int = obj.move(row, col, player) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment