$(function() {

    for (var i = 0; i < 14; i++) {
        var row = $('<div/>', {
                'class': 'row'
            }).appendTo('.map');
        for (var j = 0; j < 14; j++) {
            $('<div/>', {
                'data-row': i,
                'data-col': j,
                'class': 'field'
            }).appendTo(row);
        }
    };

    var Game = function (socket) {
        this.socket = socket;
        this.id = null;
        this.player = null;
        this.phase = null;
        this.units = null;
    };

    Game.prototype.send = function (t, params) {
        socket.emit("command", {
            type: t,
            params: params
        });
    };

    Game.prototype.update = function (snapshot) {
        this.id = snapshot.myId;
        this.player = snapshot.players[this.id];
        this.phase = snapshot.world.phase;
        $('.field').html('');
        for (var v in this.player.units) {
            var unit = this.player.units[v];
            $('.field[data-row="' + unit.location[0] + '"][data-col="' + unit.location[1] + '"]').html(unit.type);
        }
        console.log(this.phase, this.player);
        this[this.phase](snapshot);
    };

    Game.prototype.hasUnitHere = function (i, j) {
        for (var v in this.player.units) {
            var unit = this.player.units[v];
            if (unit.location[0] == i && unit.location[1] == j) {
                return true;
            }
        }
        return false;
    };

    Game.prototype.fieldsToPlace = function () {
        var from = this.player.homelandLocation == 'up' ? 0 : 9;
        var to = this.player.homelandLocation == 'up' ? 5 : 14;
        var fields = [];
        for (var i = from; i < to; i++) {
            for (var j = 0; j < 14; j++) {
                if (!this.hasUnitHere(i, j)) {
                    fields.push([i, j]);
                }
            }
        }
        return fields;
    };

    Game.prototype.planning_phase = function (snapshot) {
        var unit = null;
        for (var v in this.player.unitsToPlace) {
            if (this.player.unitsToPlace[v] > 0) {
                unit = v;
            }
        }
        var voidFields = this.fieldsToPlace();
        if (unit) {
            var field = voidFields.splice(Math.floor(Math.random() * voidFields.length), 1)[0];
            this.placeUnit(unit, field);
        }
    };

    Game.prototype.placeUnit = function (unit, place) {
        console.log(unit, place);
        this.send('place', { 'location': place, 'type': unit });
    };

    var socket = io.connect(":" + LocalConfig.port);
    var game = new Game(socket);

    socket.on("update", function (snapshot) {
        console.log(snapshot);
        game.update(snapshot);
    });
});


@@ -26,24 +26,24 @@
 var 
-i
+y
  = 0; 
-i
+y
  %3C 14; 
-i
+y
 ++) 
@@ -157,32 +157,32 @@
 or (var 
-j
+x
  = 0; 
-j
+x
  %3C 14; 
-j
+x
 ++) %7B%0A  
@@ -231,15 +231,13 @@
 ata-
-row': i
+x': x
 ,%0A  
@@ -260,15 +260,13 @@
 ata-
-col': j
+y': y
 ,%0A  
@@ -993,19 +993,17 @@
 ld%5Bdata-
-row
+x
 =%22' + un
@@ -1032,11 +1032,9 @@
 ata-
-col
+y
 =%22' 
@@ -1220,20 +1220,20 @@
 nction (
-i, j
+x, y
 ) %7B%0A    
@@ -1352,17 +1352,17 @@
 n%5B0%5D == 
-i
+x
  && unit
@@ -1377,17 +1377,17 @@
 n%5B1%5D == 
-j
+y
 ) %7B%0A    
@@ -1685,17 +1685,17 @@
 or (var 
-i
+y
  = from;
@@ -1699,17 +1699,17 @@
 om; 
-i
+y
  %3C to; 
-i
+y
 ++) 
@@ -1735,24 +1735,24 @@
 var 
-j
+x
  = 0; 
-j
+x
  %3C 14; 
-j
+x
 ++) 
@@ -1791,20 +1791,20 @@
 nitHere(
-i, j
+x, y
 )) %7B%0A   
@@ -1837,12 +1837,12 @@
 sh(%5B
-i, j
+x, y
 %5D);%0A

i-j to x-y