Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tic-Tac-Toe
#1
Here's a classic text-based Tic Tac Toe game — pure nostalgia, no graphics, just clean text and old-school terminal vibes.  And yes — the computer is intentionally easy to beat (as promised) :-)

Code:
' ==========================
' TIC TAC TOE
' Human     (Player 1) = X
' Computer  (Player 2) = O
' ==========================

randomize clock()         ' Initialize random numbers

visible board   =   dim(3, 3)
visible row
visible col
visible player
visible moveCount
visible win

player = 1
moveCount = 0

' Initialize board
for row = 0 to 2
    for col = 0 to 2
        board[row][col] = " "
    next
next


' MAIN GAME loop
do
    DrawBoard()

    if player = 1 then
        ' HUMAN TURN
        pln
        pln "Your turn (X)"
        write "Enter row (0-2): ";
        row = rln(1,TYPE_NUMBER)
        write "Enter column (0-2): ";
        col = rln(1,TYPE_NUMBER)

        if row < 0 or row > 2 or col < 0 or col > 2 then
            pln "Invalid position!"
            wait 3000
        elseif board[row][col] <> " " then
            pln "That spot is taken!"
            wait 3000
        else
            board[row][col] = "X"
            moveCount = moveCount + 1
            CheckWinner()
            if win = 1 then GameOver()
            player = 2
        endif

    else
        ' COMPUTER TURN .... no AI :)
        pln
        write "Computer is thinking..."
        wait 3000

        do
            row = round(rnd() * 2)
            col = round(rnd() * 2)
        until board[row][col] = " "
        pln "["+row+","+col+"]"

        board[row][col] = "O"
        moveCount = moveCount + 1
        CheckWinner()
        if win = 1 then GameOver()
        player = 1
    endif

until moveCount = 9

' DRAW GAME
DrawBoard()
pln
pln "It's a DRAW!"
system("pause")
end


'------------
' FUNCTIONS
'------------
function DrawBoard()
    pln
    pln "   TIC TAC TOE"
    pln
    pln "    0   1   2 "
    pln "  +---+---+---+"
    for row = 0 to 2
        pln row+" | "+ board[row][0]+ " | "+ board[row][1]+ " | "+ board[row][2]+ " | "
        if row < 3 then pln "  +---+---+---+"
    next
endfunc

function CheckWinner()
    win = 0
   
    ' Rows
    for row = 0 to 2
        if board[row][0] <> " " and
           board[row][0] = board[row][1] and
           board[row][1] = board[row][2] then win = 1
    next
   
    ' Columns
    for col = 0 to 2
        if board[0][col] <> " " and
           board[0][col] = board[1][col] and
           board[1][col] = board[2][col] then win = 1
    next
   
    ' Diagonals
    if board[0][0] <> " " and
       board[0][0] = board[1][1] and
       board[1][1] = board[2][2] then win = 1
   
    if board[0][2] <> " " and
       board[0][2] = board[1][1] and
       board[1][1] = board[2][0] then win = 1
endfunc

function GameOver()
    DrawBoard()
    pln

    if player = 1 then
        pln "YOU WIN!"
    else
        pln "COMPUTER WINS!"
    endif
   
    system("pause")
   
    end
endfunc
Reply
#2
Nicely done, thank you. Would be fun to build in some more logic for the computer move, but quite challenging to get the level of "intelligence" right - I think it would be easier to make the computer unbeatable, which wouldn't be much fun Smile
Reply
#3
(01-05-2026, 12:55 PM)kevin Wrote: Nicely done, thank you. Would be fun to build in some more logic for the computer move, but quite challenging to get the level of "intelligence" right - I think it would be easier to make the computer unbeatable, which wouldn't be much fun Smile

If you're into text-based games, you might enjoy Star Trek N7 too, check it out here  https://naalaa.com/forum/thread-149-post...ml#pid1084 

I'm just a bit too lazy to implement an unbeatable strategy Big Grin for the computer ...hope this new version make the computer unbeatable .... lol
Code:
' ==========================
' TIC TAC TOE
' Human (X) vs Computer (O)
' ==========================

