|
Post by alexfish on May 10, 2013 19:57:15 GMT 1
Hey Joe
This produced
myrand = 1-------------- 5 4 12 12 12 0 4 0 7 11 9 14 9 12 13
now need method to get rid of duplicates , until no duplicates , so may have to run rand a few times.
Nice work & only 6 lines of code
BR
Alex
|
|
|
Post by bigbass on May 10, 2013 21:04:22 GMT 1
Hey Alex being random is just that but we can make a condition that the next number in the list is never a direct duplication of the last one
no two numbers are the same in series
@alex P.S nice job putting it in an array
'--- set up for compiling embedded library GTK PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- random number generator shamelessly stolen from a game :D '--- the number % 15 makes number 0-15 you can change the 15 '------------------------------------------------- myrand = abs(g_rand_int ( g_rand_new () )) % 15; '--------------------------------------------------
PRINT "myrand = " ,myrand
PRINT "--------------"
'--- maybe a loop is needed FOR i = 1 TO 15 newrand = myrand myrand = abs(g_rand_int ( g_rand_new () )) % 15; IF newrand != myrand THEN PRINT myrand END IF NEXT i
|
|
|
Post by vovchik on May 10, 2013 22:25:36 GMT 1
|
|
|
Post by vovchik on May 10, 2013 23:22:50 GMT 1
This is an example:
DECLARE tiles[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } TYPE int SEED NOW
' ------------------ SUB SHUFFLE_TILES(int squares[]) ' ------------------ LOCAL j, k, temp TYPE int FOR j = 15 TO 0 STEP -1 k = RANDOM(16) temp = squares[j] squares[j] = squares[k] squares[k] = temp NEXT j END SUB
' test of ten tile shuffles FOR j = 0 TO 9 SHUFFLE_TILES(tiles) PRINT j, ". "; FOR i = 0 TO 15 PRINT tiles[i], " "; NEXT i PRINT NEXT j PRINT
|
|
|
Post by alexfish on May 10, 2013 23:39:55 GMT 1
Dear Vovchik Me thinks U been on Phone to Peter but Looks like a winner . But since U be one of the Authors , Lose one Karma point Plus a Further Punishment of 24 hrs Community Forum Service , Task :: Implement This Code with That Button + shuffle the Tiles , Bugs will Not Be Allowed ;D DECLARE tiles[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } TYPE int SEED NOW
' ------------------ SUB SHUFFLE_TILES(int squares[]) ' ------------------ LOCAL j, k, temp TYPE int FOR j = 15 TO 0 STEP -1 k = RANDOM(16) temp = squares[j] squares[j] = squares[k] squares[k] = temp NEXT j END SUB
' test of ten tile shuffles FOR j = 0 TO 9 SHUFFLE_TILES(tiles) PRINT j, ". "; FOR i = 0 TO 15 PRINT tiles[i], " "; NEXT i PRINT NEXT j PRINT Have Fun , see you Soon Alex Thought I would post some of the Results 0. 2 0 14 8 4 3 13 5 10 7 15 11 12 9 1 6 1. 6 12 0 7 14 13 3 5 15 4 2 10 8 11 1 9 2. 1 8 9 13 12 3 6 10 4 15 0 2 7 5 14 11 3. 9 2 13 14 7 5 15 8 4 6 3 10 11 12 1 0 4. 8 13 7 2 5 6 4 3 1 15 9 14 11 0 12 10 5. 12 14 7 4 9 5 11 15 0 13 10 2 8 6 1 3 6. 9 6 4 13 1 7 3 2 8 14 0 5 15 11 10 12 7. 13 7 2 12 4 0 6 9 14 5 8 15 3 11 1 10 8. 11 1 12 3 10 7 13 0 5 2 14 8 9 4 6 15 9. 6 14 15 5 3 8 9 2 0 7 12 10 11 4 13 1
alexfish@alexfish-desktop:~/Desktop/bacon/Bacon2$ ./RAND3 0. 13 12 6 10 15 7 2 0 3 11 8 4 1 9 5 14 1. 1 0 4 6 8 12 14 7 11 10 9 2 15 5 13 3 2. 14 1 0 6 5 3 10 9 8 7 2 11 4 13 15 12 3. 13 8 10 4 14 0 12 6 7 15 9 2 5 11 1 3 4. 4 11 13 8 3 0 15 9 14 1 10 2 12 6 7 5 5. 2 0 11 5 10 8 14 9 13 3 4 12 15 7 1 6 6. 13 4 14 9 11 12 15 1 5 7 6 0 10 2 8 3 7. 10 14 11 12 4 5 15 1 8 7 6 3 13 9 0 2 8. 3 7 4 15 8 0 5 9 10 12 14 11 13 2 1 6 9. 13 0 2 3 10 11 12 15 7 4 9 5 14 6 8 1
alexfish@alexfish-desktop:~/Desktop/bacon/Bacon2$ ./RAND3 0. 12 9 13 15 5 0 10 11 8 1 2 3 7 6 14 4 1. 11 7 2 1 10 15 13 3 9 8 4 0 5 6 14 12 2. 10 1 8 12 15 7 11 3 9 5 14 13 0 6 2 4 3. 4 0 2 15 1 9 12 10 3 7 11 14 6 13 8 5 4. 3 4 12 9 5 11 15 1 2 14 13 7 0 6 8 10 5. 14 0 11 1 8 4 10 7 9 13 6 2 15 3 5 12 6. 12 0 15 7 6 10 4 8 13 2 1 5 11 9 3 14 7. 3 4 2 14 12 5 15 8 11 13 6 0 10 9 7 1 8. 3 10 13 12 14 7 9 6 15 8 2 4 0 5 11 1 9. 15 2 10 6 14 0 3 13 4 9 7 12 5 1 8 11 PS :: This code looks Like a Candidate Just had time to review Latest 15 , Looking Good '***************************************************** ' ************************************************************** ' PROGRAM: PROGRAM: Fifteen : Goo Canvas Demo ' PURPOSE: goocanvas animation demo ' AUTHORS: .org alexfish (BaCon Converter Forum) ' .org vovchik(Puppy Linux Forums) ' DATE: 06-05-2013 :: 11-05-2015 ' LICENSE: GLP3 ' ************************************************************** '*****************************************************
' *********************** ' INCLUDES ' ***********************
INCLUDE "hug.bac"
' *********************** ' END INCLUDES ' ***********************
' *********************** ' INITIALIZATION ' ***********************
' lib cairo CONST cairo$ = "libcairo.so" ' hug options HUGOPTIONS("NOSCALING") HUGOPTIONS("CANVAS GOO") ' some goo cavas widgets properties need ' to have declares set as TYPE CONST GTK_ANCHOR_CENTER = 0 DECLARE PIECE_SIZE TYPE double PIECE_SIZE = 50 DECLARE color$[16] DECLARE sets[5][5] DECLARE colour ASSOC STRING DECLARE selwidget ASSOC NUMBER DECLARE i TYPE NUMBER DECLARE rect[16] TYPE NUMBER DECLARE text[16] TYPE NUMBER ' set the rect array to 0 FOR t = 0 TO 15 rect[t] = 0 NEXT t ' ----- ' define colors ' ----- color$[ 0] = "white" color$[ 1] = "#56FF00" color$[ 2] = "#43C102" color$[ 3] = "#43AA0E" ' ----- color$[ 4] = "#368E08" color$[ 5] = "#FF4D00" color$[ 6] = "#D14C13" color$[ 7] = "#B24110" ' ----- color$[ 8] = "#933811" color$[ 9] = "#4100FF" color$[10] = "#4312D2" color$[11] = "#4722B2" ' ----- color$[12] = "#22037A" color$[13] = "#FAF80D" color$[14] = "#E7E60F" color$[15] = "#C9C80C"
' *********************** ' END INITIALIZATION ' ***********************
' *********************** ' IMPORTS ' ***********************
' goo IMPORT "goo_canvas_item_set_child_properties(long,long,...)" FROM HUGLIB$("goo") TYPE void IMPORT "goo_canvas_table_new(long,...)" FROM HUGLIB$("goo") TYPE long IMPORT "goo_canvas_text_new(long,char*,double,double,double,int,...)" FROM HUGLIB$("goo") TYPE long ' cairo/pango IMPORT "cairo_pattern_create_radial(double,double,double,double,double,double)" FROM cairo$ TYPE long ' (double cx0, double cy0,double radius0,double cx1,double cy1,double radius1) IMPORT "cairo_pattern_create_linear(double,double,double,double)" FROM cairo$ TYPE long ' (double x0,double y0,double x1,double y1) IMPORT "cairo_pattern_add_color_stop_rgba(long,double,double,double,double,double)" FROM cairo$ TYPE void ' (cairo_pattern_t *pattern,double offset,double red,double green,double blue,double alpha)
' *********************** ' END IMPORTS ' ***********************
' *********************** ' SUBS & FUNCTIONS ' ***********************
' ------------------ SUB SELECTED(long widget, NUMBER event, NUMBER textdat) ' ------------------ ' find the white first FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 wigmove = sets[y][x] st$ = colour(STR$(wigmove)) IF st$ = "white" THEN tmpx = x tmpy = y i_first = 1 tempwigmove = wigmove row_up = x - 1 row_down = x + 1 col_left = y - 1 col_right = y + 1 BREAK END IF NEXT i ' ----- ' main checking FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 b = sets[y][x] IF b = widget THEN pos_x = x pos_y = y i_second = i BREAK END IF NEXT i ' ----- ' check squares row_up = pos_x - 1 row_down = pos_x + 1 col_left = pos_y - 1 col_right = pos_y + 1 IF (row_up > -1 ) & (row_up < 3) THEN b = sets[pos_y][row_up] st$ = colour(STR$(b)) IF st$ = "white" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], "row", \ pos_x - 1, "column", pos_y, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y][pos_x - 1] = widget END IF END IF ' ----- IF (row_down > 0 ) & (row_down < 4) THEN b = sets[pos_y][row_down] st$ = colour(STR$(b)) IF st$ = "white" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x + 1, "column", pos_y, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y][pos_x + 1] = widget END IF END IF ' ----- IF (col_left > -1) & (col_left < 3) THEN b = sets[col_left][pos_x] st$ = colour(STR$(b)) IF st$ = "white" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x, "column", pos_y - 1, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y - 1][pos_x] = widget END IF END IF ' ----- IF (col_right > 0 ) & (col_right < 4) THEN b = sets[col_right][pos_x] st$ = colour(STR$(b)) IF st$ = "white" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the Numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x, "column", pos_y + 1, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y + 1][pos_x] = widget END IF END IF END SUB
' ------------------ FUNCTION CREATE_CANVAS_FIFTEEN() ' ------------------ LOCAL text$ TYPE char LOCAL linear ' create the table and add tiles + text numbers table = goo_canvas_table_new(root_canvas1, "row-spacing", 1.0, \ "column-spacing", 1.0, NULL) ' create ciaro gradient and then set value in goo rect radial = cairo_pattern_create_radial(30, 30, 10, 30, 30, 90) cairo_pattern_add_color_stop_rgba(radial, 0, 1, 1, 1, 1); cairo_pattern_add_color_stop_rgba(radial, 1, 0.6, 0.6, 0.6, 1) FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 ' make the tiles from rectangles rect[i] = goo_canvas_rect_new(table, 0, 0, PIECE_SIZE, PIECE_SIZE, \ "radius-x", 6.0, "radius-y", 6.0, "stroke-color", "black", \ "line-width" , 1.0, "fill_pattern", radial, NULL) ' set the item in the table goo_canvas_item_set_child_properties (table, rect[i] , "row", \ x, "column", y, NULL) NEXT i FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 ' add TEXT as number text[i] = goo_canvas_text_new(table, STR$(i), 0, 0, PIECE_SIZE, \ GTK_ANCHOR_CENTER, "font", "Sans bold 24", "fill-color", \ color$[i], NULL) ' use markup for y-axis text positioning g_object_set(text[i], "use-markup", TRUE, 0) ' fix vertical centring via span g_object_set(text[i], "text", CONCAT$("<span rise='-37000'>", \ STR$(i), "</span>"), 0) ' set the height of the text; this property is not set by default g_object_set(text[i], "height", PIECE_SIZE, NULL) ' assoc the text with the number selwidget(STR$(text[i])) = i ' assoc the text with its color colour(STR$(text[i])) = color$[i] ' get the text and print it g_object_get(text[i], "text", ADDRESS(text$), NULL) PRINT "TEXT : ", text$ ' set the item in the table goo_canvas_item_set_child_properties(table, text[i] , "row", x, \ "column", y, NULL) ' store the text in array sets[y][x] = text[i] ' goo canvas items can have callbacks, so here set the callback ' for the TEXT; the sub is 'SELECTED' g_signal_connect_data(text[i], "button-press-event", SELECTED, 0, 0, 0) NEXT i ' set first tile to no text so it cannot be seen g_object_set(text[0], "text", "", NULL) RETURN TRUE END FUNCTION
' ------------------ SUB MK_GUI() ' ------------------ win = WINDOW("Fifteen", 207, 247) canvas1 = CANVAS(207, 207) ATTACH(win, canvas1, 0, 0) ' the canvas id is the ebox = hug_widget_image(STR$(ebox)) root_canvas1 = goo_canvas_get_root_item(hug_widget_image(STR$(canvas1))) ' create a goo canvas table + add tiles and numbers fifteen = CREATE_CANVAS_FIFTEEN() but1 = BUTTON("New", 65, 30) PROPERTY(but1, "tooltip-text", "Start a new game.") ATTACH(win, but1, 0, 212) but2 = BUTTON("Quit", 65, 30) PROPERTY(but2, "tooltip-text", "Quit this game.") ATTACH(win, but2, 142, 212) CALLBACK(but2, QUIT) END SUB
' *********************** ' END SUBS & FUNCTIONS ' ***********************
' *********************** ' MAIN ' ***********************
MK_GUI DISPLAY
' *********************** ' END MAIN ' *********************** Appolls for delays , Keep getting Time outs / Server
|
|
|
Post by Pjot on May 11, 2013 19:52:46 GMT 1
Thanks Alex, Looks nice! Note that the whole random approach of placing tiles may lead to an unresolvable situation as is explained here. In my own 15 Puzzle I do not use your random approach. Instead, the program moves tiles in random directions from the solved situation back to a shuffled start position. This ensures that the puzzle always can be solved. BR, Peter
|
|
|
Post by alexfish on May 11, 2013 21:24:22 GMT 1
Thanks Alex, Looks nice! Note that the whole random approach of placing tiles may lead to an unresolvable situation as is explained here. In my own 15 Puzzle I do not use your random approach. Instead, the program moves tiles in random directions from the solved situation back to a shuffled start position. This ensures that the puzzle always can be solved. BR, Peter Comment was = not necessary Hence my previous comment ' Think Peter Will Win This one" ;D Looks like it still stands Goo Canvas and I await a solotion BR Alex
|
|
|
Post by vovchik on May 11, 2013 23:57:13 GMT 1
Dear guys,
This seems to check for valid -i.e. solvable - layouts:
DECLARE tiles[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } TYPE int SEED NOW
' ------------------ FUNCTION SOLVABLE(int a[]) ' ------------------ LOCAL i, j, inv, result TYPE int FOR i = 0 TO 15 IF a[i] = 0 THEN inv = FLOOR((i + 3) / 4) END IF NEXT i FOR i = 0 TO 14 FOR j = i + 1 TO 15 IF (a[j] <> 0) AND (a[j] < a[i]) THEN inv = inv + 1 END IF NEXT j NEXT i IF MOD(inv, 2) <> 0 THEN RETURN FALSE ELSE RETURN TRUE END IF END FUNCTION
' ------------------ SUB SHUFFLE_TILES(int squares[]) ' ------------------ LOCAL j, k, temp TYPE int FOR j = 15 TO 0 STEP -1 k = RANDOM(16) temp = squares[j] squares[j] = squares[k] squares[k] = temp NEXT j END SUB
' test FOR j = 0 TO 9 SHUFFLE_TILES(tiles) PRINT j, ". "; FOR i = 0 TO 15 PRINT tiles[i], " "; NEXT i PRINT " --- Solvable: ", SOLVABLE(tiles) NEXT j PRINT END
With kind regards, vovchik
|
|
|
Post by alexfish on May 12, 2013 1:55:22 GMT 1
|
|
|
Post by vovchik on May 12, 2013 9:30:39 GMT 1
Dear Alex (et. al), Here is fifteens with error checking, meaning that only solvable tile layouts are presented (I hope) . With kind regards, vovchik ' ************************************************************** ' PROGRAM: PROGRAM: Fifteen : Goo Canvas Demo ' PURPOSE: goocanvas animation demo ' AUTHORS: .org alexfish (BaCon Converter Forum) ' .org vovchik(Puppy Linux Forums) ' DATE: 06-05-2013 ' LICENSE: GLP3 ' **************************************************************
' *********************** ' INCLUDES ' ***********************
INCLUDE "hug.bac"
' *********************** ' END INCLUDES ' ***********************
' *********************** ' INITIALIZATION ' ***********************
' lib cairo CONST cairo$ = "libcairo.so" ' hug options HUGOPTIONS("NOSCALING") HUGOPTIONS("CANVAS GOO") ' some goo cavas widgets properties need ' to have declares set as TYPE CONST GTK_ANCHOR_CENTER = 0 DECLARE PIECE_SIZE TYPE double PIECE_SIZE = 50 DECLARE wins[16] DECLARE color$[16] DECLARE sets[5][5] DECLARE colour ASSOC STRING DECLARE selwidget ASSOC NUMBER DECLARE i TYPE NUMBER DECLARE rect[16] TYPE NUMBER DECLARE text[16] TYPE NUMBER DECLARE tiles[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } TYPE int ' set the rect array to 0 FOR t = 0 TO 15 rect[t] = 0 wins[t]=t+1 NEXT t wins[15]=0 MOVES = 0 ' ----- ' define colors ' ----- color$[ 0] = "#FFFFFF" color$[ 1] = "#56FF00" color$[ 2] = "#43C102" color$[ 3] = "#43AA0E" ' ----- color$[ 4] = "#368E08" color$[ 5] = "#FF4D00" color$[ 6] = "#D14C13" color$[ 7] = "#B24110" ' ----- color$[ 8] = "#933811" color$[ 9] = "#4100FF" color$[10] = "#4312D2" color$[11] = "#4722B2" ' ----- color$[12] = "#22037A" color$[13] = "#FAF80D" color$[14] = "#E7E60F" color$[15] = "#C9C80C"
' *********************** ' END INITIALIZATION ' ***********************
' *********************** ' IMPORTS ' ***********************
' goo IMPORT "goo_canvas_item_set_child_properties(long,long,...)" FROM HUGLIB$("goo") TYPE void IMPORT "goo_canvas_table_new(long,...)" FROM HUGLIB$("goo") TYPE long IMPORT "goo_canvas_text_new(long,char*,double,double,double,int,...)" FROM HUGLIB$("goo") TYPE long ' cairo/pango IMPORT "cairo_pattern_create_radial(double,double,double,double,double,double)" FROM cairo$ TYPE long IMPORT "cairo_pattern_add_color_stop_rgba(long,double,double,double,double,double)" FROM cairo$ TYPE void
' *********************** ' END IMPORTS ' ***********************
' *********************** ' SUBS & FUNCTIONS ' ***********************
' ------------------ FUNCTION SOLVABLE(int a[]) ' ------------------ LOCAL i, j, inv, result TYPE int FOR i = 0 TO 15 IF a[i] = 0 THEN inv = FLOOR((i + 3) / 4) END IF NEXT i FOR i = 0 TO 14 FOR j = i + 1 TO 15 IF (a[j] <> 0) AND (a[j] < a[i]) THEN inv = inv + 1 END IF NEXT j NEXT i IF MOD(inv, 2) <> 0 THEN RETURN FALSE ELSE RETURN TRUE END IF END FUNCTION
' ------------------ SUB SHUFFLE_TILES(int squares[]) ' ------------------ MOVES = 0 LOCAL text$ TYPE char LOCAL j, k, temp TYPE int FOR j = 15 TO 0 STEP -1 k = RANDOM(16) temp = squares[j] squares[j] = squares[k] squares[k] = temp NEXT j FOR j = 15 TO 0 STEP -1 y = j / 4 x = j % 4 goo_canvas_item_set_child_properties (table, text[tiles[j]] , "row", x, "column", y, NULL) sets[y][x] = text[tiles[j]] NEXT j PRINT END SUB
' ------------------ SUB CHECK_WIN() ' ------------------ ' check for win + gives total moves ckwin = 0 FOR j = 0 TO 15 y = j / 4 x = j % 4 IF (selwidget(STR$(sets[x][y])) = wins[j] ) THEN INCR ckwin END IF NEXT j ' here are the total moves + if winnner PRINT " Check correct :", ckwin PRINT "TOTAL MOVES :", MOVES TEXT(moves, "<span font='Sans Bold 10' color='red'>" & STR$(MOVES) & "</span>") IF ckwin = 16 THEN TEXT(winner, "<span color='red'><b>Well done!</b>" & \ NL$ & "You managed to win in " & NL$ & STR$(MOVES) \ & " moves.</span>") SHOW(winner) PRINT "WINNER" END IF END SUB
' ------------------ SUB SELECTED(long widget, NUMBER event, NUMBER textdat) ' ------------------ checkmove = 0 ' find the white first FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 wigmove = sets[y][x] st$ = colour(STR$(wigmove)) IF st$ = "#FFFFFF" THEN tmpx = x tmpy = y i_first = 1 tempwigmove = wigmove row_up = x - 1 row_down = x + 1 col_left = y - 1 col_right = y + 1 BREAK END IF NEXT ' ----- ' main checking FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 b = sets[y][x] IF b = widget THEN pos_x = x pos_y = y i_second = i BREAK END IF NEXT i ' ----- ' check squares row_up = pos_x - 1 row_down = pos_x + 1 col_left = pos_y - 1 col_right = pos_y + 1 IF (row_up > -1 ) & (row_up < 3) THEN b = sets[pos_y][row_up] st$ = colour(STR$(b)) IF st$ = "#FFFFFF" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], "row", \ pos_x - 1, "column", pos_y, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y][pos_x - 1] = widget checkmove = 1 INCR MOVES END IF END IF ' ----- IF (row_down > 0 ) & (row_down < 4) THEN
b = sets[pos_y][row_down] st$ = colour(STR$(b)) IF st$ = "#FFFFFF" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x + 1, "column", pos_y, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y][pos_x + 1] = widget checkmove = 1 INCR MOVES END IF END IF ' ----- IF (col_left > -1) & (col_left < 3) THEN b = sets[col_left][pos_x] st$ = colour(STR$(b)) IF st$ = "#FFFFFF" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x, "column", pos_y - 1, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y - 1][pos_x] = widget checkmove = 1 INCR MOVES END IF END IF ' ----- IF (col_right > 0 ) & (col_right < 4) THEN b = sets[col_right][pos_x] st$ = colour(STR$(b)) IF st$ = "#FFFFFF" THEN movwidget = selwidget(STR$(tempwigmove)) swap = selwidget(STR$(widget)) ' swap the Numbers goo_canvas_item_set_child_properties(table, text[movwidget], \ "row", pos_x, "column", pos_y, NULL) goo_canvas_item_set_child_properties(table, text[swap], \ "row", pos_x, "column", pos_y + 1, NULL) sets[pos_y][pos_x] = tempwigmove sets[pos_y + 1][pos_x] = widget checkmove = 1 INCR MOVES END IF END IF IF (checkmove = 1) THEN CHECK_WIN() END SUB
' ------------------ FUNCTION CREATE_CANVAS_FIFTEEN() ' ------------------ LOCAL text$ TYPE char ' create the table and add tiles + text numbers table = goo_canvas_table_new(root_canvas1,"row-spacing", 1.0, \ "column-spacing", 1.0, NULL) ' create ciaro gradient and then set value in goo rect radial = cairo_pattern_create_radial(30, 30, 10, 30, 30, 90) cairo_pattern_add_color_stop_rgba(radial, 0, 1, 1, 1, 1); cairo_pattern_add_color_stop_rgba(radial, 1, 0.6, 0.6, 0.6, 1) FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 ' make the tiles from rectangles rect[i] = goo_canvas_rect_new(table, 0, 0, PIECE_SIZE, PIECE_SIZE, \ "radius-x", 6.0, "radius-y", 6.0, "stroke-color", "black", \ "line-width" , 1.0, "fill_pattern", radial, NULL) ' set the item in the table goo_canvas_item_set_child_properties (table, rect[i] , "row", \ x, "column", y, NULL) NEXT i FOR i = 15 TO 0 STEP -1 y = i / 4 x = i % 4 ' add TEXT as number ' TILES tiles[] text[i] = goo_canvas_text_new(table, STR$(i), 0, 0, PIECE_SIZE, \ GTK_ANCHOR_CENTER, "font", "Sans bold 30px", "fill-color", \ color$[tiles[i]], NULL) ' set the height of the text; this property is not set by default g_object_set(text[i], "use-markup", TRUE, 0) ' fix vertical centring g_object_set(text[i], "text", CONCAT$("<span rise='-37000'>", STR$(i), "</span>"), 0) g_object_set(text[i], "height", PIECE_SIZE, NULL) ' assoc the text with the number selwidget(STR$(text[i])) = i ' assoc the text with its color colour(STR$(text[i])) = color$[i] ' get the text and print it g_object_get(text[i], "text", ADDRESS(text$), NULL) PRINT "TEXT : ", text$ ' set the item in the table goo_canvas_item_set_child_properties(table, text[i] , "row", x, \ "column", y, NULL) ' store the text in array sets[y][x] = text[i] ' goo canvas items can have callbacks, so here set the callback ' for the TEXT; the sub is 'SELECTED' g_signal_connect_data(text[i], "button-press-event", SELECTED, 0, 0, 0) NEXT i ' set first tile to no text so it cannot be seen g_object_set(text[0], "text", "", NULL) RETURN TRUE END FUNCTION
' ------------------ SUB SET_TILES() ' ------------------ REPEAT SHUFFLE_TILES(tiles) UNTIL SOLVABLE(tiles) = TRUE END SUB
' ------------------ SUB HANDLE_ANSWER(NUMBER dialog, int button) ' ------------------ HIDE(dialog) END SUB
' ------------------ SUB MK_GUI() ' ------------------ win = WINDOW("Fifteen", 207, 247) canvas1 = CANVAS(207, 207) ATTACH(win, canvas1, 0, 0) ' the canvas id is the ebox = hug_widget_image(STR$(ebox)) root_canvas1 = goo_canvas_get_root_item(hug_widget_image(STR$(canvas1))) ' create a goo canvas table + add tiles and numbers fifteen = CREATE_CANVAS_FIFTEEN() but1 = BUTTON("New", 65, 30) PROPERTY(but1, "tooltip-text", "Start a new game.") CALLBACK(but1, SET_TILES) ATTACH(win, but1, 0, 212) move_label = MARK("<span font='Sans Bold 10' color='black'>Moves</span>", 80, 30) ATTACH(win, move_label, 65, 207) moves = MARK("<span font='Sans Bold 10' color='red'>0</span>", 80, 22) ATTACH(win, moves, 65, 225) but2 = BUTTON("Quit", 65, 30) PROPERTY(but2, "tooltip-text", "Quit this game.") ATTACH(win, but2, 142, 212) CALLBACK(but2, QUIT) winner = MSGDIALOG("<span color='red'><b>Well done!</b></span>", 300, 220, 0, 2) CALLBACK(winner, HANDLE_ANSWER) HIDE(winner) END SUB
' *********************** ' END SUBS & FUNCTIONS ' ***********************
' *********************** ' MAIN ' ***********************
MK_GUI DISPLAY
' *********************** ' END MAIN ' ***********************
|
|
|
Post by alexfish on May 12, 2013 11:03:59 GMT 1
Hey Vovchik Looks Good to me , until proved wrong Have Fun All + BR Alex also did a mod to 'SUB_SET_TILES' , to show how well it works SUB SET_TILES() ' ------------------ counter1 = 0 t1=TIMER REPEAT SHUFFLE_TILES(tiles) INCR counter1 UNTIL SOLVABLE(tiles) = TRUE t2=TIMER PRINT NL$, "--------------------------------" PRINT "Iterations :",counter1," In :",(t2-t1) PRINT NL$, "--------------------------------" END SUB Attachments:
|
|
|
Post by vovchik on May 12, 2013 16:12:55 GMT 1
Dear all, I just couldn't help myself... With kind regards, vovchik
|
|
|
Post by alexfish on May 13, 2013 20:22:47 GMT 1
Hi all
Simple Drag n Drop Demo
'******************************************* ' PROGRAM: Simple drag drop demo * ' PURPOSE: goocanvas demo * ' -------------------------------------------------------* ' AUTHORS * ' .org alexfish (BaCon Converter Forum) * ' .org vovchik(Puppy Linux Forums) * ' * ' -------------------------------------------------------* ' DATE: 06-05-2013 * ' LICENSE: GLP3 * '******************************************* ' Requires GooCanvas Dev INCLUDE "hug.bac" HUGOPTIONS("NOSCALING") HUGOPTIONS("CANVAS GOO")
SUB RECT_CLICKED(NUMBER item,NUMBER target_item,NUMBER event , NUMBER user_data)
DECLARE S_X , S_Y TYPE double '@ get the cords of the rectangle in ADDRESS of vars(S_X , S_Y) g_object_get(item,"x",ADDRESS(S_X),"y",ADDRESS(S_Y),NULL)
'@ get where the mouse is M_X = MOUSE(0) M_Y = MOUSE(1) '@ get true cords TRUE_X = M_X -S_X TRUE_Y = M_Y -S_Y END SUB
'@ Goo Canvas data item as from dev's SUB SIMPLE_DRAG_DROP_EVENT(NUMBER item,NUMBER target_item,NUMBER event , NUMBER user_data)
LOCAL M_X , M_Y TYPE double
'@ IF MOUSE BUTTON = 1 "DRAG until BUTTON 1 Released" IF MOUSE(2)= 1 THEN '@ Set the actule rectangle x - y relative to off-set of mouse pointers actual_x = MOUSE(0)-TRUE_X actual_y = MOUSE(1)-TRUE_Y '@ Need to static-cast int MOUSE pointers x and y as double M_X = (double)actual_x M_Y = (double)actual_y '@ DRAG THE RECTANGLE TO NEW POSITION , M_X & M_Y g_object_set(item,"x",M_X,"y",M_Y, NULL)
END IF END SUB
FUNCTION MAKE_GUI
win = WINDOW("Goo Canvas - Simple drag-drop", 600, 400) canvas1 = CANVAS(600, 400) '@ OFFSET the cavas so can have boarder ATTACH(win, canvas1,0,0) LOCAL rectangle ' the canvas id is the ebox = hug_widget_image(STR$(ebox)) root_canvas1 = goo_canvas_get_root_item(hug_widget_image(STR$(canvas1))) '@ make 5 rectangles , add motion-notify-event + button-press-event. FOR t = 0 TO 5 rectangle=goo_canvas_rect_new(root_canvas1,0, 0, 72, 100,"fill-color" ,"brown","radius-x", 6.0, "radius-y", 6.0, \ "stroke-color", "red","line-width" ,2.0,NULL) g_signal_connect_data(rectangle, "motion-notify-event", SIMPLE_DRAG_DROP_EVENT, 0, 0, 0) g_signal_connect_data(rectangle, "button-press-event", RECT_CLICKED, 0, 0, 0) NEXT RETURN FALSE END FUNCTION '@ ------------------------------------------------MAIN mk_gui = MAKE_GUI()
DISPLAY
Any Problems Please Notify::
Have Fun
Alex
|
|
|
Post by alexfish on May 13, 2013 21:20:21 GMT 1
Hi All
DnD
This one Brings The Drag widget to the front + added some color variation
'******************************************* ' PROGRAM: Simple drag drop demo * ' PURPOSE: goocanvas demo * ' -------------------------------------------------------* ' AUTHORS * ' .org alexfish (BaCon Converter Forum) * ' .org vovchik(Puppy Linux Forums) * ' * ' -------------------------------------------------------* ' DATE: 06-05-2013 * ' LICENSE: GLP3 * '******************************************* ' Mod added " Raise The Item ' Requires lib GooCanvas Dev any Problems ask ! ' Requires HUG VERSION >= 86 INCLUDE "hug.bac" HUGOPTIONS("NOSCALING") HUGOPTIONS("CANVAS GOO") '@ for last item drag DECLARE LAST_DRAG TYPE NUMBER DECLARE color$[10] '@ some colors color$[0] = "#FF00C8" color$[1] = "#008EFF" color$[2] = "#AEFF00" color$[3] = "#FFA800" color$[4] = "#ABC66F" color$[5]= "#CE41AA" color$[6] = "#B24110" color$[7] = "#933811" SUB RECT_CLICKED(NUMBER item,NUMBER target_item,NUMBER event , NUMBER user_data)
DECLARE S_X , S_Y TYPE double '@ get the cords of the rectangle in ADDRESS of vars(S_X , S_Y) g_object_get(item,"x",ADDRESS(S_X),"y",ADDRESS(S_Y),NULL)
'@ get where the mouse is M_X = MOUSE(0) M_Y = MOUSE(1) '@ get true cords TRUE_X = M_X -S_X TRUE_Y = M_Y -S_Y END SUB
'@ Goo Canvas data item as from dev's SUB SIMPLE_DRAG_DROP_EVENT(NUMBER item,NUMBER target_item,NUMBER event , NUMBER user_data)
LOCAL M_X , M_Y TYPE double
'@ IF MOUSE BUTTON = 1 "DRAG until BUTTON 1 Released" IF MOUSE(2)= 1 THEN ' @ BRING THE DRAGED WIDGET TO FRONT IF LAST_DRAG != item THEN goo_canvas_item_raise(item, LAST_DRAG) END IF '@ Set the actule rectangle x - y relative to off-set of mouse pointers actual_x = MOUSE(0)-TRUE_X actual_y = MOUSE(1)-TRUE_Y '@ Need to static-cast int MOUSE pointers x and y as double M_X = (double)actual_x M_Y = (double)actual_y '@ DRAG THE RECTANGLE TO NEW POSITION , M_X & M_Y g_object_set(item,"x",M_X,"y",M_Y, NULL) LAST_DRAG = item END IF END SUB
FUNCTION MAKE_GUI
win = WINDOW("Goo Canvas - Simple drag-drop", 600, 400) canvas1 = CANVAS(600, 400) ATTACH(win, canvas1,0,0) LOCAL rectangle ' the canvas id is the ebox = hug_widget_image(STR$(ebox)) root_canvas1 = goo_canvas_get_root_item(hug_widget_image(STR$(canvas1))) '@ make 5 rectangles , add motion-notify-event + button-press-event. FOR t = 0 TO 5 rectangle=goo_canvas_rect_new(root_canvas1,0, 0, 72, 100,"fill-color" ,color$[t] ,"radius-x", 6.0, "radius-y", 6.0, \ "stroke-color", "red","line-width" ,2.0,NULL) g_signal_connect_data(rectangle, "motion-notify-event", SIMPLE_DRAG_DROP_EVENT, 0, 0, 0) g_signal_connect_data(rectangle, "button-press-event", RECT_CLICKED, 0, 0, 0) LAST_DRAG=rectangle NEXT RETURN FALSE END FUNCTION '@ ------------------------------------------------MAIN mk_gui = MAKE_GUI()
DISPLAY Have Fun
Alex
|
|
|
Post by vovchik on May 14, 2013 11:38:42 GMT 1
Hi guys,
Adding a little eye candy...filled background with a nice pattern.
With kind regards, vovchik
'**************************************************** ' PROGRAM: Simple GROUP drag drop demo ' PURPOSE: goocanvas demo ' ------------------------------------------ ' AUTHORS: .org alexfish (BaCon Converter Forum) ' .org vovchik(Puppy Linux Forums) ' ------------------------------------------ ' DATE: 06-05-2013 ' LICENSE: GLP3 ' MODS: raise item; gradients '****************************************************
' *********************** ' INCLUDES ' ***********************
INCLUDE "hug.bac"
' *********************** ' END INCLUDES ' ***********************
' *********************** ' INITIALIZATION ' ***********************
' lib cairo CONST cairo$ = "libcairo.so" HUGOPTIONS("NOSCALING") HUGOPTIONS("CANVAS GOO") ' for last item drag DECLARE LAST_DRAG TYPE NUMBER
' *********************** ' END INITIALIZATION ' ***********************
' *********************** ' IMPORTS ' ***********************
' goo IMPORT "goo_canvas_item_set_child_properties(long,long,...)" FROM HUGLIB$("goo") TYPE void IMPORT "goo_canvas_group_new(long,...)" FROM HUGLIB$("goo") TYPE long IMPORT "goo_canvas_item_remove_child(long,int)" FROM HUGLIB$("goo") TYPE void IMPORT "goo_canvas_item_add_child(long,long,int)" FROM HUGLIB$("goo") TYPE void IMPORT "goo_canvas_item_find_child(long,long)" FROM HUGLIB$("goo") TYPE int IMPORT "goo_canvas_item_get_parent(long)" FROM HUGLIB$("goo") TYPE long ' cairo/pango IMPORT "cairo_pattern_create_radial(double,double,double,double,double,double)" FROM cairo$ TYPE long IMPORT "cairo_pattern_add_color_stop_rgba(long,double,double,double,double,double)" FROM cairo$ TYPE void IMPORT "cairo_pattern_create_linear(double,double,double,double)" FROM cairo$ TYPE long
' *********************** ' END IMPORTS ' ***********************
' *********************** ' SUBS & FUNCTIONS ' ***********************
' ------------------ SUB RECT_CLICKED(NUMBER item,NUMBER target_item,NUMBER event , NUMBER user_data) ' ------------------ DECLARE S_X , S_Y TYPE double ' get the cords of the rectangle in ADDRESS of vars(S_X , S_Y) g_object_get(item, "x", ADDRESS(S_X), "y", ADDRESS(S_Y), NULL) ' get where the mouse is M_X = MOUSE(0) M_Y = MOUSE(1) ' get true coords TRUE_X = M_X - S_X TRUE_Y = M_Y - S_Y END SUB
' ------------------ SUB SIMPLE_DRAG_DROP_EVENT(NUMBER item, NUMBER target_item, NUMBER event, NUMBER user_data) ' ------------------ ' Goo Canvas data item as from dev's LOCAL M_X, M_Y TYPE double ' IF MOUSE BUTTON = 1 "DRAG until BUTTON 1 Released" IF MOUSE(2) = 1 THEN IF LAST_DRAG != item THEN goo_canvas_item_raise(item, LAST_DRAG) END IF ' Set the actule rectangle x - y relative to off-set of mouse pointers actual_x = MOUSE(0) - TRUE_X actual_y = MOUSE(1) - TRUE_Y ' Need to static-cast int MOUSE pointers x and y as double M_X = (double)actual_x M_Y = (double)actual_y ' DRAG THE RECTANGLE TO NEW POSITION , M_X & M_Y g_object_set(item, "x", M_X, "y", M_Y, NULL) LAST_DRAG = item END IF END SUB
' ------------------ SUB MK_GUI() ' ------------------ win = WINDOW("Goo Canvas - Simple GROUP drag-drop", 600, 400) canvas1 = CANVAS(600, 400) ATTACH(win, canvas1, 0, 0) DECLARE XPO , YPO TYPE double root_canvas1 = goo_canvas_get_root_item(hug_widget_image(STR$(canvas1))) parent0 = goo_canvas_group_new(root_canvas1, NULL) parent1 = goo_canvas_group_new(root_canvas1, NULL) g_signal_connect_data(parent1, "motion-notify-event", SIMPLE_DRAG_DROP_EVENT, 0, 0, 0) g_signal_connect_data(parent1, "button-press-event", RECT_CLICKED, 0, 0, 0) ' create ciaro gradients and then set value in goo rects ' radial0 radial0 = cairo_pattern_create_radial(320, 25, 110, 25, 320, 250) cairo_pattern_add_color_stop_rgba(radial0, 0, 0.56, 1, 1, 1) cairo_pattern_add_color_stop_rgba(radial0, 1, 0.4, 0.2, 0.6, 1) ' radial radial = cairo_pattern_create_radial(25, 25, 10, 25, 25, 50) cairo_pattern_add_color_stop_rgba(radial, 0, 0.56, 1, 1, 1) cairo_pattern_add_color_stop_rgba(radial, 1, 0.4, 0.2, 0.6, 1) ' linear linear = cairo_pattern_create_linear(10, 10, 100, 100) cairo_pattern_add_color_stop_rgba(linear, 0, 1, 1, 1, 1) cairo_pattern_add_color_stop_rgba(linear, 1, 0.2, 0.2, 0.6, 1) ' for drag drop demo rectangle0 = goo_canvas_rect_new(parent0, 0, 0, 600, 400, "fill_pattern", radial0, NULL) rectangle = goo_canvas_rect_new(parent1, 0, 0, 50, 50, "fill_pattern", radial, NULL) rectangle = goo_canvas_rect_new(parent1, 0, 60, 50, 50, "fill_pattern", linear, NULL) XPO = 50.0 YPO = 20.0 g_object_set(parent1, "x", XPO , "y", YPO, NULL) parent2 = goo_canvas_group_new(root_canvas1, NULL) ' for drag drop demo g_signal_connect_data(parent2, "motion-notify-event", SIMPLE_DRAG_DROP_EVENT, 0, 0, 0) g_signal_connect_data(parent2, "button-press-event", RECT_CLICKED, 0, 0, 0) rectangle = goo_canvas_rect_new(parent2, 0, 0, 50, 50, "fill_pattern", linear, NULL) rectangle = goo_canvas_rect_new(parent2, 0, 60, 50, 50, "fill_pattern", radial, NULL) XPO = 50.0 YPO = 20.0 g_object_set(parent2, "x", XPO , "y", YPO, NULL) LAST_DRAG = parent2 END SUB
' *********************** ' END SUBS & FUNCTIONS ' ***********************
' *********************** ' MAIN ' ***********************
MK_GUI DISPLAY
' *********************** ' END MAIN ' ***********************
|
|