-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (143 loc) · 4.67 KB
/
index.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
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
n: 8,
arr: [],
cellSize: 40,
queen: 0
},
methods: {
generate() {
this.initArr()
},
range(start, stop, step) {
if (typeof stop == 'undefined') {
// one param defined
stop = start;
start = 0;
}
if (typeof step == 'undefined') {
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return [];
}
let result = [];
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i);
}
return result;
},
hit(i, j){
if (this.arr[i][j] === 1) {
this.arr[i][j] = 0
this.removeThreat(i, j)
this.queen--;
} else if (this.arr[i][j] === 0) {
this.arr[i][j] = 1
this.putThreat(i, j)
this.queen++;
}
Vue.set(this.arr, i, this.arr[i])
},
initArr(){
this.arr = []
for (let i=0;i<this.n;i++) {
this.arr[i] = [];
for (let j=0; j<this.n; j++) {
this.arr[i][j] = 0 // No piece
}
}
this.queen = 0
},
putThreat (row, column) {
for(let i=0; i<this.n; i++) {
if (this.arr[row][i] === 0) { // If empty, put red
this.arr[row][i] = 2
} else if (this.arr[row][i] >= 2) { // If already red, increase
this.arr[row][i]++
}
}
for(let i=0; i<this.n; i++) {
if (this.arr[i][column] === 0) { // If empty, put red
this.arr[i][column] = 2
} else if (this.arr[i][column] >= 2) { // If already red, increase
this.arr[i][column]++
}
}
for(let k=0; row+k < this.n && column+k < this.n; k++){
if(this.arr[row+k][column+k] === 0 ){
this.arr[row+k][column+k] = 2
} else if (this.arr[row+k][column+k] >= 2) { // If already red, increase
this.arr[row+k][column+k]++
}
}
for(let k=0; row+k < this.n && column-k >= 0; k++){
if(this.arr[row+k][column-k] === 0 ){
this.arr[row+k][column-k] = 2
} else if (this.arr[row+k][column-k] >= 2) { // If already red, increase
this.arr[row+k][column-k]++
}
}
for(let k=0; row-k >= 0 && column-k >= 0; k++){
if(this.arr[row-k][column-k] === 0 ){
this.arr[row-k][column-k] = 2
} else if (this.arr[row-k][column-k] >= 2) { // If already red, increase
this.arr[row-k][column-k]++
}
}
for(let k=0; row-k >= 0 && column+k < this.n; k++){
if(this.arr[row-k][column+k] === 0 ){
this.arr[row-k][column+k] = 2
} else if (this.arr[row-k][column+k] >= 2) { // If already red, increase
this.arr[row-k][column+k]++
}
}
},
removeThreat (row, column) {
for(let i=0; i<this.n; i++) {
if (this.arr[row][i] >= 3) { // If already red and threatened by more than 2 queen, decrease
this.arr[row][i]--
} else if (this.arr[row][i] === 2) { // If threatened by one queen, put empty
this.arr[row][i] = 0
}
}
for(let i=0; i<this.n; i++) {
if (this.arr[i][column] >= 3) { // If already red and threatened by more than 2 queen, decrease
this.arr[i][column]--
} else if (this.arr[i][column] === 2) { // If threatened by one queen, put empty
this.arr[i][column] = 0
}
}
for(let k=0; row+k < this.n && column+k < this.n; k++){
if(this.arr[row+k][column+k] >= 3 ){
this.arr[row+k][column+k]--
} else if (this.arr[row+k][column+k] === 2) { // If already red, increase
this.arr[row+k][column+k] = 0
}
}
for(let k=0; row+k < this.n && column-k >= 0; k++){
if(this.arr[row+k][column-k] >= 3 ){
this.arr[row+k][column-k]--
} else if (this.arr[row+k][column-k] === 2) { // If already red, increase
this.arr[row+k][column-k] = 0
}
}
for(let k=0; row-k >= 0 && column-k >= 0; k++){
if(this.arr[row-k][column-k] >= 3 ){
this.arr[row-k][column-k]--
} else if (this.arr[row-k][column-k] === 2) { // If already red, increase
this.arr[row-k][column-k] = 0
}
}
for(let k=0; row-k >= 0 && column+k < this.n; k++){
if(this.arr[row-k][column+k] >= 3 ){
this.arr[row-k][column+k]--
} else if (this.arr[row-k][column+k] === 2) { // If already red, increase
this.arr[row-k][column+k] = 0
}
}
}
}
})