randomize clock()

visible board
visible row
visible col
visible player
visible moveCount
visible win
visible playAgain

' Outer loop for replay
do
    board = dim(3, 3)
    for row = 0 to 2
        for col = 0 to 2
            board[row][col] = " "
        next
    next
   
    player = 1
    moveCount = 0
    win = 0

    ' MAIN GAME loop
    do
        DrawBoard()

        if player = 1 then
            ' HUMAN TURN
            pln
            pln "Your turn (X)"
            write "Enter row (0-2): ";
            row = rln(1, TYPE_NUMBER)
            write "Enter column (0-2): ";
            col = rln(1, TYPE_NUMBER)

            if row < 0 or row > 2 or col < 0 or col > 2 then
                pln "Invalid position!"
                wait 2000
            elseif board[row][col] <> " " then
                pln "That spot is taken!"
                wait 2000
            else
                board[row][col] = "X"
                moveCount = moveCount + 1
                CheckWinner()
                if win = 1 then
                    GameOver()
                    break
                endif
                player = 2
            endif

        else
            ' ===  COMPUTER TURN  ===
            pln
            write "Computer is thinking..."
            wait 600

            found = 0

            ' STEP 1: WIN
            if found = 0 then
                for tryRow = 0 to 2
                    for tryCol = 0 to 2
                        if board[tryRow][tryCol] = " " and found = 0 then
                            board[tryRow][tryCol] = "O"
                            CheckWinner()
                            if win = 1 then
                                row = tryRow
                                col = tryCol
                                found = 1
                            endif
                            board[tryRow][tryCol] = " "
                        endif
                    next
                next
            endif

            ' STEP 2: BLOCK
            if found = 0 then
                for tryRow = 0 to 2
                    for tryCol = 0 to 2
                        if board[tryRow][tryCol] = " " and found = 0 then
                            board[tryRow][tryCol] = "X"
                            CheckWinner()
                            if win = 1 then
                                row = tryRow
                                col = tryCol
                                found = 1
                            endif
                            board[tryRow][tryCol] = " "
                        endif
                    next
                next
            endif

            ' STEP 3: BLOCK FORKS
            if found = 0 then
                if board[0][0] = "X" and board[2][2] = "X" and board[1][1] = "O" then
                    if board[0][1] = " " then
                        row = 0
                        col = 1
                        found = 1
                    endif
                    if found = 0 and board[1][0] = " " then
                        row = 1
                        col = 0
                        found = 1
                    endif
                    if found = 0 and board[1][2] = " " then
                        row = 1
                        col = 2
                        found = 1
                    endif
                    if found = 0 and board[2][1] = " " then
                        row = 2
                        col = 1
                        found = 1
                    endif
                endif
            endif

            if found = 0 then
                if board[0][2] = "X" and board[2][0] = "X" and board[1][1] = "O" then
                    if board[0][1] = " " then
                        row = 0
                        col = 1
                        found = 1
                    endif
                    if found = 0 and board[1][0] = " " then
                        row = 1
                        col = 0
                        found = 1
                    endif
                    if found = 0 and board[1][2] = " " then
                        row = 1
                        col = 2
                        found = 1
                    endif
                    if found = 0 and board[2][1] = " " then
                        row = 2
                        col = 1
                        found = 1
                    endif
                endif
            endif

            if found = 0 then
                if board[0][0] = "X" and board[0][2] = "X" and board[1][1] = "O" then
                    if board[0][1] = " " then
                        row = 0
                        col = 1
                        found = 1
                    endif
                endif
            endif

            if found = 0 then
                if board[0][0] = "X" and board[2][0] = "X" and board[1][1] = "O" then
                    if board[1][0] = " " then
                        row = 1
                        col = 0
                        found = 1
                    endif
                endif
            endif

            ' STEP 4: CENTER
            if found = 0 then
                if board[1][1] = " " then
                    row = 1
                    col = 1
                    found = 1
                endif
            endif

            ' STEP 5: OPPOSITE CORNER
            if found = 0 then
                if board[1][1] = "O" then
                    if board[0][0] = "X" and board[2][2] = " " then
                        row = 2
                        col = 2
                        found = 1
                    endif
                    if found = 0 and board[0][2] = "X" and board[2][0] = " " then
                        row = 2
                        col = 0
                        found = 1
                    endif
                    if found = 0 and board[2][0] = "X" and board[0][2] = " " then
                        row = 0
                        col = 2
                        found = 1
                    endif
                    if found = 0 and board[2][2] = "X" and board[0][0] = " " then
                        row = 0
                        col = 0
                        found = 1
                    endif
                endif
            endif

            ' STEP 6: CORNERS
            if found = 0 then
                if board[0][0] = " " then
                    row = 0
                    col = 0
                    found = 1
                endif
                if found = 0 and board[0][2] = " " then
                    row = 0
                    col = 2
                    found = 1
                endif
                if found = 0 and board[2][0] = " " then
                    row = 2
                    col = 0
                    found = 1
                endif
                if found = 0 and board[2][2] = " " then
                    row = 2
                    col = 2
                    found = 1
                endif
            endif

            ' STEP 7: SIDES
            if found = 0 then
                if board[0][1] = " " then
                    row = 0
                    col = 1
                    found = 1
                endif
                if found = 0 and board[1][0] = " " then
                    row = 1
                    col = 0
                    found = 1
                endif
                if found = 0 and board[1][2] = " " then
                    row = 1
                    col = 2
                    found = 1
                endif
                if found = 0 and board[2][1] = " " then
                    row = 2
                    col = 1
                    found = 1
                endif
            endif

            ' STEP 8: FINAL SCAN
            if found = 0 then
                for r = 0 to 2
                    for c = 0 to 2
                        if board[r][c] = " " and found = 0 then
                            row = r
                            col = c
                            found = 1
                        endif
                    next
                next
            endif

            pln " ? [" + row + "," + col + "]"

            board[row][col] = "O"
            moveCount = moveCount + 1
            CheckWinner()
            if win = 1 then
                GameOver()
                break
            endif
            player = 1
        endif

    until moveCount = 9

    ' DRAW
    if win = 0 then
        DrawBoard()
        pln
        pln "It's a DRAW! (Perfectly played!)"
    endif

    pln
    write "Play again? (Y = yes, N = no): "
    do
        playAgain = upper(rln(1))
    until playAgain = "Y" or playAgain = "N"

