Dinfio Playground

Let's play with Dinfio!




3x3 Tic-Tac-Toe Game

This is the Computer vs Human 3x3 Tic-Tac-Toe game example.

' Tic Tac Toe Game
 
import math, gui
 
start
    global box = array(9)
    global random_pos
    global wins, loses, draws
 
    create_gui()
    new_game()
stop
 
function create_gui()
    global gui_window: window = gui_window("Tic Tac Toe", gui_window_fixed, 400, 280)
 
    window.disablemaximize()
    window.movetocenter()
    window.show()
 
    global gui_canvas: canvas     = array(9)
    global gui_label: label       = array(9)
    global gui_label: label_wins  = gui_label("Win: 0", window, 220, 20, 150, 40)
    global gui_label: label_loses = gui_label("Lose: 0", window, 220, 45, 150, 40)
    global gui_label: label_draws = gui_label("Drawn: 0", window, 220, 71, 150, 40)
 
    canvas[1] = gui_canvas(window, 20, 20, 60, 60)
    canvas[2] = gui_canvas(window, 79, 20, 60, 60)
    canvas[3] = gui_canvas(window, 138, 20, 60, 60)
    canvas[4] = gui_canvas(window, 20, 79, 60, 60)
    canvas[5] = gui_canvas(window, 79, 79, 60, 60)
    canvas[6] = gui_canvas(window, 138, 79, 60, 60)
    canvas[7] = gui_canvas(window, 20, 138, 60, 60)
    canvas[8] = gui_canvas(window, 79, 138, 60, 60)
    canvas[9] = gui_canvas(window, 138, 138, 60, 60)
 
    for i, 1, 9
        canvas[i].setbackgroundcolor(gui_color_white)
 
        label[i] = gui_label("", window, 0, 18, 60, 60)
        label[i].setparent(canvas[i])
        label[i].setbackgroundcolor(gui_color_white)
        label[i].setalignment(gui_alignment_center)
        label[i].setfontsize(16)
        label[i].setfontbold(true)
 
        canvas[i].addevent(gui_onclick, "player_click(" & i & ")")
        label[i].addevent(gui_onclick, "player_click(" & i & ")")
    endfor
 
    label_wins.setfontsize(14)
    label_loses.setfontsize(14)
    label_draws.setfontsize(14)
stop
 
function new_game()
    wins = 0; loses = 0; draws = 0
    reset_board()
stop
 
function reset_board()
    random_pos = randomint(1, 3)
 
    for i, 1, 9
        fill_box(i, "")
    endfor
stop
 
function fill_box(i, xo)
    box[i] = xo
 
    if xo == "x"
        label[i].settext("X")
        label[i].setforegroundcolor(hex("00c0c0"))
    elseif xo == "o"
        label[i].settext("O")
        label[i].setforegroundcolor(hex("f2880a"))
    else
        label[i].settext("")
        label[i].setforegroundcolor(gui_color_black)
    endif
stop
 
function player_click(i)
    if box[i] != ""
        return
    else
        fill_box(i, "x")
        check()
    endif
stop
 
function check()
    if is_player_won()
        player_won()
    else
        if !computer_attacks()
            if !computer_defenses()
                computer_use_random()
            endif
        endif
        if is_computer_won()
            computer_won()
        elseif is_drawn()
            game_drawn()
        endif
    endif
stop
 
function player_won()
    messagebox("Horay! You win!", "Tic Tac Toe", gui_messagebox_info)
 
    wins += 1
    label_wins.settext("Win: " & wins)
 
    reset_board()
stop
 
function computer_won()
    messagebox("Ouch! You lose.", "Tic Tac Toe", gui_messagebox_warning)
 
    loses += 1
    label_loses.settext("Lose: " & loses)
 
    reset_board()
stop
 
function game_drawn()
    messagebox("Hmmm, game is drawn.", "Tic Tac Toe", gui_messagebox_info)
 
    draws += 1
    label_draws.settext("Drawn: " & draws)
 
    reset_board()
stop
 
