vi studio

Lessons · Examples · 33 of 36

Example: tic-tac-toe

A two-player game with a 3×3 grid and a scoreboard.

The Tic-tac-toe template. src/game.vi knows whose turn it is and the score; src/components/board.vi draws the 3 × 3 board with repeat: 3, 3 and checks for a winner by reading the cells with cell.at(x, y).

src/main.vi
src/config.vi
src/game.vi
src/theme.vi
src/components/board.vi
src/components/scoreboard.vi

src/main.vi

<# Tic-tac-toe for two players on one phone.
   config.vi    app name, icon and splash screen
   theme.vi     colours
   game.vi      whose turn it is and the score
   components/  the board and the scoreboard #>

main home:
  color: page
  space_inside: top bottom 24, left right 20
  space_between: 20
  children:
    header
    scoreboard
    board
    status_line
    new_game_button

  header:
    size: max, auto
    space_between: 4
    children:
      eyebrow
      title

    eyebrow:
      text: "TWO PLAYERS"
      text_style: 13, bold, violet

    title:
      text: "Tic-tac-toe"
      text_style: 30, bold, ink

  status_line:
    size: max, auto
    text: status()
    text_style: 22, bold, ink
    arrange: center

  new_game_button:
    button: "New game"
    size: max, auto
    color: violet
    on_tap: new_game()

src/config.vi

<# App settings: name, version, launcher icon, splash screen and screen sizes.
   Every file in src/ can see them, so nothing needs importing.
   Before your first store build, add a store id that never changes again:
     id: com.yourname.myapp #>
config:
  name: "{{app_name}}"
  version: "1.0.0"
  icon: @icon.png
  text_size: follow_phone
  splash:
    color: white
    logo: @splash.png
    time: 1 second
  screens:
    mobile: 0 to 600
    tablet: 600 to 1200
    desktop: 1200 to max

src/game.vi

<# Whose turn it is, who won, and the running score. No screens here. #>
current_player = "X"
game_over = false
winner = ""
x_wins = 0
o_wins = 0
draws = 0

<# Called once when a game ends: "X", "O" or "Draw" #>
finish(result):
  game_over = true
  winner = result
  if (result == "X"):
    x_wins = x_wins + 1
  else if (result == "O"):
    o_wins = o_wins + 1
  else:
    draws = draws + 1

next_turn():
  current_player = "O" if (current_player == "X") else "X"

status():
  if (not game_over):
    return "{current_player}'s turn"
  if (winner == "Draw"):
    return "It's a draw"
  return "{winner} wins!"

src/theme.vi

<# The app's colours, in one place. Change one here and every screen follows. #>
violet = rgb(109, 74, 255)
violet_soft = rgb(237, 233, 255)
coral = rgb(255, 122, 89)
coral_soft = rgb(255, 236, 230)
ink = rgb(28, 25, 46)
muted = rgb(124, 120, 145)
page = rgb(247, 246, 252)
cell_color = rgb(242, 240, 250)

src/components/board.vi

<# The 3 × 3 board. Each cell is one copy of `cell` (repeat: 3, 3);
   board.at(x, y) finds one by column and row. #>
board:
  size: max, auto
  color: white
  shape: rounded
  shadow: true
  arrange: center
  space_inside: top bottom 14
  children:
    cell

cell:
  repeat: 3, 3
  size: 28
  color: cell_color
  shape: rounded
  text: ""
  text_style: 46, bold
  arrange: center
  space_around: 5
  on_tap: make_move(cell)

make_move(target):
  if (not game_over and target.text == ""):
    target:
      text: current_player
      text_style: violet if (current_player == "X") else coral
    check_winner()
    if (not game_over):
      next_turn()

<# Same mark three times in a line? #>
line(a, b, c):
  return a != "" and a == b and b == c

check_winner():
  A = cell.at(0, 0).text
  B = cell.at(1, 0).text
  C = cell.at(2, 0).text
  D = cell.at(0, 1).text
  E = cell.at(1, 1).text
  F = cell.at(2, 1).text
  G = cell.at(0, 2).text
  H = cell.at(1, 2).text
  I = cell.at(2, 2).text
  if (line(A, B, C) or line(A, D, G) or line(A, E, I)):
    finish(A)
  else if (line(D, E, F) or line(B, E, H) or line(C, E, G)):
    finish(E)
  else if (line(G, H, I) or line(C, F, I)):
    finish(I)
  else:
    all_filled = true
    for c in cell.children:
      if (c.text == ""):
        all_filled = false
    if (all_filled):
      finish("Draw")

new_game():
  for c in cell.children:
    c:
      text: ""
  current_player = "X"
  game_over = false
  winner = ""

src/components/scoreboard.vi

<# Three small cards: X's wins, draws, O's wins.
   The card of the player whose turn it is lights up. #>
scoreboard:
  size: max, auto
  arrange: row, center
  space_between: 10
  children:
    score_card("X", x_wins, violet, violet_soft)
    score_card("Draws", draws, muted, white)
    score_card("O", o_wins, coral, coral_soft)

score_card(label, value, accent, glow):
  size: 30, auto
  color: glow if (label == current_player and not game_over) else white
  shape: rounded
  shadow: true
  arrange: center
  space_inside: top bottom 12
  children:
    score_value(value, accent)
    score_label(label)

score_value(value, accent):
  text: "{value}"
  text_style: 26, bold, accent

score_label(label):
  text: label
  text_style: 13, semibold, muted

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.

More in Examples

About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.