ID:1916074
 
Code:
//I have picture(148x148) and i cuted it to (32x32) and it icon_state = (1,1);(1,2)... to (4,4)



#define DEBUG


turf/icon='turf.dmi'
obj
Grid
icon='Charizard.dmi'
var/screen_x
var/screen_y

Click()
world<<src.screen_loc
mob
Login()
src.loc=locate(1,1,1)

verb/GeneratePicture()
set background=1
var/list/x_list=list(1,2,3,4)
var/list/y_list=list(1,2,3,4)
var/X;var/Y
for(var/Column = 1 to 4) for(var/Row = 1 to 4)
var/obj/Grid/G = new
G.screen_loc = "[Row],[Column]"
G.screen_x=Row
G.screen_y=Column
G.icon_state= "[Column],[Row]"
src.client.screen += G
sleep(1)
/////////////
var/counter=0
for(var/obj/Grid/G in src.client.screen)
if(counter==4)
x_list=list(1,2,3,4)
y_list=list(1,2,3,4)
counter=0
X=pick(x_list)
G.screen_x=X
Y=pick(y_list)
G.screen_y=Y
G.screen_loc = "[G.screen_x],[G.screen_y]"
x_list.Remove(X)
y_list.Remove(Y)
counter++
world<<"[G.screen_x],[G.screen_y]"


Problem description:

Hi I have torrible with this code. I wanna create system to create picture from little pieces. I know how to create full picture but I have no idea how to set random screen_loc for them.
Observations:

1) It strikes me that you don't in fact mean "random". You have a specific place in mind, do you not?

2) Your screen_loc in the column/row loops is wrong; it's column first, row second.

3) What the heck is that last loop supposed to do?
If you want to randomize screen_loc, the simple method would be to get a list of all the objects, shuffle the list, and then reassign the shuffled locs.

// pseudocode
var/obj/O
var/i, len
var/list/L = new
for(O in tiles) L += O.screen_loc
len = tiles.len
for(i=1, i<len, ++i) L.Swap(i, rand(i, len))
for(i=1, i<=len, ++i)
O = tiles[i]
O.screen_loc = L[i]

That's the simple way. What I would probably do is assign each tile a column and row that says where it belongs, and also have a var indicating its current column and row.

obj/tile
var/column, row
var/final_column, final_row // where it belongs

New(client/C, c, r)
C.screen += src
column = c; row = r
final_column = c; final_row = r
// don't set screen_loc yet

proc/GetTransform(c, r)
// ICON_SIZE would be a constant like 32
return matrix((c-final_column)*ICON_SIZE,\
(r-final_row)*ICON_SIZE,\
MATRIX_TRANSLATE)

proc/Finalize(c, r)
column = c; row = r
transform = GetTransform(c,r)
screen_loc = "[PUZZLE_CORNER_X+final_column],[PUZZLE_CORNER_Y+final_row]"

proc/SlideTo(c, r)
column = c; row = r
animate(src, transform=GetTransform(c,r), 5, easing = SINE_EASING)

When you want to scramble the puzzle:

var/obj/tile/O
var/obj/tile/O2
var/i, len
var/list/L = tiles.Copy()
len = tiles.len
for(i=1, i<len, ++i) L.Swap(i, rand(i, len))
for(i=1, i<=len, ++i)
O = L[i]
O2 = tiles[i]
O.Finalize(O2.final_column, O2.final_row)

To explain this a little better, L is a shuffled copy of the tiles list. The same objs are in both lists, but in a different order. Once that's shuffled, go through the list L; the ith object in the shuffled list is moved to the position where the ith object of the original array would normally belong.