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

73
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;
@ -37,7 +38,7 @@ $(function () {
var div = $("<div style='background-color:"+color+"'/>").addClass("pin").addClass("pin-"+pos).attr('id', 'pin-'+i); 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_div = $("<div />").addClass("checkers").attr('id', 'checkers-'+i);
var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-'+i); var checkers_list = $("<ul />").addClass("checkers_list").attr('id', 'checkers_list-'+i).attr('pinId', i);
checkers_div.append(checkers_list); checkers_div.append(checkers_list);
@ -48,7 +49,7 @@ $(function () {
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);
@ -67,16 +68,51 @@ $(function () {
// Something like look at the index you drop the checker at // Something like look at the index you drop the checker at
board = convertToBoard();
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); prev_board = handleMove(prev_board, board);
convertToPins(prev_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);
} // }
} }
}); });
@ -86,7 +122,11 @@ $(function () {
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;
}; };
@ -97,18 +137,23 @@ function convertToBoard() {
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]);
if (i == 21) {
console.log(amount)
console.log(sign)
}
board.push(sign*amount); board.push(sign*amount);
} else { } else {
board.push(0); 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();
} }
} }