1 public class TicTacToe {
 2     private int[] rows;
 3     private int[] cols;
 4     private int diag;
 5     private int antidiag;
 6     private int n;
 7     /** Initialize your data structure here. */
 8     public TicTacToe(int n) {
 9         this.n = n;
10         rows = new int[n];
11         cols = new int[n];
12         diag = 0;
13         antidiag = 0;
14     }
15     
16     /** Player {player} makes a move at ({row}, {col}).
17         @param row The row of the board.
18         @param col The column of the board.
19         @param player The player, can be either 1 or 2.
20         @return The current winning condition, can be either:
21                 0: No one wins.
22                 1: Player 1 wins.
23                 2: Player 2 wins. */
24     public int move(int row, int col, int player) {
25         int toAdd = player == 1 ? 1 : -1;
26         rows[row] += toAdd;
27         cols[col] += toAdd;
28         diag += row == col ? toAdd : 0;
29         antidiag += row == (n - col - 1) ? toAdd : 0;
30         
31         if (Math.abs(rows[row]) == n ||
32             Math.abs(cols[col]) == n ||
33             Math.abs(diag) == n ||
34             Math.abs(antidiag) == n) {
35                 return player;
36             }
37         return 0;
38     }
39 }
40 
41 /**
42  * Your TicTacToe object will be instantiated and called as such:
43  * TicTacToe obj = new TicTacToe(n);
44  * int param_1 = obj.move(row,col,player);
45  */

1. One thing need to be notice that if player put to wrong place...

posted on 2016-07-01 05:01  keepshuatishuati  阅读(139)  评论(0编辑  收藏  举报