Skip to content

Commit 2c61ea0

Browse files
committed
feat: add init methods to board
1 parent 0e506f0 commit 2c61ea0

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/Board/Board.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
1+
import { Cell } from "~/Cell"
2+
13
export class Board {
2-
rows: number = 100
3-
cols: number = 100
4-
cellSize: number = 10
4+
_board: Cell[][] = []
5+
_rows: number = 100
6+
_cols: number = 100
57
ctx: CanvasRenderingContext2D
68

79
constructor(ctx: CanvasRenderingContext2D) {
810
this.ctx = ctx
911
}
12+
init() {
13+
this.fillBoard()
14+
this.drawBoard()
15+
}
16+
17+
get board(): Cell[][] {
18+
return this._board
19+
}
20+
21+
fillBoard() {
22+
for (let i = 0; i < this._rows; i++) {
23+
this._board[i] = []
24+
for (let j = 0; j < this._cols; j++) {
25+
this._board[i][j] = new Cell(i, j)
26+
}
27+
}
28+
}
1029

11-
draw() {
30+
drawBoard() {
1231
this.ctx.beginPath()
13-
this.ctx.rect(0, 0, this.rows * this.cellSize, this.cols * this.cellSize)
14-
this.ctx.stroke()
32+
for (let i = 0; i < this._board.length; i++) {
33+
for(let j = 0; j < this._board[i].length; j++) {
34+
this._board[i][j].draw(this.ctx)
35+
}
36+
}
1537
}
1638
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ if (container) {
1515
container.appendChild(canvas)
1616

1717
const board = new Board(ctx)
18-
board.draw()
18+
board.init()
1919
}

0 commit comments

Comments
 (0)