-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,80 @@ | ||
class Event { | ||
constructor(sender) { | ||
this.sender = sender; | ||
this.listeners = []; | ||
} | ||
attach(listener) { | ||
this.listeners.push(listener); | ||
} | ||
notify(args) { | ||
this.listeners.forEach((listener) => { | ||
listener(this.sender, args); | ||
}); | ||
} | ||
} | ||
import Event from '../utils/Event'; | ||
|
||
class Board { | ||
constructor(rows = 10, columns = 10) { | ||
// матрица m на n заполненная false | ||
this.matrix = []; | ||
this.initMatrix(rows, columns); | ||
this.rows = rows; | ||
this.columns = columns; | ||
this.listOldMatrix = []; | ||
this.matrixChanged = new Event(this); | ||
this.cellToggled = new Event(this); | ||
} | ||
initMatrix(rows, columns) { | ||
this.matrix = []; | ||
for (let i = 0; i < rows; i += 1) { | ||
const row = []; | ||
let row = []; | ||
for (let j = 0; j < columns; j += 1) { | ||
row.push(false); | ||
} | ||
this.matrix.push(row); | ||
} | ||
} | ||
resize(m, n) { | ||
const { matrix } = this; | ||
const o = matrix.length; | ||
const p = matrix[0].length; | ||
// убираем столбцы | ||
if (p > n) { | ||
for (let i = 0; i < o; i += 1) { | ||
matrix[i].splice(n - 1, p - n);// изменить length? | ||
} | ||
} | ||
|
||
// добавляем столбцы | ||
if (p < n) { | ||
for (let i = 0; i < o; i += 1) { | ||
for (let j = p; j < n; j += 1) { | ||
matrix[i].push(false); | ||
} | ||
} | ||
} | ||
|
||
// убираем строки | ||
if (o > m) matrix.splice(m - 1, o - m);// изменить length? | ||
|
||
// добавляем строки | ||
if (o < m) { | ||
const line = []; | ||
for (let j = 0; j < n; j += 1) { | ||
line.push(false); | ||
} | ||
|
||
for (let i = o; i < m; i += 1) { | ||
matrix.push(line.slice()); | ||
} | ||
} | ||
|
||
this.rows = m; | ||
this.columns = n; | ||
resizeMatrix(rows, columns) { | ||
this.initMatrix(rows, columns); | ||
this.rows = rows; | ||
this.columns = columns; | ||
this.listOldMatrix = []; | ||
this.matrixChanged.notify({ matrix: this.matrix, resized: true }); | ||
} | ||
clear() { | ||
this.matrix.forEach((row) => { | ||
row.forEach((item, i, arr) => { | ||
arr[i] = false; | ||
}); | ||
}); | ||
clearMatrix() { | ||
this.initMatrix(this.rows, this.columns); | ||
this.listOldMatrix = []; | ||
this.matrixChanged.notify({ matrix: this.matrix, clear: true }); | ||
this.matrixChanged.notify({ matrix: this.matrix }); | ||
} | ||
worker() { | ||
calculateMatrix() { | ||
// обход всех ячеек с записью нового состояния | ||
const newMatrix = []; | ||
let flag = false;// изменилась ли матрица? | ||
this.matrix.forEach((row, i) => { | ||
const newRow = []; | ||
row.forEach((cell, j) => { | ||
const newCell = this.calculateCell(i, j); | ||
newRow.push(newCell); | ||
if (newCell !== cell) flag = true; | ||
}); | ||
newMatrix.push(newRow); | ||
}); | ||
|
||
if (flag) { // изменилась ли матрица в сравнении с предыдущей ? | ||
this.matrix = newMatrix; | ||
flag = !this.isRepeat(newMatrix); // а в сравнении со всеми предыдущими ? | ||
} | ||
this.matrixChanged.notify({ matrix: this.matrix, isChanged: !flag }); | ||
const newMatrix = this.matrix.map((row, i) => row.map((cell, j) => this.calculateCell(i, j))); | ||
const flag = this.isRepeatMatrix(newMatrix); // повторилась ли матрица? | ||
this.matrixChanged.notify({ matrix: this.matrix }); | ||
this.matrix = newMatrix; | ||
return flag; | ||
} | ||
isRepeat(newMatrix) { | ||
isRepeatMatrix(newMatrix) { | ||
const flag = this.listOldMatrix.some((matrix) => { | ||
if (matrix.length !== newMatrix.length) return false; | ||
if (matrix[0].length !== newMatrix[0].length) return false; | ||
return matrix.every((row, i) => row.every((cell, j) => (cell === newMatrix[i][j]))); | ||
}); | ||
this.listOldMatrix.push(newMatrix); | ||
if (flag) this.listOldMatrix = []; | ||
else this.listOldMatrix.push(newMatrix); | ||
return flag; | ||
} | ||
calculateCell(i, j) { | ||
calculateCell(row, column) { | ||
// соседи за пределами поля считаются мертвыми | ||
let count = 0;// живые соседи | ||
let newCell = this.matrix[i][j]; | ||
let newCell = this.matrix[row][column]; | ||
|
||
if (this.matrix[i - 1]) { | ||
if (this.matrix[i - 1][j - 1]) count += 1; | ||
if (this.matrix[i - 1][j]) count += 1; | ||
if (this.matrix[i - 1][j + 1]) count += 1; | ||
if (this.matrix[row - 1]) { | ||
if (this.matrix[row - 1][column - 1]) count += 1; | ||
if (this.matrix[row - 1][column]) count += 1; | ||
if (this.matrix[row - 1][column + 1]) count += 1; | ||
} | ||
|
||
if (this.matrix[i][j - 1]) count += 1; | ||
if (this.matrix[i][j + 1]) count += 1; | ||
if (this.matrix[row][column - 1]) count += 1; | ||
if (this.matrix[row][column + 1]) count += 1; | ||
|
||
if (this.matrix[i + 1]) { | ||
if (this.matrix[i + 1][j - 1]) count += 1; | ||
if (this.matrix[i + 1][j]) count += 1; | ||
if (this.matrix[i + 1][j + 1]) count += 1; | ||
if (this.matrix[row + 1]) { | ||
if (this.matrix[row + 1][column - 1]) count += 1; | ||
if (this.matrix[row + 1][column]) count += 1; | ||
if (this.matrix[row + 1][column + 1]) count += 1; | ||
} | ||
|
||
if (count < 2 || count > 3) newCell = false; | ||
else if (count === 3) newCell = true; | ||
return newCell; | ||
} | ||
toggleCell(i, j) { | ||
this.matrix[i][j] = !this.matrix[i][j]; | ||
this.cellToggled.notify(i, j); | ||
toggleCell(row, column) { | ||
this.matrix[row][column] = !this.matrix[row][column]; | ||
this.matrixChanged.notify({ matrix: this.matrix }); | ||
} | ||
} | ||
export default Board; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Event { | ||
constructor(sender) { | ||
this.sender = sender; | ||
this.listeners = []; | ||
} | ||
attach(listener) { | ||
this.listeners.push(listener); | ||
} | ||
notify(args) { | ||
this.listeners.forEach((listener) => { | ||
listener(this.sender, args); | ||
}); | ||
} | ||
} | ||
export default Event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7d716b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#17
#16
#15
#14
#13
#11
#10
#5