js_backgammon/func.js

101 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-05-17 22:49:54 +00:00
$(function () {
2018-05-18 14:53:23 +00:00
2018-05-17 22:49:54 +00:00
// Build pins
for (var i = 1; i < 25; i++) {
2018-05-18 14:53:23 +00:00
if (i < 13) {
var pos = 'bottom';
} else {
var pos = 'top';
}
var div = $("<div />").addClass("pin").addClass("pin-"+pos).attr('id', 'pin-'+i);
2018-05-17 22:49:54 +00:00
var checkers_div = $("<div />").addClass("checkers").attr('id', 'checkers-'+i);
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-'+i);
2018-05-18 14:53:23 +00:00
2018-05-17 22:49:54 +00:00
checkers_div.append(checkers_list);
div.append(checkers_div);
$( "#pins" ).append(div);
}
convertBoard();
convertToBoard();
2018-05-18 14:53:23 +00:00
$( ".checkers_list" ).sortable({
2018-05-17 22:49:54 +00:00
connectWith: ".checkers_list"
});
});
function convertToBoard() {
var board = [0]
for (var i = 1; i < 25; i++) {
var amount = $( "#checkers_list-"+i ).children('li').length;
board.push(amount);
}
board.push(0)
console.log(board);
}
function convertBoard() {
// TODO: Take board as input
var board = [0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0];
// We don't want to deal with bars RIGHT now, TODO: Fix this.
for (var i = 1; i < 25; i++) {
for (var amt_at_pin = 0; amt_at_pin < Math.abs(board[i]); amt_at_pin++) {
var sign = Math.sign(board[i]);
if (sign > 0) {
var list_obj = $( "<li><img src='black.png' width='40'></li>").addClass(Math.sign(board[i]).toString());
} else if (sign < 0) {
var list_obj = $( "<li><img src='white.png' width='40'></li>").addClass(Math.sign(board[i]).toString());
}
$( '#checkers_list-'+i ).append(list_obj);
}
}
};
function onReceived(data) {
var obj = JSON.stringify(data);
alert(obj);
};
function get_request() {
$.ajax({
2018-05-18 14:53:23 +00:00
url: 'http://127.0.0.1:5000/get_board',
2018-05-17 22:49:54 +00:00
dataType: 'JSONP',
data: {
format: 'json'
},
type: 'GET',
success: onReceived
});
};
function onPost(data) {
console.log(data);
};
function post_request() {
var board = [0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0]
var data = {'board' : board.toString()}
$.ajax({
type : "POST",
2018-05-18 14:53:23 +00:00
url : "http://127.0.0.1:5000/post_board",
2018-05-17 22:49:54 +00:00
data: JSON.stringify(data),
success: onPost
});
};