Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //set everything up, including the WebSocket
  2. // (function setup(){
  3. var socket = new WebSocket("ws://localhost:3000");
  4. var localb;
  5.  
  6.  
  7. socket.onopen = function(){
  8.    // socket.send("Hi!!");
  9.     document.getElementById("status").innerHTML = 'CONNECTED';
  10. };
  11.  
  12. socket.onmessage = function(event){
  13. //var a = new game();
  14.  
  15. localb = JSON.parse(event.data);
  16. console.log(localb + " teh localb");
  17.    
  18.         for (var i = 0; i < 8; i++){
  19.             for(var j = 0; j < 8; j++){
  20.                     var position =  "#" + i + j + "r.checkerpiece";
  21.                     var position2 = "#" + i + j + "b.checkerpiece";
  22.                     $(position2).hide();
  23.                     $(position).hide();
  24.                 if(localb[i][j] == 1){
  25.                     var position =  "#" + i + j + "r.checkerpiece";
  26.                     $(position).show();
  27.                 }
  28.                 if(localb[i][j] == 2){
  29.                     var position =  "#" + i + j + "b.checkerpiece";
  30.                     $(position).show();
  31.                 }
  32.             }
  33.         }
  34.  
  35. };
  36.  
  37.  
  38.  
  39. var oldpos;
  40. $(".checkerpiece").on("click", function(event){
  41.     oldpos = event.target.id;
  42. });
  43.  
  44. $(".checkerr").on("click", function(event){
  45.     var newpos = event.target.id;
  46.         var x1 = oldpos.substring(0,1);
  47.         var y1 = oldpos.substring(1,2);
  48.         var x2 = newpos.substring(0,1);
  49.         var y2 = newpos.substring(1,2);
  50.         var color = oldpos.substring(2,3);
  51.         var d = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1-y2,2))
  52.         if(d === Math.sqrt(2)){
  53.             if(color === "r" && x2 > x1 && !has(localb, x2, y2)){
  54.                 localb[x1][y1] = 0;
  55.                 localb[x2][y2] = 1;
  56.         var old = "#" + oldpos + ".checkerpiece";
  57.         var updated = "#" +newpos + color +".checkerpiece";
  58.         $(old).hide();
  59.         $(updated).show();
  60.         }
  61.         if(color === "b" && x2 < x1 && !has(localb, x2, y2)){
  62.             localb[x1][y1] = 0;
  63.             localb[x2][y2] = 2;
  64.             var old = "#" + oldpos + ".checkerpiece";
  65.             var updated = "#" +newpos + color +".checkerpiece";
  66.             $(old).hide();
  67.             $(updated).show();
  68.             }
  69.             socket.send(JSON.stringify(localb));
  70.         }})
  71.  
  72.  
  73.  
  74. socket.onclose = function(){
  75.     document.getElementById("status").innerHTML = 'NOT CONNECTED';
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement