Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.  
  3.  
  4.     // each intersection[x][y] contains arrays of every possible direction from this intersection
  5.     intersections = {};
  6.  
  7.     colors = ["blue", "green"];
  8.     activeStone = {};
  9.     targetStone = {};
  10.     moveDirection = "";
  11.     activePlayer = "red";
  12.  
  13.     function updateActivePlayerStones() {
  14.         $(":not([data-color='"+activePlayer+"'])").removeClass("activePlayer");
  15.         $("[data-color='"+activePlayer+"']").addClass("activePlayer");
  16.     }
  17.  
  18.     function nextPlayer() {
  19.         switch(activePlayer){
  20.             case "blue":
  21.                 activePlayer = "red";
  22.                 break;
  23.             case "red":
  24.                 activePlayer = "blue";
  25.                 break;
  26.             default:
  27.                 break;
  28.         }
  29.         updateActivePlayerStones();
  30.     }
  31.  
  32.     function stone(x, y) {
  33.         return $("#intersection_"+x+"_"+y);
  34.     }
  35.  
  36.     function setStone(x, y, color) {
  37.         clearStone(x,y);
  38.         stone(x,y).attr("data-color", color);
  39.         intersections[x][y]["color"] = color;
  40.         updateActivePlayerStones();
  41.     }
  42.  
  43.     // board creation
  44.     for(y = 0; y < 5; y++) {
  45.  
  46.  
  47.         for(x = 0; x < 9; x++) {
  48.  
  49.             directions = {};
  50.             color = "";
  51.  
  52.             if(!(x in intersections)) {
  53.                 intersections[x] = [];
  54.             }
  55.  
  56.             if(x > 0) { directions.left={"x": -1, "y": 0}; }
  57.             if(x < 8) { directions.right={"x": 1, "y": 0}; }
  58.             if(y > 0) { directions.top={"x": 0, "y": -1}; }
  59.             if(y < 4) { directions.bottom={"x": 0, "y": 1}; }
  60.             if((x%2 == 0 && y%2 == 0) || (x%2 != 0 && y%2 != 0)) { // diagonals
  61.                 if(x > 0 && y > 0) { directions.topLeft={"x": -1, "y": -1}; }
  62.                 if(x < 8 && y < 5) { directions.bottomRight={"x": 1, "y": 1}; }
  63.                 if(x > 0 && y < 4) { directions.bottomLeft={"x": -1, "y": 1}; }
  64.                 if(x < 8 && y > 0) { directions.topRight={"x": 1, "y": -1}; }
  65.             }
  66.             intersections[x][y] = {};
  67.             intersections[x][y]["directions"] = directions;
  68.  
  69.             $("#board").append("<div id='intersection_"+x+"_"+y+"' class='intersection' data-x='"+x+"' data-y='"+y+"' data-color='"+color+"'></div>");
  70.  
  71.             // starting colors
  72.             if(y < 3 && !(x == 4 && y == 2)) {
  73.                 setStone(x, y, "blue");
  74.             }
  75.             if((y>1 && x%2 != 0) || y > 2) {
  76.                 setStone(x, y, "red");
  77.             }
  78.         }
  79.     }
  80.     nextPlayer();
  81.  
  82.     function getStoneColor(x, y) {
  83.         // return the color of the stone. Returns empty valye if thre is no stone
  84.         return stone(x,y).attr("data-color");
  85.     }
  86.  
  87.     function setPossibleMove(x, y, direction) {
  88.         stone(x,y).attr("data-direction", direction);
  89.     }
  90.  
  91.     function clearStone(x,y) {
  92.         stone(x,y).removeAttr("data-color");
  93.         updateActivePlayerStones();
  94.     }
  95.  
  96.     function clearPossibleMoves() {
  97.         $("[data-direction]").removeAttr("data-direction");
  98.         $(".intersection.active").removeClass("active");
  99.     }
  100.  
  101.     clearStone(3,1);
  102.     clearStone(2,1);
  103.     clearStone(3,2);
  104.     clearStone(1,0);
  105.     clearStone(0,1);
  106.     clearStone(1,1);
  107.  
  108.     function checkPossibleMoves(x, y, color) {        
  109.         clearPossibleMoves();
  110.  
  111.         intersection = intersections[x][y];
  112.         startingIntersections = [];
  113.         $.each(intersection.directions, function(direction, coordinates){
  114.             targetX = parseInt(x)+parseInt(coordinates.x);
  115.             targetY = parseInt(y)+parseInt(coordinates.y);
  116.             // if we are looking for a first empty intersection and there is no stone, we add this intersection to the starting intersections list
  117.             if (!getStoneColor(targetX, targetY)) {
  118.                 startingIntersections.push(intersections[targetX, targetY]);
  119.                 setPossibleMove(targetX,targetY, direction);
  120.             }
  121.         });
  122.         if(!($.isEmptyObject(startingIntersections))) {
  123.             stone(x,y).addClass("active");
  124.         }
  125.         return startingIntersections;
  126.     }
  127.  
  128.     moveDirection = "";
  129.  
  130.     function moveStone() {
  131.         clearPossibleMoves();
  132.         $("[data-direction]").removeAttr("data-direction");
  133.         setStone(activeStone.x, activeStone.y, "");
  134.         setStone(targetStone.x, targetStone.y, activePlayer);
  135.     }
  136.  
  137.     $("#board").on("click", "[data-color='"+activePlayer+"']", function(){
  138.         console.log(activePlayer);
  139.         console.log($(this).attr("data-color"));
  140.         activeStone["x"] = $(this).attr("data-x");
  141.         activeStone["y"] = $(this).attr("data-y");
  142.         checkPossibleMoves(activeStone["x"],activeStone["y"], "blue");
  143.     });
  144.  
  145.     $("#board").on("click", "[data-direction]", function(){
  146.         targetStone["x"] = $(this).attr("data-x");
  147.         targetStone["y"] = $(this).attr("data-y");
  148.         moveDirection = $(this).attr("data-y");
  149.         moveStone();
  150.         nextPlayer();
  151.     });
  152.  
  153. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement