integer CURR_POS = 1;
integer SET_POS = 10;
integer REPORT_POSITION = 20;
integer REQUEST_MOVE = 30;
list TileList;
integer tiles_reported_in;
integer gap_position;
integer tile_touched;


default
{
    state_entry()
    {
    }
   
    link_message(integer sender_num, integer command, string str, key id)
    {
        if (command == REQUEST_MOVE)
        {
            tile_touched = sender_num - 1;
            TileList = [0, 0, 0, 0, 0, 0, 0, 0];
            integer loop_int;
            tiles_reported_in = 0;
            llMessageLinked (2, REPORT_POSITION, "", NULL_KEY); // Send signal to tile 1 - the second prim in the linked object- it will then pass the message to the other tiles
           
            state awaiting_checkin;
        }       
     
    }
   
}

state awaiting_checkin
{
    state_entry()
    {
    }
   
     link_message(integer sender_num, integer command, string str, key id)
    {
        if (command == REPORT_POSITION)
        {
            // str is position of tile
            // sender_num is tile number + 1
            string position = str;
            TileList = llListReplaceList(TileList, (list)str, (sender_num - 2), (sender_num - 2));
            tiles_reported_in += 1;
            if (tiles_reported_in == 8)
            {
                state finding_gap;
            }
        }       
    }
}

state finding_gap
{
    state_entry()
    {
            integer tile_search = 1;
            string s_tile_search;
            integer found = 0;           

            while (found == 0)
            {               
                s_tile_search = (string)tile_search;           
                if (llListFindList(TileList, (list)s_tile_search) == -1)
                {
                    found = 1;
                }
                else
                {
                    tile_search += 1;
                }
            }
            // At this point tile_search  holds the position of the gap
            gap_position = tile_search;
           
            // Check whether touched tile is in same column or row as gap
            integer position_touched = llList2Integer(TileList, tile_touched - 1);           
                          
            if (gap_position == 1)
            {
                if (position_touched == 2)
                {
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "1", NULL_KEY);
                }
                if (position_touched == 3)
                {
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "1", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["3"]) + 2, SET_POS, "2", NULL_KEY);
                }
                if (position_touched == 4) 
                {
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "1", NULL_KEY);
                }
                if (position_touched == 7)
                {
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "1", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["7"]) + 2, SET_POS, "4", NULL_KEY);
                }
            } 

            if (gap_position == 2)
            {
                if (position_touched == 1)
                {
                    llMessageLinked(llListFindList(TileList, ["1"]) + 2, SET_POS, "2", NULL_KEY);
                }
                if (position_touched == 3)
                {
                    llMessageLinked(llListFindList(TileList, ["3"]) + 2, SET_POS, "2", NULL_KEY);
                }
                if (position_touched == 5) 
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "2", NULL_KEY);
                }
                if (position_touched == 8)
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "2", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "5", NULL_KEY);
                }
            }        
                                 
           
            if (gap_position == 3)
            {
                if (position_touched == 6)
                {
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "3", NULL_KEY);
                }
                if (position_touched == 9)
                {
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "3", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["9"]) + 2, SET_POS, "6", NULL_KEY);
                }
                if (position_touched == 2) 
                {
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "3", NULL_KEY);
                }
                if (position_touched == 1)
                {
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "3", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["1"]) + 2, SET_POS, "2", NULL_KEY);
                }
            }    
           
            if (gap_position == 4)
            {
                if (position_touched == 1)
                {
                    llMessageLinked(llListFindList(TileList, ["1"]) + 2, SET_POS, "4", NULL_KEY);
                }
                if (position_touched == 7)
                {
                    llMessageLinked(llListFindList(TileList, ["7"]) + 2, SET_POS, "4", NULL_KEY);
                }
                if (position_touched == 5) 
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "4", NULL_KEY);
                }
                if (position_touched == 6)
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "4", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "5", NULL_KEY);
                }
            }        
           
            if (gap_position == 5)
            {
                if (position_touched == 2)
                {
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "5", NULL_KEY);
                }
                if (position_touched == 8)
                {
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "5", NULL_KEY);
                }
                if (position_touched == 4) 
                {
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "5", NULL_KEY);
                }
                if (position_touched == 6)
                {
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "5", NULL_KEY);
                }
            }     
           
            if (gap_position == 6)
            {
                if (position_touched == 3)
                {
                    llMessageLinked(llListFindList(TileList, ["3"]) + 2, SET_POS, "6", NULL_KEY);
                }
                if (position_touched == 9)
                {
                    llMessageLinked(llListFindList(TileList, ["9"]) + 2, SET_POS, "6", NULL_KEY);
                }
                if (position_touched == 5) 
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "6", NULL_KEY);
                }
                if (position_touched == 4)
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "6", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "5", NULL_KEY);
                }
            }   
           
            if (gap_position == 7)
            {
                if (position_touched == 4)
                {
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "7", NULL_KEY);
                }
                if (position_touched == 1)
                {
                    llMessageLinked(llListFindList(TileList, ["4"]) + 2, SET_POS, "7", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["1"]) + 2, SET_POS, "4", NULL_KEY);
                }
                if (position_touched == 8) 
                {
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "7", NULL_KEY);
                }
                if (position_touched == 9)
                {
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "7", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["9"]) + 2, SET_POS, "8", NULL_KEY);
                }
            }        
                 
                 
            if (gap_position == 8)
            {
                if (position_touched == 5)
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "8", NULL_KEY);
                }
                if (position_touched == 2)
                {
                    llMessageLinked(llListFindList(TileList, ["5"]) + 2, SET_POS, "8", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["2"]) + 2, SET_POS, "5", NULL_KEY);
                }
                if (position_touched == 7) 
                {
                    llMessageLinked(llListFindList(TileList, ["7"]) + 2, SET_POS, "8", NULL_KEY);
                }
                if (position_touched == 9)
                {
                    llMessageLinked(llListFindList(TileList, ["9"]) + 2, SET_POS, "8", NULL_KEY);
                }
            }   
           
            if (gap_position == 9)
            {
                if (position_touched == 6)
                {
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "9", NULL_KEY);
                }
                if (position_touched == 3)
                {
                    llMessageLinked(llListFindList(TileList, ["6"]) + 2, SET_POS, "9", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["3"]) + 2, SET_POS, "6", NULL_KEY);
                }
                if (position_touched == 8) 
                {
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "9", NULL_KEY);
                }
                if (position_touched == 7)
                {
                    llMessageLinked(llListFindList(TileList, ["8"]) + 2, SET_POS, "9", NULL_KEY);
                    llMessageLinked(llListFindList(TileList, ["7"]) + 2, SET_POS, "8", NULL_KEY);  
                }
            }    

           
            state default;

    }
}