function is_player_won()
    if box[1] == "x" && box[2] == "x" && box[3] == "x"
        return true
    elseif box[1] == "x" && box[4] == "x" && box[7] == "x"
        return true
    elseif box[1] == "x" && box[5] == "x" && box[9] == "x"
        return true
    elseif box[2] == "x" && box[5] == "x" && box[8] == "x"
        return true
    elseif box[3] == "x" && box[5] == "x" && box[7] == "x"
        return true
    elseif box[3] == "x" && box[6] == "x" && box[9] == "x"
        return true
    elseif box[4] == "x" && box[5] == "x" && box[6] == "x"
        return true
    elseif box[7] == "x" && box[8] == "x" && box[9] == "x"
        return true
    endif
 
    return false
stop
 
function is_computer_won()
    if box[1] == "o" && box[2] == "o" && box[3] == "o"
        return true
    elseif box[1] == "o" && box[4] == "o" && box[7] == "o"
        return true
    elseif box[1] == "o" && box[5] == "o" && box[9] == "o"
        return true
    elseif box[2] == "o" && box[5] == "o" && box[8] == "o"
        return true
    elseif box[3] == "o" && box[5] == "o" && box[7] == "o"
        return true
    elseif box[3] == "o" && box[6] == "o" && box[9] == "o"
        return true
    elseif box[4] == "o" && box[5] == "o" && box[6] == "o"
        return true
    elseif box[7] == "o" && box[8] == "o" && box[9] == "o"
        return true
    endif
 
    return false
stop
 
function is_drawn()
    if box[1] != "" && box[2] != "" && box[3] != "" && box[4] != "" && box[5] != "" && box[6] != "" && box[7] != "" && box[8] != "" && box[9] != ""
        return true
    endif
 
    return false
stop
 
function computer_attacks()
    if     box[1] == "o" && box[2] == "o" && box[3] == ""; fill_box(3, "o"); return true
    elseif box[1] == "o" && box[3] == "o" && box[2] == ""; fill_box(2, "o"); return true
    elseif box[2] == "o" && box[3] == "o" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[4] == "o" && box[5] == "o" && box[6] == ""; fill_box(6, "o"); return true
    elseif box[4] == "o" && box[6] == "o" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "o" && box[6] == "o" && box[4] == ""; fill_box(4, "o"); return true
 
    elseif box[7] == "o" && box[8] == "o" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[7] == "o" && box[9] == "o" && box[8] == ""; fill_box(8, "o"); return true
    elseif box[8] == "o" && box[9] == "o" && box[7] == ""; fill_box(7, "o"); return true
 
    elseif box[1] == "o" && box[4] == "o" && box[7] == ""; fill_box(7, "o"); return true
    elseif box[1] == "o" && box[7] == "o" && box[4] == ""; fill_box(4, "o"); return true
    elseif box[4] == "o" && box[7] == "o" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[2] == "o" && box[5] == "o" && box[8] == ""; fill_box(8, "o"); return true
    elseif box[2] == "o" && box[8] == "o" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "o" && box[8] == "o" && box[2] == ""; fill_box(2, "o"); return true
 
    elseif box[3] == "o" && box[6] == "o" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[3] == "o" && box[9] == "o" && box[6] == ""; fill_box(6, "o"); return true
    elseif box[6] == "o" && box[9] == "o" && box[3] == ""; fill_box(3, "o"); return true
 
    elseif box[1] == "o" && box[5] == "o" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[1] == "o" && box[9] == "o" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "o" && box[9] == "o" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[3] == "o" && box[5] == "o" && box[7] == ""; fill_box(7, "o"); return true
    elseif box[3] == "o" && box[7] == "o" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "o" && box[7] == "o" && box[3] == ""; fill_box(3, "o"); return true; endif
 
    return false
stop
 
