avatar

21

interface

21 is a PHONK implementation of the 21 game

ui.background(0, 0, 0)

var sum = 0
var turn = 0

const txt = ui.addText(sum, .2, .2, .6, .6)
txt.props.textAlign = 'center'
txt.props.textSize = 64

function reset () {
  util.delay(2000, function () {
    sum = 0
    txt.text(sum)
  })
}

function add (player, n) {
  const btn = ui.addButton(
    n, (.8 * player), (.33 * (n - 1)), .2, .33
  ).onClick(() => {
    if (turn !== player) {
      ui.toast('it\'s not your turn')
      return
    }

    sum += n
    turn = +!player
    txt.text(sum)

    if (sum === 21) {
      ui.toast('player ' + (player + 1) + ' wins')
      reset()
    } else if (sum >= 21) {
      ui.toast('draw')
      reset()
    }
  })

  btn.props.background = '#000000'
  btn.props.backgroundPressed = '#000000'
}

add(0, 1)
add(0, 2)
add(0, 3)
add(1, 1)
add(1, 2)
add(1, 3)
21 source code