-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0348_design_tic_tac_toe.rs
73 lines (62 loc) · 1.8 KB
/
s0348_design_tic_tac_toe.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#![allow(unused)]
struct TicTacToe {
rows: Vec<i32>,
cols: Vec<i32>,
diagonal: i32,
antdiagonal: i32,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl TicTacToe {
/** Initialize your data structure here. */
fn new(n: i32) -> Self {
Self {
rows: vec![0; n as usize],
cols: vec![0; n as usize],
diagonal: 0,
antdiagonal: 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. */
fn make_a_move(&mut self, row: i32, col: i32, player: i32) -> i32 {
let (row, col) = (row as usize, col as usize);
let to_add = if player == 1 { 1 } else { -1 };
self.rows[row] += to_add;
self.cols[col] += to_add;
if row == col {
self.diagonal += to_add;
}
if col == (self.cols.len() - row - 1) {
self.antdiagonal += to_add;
}
let size = self.rows.len();
if i32::abs(self.rows[row]) == size as i32
|| i32::abs(self.cols[col]) == size as i32
|| i32::abs(self.diagonal) == size as i32
|| i32::abs(self.antdiagonal) == size as i32
{
return player;
}
return 0;
}
}
/**
* Your TicTacToe object will be instantiated and called as such:
* let obj = TicTacToe::new(n);
* let ret_1: i32 = obj.make_a_move(row, col, player);
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_346() {}
}