-
Notifications
You must be signed in to change notification settings - Fork 0
/
winner-calculator.js
175 lines (170 loc) · 4.57 KB
/
winner-calculator.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* 计算胜利者
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
*/
function calculateWinner(squares, rowSize, colSize, targetCount){
const squareRows = getSquareRows(squares, rowSize, colSize, targetCount);
let winner = caculateSeria(squareRows, targetCount);
if(winner){
return winner;
}
const squareCols = getSquareCols(squares, rowSize, colSize, targetCount);
winner = caculateSeria(squareCols, targetCount);
if(winner){
return winner;
}
const squareLeftDias = getSquareLeftDias(squares, rowSize, colSize, targetCount);
winner = caculateSeria(squareLeftDias, targetCount);
if(winner){
return winner;
}
const squareDiaRight = getSquareRightDias(squares, rowSize, colSize, targetCount);
winner = caculateSeria(squareDiaRight, targetCount);
if(winner){
return winner;
}
return null;
}
/**
* 获取棋盘所有的行
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
*/
function getSquareRows(squares, rowSize, colSize, targetCount){
const squareRows = [];
for(let r = 0; r < rowSize; r++){
let row = [];
for(let c = 0; c < colSize; c++){
row.push({r:r, c:c, s:squares[r][c]});
}
squareRows.push(row);
}
return squareRows;
}
/**
* 获取棋盘所有的列
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
*/
function getSquareCols(squares, rowSize, colSize, targetCount){
const squareCols = [];
for(let c = 0; c < colSize; c++){
let col = [];
for(let r = 0; r < rowSize; r++){
col.push({r:r, c:c, s:squares[r][c]});
}
squareCols.push(col);
}
return squareCols;
}
/**
* 获取棋盘所有的左斜线(/)方向格子数组
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
*/
function getSquareLeftDias(squares, rowSize, colSize, targetCount){
const squareLeftDias = [];
for(let c = targetCount - 1; c < colSize; c++){
let dia = [];
let colInc = 0;
for(let r = 0; r < rowSize; r++){
if(c - colInc >= 0){
dia.push({r:r, c: c-colInc, s: squares[r][c-colInc]});
colInc++;
}else{
break;
}
}
squareLeftDias.push(dia);
}
for(let r = 1; r < rowSize - targetCount + 1; r++){
let dia = [];
let rowInc = 0;
for(let c = colSize - 1; c >= 0; c--){
if(r + rowInc < rowSize){
dia.push({r:r + rowInc, c:c, s: squares[r + rowInc][c]});
rowInc++;
}else{
break;
}
}
squareLeftDias.push(dia);
}
return squareLeftDias;
}
/**
* 获取棋盘所有的右斜线(\\)方向格子数组
* @param {*} squares
* @param {*} rowSize
* @param {*} colSize
* @param {*} targetCount
*/
function getSquareRightDias(squares, rowSize, colSize, targetCount){
const squareRightDias = [];
for(let c = 0; c <= colSize - targetCount; c++){
let dia = [];
let colInc = 0;
for(let r = 0; r < rowSize; r++){
if(c + colInc < colSize){
dia.push({r:r, c: c + colInc, s:squares[r][c + colInc]});
colInc++;
}else{
break;
}
}
squareRightDias.push(dia);
}
for(let r = rowSize - targetCount; r >= 0; r--){
let dia = [];
let rowInc = 0;
for(let c = 0; c < colSize; c++){
if(r + rowInc < rowSize){
dia.push({r:r + rowInc, c:c, s:squares[r + rowInc][c]});
rowInc++;
}else{
break;
}
}
squareRightDias.push(dia);
}
return squareRightDias;
}
/**
* 计算某一行、列或斜线上是否有胜利者,若有返回胜利者
* @param {*} seria
* @param {*} targetCount
*/
function caculateSeria(seria, targetCount){
for(let r = 0; r < seria.length; r++){
for(let c = 0; c <= seria[r].length - targetCount; c++){
if(seria[r][c].s){
let win = true;
let winPos = [seria[r][c]];
for(let j = c + 1; j < c + targetCount; j++){
win = win && ( seria[r][c].s === seria[r][j].s );
if(!win){
break;
}else{
winPos.push(seria[r][j]);
}
}
if(win){
return {
winner:seria[r][c].s,
winPos: winPos
};
}
}
}
}
return null;
}