2018-05-18 18:53:45 +00:00
|
|
|
var prev_board = []
|
2018-05-17 22:49:54 +00:00
|
|
|
|
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
$(function () {
|
|
|
|
var init_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];
|
2018-05-18 15:53:57 +00:00
|
|
|
var red = "#E83D04"
|
|
|
|
var white = "#E8E077"
|
|
|
|
var brown = "#4C2E00"
|
2018-05-18 18:53:45 +00:00
|
|
|
var prev_board = []
|
|
|
|
var color = white;
|
2018-05-18 15:53:57 +00:00
|
|
|
|
2018-05-17 22:49:54 +00:00
|
|
|
// Build pins
|
2018-05-18 14:53:23 +00:00
|
|
|
|
2018-05-18 15:53:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 26; i++) {
|
|
|
|
|
|
|
|
if (i < 13 && 0 < i) {
|
2018-05-18 14:53:23 +00:00
|
|
|
var pos = 'bottom';
|
2018-05-18 15:53:57 +00:00
|
|
|
} else if (i > 12 && i < 25) {
|
2018-05-18 14:53:23 +00:00
|
|
|
var pos = 'top';
|
2018-05-18 15:53:57 +00:00
|
|
|
} else {
|
|
|
|
var pos = 'bar';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
if (color == white) {
|
|
|
|
color = red;
|
|
|
|
} else {
|
|
|
|
color = white;
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:53:57 +00:00
|
|
|
if (i == 0 || i == 25) {
|
|
|
|
color = brown;
|
2018-05-18 14:53:23 +00:00
|
|
|
}
|
2018-05-18 15:53:57 +00:00
|
|
|
|
|
|
|
var div = $("<div style='background-color:"+color+"'/>").addClass("pin").addClass("pin-"+pos).attr('id', 'pin-'+i);
|
2018-05-18 14:53:23 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
var div = $("<div style='background-color:black'/>").addClass("pin").addClass("pin-26").attr('id', 'pin-26');
|
|
|
|
|
|
|
|
var checkers_div = $("<div />").addClass("checkers").attr('id', 'checkers-26');
|
|
|
|
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-26');
|
|
|
|
|
|
|
|
checkers_div.append(checkers_list);
|
|
|
|
div.append(checkers_div);
|
|
|
|
$( "#pins" ).append(div);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convertToPins(init_board);
|
|
|
|
prev_board = convertToBoard();
|
2018-05-18 14:53:23 +00:00
|
|
|
|
|
|
|
$( ".checkers_list" ).sortable({
|
2018-05-18 18:53:45 +00:00
|
|
|
connectWith: ".checkers_list",
|
|
|
|
receive: function (event, ui) {
|
2018-05-19 12:10:31 +00:00
|
|
|
|
|
|
|
// Handle putting people on the bar correclty and getting off it.
|
|
|
|
// Something like look at the index you drop the checker at
|
|
|
|
|
|
|
|
|
|
|
|
board = convertToBoard();
|
|
|
|
|
|
|
|
prev_board = handleMove(prev_board, board);
|
|
|
|
|
|
|
|
convertToPins(prev_board);
|
|
|
|
|
|
|
|
if (board != convertToBoard()) {
|
|
|
|
prev_board = getBotMove(prev_board);
|
|
|
|
convertToPins(prev_board);
|
|
|
|
}
|
2018-05-18 18:53:45 +00:00
|
|
|
|
|
|
|
}
|
2018-05-17 22:49:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-19 12:10:31 +00:00
|
|
|
function handleMove(prev_board, board) {
|
|
|
|
var new_board = postRequest(prev_board, board);
|
|
|
|
return new_board;
|
2018-05-18 18:53:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-17 22:49:54 +00:00
|
|
|
function convertToBoard() {
|
2018-05-18 18:53:45 +00:00
|
|
|
var board = []
|
2018-05-17 22:49:54 +00:00
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
for (var i = 0; i < 26; i++) {
|
2018-05-17 22:49:54 +00:00
|
|
|
var amount = $( "#checkers_list-"+i ).children('li').length;
|
2018-05-18 18:53:45 +00:00
|
|
|
if (amount != 0) {
|
|
|
|
var sign = ($("#checkers_list-"+i+"> li").attr('class').split(' ')[0]);
|
|
|
|
board.push(sign*amount);
|
|
|
|
} else {
|
|
|
|
board.push(0);
|
|
|
|
}
|
2018-05-17 22:49:54 +00:00
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
}
|
|
|
|
// console.log(board);
|
|
|
|
return board
|
2018-05-17 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 12:10:31 +00:00
|
|
|
function emptyLists() {
|
|
|
|
for (var i=0; i < 26; i++) {
|
|
|
|
$( '#checkers_list-'+i ).empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
function convertToPins(board ) {
|
2018-05-19 12:10:31 +00:00
|
|
|
console.log(board);
|
2018-05-18 18:53:45 +00:00
|
|
|
|
2018-05-19 12:10:31 +00:00
|
|
|
emptyLists();
|
|
|
|
|
|
|
|
for (var i = 0; i < 26; i++) {
|
|
|
|
var tmp = parseInt(board[i]);
|
|
|
|
for (var amt_at_pin = 0; amt_at_pin < Math.abs(tmp); amt_at_pin++) {
|
|
|
|
var sign = Math.sign(tmp);
|
2018-05-17 22:49:54 +00:00
|
|
|
if (sign > 0) {
|
2018-05-19 12:10:31 +00:00
|
|
|
var list_obj = $( "<li><img src='black.png' width='40'></li>").addClass(Math.sign(tmp).toString());
|
2018-05-17 22:49:54 +00:00
|
|
|
} else if (sign < 0) {
|
2018-05-19 12:10:31 +00:00
|
|
|
var list_obj = $( "<li><img src='white.png' width='40'></li>").addClass(Math.sign(tmp).toString());
|
2018-05-17 22:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$( '#checkers_list-'+i ).append(list_obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function onReceived(data) {
|
2018-05-19 12:10:31 +00:00
|
|
|
var obj = JSON.parse(JSON.stringify(data));
|
|
|
|
alert(obj['board']);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getBotMove(board) {
|
|
|
|
|
|
|
|
var data = {'board' : board.toString()}
|
|
|
|
var result = "";
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type : "POST",
|
|
|
|
async: false,
|
|
|
|
url : "http://127.0.0.1:5000/bot_move",
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
success: function(data) {
|
|
|
|
result = data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result.split(',');
|
2018-05-17 22:49:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-19 12:10:31 +00:00
|
|
|
|
2018-05-18 18:53:45 +00:00
|
|
|
function getRequest() {
|
2018-05-17 22:49:54 +00:00
|
|
|
$.ajax({
|
2018-05-18 14:53:23 +00:00
|
|
|
url: 'http://127.0.0.1:5000/get_board',
|
2018-05-19 12:10:31 +00:00
|
|
|
async: false,
|
2018-05-17 22:49:54 +00:00
|
|
|
dataType: 'JSONP',
|
|
|
|
data: {
|
|
|
|
format: 'json'
|
|
|
|
},
|
|
|
|
type: 'GET',
|
|
|
|
success: onReceived
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function onPost(data) {
|
|
|
|
console.log(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-05-19 12:10:31 +00:00
|
|
|
function postRequest(prev_board, board) {
|
|
|
|
console.log(prev_board);
|
|
|
|
var result = "";
|
|
|
|
var data = {'board' : board.toString(), 'prev_board': prev_board.toString()}
|
2018-05-17 22:49:54 +00:00
|
|
|
$.ajax({
|
|
|
|
type : "POST",
|
2018-05-19 12:10:31 +00:00
|
|
|
async: false,
|
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),
|
2018-05-19 12:10:31 +00:00
|
|
|
success: function(data) {
|
|
|
|
result = data;
|
|
|
|
}
|
2018-05-17 22:49:54 +00:00
|
|
|
});
|
2018-05-19 12:10:31 +00:00
|
|
|
console.log(result);
|
|
|
|
return result.split(',');
|
2018-05-17 22:49:54 +00:00
|
|
|
};
|