Skip to content

Commit 4a60cf3

Browse files
Merge branch 'main' into feat/add_size_update
2 parents c35ad5c + 2c61ea0 commit 4a60cf3

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

src/Board/Board.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Cell } from "~/Cell"
2+
3+
export class Board {
4+
_board: Cell[][] = []
5+
_rows: number = 100
6+
_cols: number = 100
7+
ctx: CanvasRenderingContext2D
8+
9+
constructor(ctx: CanvasRenderingContext2D) {
10+
this.ctx = ctx
11+
}
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+
}
29+
30+
drawBoard() {
31+
this.ctx.beginPath()
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+
}
37+
}
38+
}

src/Cell.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
11
type CellColor = 'red' | 'green' | 'blue' | 'yellow' | 'none';
22

3-
class CellAgent {
4-
private _cell: Cell;
3+
export class Cell {
4+
private _color: CellColor = 'blue';
5+
private _x: number;
6+
private _y: number;
7+
private _size: number;
58

6-
constructor(cell: Cell) {
7-
this._cell = cell;
9+
constructor( x: number, y: number, size: number = 20) {
10+
this._x = x;
11+
this._y = y;
12+
this._size = size;
813
}
914

10-
public get cell(): Cell {
11-
return this._cell;
15+
public get color(): CellColor {
16+
return this._color;
1217
}
1318

14-
public set cell(cell: Cell) {
15-
this._cell = cell;
19+
public set color(color: CellColor) {
20+
this._color = color;
1621
}
17-
}
1822

19-
export class Cell {
20-
private _color: CellColor;
21-
private _agent: CellAgent | null;
23+
public get x(): number {
24+
return this._x;
25+
}
2226

23-
constructor(color: CellColor = 'none', agent: CellAgent | null = null) {
24-
this._color = color;
25-
this._agent = agent;
27+
public set x(x: number) {
28+
this._x = x;
2629
}
2730

28-
public get color(): CellColor {
29-
return this._color;
31+
public get y(): number {
32+
return this._y;
3033
}
3134

32-
public set color(color: CellColor) {
33-
this._color = color;
35+
public set y(y: number) {
36+
this._y = y;
37+
}
38+
39+
public get size(): number {
40+
return this._size;
3441
}
3542

36-
public get agent(): CellAgent | null {
37-
return this._agent;
43+
public set size(size: number) {
44+
this._size = size;
3845
}
3946

40-
public set agent(agent: CellAgent | null) {
41-
this._agent = agent;
47+
public draw(ctx: CanvasRenderingContext2D): void {
48+
ctx.fillStyle = this._color;
49+
ctx.fillRect(this._x, this._y, this._size, this._size);
4250
}
4351
}

src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sizeUpdater } from './components/sizeUpdater'
2+
import { Board } from "./Board/Board"
23

34
const container = document.getElementById('app')
45

@@ -7,7 +8,11 @@ if (container) {
78
container.appendChild(sizeUpdaterElement)
89

910
const canvas = document.createElement('canvas')
11+
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
1012
canvas.width = 500
1113
canvas.height = 500
1214
container.appendChild(canvas)
15+
16+
const board = new Board(ctx)
17+
board.init()
1318
}

0 commit comments

Comments
 (0)