Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ab62d1

Browse files
authored
Update Design Tic-Tac-Toe.java
1 parent0c0489a commit2ab62d1

File tree

1 file changed

+30
-100
lines changed

1 file changed

+30
-100
lines changed

‎Medium/Design Tic-Tac-Toe.java

Lines changed: 30 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
classTicTacToe {
22

33
/** Initialize your data structure here. */
4-
int[][]board;
5-
intn;
6-
booleanwin;
4+
privateint[]rows;
5+
privateint[]cols;
6+
privateintleftDiagCount;
7+
privateintrightDiagCount;
8+
privateintn;
79

810
publicTicTacToe(intn) {
911
this.n =n;
10-
board =newint[n][n];
12+
this.rows =newint[n];
13+
this.cols =newint[n];
14+
leftDiagCount =0;
15+
rightDiagCount =0;
1116
}
1217

1318
/** Player {player} makes a move at ({row}, {col}).
@@ -19,104 +24,29 @@ public TicTacToe(int n) {
1924
1: Player 1 wins.
2025
2: Player 2 wins. */
2126
publicintmove(introw,intcol,intplayer) {
22-
if (win) {
23-
return0;
24-
}
25-
26-
board[row][col] =player;
27-
28-
booleancCheck =columnCheck(col,n,player);
29-
booleanrCheck =rowCheck(row,n,player);
30-
booleandiagCheck =diagonalCheck(row,col,n,player);
31-
32-
if (cCheck ||rCheck ||diagCheck) {
33-
win =true;
27+
intmoveValue =player ==1 ?1 : -1;
28+
rows[row] +=moveValue;
29+
cols[col] +=moveValue;
30+
if (row ==col) {
31+
leftDiagCount +=moveValue;
32+
}
33+
if (col ==n -row -1) {
34+
rightDiagCount +=moveValue;
35+
}
36+
if (
37+
Math.abs(rows[row]) ==n ||
38+
Math.abs(cols[col]) ==n ||
39+
Math.abs(leftDiagCount) ==n ||
40+
Math.abs(rightDiagCount) ==n
41+
) {
3442
returnplayer;
3543
}
36-
3744
return0;
3845
}
39-
40-
privatebooleandiagonalCheck(introw,intcol,intn,intplayer) {
41-
intcount =0;
42-
43-
introwNum =row;
44-
intcolNum =col;
45-
46-
while (rowNum <n &&colNum <n) {
47-
if (board[rowNum][colNum] ==player) {
48-
count++;
49-
}
50-
51-
rowNum++;
52-
colNum++;
53-
}
54-
55-
rowNum =row-1;
56-
colNum =col-1;
57-
58-
while (rowNum >=0 &&colNum >=0) {
59-
if (board[rowNum][colNum] ==player) {
60-
count++;
61-
}
62-
63-
rowNum--;
64-
colNum--;
65-
}
66-
67-
if (count ==n) {
68-
returntrue;
69-
}
70-
71-
count =0;
72-
rowNum =row;
73-
colNum =col;
74-
75-
while (rowNum >=0 &&colNum <n) {
76-
if (board[rowNum][colNum] ==player) {
77-
count++;
78-
}
79-
80-
rowNum--;
81-
colNum++;
82-
}
83-
84-
rowNum =row;
85-
colNum =col;
86-
87-
while (rowNum <n &&colNum >=0) {
88-
if (board[rowNum][colNum] ==player) {
89-
count++;
90-
}
91-
92-
rowNum++;
93-
colNum--;
94-
}
95-
96-
if (count ==n+1) {
97-
returntrue;
98-
}
99-
100-
returnfalse;
101-
}
102-
103-
privatebooleanrowCheck(introw,intn,intplayer) {
104-
for (inti=0;i<n;i++) {
105-
if (board[row][i] !=player) {
106-
returnfalse;
107-
}
108-
}
109-
110-
returntrue;
111-
}
112-
113-
privatebooleancolumnCheck(intcol,intn,intplayer) {
114-
for (inti=0;i<n;i++) {
115-
if (board[i][col] !=player) {
116-
returnfalse;
117-
}
118-
}
119-
120-
returntrue;
121-
}
12246
}
47+
48+
/**
49+
* Your TicTacToe object will be instantiated and called as such:
50+
* TicTacToe obj = new TicTacToe(n);
51+
* int param_1 = obj.move(row,col,player);
52+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp