javascript - How to create a two-dimensional array within a table? -


i create table connect 4 game, setting two-dimensional 7*6 array , put each array within each cell if makes sense. new javascript , not have lot of knowledge in object orientated programming. trying give each cell xposition , yposition (coordinates, perhaps in "id") game can check if there row or column of blue or yellow.

code far, rough attempt:

    function make()     {     var table = document.createelement("table");     (var = 0; < 6; i++)     {     var row = table.inserrow();     (var j = 0; j < 5; j++)     {     var cell = row.insertcell;     }     document.body.appendchild(table);     }     } 

some quick solution written jquery. pasted whole html, can save out html file , open in browser. can click on cells see coordinates (0-based).

<html>     <head>         <title>grid</title>         <style type="text/css">             table tr td { width: 50px; height: 50px; background-color: silver; border: 1px solid black; }             table tr td.over { background-color: yellow; }             table tr td.active { background-color: red; }             .controls { padding: 20px; }         </style>     </head>     <body>         <div class="controls">             <a href="javascript:void(0)" data-move="[-1, 0]">left</a>             <a href="javascript:void(0)" data-move="[0, -1]">top</a>             <a href="javascript:void(0)" data-move="[1, 0]">right</a>             <a href="javascript:void(0)" data-move="[0, 1]">bottom</a>             <a href="javascript:void(0)" data-move="[-1, -1]">topleft</a>             <a href="javascript:void(0)" data-move="[1, -1]">topright</a>             <a href="javascript:void(0)" data-move="[1, 1]">bottomright</a>             <a href="javascript:void(0)" data-move="[-1, 1]">bottomleft</a>         </div>         <table></table>         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>         <script type="text/javascript">         $(document).ready(function() {             var rows = 6,                 cols = 7;              for(var = 0; < rows; i++) {                 $('table').append('<tr></tr>');                 for(var j = 0; j < cols; j++) {                     $('table').find('tr').eq(i).append('<td></td>');                     $('table').find('tr').eq(i).find('td').eq(j).attr('data-row', i).attr('data-col', j);                 }             }              $('table tr td').mouseover(function() {                 $(this).addclass('over');             }).mouseout(function() {                 $(this).removeclass('over');             }).click(function() {                 $(this).addclass('active');             });              $(".controls a").click(function() {                 var $active = $("table tr td.active");                 if($active.length > 0) {                     var move = $.parsejson($(this).attr('data-move'));                     if(move.length >= 2) {                         $active.each(function() {                             var row = parseint($(this).attr('data-row')) + move[1],                                 col = parseint($(this).attr('data-col')) + move[0];                             if(col >= cols) col = cols - 1;                             if(col < 0) col = 0;                             if(row >= rows) row = rows - 1;                             if(row < 0) row = 0;                             $(this).removeclass('active');                             $('table tr').eq(row).find('td').eq(col).addclass('active');                         });                     }                 }             });         });         </script>     </body> </html> 

notice if change rows , cols variables can draw bigger grids.

i have added controls div buttons can play directions. first of need mark element active, , them can click move.

there 1 bug (or feature in opinion) can mark multiple fields , move them @ once.

it's base start with!


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -