diff --git a/func.js b/func.js index 7736a4c..c440462 100644 --- a/func.js +++ b/func.js @@ -62,20 +62,31 @@ $(function () { $( ".checkers_list" ).sortable({ connectWith: ".checkers_list", receive: function (event, ui) { - console.log(prev_board); - handleMove(); + + // 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); + } - console.log(prev_board); } }); }); -function handleMove() { - var board = convertToBoard(); - postRequest(board); - prev_board = board; +function handleMove(prev_board, board) { + var new_board = postRequest(prev_board, board); + return new_board; }; @@ -96,16 +107,25 @@ function convertToBoard() { return board } -function convertToPins(board ) { +function emptyLists() { + for (var i=0; i < 26; i++) { + $( '#checkers_list-'+i ).empty(); + } +} - // 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]); +function convertToPins(board ) { + console.log(board); + + 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); if (sign > 0) { - var list_obj = $( "
  • ").addClass(Math.sign(board[i]).toString()); + var list_obj = $( "
  • ").addClass(Math.sign(tmp).toString()); } else if (sign < 0) { - var list_obj = $( "
  • ").addClass(Math.sign(board[i]).toString()); + var list_obj = $( "
  • ").addClass(Math.sign(tmp).toString()); } $( '#checkers_list-'+i ).append(list_obj); @@ -115,14 +135,34 @@ function convertToPins(board ) { function onReceived(data) { - var obj = JSON.stringify(data); - alert(obj); + 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(','); +}; + + + function getRequest() { $.ajax({ url: 'http://127.0.0.1:5000/get_board', + async: false, dataType: 'JSONP', data: { format: 'json' @@ -138,13 +178,19 @@ function onPost(data) { }; -function postRequest(board) { - - var data = {'board' : board.toString()} +function postRequest(prev_board, board) { + console.log(prev_board); + var result = ""; + var data = {'board' : board.toString(), 'prev_board': prev_board.toString()} $.ajax({ type : "POST", + async: false, url : "http://127.0.0.1:5000/post_board", data: JSON.stringify(data), - success: onPost + success: function(data) { + result = data; + } }); + console.log(result); + return result.split(','); };