Skip to content

Commit 5086152

Browse files
committed
Refactoring and Board class
1 parent 4bca6c4 commit 5086152

File tree

9 files changed

+85
-16
lines changed

9 files changed

+85
-16
lines changed

flow-typed/go-lib.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ declare module 'go-lib' {
1212
coordinate: Coordinate,
1313
color: Color,
1414
}
15+
16+
declare type BoardStatus = Color | 'EMPTY'
1517
}

src/board.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @flow
2+
3+
import type { BoardStatus } from 'go-lib'
4+
import * as util from './util'
5+
import { EMPTY } from './constants'
6+
7+
type BoardData = Array<BoardStatus>
8+
9+
class Board {
10+
data: BoardData = []
11+
boardSize: number
12+
13+
constructor(boardSize: number) {
14+
this.boardSize = boardSize
15+
this.data = util.range(boardSize * boardSize).map(() => EMPTY)
16+
}
17+
18+
clear() {
19+
this.data = this.data.map(() => EMPTY)
20+
}
21+
22+
setStatus(x: number, y: number, status: BoardStatus) {
23+
const boardIndex = this.boardSize * x + y
24+
25+
this.data = this.data.map((value, i) => (i === boardIndex ? status : value))
26+
}
27+
28+
getStatus(x: number, y: number): BoardStatus {
29+
const boardIndex = this.boardSize * x + y
30+
31+
return this.data[boardIndex]
32+
}
33+
}
34+
35+
export default Board

src/board.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Board from './board'
2+
import * as util from './util'
3+
import { EMPTY, WHITE } from './constants'
4+
5+
describe('Board', () => {
6+
it('should clear the board', () => {
7+
const expected = util.range(9 * 9).map(() => EMPTY)
8+
const board = new Board(9)
9+
board.clear()
10+
11+
expect(board.data).toEqual(expected)
12+
})
13+
14+
it('should set und get a boardstatus correctly', () => {
15+
const board = new Board(9)
16+
board.setStatus(3, 3, WHITE)
17+
18+
expect(board.getStatus(3, 3)).toBe(WHITE)
19+
expect(board.getStatus(3, 4)).toBe(EMPTY)
20+
})
21+
})

src/components/util/board-drawer.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import type { Move, Coordinate } from 'go-lib'
4+
import { BLACK } from '../../constants'
45

56
const LINE_WIDTH_FACTOR = 20
67
const FONT_SIZE_FACTOR = 2.5
@@ -84,17 +85,19 @@ class BoardDrawer {
8485
ctx.lineWidth = lineWidth * 2
8586

8687
moves.forEach((move, index) => {
88+
const moveX = move.coordinate.x + 1
89+
const moveY = move.coordinate.y + 1
8790
const isLastMove = index === moves.length - 1
88-
const fillStyle = move.color === 'B'
91+
const fillStyle = move.color === BLACK
8992
? colorBlack(opacity)
9093
: colorWhite(opacity)
9194

9295
// Draw stone
9396
ctx.fillStyle = fillStyle
9497
ctx.beginPath()
9598
ctx.arc(
96-
move.coordinate.x * boxSize,
97-
move.coordinate.y * boxSize,
99+
moveX * boxSize,
100+
moveY * boxSize,
98101
boxSize / 2 - lineWidth,
99102
0,
100103
2 * Math.PI,
@@ -106,20 +109,14 @@ class BoardDrawer {
106109

107110
// Mark last move
108111
if (isLastMove && !isHover) {
109-
const strokeStyle = move.color === 'B'
112+
const strokeStyle = move.color === BLACK
110113
? colorWhite(opacity)
111114
: colorBlack(opacity)
112115

113116
ctx.beginPath()
114117
ctx.strokeStyle = strokeStyle
115118
ctx.lineWidth = lineWidth
116-
ctx.arc(
117-
move.coordinate.x * boxSize,
118-
move.coordinate.y * boxSize,
119-
boxSize / 3.5,
120-
0,
121-
2 * Math.PI,
122-
)
119+
ctx.arc(moveX * boxSize, moveY * boxSize, boxSize / 3.5, 0, 2 * Math.PI)
123120
ctx.stroke()
124121
}
125122
})
@@ -196,8 +193,8 @@ class BoardDrawer {
196193

197194
// calculateCoordinateFromMousePosition convert's a canvas mouse position to a board coordinate
198195
calculateCoordinateFromMousePosition(mousePosition: Coordinate): Coordinate {
199-
const x = Math.round(mousePosition.x / this.boxSize)
200-
const y = Math.round(mousePosition.y / this.boxSize)
196+
const x = Math.round(mousePosition.x / this.boxSize) - 1
197+
const y = Math.round(mousePosition.y / this.boxSize) - 1
201198

202199
return { x, y }
203200
}

src/components/util/board-drawer.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ describe('BoardDrawer', () => {
7777
mousePosition,
7878
)
7979

80-
expect(coordinate).toEqual({ x: 10, y: 10 })
80+
expect(coordinate).toEqual({ x: 9, y: 9 })
8181
})
8282
})

src/components/util/canvas.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import type { Coordinate } from 'go-lib'
44

5+
// calculateCanvasPosition takes clientX/Y and returns the relative clientX/Y of the canvas
56
export const calculateCanvasPosition = (
67
canvas: HTMLCanvasElement,
78
x: number,

src/constants.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @flow
2+
3+
import type { BoardStatus } from 'go-lib'
4+
5+
export const PASS = 'PASS'
6+
7+
export const WHITE: BoardStatus = 'W'
8+
9+
export const BLACK: BoardStatus = 'B'
10+
11+
export const EMPTY: BoardStatus = 'EMPTY'

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @flow
22

33
import * as components from './components'
4+
import Board from './board'
5+
import Zobrist from './zobrist'
46

5-
export { components }
7+
export { components, Board, Zobrist }

src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// @flow
22

3-
export type { Coordinate, Move } from 'go-lib'
3+
export type { Coordinate, Move, BoardStatus } from 'go-lib'

0 commit comments

Comments
 (0)