Interactive
Tic Tac Toe
Full two-player game with board rendering and win detection. Click Run, then type your moves (1-9) and press Enter.
examples/tictactoe.bubble-- ╔══════════════════════════════════════╗-- ║ 🫧 Tic Tac Toe in Bubble 🫧 ║-- ║ Two-player terminal game ║-- ╚══════════════════════════════════════╝ -- The board is a list of 9 cells.-- Empty cells hold "." and are displayed as their position number. on showBoard board say "" put "" into row1 put "" into row2 put "" into row3 repeat with i from 1 to 3 put itemof(i, board) into c if c is "." then put text(i) into c end if put row1 & " " & padright(c, 1) into row1 if i < 3 then put row1 & " |" into row1 end if end repeat repeat with i from 4 to 6 put itemof(i, board) into c if c is "." then put text(i) into c end if put row2 & " " & padright(c, 1) into row2 if i < 6 then put row2 & " |" into row2 end if end repeat repeat with i from 7 to 9 put itemof(i, board) into c if c is "." then put text(i) into c end if put row3 & " " & padright(c, 1) into row3 if i < 9 then put row3 & " |" into row3 end if end repeat say " " & row1 say " ---+---+---" say " " & row2 say " ---+---+---" say " " & row3 say ""end showBoard function checkWin board, mark -- Rows if itemof(1, board) is mark and itemof(2, board) is mark and itemof(3, board) is mark then return true end if if itemof(4, board) is mark and itemof(5, board) is mark and itemof(6, board) is mark then return true end if if itemof(7, board) is mark and itemof(8, board) is mark and itemof(9, board) is mark then return true end if -- Columns if itemof(1, board) is mark and itemof(4, board) is mark and itemof(7, board) is mark then return true end if if itemof(2, board) is mark and itemof(5, board) is mark and itemof(8, board) is mark then return true end if if itemof(3, board) is mark and itemof(6, board) is mark and itemof(9, board) is mark then return true end if -- Diagonals if itemof(1, board) is mark and itemof(5, board) is mark and itemof(9, board) is mark then return true end if if itemof(3, board) is mark and itemof(5, board) is mark and itemof(7, board) is mark then return true end if return falseend checkWin function boardFull board repeat with i from 1 to 9 if itemof(i, board) is "." then return false end if end repeat return trueend boardFull -- ── Main Game ────────────────────────── clearsay "╔══════════════════════════════════╗"say "║ 🫧 TIC TAC TOE 🫧 ║"say "╠══════════════════════════════════╣"say "║ Player 1 = X Player 2 = O ║"say "║ Enter 1-9 to place your mark. ║"say "╚══════════════════════════════════╝" put [".", ".", ".", ".", ".", ".", ".", ".", "."] into boardput "X" into currentMarkput 1 into currentPlayerput false into gameOver repeat until gameOver is true showBoard(board) say " Player" && currentPlayer && "(" & currentMark & "), your turn." ask " Pick a square (1-9):" into move put number(move) into cell if cell < 1 or cell > 9 then say " ⚠ Pick a number from 1 to 9!" wait 1 clear next repeat end if if itemof(cell, board) is "X" or itemof(cell, board) is "O" then say " ⚠ That square is already taken!" wait 1 clear next repeat end if put currentMark into item cell of board if checkWin(board, currentMark) then clear showBoard(board) say " 🎉 Player" && currentPlayer && "(" & currentMark & ") wins!" put true into gameOver else if boardFull(board) then clear showBoard(board) say " 🤝 It's a draw!" put true into gameOver else if currentMark is "X" then put "O" into currentMark put 2 into currentPlayer else put "X" into currentMark put 1 into currentPlayer end if clear end if end repeat say ""say " Thanks for playing! 🫧"