function computer_defenses()
    if     box[1] == "x" && box[2] == "x" && box[3] == ""; fill_box(3, "o"); return true
    elseif box[1] == "x" && box[3] == "x" && box[2] == ""; fill_box(2, "o"); return true
    elseif box[2] == "x" && box[3] == "x" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[4] == "x" && box[5] == "x" && box[6] == ""; fill_box(6, "o"); return true
    elseif box[4] == "x" && box[6] == "x" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "x" && box[6] == "x" && box[4] == ""; fill_box(4, "o"); return true
 
    elseif box[7] == "x" && box[8] == "x" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[7] == "x" && box[9] == "x" && box[8] == ""; fill_box(8, "o"); return true
    elseif box[8] == "x" && box[9] == "x" && box[7] == ""; fill_box(7, "o"); return true
 
    elseif box[1] == "x" && box[4] == "x" && box[7] == ""; fill_box(7, "o"); return true
    elseif box[1] == "x" && box[7] == "x" && box[4] == ""; fill_box(4, "o"); return true
    elseif box[4] == "x" && box[7] == "x" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[2] == "x" && box[5] == "x" && box[8] == ""; fill_box(8, "o"); return true
    elseif box[2] == "x" && box[8] == "x" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "x" && box[8] == "x" && box[2] == ""; fill_box(2, "o"); return true
 
    elseif box[3] == "x" && box[6] == "x" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[3] == "x" && box[9] == "x" && box[6] == ""; fill_box(6, "o"); return true
    elseif box[6] == "x" && box[9] == "x" && box[3] == ""; fill_box(3, "o"); return true
 
    elseif box[1] == "x" && box[5] == "x" && box[9] == ""; fill_box(9, "o"); return true
    elseif box[1] == "x" && box[9] == "x" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "x" && box[9] == "x" && box[1] == ""; fill_box(1, "o"); return true
 
    elseif box[3] == "x" && box[5] == "x" && box[7] == ""; fill_box(7, "o"); return true
    elseif box[3] == "x" && box[7] == "x" && box[5] == ""; fill_box(5, "o"); return true
    elseif box[5] == "x" && box[7] == "x" && box[3] == ""; fill_box(3, "o"); return true; endif
 
    return false
stop
 
function computer_use_random()
    if random_pos == 1
        if     box[5] == ""; fill_box(5, "o"); return true
        elseif box[1] == ""; fill_box(1, "o"); return true
        elseif box[9] == ""; fill_box(9, "o"); return true
        elseif box[7] == ""; fill_box(7, "o"); return true
        elseif box[2] == ""; fill_box(2, "o"); return true
        elseif box[6] == ""; fill_box(6, "o"); return true
        elseif box[3] == ""; fill_box(3, "o"); return true
        elseif box[8] == ""; fill_box(8, "o"); return true
        elseif box[4] == ""; fill_box(4, "o"); return true; endif
    elseif random_pos == 2
        if     box[5] == ""; fill_box(5, "o"); return true
        elseif box[1] == ""; fill_box(1, "o"); return true
        elseif box[9] == ""; fill_box(9, "o"); return true
        elseif box[8] == ""; fill_box(8, "o"); return true
        elseif box[2] == ""; fill_box(2, "o"); return true
        elseif box[6] == ""; fill_box(6, "o"); return true
        elseif box[3] == ""; fill_box(3, "o"); return true
        elseif box[7] == ""; fill_box(7, "o"); return true
        elseif box[4] == ""; fill_box(4, "o"); return true; endif
    elseif random_pos == 3
        if     box[5] == ""; fill_box(5, "o"); return true
        elseif box[1] == ""; fill_box(1, "o"); return true
        elseif box[9] == ""; fill_box(9, "o"); return true
        elseif box[6] == ""; fill_box(6, "o"); return true
        elseif box[2] == ""; fill_box(2, "o"); return true
        elseif box[8] == ""; fill_box(8, "o"); return true
        elseif box[3] == ""; fill_box(3, "o"); return true
        elseif box[7] == ""; fill_box(7, "o"); return true
        elseif box[4] == ""; fill_box(4, "o"); return true; endif
    endif
stop

Screenshots:



Requirement: Dinfio 3.0.xx or older

← Back to the Dinfio Playground / Download this program