Dinfio Playground

Let's play with Dinfio!




CalmoSoft Sixteen Puzzle Game

CalmoSoft Sixteen Puzzle Game is a classic 16-puzzle. Sixteen is visually similar to Fifteen but with a different type of move. This time, there is no hole: all 16 squares on the grid contain numbered squares. Your goal is to shift an entire row left or right, or shift an entire column up or down; every time you do that, the tile you shift off the grid re-appears at the other end of the same row, in the space you just vacated. To win, arrange the tiles into numerical order (1, 2, 3, 4 on the top row, 13, 14, 15, 16 on the bottom).

Author: CalmoSoft

' CalmoSoft Sixteen Puzzle Game
' Author: CalmoSoft
 
import math, gui, string
 
start
    global movenr = 0
    create_gui()
stop
 
function create_gui()
    global window
 
    global button = []
    global button_scramble, button_reset
    global button_up1, button_up2, button_up3, button_up4
    global button_down1, button_down2, button_down3, button_down4
    global button_left1, button_left2, button_left3, button_left4
    global button_right1, button_right2, button_right3, button_right4
    global textbox
 
    global btn = []
 
    window = gui_window("CalmoSoft Sixteen Puzzle Game", 270, 424)
 
    for row, 1, 4
        for col, 1, 4
            item = (row - 1) * 4 + col        
            btn[item] = item
            button[item] = button_alt(str(item), window, col * 40 + 6, row * 40 + 6, 40, 40)
        endfor
    endfor
 
    button_scramble = gui_button("Scramble", window, 46, 264, 160, 40)
    button_scramble.addevent(event.click, scramble())
    button_reset = gui_button("Reset", window, 46, 304, 160, 40)			
    button_reset.addevent(event.click, reset())
 
    button_up1 = gui_button("up", window, 46, 0, 40, 40)
    button_up1.addevent(event.click, up1())
    button_up2 = gui_button("up", window, 86, 0, 40, 40)
    button_up2.addevent(event.click, up2())
    button_up3 = gui_button("up", window, 126, 0, 40, 40)
    button_up3.addevent(event.click, up3())
    button_up4 = gui_button("up", window, 166, 0, 40, 40)
    button_up4.addevent(event.click, up4())	
 
    button_down1 = gui_button("down", window, 46, 212, 40, 40)
    button_down1.addevent(event.click, down1())
    button_down2 = gui_button("down", window, 86, 212, 40, 40)
    button_down2.addevent(event.click, down2())
    button_down3 = gui_button("down", window, 126, 212, 40, 40)
    button_down3.addevent(event.click, down3())
    button_down4 = gui_button("down", window, 166, 212, 40, 40)
    button_down4.addevent(event.click, down4())	
 
    button_left1 = gui_button("<<<", window, 0, 46, 40, 40)
    button_left1.addevent(event.click, left1())
    button_left2 = gui_button("<<<", window, 0, 86, 40, 40)
    button_left2.addevent(event.click, left2())
    button_left3 = gui_button("<<<", window, 0, 126, 40, 40)
    button_left3.addevent(event.click, left3())
    button_left4 = gui_button("<<<", window, 0, 166, 40, 40)
    button_left4.addevent(event.click, left4())
 
    button_right1 = gui_button(">>>", window, 212, 46, 40, 40)
    button_right1.addevent(event.click, right1())
    button_right2 = gui_button(">>>", window, 212, 86, 40, 40)
    button_right2.addevent(event.click, right2())
    button_right3 = gui_button(">>>", window, 212, 126, 40, 40)
    button_right3.addevent(event.click, right3())
    button_right4 = gui_button(">>>", window, 212, 166, 40, 40)
    button_right4.addevent(event.click, right4())
 
    textbox = gui_textbox("", window, 46, 344, 160, 20)
 
    textbox.settext("Moves = " & movenr)
    textbox.setlocked(true)
 
    window.show()
    button_scramble.setfocus()
stop
 
class button_alt: gui_button
    function construct(label, parent, x, y, width, height)
        extend(this, gui_button(label, parent, x, y, width, height))
    stop
 
    function settext(text)
        this.gui_button.settext(str(text))
    stop
endclass
 
function scramble()
    var used = array(16)
 
    for i, 0, 16
        used[i] = false
    endfor
 
    for row, 1, 4 
        for col, 1, 4 
            rand = randomint(1, 16)
 
            while used[rand]
                rand = randomint(1, 16)
            endwhile
 
            used[rand] = true
            item = (row - 1) * 4 + col
            btn[item] = rand
 
            button[item].settext(str(btn[item]))
        endfor
    endfor
 
    movenr = 0
    textbox.settext("Moves = "  & movenr)
stop
 
function reset()
    for row, 1, 4 
        for col, 1, 4 
            item = (row - 1) * 4 + col
            button[item].settext(str(item))
            btn[item] = item
        endfor
    endfor
 
    movenr = 0
    textbox.settext("Moves = "  & movenr)
stop
 
