Can now sorta handle the bar

This commit is contained in:
Alexander Munch-Hansen 2018-05-19 21:05:03 +02:00
parent 3e8f5987aa
commit 205dd2a699

161
func.js
View File

@ -2,10 +2,11 @@ var prev_board = []
$(function () { $(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]; var init_board = [0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 1, 0, 4, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0];
var red = "#E83D04" // var init_board = [0,2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
var white = "#E8E077" const red = "#E83D04"
var brown = "#4C2E00" const white = "#E8E077"
const brown = "#4C2E00"
var prev_board = [] var prev_board = []
var color = white; var color = white;
@ -15,40 +16,40 @@ $(function () {
for (var i = 0; i < 26; i++) { for (var i = 0; i < 26; i++) {
if (i < 13 && 0 < i) { if (i < 13 && 0 < i) {
var pos = 'bottom'; var pos = 'bottom';
} else if (i > 12 && i < 25) { } else if (i > 12 && i < 25) {
var pos = 'top'; var pos = 'top';
} else { } else {
var pos = 'bar'; var pos = 'bar';
} }
if (color == white) { if (color == white) {
color = red; color = red;
} else { } else {
color = white; color = white;
} }
if (i == 0 || i == 25) { if (i == 0 || i == 25) {
color = brown; color = brown;
}
var div = $("<div style='background-color:"+color+"'/>").addClass("pin").addClass("pin-"+pos).attr('id', 'pin-'+i);
var checkers_div = $("<div />").addClass("checkers").attr('id', 'checkers-'+i);
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-'+i).attr('pinId', i);
checkers_div.append(checkers_list);
div.append(checkers_div);
$( "#pins" ).append(div);
} }
var div = $("<div style='background-color:"+color+"'/>").addClass("pin").addClass("pin-"+pos).attr('id', 'pin-'+i);
var checkers_div = $("<div />").addClass("checkers").attr('id', 'checkers-'+i);
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-'+i);
checkers_div.append(checkers_list);
div.append(checkers_div);
$( "#pins" ).append(div);
}
var div = $("<div style='background-color:black'/>").addClass("pin").addClass("pin-26").attr('id', 'pin-26'); 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_div = $("<div />").addClass("checkers").attr('id', 'checkers-26');
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-26'); var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-26').attr('pinId', 26);
checkers_div.append(checkers_list); checkers_div.append(checkers_list);
div.append(checkers_div); div.append(checkers_div);
@ -60,33 +61,72 @@ $(function () {
prev_board = convertToBoard(); prev_board = convertToBoard();
$( ".checkers_list" ).sortable({ $( ".checkers_list" ).sortable({
connectWith: ".checkers_list", connectWith: ".checkers_list",
receive: function (event, ui) { receive: function (event, ui) {
// Handle putting people on the bar correclty and getting off it. // Handle putting people on the bar correclty and getting off it.
// Something like look at the index you drop the checker at // Something like look at the index you drop the checker at
board = convertToBoard();
prev_board = handleMove(prev_board, board);
convertToPins(prev_board);
var toIdx = event.target.attributes.pinId.value;
var fromIdx = ui.sender.attr("pinId");
console.log(fromIdx);
console.log(toIdx);
if (toIdx < 26) {
var cloned_board = prev_board.slice();
var sign = Math.sign(cloned_board[fromIdx]);
// if bar stuff has to be handled
if (cloned_board[toIdx] * sign == -1) {
// Lift checker
cloned_board[fromIdx] += -(sign);
// Check where the opponents bar is
if (sign == -1) {
cloned_board[0] += -(sign);
} else {
cloned_board[25] += -(sign);
}
// flip the piece at target index
cloned_board[toIdx] *= -1
// Load the board into the pins
convertToPins(cloned_board);
}
}
// console.log("lol");
// console.log(prev_board[fromIdx]);
// console.log(prev_board[toIdx]);
board = convertToBoard();
console.log("board!!!!");
console.log(board);
prev_board = handleMove(prev_board, board);
convertToPins(prev_board);
if (board != convertToBoard()) { // if (board != convertToBoard()) {
prev_board = getBotMove(prev_board); // prev_board = getBotMove(prev_board);
convertToPins(prev_board); // convertToPins(prev_board);
} // }
} }
}); });
}); });
function handleMove(prev_board, board) { function handleMove(prev_board, board) {
var new_board = postRequest(prev_board, board); var new_board = postRequest(prev_board, board);
return new_board; test = [];
for (var i = 0; i<26; i++) {
test.push(parseInt(new_board[i]));
}
return test;
}; };
@ -94,22 +134,27 @@ function convertToBoard() {
var board = [] var board = []
for (var i = 0; i < 26; i++) { for (var i = 0; i < 26; i++) {
var amount = $( "#checkers_list-"+i ).children('li').length; var amount = $( "#checkers_list-"+i ).children('li').length;
if (amount != 0) { if (amount != 0) {
var sign = ($("#checkers_list-"+i+"> li").attr('class').split(' ')[0]); var sign = ($("#checkers_list-"+i+"> li").attr('class').split(' ')[0]);
board.push(sign*amount); if (i == 21) {
} else { console.log(amount)
board.push(0); console.log(sign)
} }
board.push(sign*amount);
} else {
board.push(0);
}
} }
// console.log(board); console.log("The built board");
console.log(board);
return board return board
} }
function emptyLists() { function emptyLists() {
for (var i=0; i < 26; i++) { for (var i=0; i < 27; i++) {
$( '#checkers_list-'+i ).empty(); $( '#checkers_list-'+i ).empty();
} }
} }