until playAgain = "N"

pln "Thanks for playing "
end


'------------
' UTILITIES
'------------

function DrawBoard()
    cls
    pln
    pln "   TIC TAC TOE"
    pln
    pln "    0   1   2 "
    pln "  +---+---+---+"
    for row = 0 to 2
        pln row + " | " + board[row][0] + " | " + board[row][1] + " | " + board[row][2] + " | "
        pln "  +---+---+---+"
    next
endfunc


function CheckWinner()
    win = 0
   
    ' Rows
    for row = 0 to 2
        if board[row][0] <> " " and
           board[row][0] = board[row][1] and
           board[row][1] = board[row][2] then win = 1
    next
   
    ' Columns
    for col = 0 to 2
        if board[0][col] <> " " and
           board[0][col] = board[1][col] and
           board[1][col] = board[2][col] then win = 1
    next
   
    ' Diagonals
    if board[0][0] <> " " and
       board[0][0] = board[1][1] and
       board[1][1] = board[2][2] then win = 1
   
    if board[0][2] <> " " and
       board[0][2] = board[1][1] and
       board[1][1] = board[2][0] then win = 1
endfunc


function GameOver()
    DrawBoard()
    pln
    if player = 1 then
        pln "YOU WIN! (Bug report appreciated!)"
    else
        pln "COMPUTER WINS — optimal play."
    endif
endfunc
Reply
#4
Yes, 4 games, 4 draws.....I would say that this is unbeatable Smile
Reply
#5
Well done!

I am reminded of the 1983 movie, War Games.... "Shall we play a game?" lol
Logic is the beginning of wisdom.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)