|
1 | 1 | classTicTacToe {
|
2 | 2 |
|
3 |
| -/** Initialize your data structure here. */ |
4 |
| -int[][]board; |
5 |
| -Map<Integer,Map<Integer,Integer>>rowMap; |
6 |
| -Map<Integer,Map<Integer,Integer>>colMap; |
7 |
| -Map<Integer,Integer>leftDiagonalMap; |
8 |
| -Map<Integer,Integer>rightDiagonalMap; |
9 |
| -intn; |
| 3 | +privateintn; |
| 4 | +privateMap<Integer,Map<Integer,Integer>>rowMapping; |
| 5 | +privateMap<Integer,Map<Integer,Integer>>colMapping; |
| 6 | +privateMap<Integer,Map<Integer,Integer>>diagonalMapping; |
| 7 | + |
| 8 | +privatefinalintLEFT_DIAGONAL =1; |
| 9 | +privatefinalintRIGHT_DIAGONAL = -1; |
| 10 | + |
10 | 11 | publicTicTacToe(intn) {
|
11 |
| -board =newint[n][n]; |
12 |
| -rowMap =newHashMap<>(); |
13 |
| -colMap =newHashMap<>(); |
14 |
| -leftDiagonalMap =newHashMap<>(); |
15 |
| -rightDiagonalMap =newHashMap<>(); |
16 | 12 | this.n =n;
|
| 13 | +this.rowMapping =newHashMap<>(); |
| 14 | +this.colMapping =newHashMap<>(); |
| 15 | +this.diagonalMapping =newHashMap<>(); |
| 16 | +this.diagonalMapping.put(LEFT_DIAGONAL,newHashMap<>()); |
| 17 | +this.diagonalMapping.put(RIGHT_DIAGONAL,newHashMap<>()); |
17 | 18 | }
|
18 | 19 |
|
19 |
| -/** Player {player} makes a move at ({row}, {col}). |
20 |
| - @param row The row of the board. |
21 |
| - @param col The column of the board. |
22 |
| - @param player The player, can be either 1 or 2. |
23 |
| - @return The current winning condition, can be either: |
24 |
| - 0: No one wins. |
25 |
| - 1: Player 1 wins. |
26 |
| - 2: Player 2 wins. */ |
27 | 20 | publicintmove(introw,intcol,intplayer) {
|
28 |
| -if (!rowMap.containsKey(row)) { |
29 |
| -Map<Integer,Integer>map =newHashMap<>(); |
30 |
| -rowMap.put(row,map); |
31 |
| - } |
32 |
| -rowMap.get(row).put(player,rowMap.get(row).getOrDefault(player,0) +1); |
33 |
| -if (ifWinner(rowMap.get(row),player)) { |
34 |
| -returnplayer; |
35 |
| - } |
36 |
| -if (!colMap.containsKey(col)) { |
37 |
| -Map<Integer,Integer>map =newHashMap<>(); |
38 |
| -colMap.put(col,map); |
39 |
| - } |
40 |
| -colMap.get(col).put(player,colMap.get(col).getOrDefault(player,0) +1); |
41 |
| -if (ifWinner(colMap.get(col),player)) { |
42 |
| -returnplayer; |
43 |
| - } |
44 |
| -if (row ==col) { |
45 |
| -leftDiagonalMap.put(player,leftDiagonalMap.getOrDefault(player,0) +1); |
46 |
| - } |
47 |
| -if (ifWinner(leftDiagonalMap,player)) { |
48 |
| -returnplayer; |
49 |
| - } |
50 |
| -if (row +col ==n -1) { |
51 |
| -rightDiagonalMap.put(player,rightDiagonalMap.getOrDefault(player,0) +1); |
52 |
| - } |
53 |
| -if (ifWinner(rightDiagonalMap,player)) { |
| 21 | +recordMove(row,col,player); |
| 22 | +if (isWinnerFound(row,col,player)) { |
54 | 23 | returnplayer;
|
55 | 24 | }
|
56 | 25 | return0;
|
57 | 26 | }
|
58 | 27 |
|
59 |
| -privatebooleanifWinner(Map<Integer,Integer>map,intplayer) { |
60 |
| -if (map.size() >1) { |
61 |
| -returnfalse; |
| 28 | +privatevoidrecordMove(introw,intcol,intplayer) { |
| 29 | +this.rowMapping.computeIfAbsent(row,k ->newHashMap<>()); |
| 30 | +this.colMapping.computeIfAbsent(col,k ->newHashMap<>()); |
| 31 | +this.rowMapping.get(row).put(player,this.rowMapping.get(row).getOrDefault(player,0) +1); |
| 32 | +this.colMapping.get(col).put(player,this.colMapping.get(col).getOrDefault(player,0) +1); |
| 33 | +if (row ==col) { |
| 34 | +this.diagonalMapping.get(LEFT_DIAGONAL) |
| 35 | + .put(player,this.diagonalMapping.get(LEFT_DIAGONAL).getOrDefault(player,0) +1); |
| 36 | + } |
| 37 | +if (row +col +1 ==this.n) { |
| 38 | +this.diagonalMapping.get(RIGHT_DIAGONAL) |
| 39 | + .put(player,this.diagonalMapping.get(RIGHT_DIAGONAL).getOrDefault(player,0) +1); |
62 | 40 | }
|
63 |
| -returnmap.values().stream().reduce(0,Integer::sum) ==n; |
| 41 | + } |
| 42 | + |
| 43 | +privatebooleanisWinnerFound(introw,intcol,intplayer) { |
| 44 | +returnthis.rowMapping.get(row).get(player) ==this.n || |
| 45 | +this.colMapping.get(col).get(player) ==this.n || |
| 46 | +this.diagonalMapping.get(LEFT_DIAGONAL).getOrDefault(player,0) ==this.n || |
| 47 | +this.diagonalMapping.get(RIGHT_DIAGONAL).getOrDefault(player,0) ==this.n; |
64 | 48 | }
|
65 | 49 | }
|
66 | 50 |
|
|