JavaScript Drag and Drop example 4

More...

Tic-tac-toe is built with Drag and Drop library. Example shows how to move and dynamically enable / disable DIV objects. Detail description of library can be read on Drag and drop table content with JavaScript. Please try to move X and O objects.

X
O

Tic Tac Toe

Drag and drop library handles dragging DIV elements and calculates dropping areas on HTML table. Additional work is to place logic to the myhandler_dropped function. Here is all JavaScript code used in this example.

var board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]], // board array
    xo = {x: 1, o: -1}, // define values for X and O elements
    redips_init,        // define redips_init variable
    rd,                 // reference to the REDIPS.drag library
    divo,               // reference to the O DIV element
    // methods
    toggle_xo,          // toggle X and O clone elements on the left
    check_board,        // method checks board
    check_line;         // method checks row, column or diagonal for value 3 or -3

// redips initialization
redips_init = function () {
    // set reference to the REDIPS.drag library
    rd = REDIPS.drag;
    // initialization
    rd.init();
    // define border for disabled element (default is dotted)
    rd.border_disabled = 'none';
    // dragged elements can be placed to the empty cells only
    rd.drop_option = 'single';
    // set reference to the O div element (needed in toggle_xo() method)
    divo = document.getElementById('o');
    // toggle X and O elements on the left side
    toggle_xo();
    // declare tasks after element is dropped
    rd.myhandler_dropped = function () {
        var obj = rd.obj,            // current element (cloned element)
            obj_old = rd.obj_old,    // previous element (this is clone element)
            tac = rd.target_cell;    // target cell
        // disable dropped DIV element
        rd.enable_drag(false, obj.id);
        // toggle X and O elements on the left
        toggle_xo();
        // check board (obj_old.id can be 'x' or 'o')
        check_board(obj_old.id, tac.parentNode.rowIndex, tac.cellIndex);
    };
};

// toggle X and O clone elements on the left
toggle_xo = function () {
    // references to the X and O elements
    if (divo.redips.enabled) {
        rd.enable_drag(false, 'o');
        rd.enable_drag(true, 'x');
    }
    else {
        rd.enable_drag(true, 'o');
        rd.enable_drag(false, 'x');
    }
};

// method checks board (KISS - keep it simple and stupid;)
check_board = function (id, row_idx, cell_idx) {
    // set value for current cell (1 or -1)
    board[row_idx][cell_idx] = xo[id];
    // test rows
    check_line(board[0][0] + board[0][1] + board[0][2]);
    check_line(board[1][0] + board[1][1] + board[1][2]);
    check_line(board[2][0] + board[2][1] + board[2][2]);
    // test columns
    check_line(board[0][0] + board[1][0] + board[2][0]);
    check_line(board[0][1] + board[1][1] + board[2][1]);
    check_line(board[0][2] + board[1][2] + board[2][2]);
    // test diagonals
    check_line(board[0][0] + board[1][1] + board[2][2]);
    check_line(board[0][2] + board[1][1] + board[2][0]);
};

// method checks line (row, column or diagonal) for value 3 or -3
check_line = function (value) {
    if (value === 3) {
        document.getElementById('message').innerHTML = 'X is the Winner!';
        rd.enable_drag(false); // disable all drag elements
    }
    else if (value === -3) {
        document.getElementById('message').innerHTML = 'O is the Winner!';
        rd.enable_drag(false); // disable all drag elements
    }
};

// add onload event listener
if (window.addEventListener) {
    window.addEventListener('load', redips_init, false);
}
else if (window.attachEvent) {
    window.attachEvent('onload', redips_init);
}

Package redips2.tar.gz contains source code and several examples including school timetable with save/recall table using PHP and MySQL.

This entry was posted on May 4, 2010 and is filed under Drag and Drop, JavaScript

Related posts

15 Responses to JavaScript Drag and Drop example 4

  1. hiiii..this is a mad man..

  2. Pingback: Redips.net – tabela chwyć i upuść w Ajax | Taipa.pl

  3. jobs cebu philippines says:

    hey! hey! this is a cool article, thanks for posting, I can use this, very nice, keep it up.

  4. Mehmet says:

    how can I understand the current situation of an object that is enable_drag is false or true .

    var myDiv = current_cell.childNodes[0].id  // for ie
    
    var  stat = document.getElementById(myDiv).?????
    

    Thank you right now ... I am waiting for you ...

  5. Mehmet says:

    Thank you for quick reply . it returns "init" but it is enough for me . Thank you again...

  6. dbunic says:

    Mehmet - "enabled" property is fixed in the latest REDIPS.drag library (version 2.6.1+). Instead of "init" it should be (boolean) true or false.

    Cheers!

  7. dbunic says:

    Mehmet - every DIV element has attached "enabled" property. For example, if DIV element has id="xc1", enabled property can be read on the following way:

    var enabled_flag = document.getElementById("xc1").enabled;
    
  8. Mehmet says:

    if enable_drag is setted false it returns "false" but not it returns "init" , but I realised that my Library version looks ( Version 2.5.0) I will redownload and review and return to you . Thanks a lot again...

  9. DSJ says:

    Hi dbunic,

    Nice work. Thanks for sharing with community. Any hint on making drag handle kind of thing like allow drag only from a header part of drag-able div (window) contents not by entire div contents, similar to igoogle ?

    Regards

  10. dbunic says:

    @DSJ - REDIPS.drag library is upgraded. Please download latest version 2.8.1 and take a look at example 11. You will see DIV elements with drag handle on titlebar. Hope this is what you looking for.

  11. beta says:

    Hello, How can I realize such an effect:
    After I put the X or O in the right-hand-side table, I want to double click one of them (cloned element) and perform some actions. So how can I do that?

    Thanks

    beta (a newer in JS)

  12. dbunic says:

    @beta - This example is not a good one for ondblclick event because elements X and O are disabled after dropping to the right table. REDIPS.drag library contains myhandler_dblclicked() event handler where you can write JavaScript code.

  13. beta says:

    Thank dbunic, thanks for your suggestion. I have solve it now.

  14. Vikas Saini says:

    Hello, Can i disable drop in some table cells.

  15. dbunic says:

    @Vikas Saini - Yes, you can define dropping rules for table cells like: only one element per table cell, only table cells with certain ID can be dropped to table cells with certain class name, completely forbid dropping to some table cell and so on. Please click on "preview" link below post title to see all examples contained in redips2.tar.gz package.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>