Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
66 lines (56 sloc) 1.92 KB
import &ArtSuite # 2 tokens >> 228 total tokens
textStyle = # 10 tokens
fontFamily: "" "Century Gothic", Futura, sans-serif
fontSize: 14
margin: 10
size: :childrenSize
calculateWinner = (board) -> # 60 tokens
find [i1, i2, i3] in []
0 1 2
3 4 5
6 7 8
0 3 6
1 4 7
2 5 8
0 4 8
2 4 6
v1 = board[i1] && v1 == board[i2] && v1 == board[i3] # << -4 tokens with chain-equality support: board[i1] == board[i2] == board[i3] != null
class Square extends Component # 28 tokens
render: ->
Element
size: 35
draw: outline: :black
on: pointerClick: @props.play
TextElement
textStyle
fontWeight: :bold
fontSize: 18
size: :parentSize
align: :centerCenter
@props
class TicTacToe extends Component # 128 tokens
@stateFields turnNumber: 0 history: [] array til 9 with null
play: (player, boardIndex) -> # 28 tokens
@history = arrayWith
@history.slice 0, @turnNumber + 1 # -3 tokens if we add "in + to" support: array in @history to @turnNumber
arrayWithElementReplaced @history[@turnNumber++], player, boardIndex
render: -> # 85 tokens
winner = calculateWinner board = @history[@turnNumber]
player = if @turnNumber % 2 then :X else :O
Element
size: w: 125
padding: 10
childrenLayout: :flow
array text, boardIndex in board
Square {} text, play: unless winner || text
-> @play player, boardIndex
TextElement
textStyle
if winner then "Winner: " + winner
else "Next player: " + player
array {label}, i in @history
TextElement
textStyle
on: pointerClick: -> @turnNumber = i
if i then "Go to move #" + i
else "Go to game start"