Skip to content

Commit

Permalink
cmd/tictactoe: Make board clickable only when needed.
Browse files Browse the repository at this point in the history
Make board cells clickable only during the time when the player whose
turn it currently is is a CellClicker.
  • Loading branch information
dmitshur committed Oct 10, 2017
1 parent 37e9c2d commit 72dce7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
34 changes: 20 additions & 14 deletions cmd/tictactoe/display.go
Expand Up @@ -14,6 +14,7 @@ import (
type page struct {
board ttt.Board
turn ttt.State
clickable bool
condition ttt.Condition
errorMessage string
players [2]player
Expand Down Expand Up @@ -47,7 +48,7 @@ func (p page) Render() []*html.Node {
// Board.
style(
`display: inline-block; margin-left: 30px; margin-right: 30px;`,
htmlg.Span(board{Board: p.board}.Render()...),
htmlg.Span(board{Board: p.board, Clickable: p.clickable}.Render()...),
),
// Player O.
style(
Expand All @@ -68,6 +69,7 @@ func (p page) Render() []*html.Node {
// board renders a board.
type board struct {
ttt.Board
Clickable bool
}

func (b board) Render() []*html.Node {
Expand All @@ -76,7 +78,7 @@ func (b board) Render() []*html.Node {
tr := &html.Node{Data: atom.Tr.String(), Type: html.ElementNode}
for col, cell := range b.Cells[3*row : 3*row+3] {
td := &html.Node{Data: atom.Td.String(), Type: html.ElementNode}
htmlg.AppendChildren(td, boardCell{State: cell, Index: 3*row + col}.Render()...)
htmlg.AppendChildren(td, boardCell{State: cell, Clickable: b.Clickable, Index: 3*row + col}.Render()...)
tr.AppendChild(td)
}
table.AppendChild(tr)
Expand All @@ -89,22 +91,26 @@ func (b board) Render() []*html.Node {
// boardCell renders a board cell.
type boardCell struct {
ttt.State
Index int
Clickable bool
Index int
}

func (c boardCell) Render() []*html.Node {
cell := &html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Style.String(), Val: "cursor: pointer;"},
{Key: atom.Onclick.String(), Val: fmt.Sprintf("CellClick(%d);", c.Index)},
},
FirstChild: style(
`display: table-cell; width: 30px; height: 30px; text-align: center; vertical-align: middle; background-color: #f4f4f4;`,
htmlg.Div(
htmlg.Text(c.String()),
),
cell := style(
`display: table-cell; width: 30px; height: 30px; text-align: center; vertical-align: middle; background-color: #f4f4f4;`,
htmlg.Div(
htmlg.Text(c.String()),
),
)
if c.Clickable {
cell = &html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Style.String(), Val: `display: block; cursor: pointer;`},
{Key: atom.Onclick.String(), Val: fmt.Sprintf(`CellClick(%d);`, c.Index)},
},
FirstChild: cell,
}
}
return []*html.Node{cell}
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/tictactoe/game.go
Expand Up @@ -32,17 +32,18 @@ func playGame(players [2]player) (ttt.Condition, error) {
var board ttt.Board
var condition ttt.Condition

for i := 0; condition == ttt.NotEnd; i++ {
for i := 0; condition == ttt.NotEnd; i = (i + 1) % 2 {
fmt.Println()
fmt.Println(board)
if runtime.GOARCH == "js" {
var document = dom.GetWindow().Document().(dom.HTMLDocument)
document.Body().SetInnerHTML(htmlg.Render(page{board: board, turn: players[i%2].Mark, condition: condition, players: players}.Render()...))
_, isCellClicker := players[i].Player.(ttt.CellClicker)
document.Body().SetInnerHTML(htmlg.Render(page{board: board, turn: players[i].Mark, clickable: isCellClicker, condition: condition, players: players}.Render()...))
}

turnStart := time.Now()

err := playerTurn(&board, players[i%2], cellClick)
err := playerTurn(&board, players[i], cellClick)
if err != nil {
if runtime.GOARCH == "js" {
var document = dom.GetWindow().Document().(dom.HTMLDocument)
Expand Down

0 comments on commit 72dce7c

Please sign in to comment.