function up1()
    temp =  button[1].gettext()
 
    button[1].settext(btn[5])
    btn[1] = btn[5]
    button[5].settext(btn[9])
    btn[5] = btn[9]		   
    button[9].settext(btn[13])
    btn[9] = btn[13]
    button[13].settext(temp)
    btn[13] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
 stop
 
function up2()
    temp =  button[2].gettext()
 
    button[2].settext(btn[6])
    btn[2] = btn[6]
    button[6].settext(btn[10])
    btn[6] = btn[10]		   
    button[10].settext(btn[14])
    btn[10] = btn[14]
    button[14].settext(temp)
    btn[14] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function up3()
    temp =  button[3].gettext()
 
    button[3].settext(btn[7])
    btn[3] = btn[7]
    button[7].settext(btn[11])
    btn[7] = btn[11]		   
    button[11].settext(btn[15])
    btn[11] = btn[15]
    button[15].settext(temp)
    btn[15] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function up4()
    temp =  button[4].gettext()
 
    button[4].settext(btn[8])
    btn[4] = btn[8]
    button[8].settext(btn[12])
    btn[8] = btn[12]		   
    button[12].settext(btn[16])
    btn[12] = btn[16]
    button[16].settext(temp)
    btn[16] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function down1()
    temp =  button[13].gettext()
 
    button[13].settext(btn[9])
    btn[13] = btn[9]
    button[9].settext(btn[5])
    btn[9] = btn[5]		   
    button[5].settext(btn[1])
    btn[5] = btn[1]
    button[1].settext(temp)
    btn[1] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function down2()
    temp =  button[14].gettext()
 
    button[14].settext(btn[10])
    btn[14] = btn[10]
    button[10].settext(btn[6])
    btn[10] = btn[6]		   
    button[6].settext(btn[2])
    btn[6] = btn[2]
    button[2].settext(temp)
    btn[2] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function down3()
    temp =  button[15].gettext()
 
    button[15].settext(btn[11])
    btn[15] = btn[11]
    button[11].settext(btn[7])
    btn[11] = btn[7]		   
    button[7].settext(btn[3])
    btn[7] = btn[3]
    button[3].settext(temp)
    btn[3] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function down4()
    temp =  button[16].gettext()
 
    button[16].settext(btn[12])
    btn[16] = btn[12]
    button[12].settext(btn[8])
    btn[12] = btn[8]		   
    button[8].settext(btn[4])
    btn[8] = btn[4]
    button[4].settext(temp)
    btn[4] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function left1()
    temp =  button[1].gettext()
 
    button[1].settext(btn[2])
    btn[1] = btn[2]
    button[2].settext(btn[3])
    btn[2] = btn[3]		   
    button[3].settext(btn[4])
    btn[3] = btn[4]
    button[4].settext(temp)
    btn[4] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function left2()
    temp =  button[5].gettext()
 
    button[5].settext(btn[6])
    btn[5] = btn[5]
    button[6].settext(btn[7])
    btn[6] = btn[7]		   
    button[7].settext(btn[8])
    btn[7] = btn[8]
    button[8].settext(temp)
    btn[8] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function left3()
    temp =  button[9].gettext()
 
    button[9].settext(btn[10])
    btn[9] = btn[10]
    button[10].settext(btn[11])
    btn[10] = btn[11]		   
    button[11].settext(btn[12])
    btn[11] = btn[12]
    button[12].settext(temp)
    btn[12] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function left4()
    temp =  button[13].gettext()
 
    button[13].settext(btn[14])
    btn[13] = btn[14]
    button[14].settext(btn[15])
    btn[14] = btn[15]		   
    button[15].settext(btn[16])
    btn[15] = btn[16]
    button[16].settext(temp)
    btn[16] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function right1()
    temp =  button[4].gettext()
 
    button[4].settext(btn[3])
    btn[4] = btn[3]
    button[3].settext(btn[2])
    btn[3] = btn[2]		   
    button[2].settext(btn[1])
    btn[2] = btn[1]
    button[1].settext(temp)
    btn[1] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function right2()
    temp =  button[8].gettext()
 
    button[8].settext(btn[7])
    btn[8] = btn[7]
    button[7].settext(btn[6])
    btn[7] = btn[6]		   
    button[6].settext(btn[5])
    btn[6] = btn[5]
    button[5].settext(temp)
    btn[5] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function right3()
    temp =  button[12].gettext()
 
    button[12].settext(btn[11])
    btn[12] = btn[11]
    button[11].settext(btn[10])
    btn[11] = btn[10]		   
    button[10].settext(btn[9])
    btn[10] = btn[9]
    button[9].settext(temp)
    btn[9] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop
 
function right4()
    temp =  button[16].gettext()
 
    button[16].settext(btn[15])
    btn[16] = btn[15]
    button[15].settext(btn[14])
    btn[15] = btn[14]		   
    button[14].settext(btn[13])
    btn[14] = btn[13]
    button[13].settext(temp)
    btn[13] = temp
 
    movenr = movenr + 1
    textbox.settext("Moves = "  & movenr)
stop

Screenshots:


← Back to the Dinfio Playground / Download this program