Proper moving is slowly taking shape

This commit is contained in:
Alexander Munch-Hansen 2018-05-19 22:01:27 +02:00
parent 205dd2a699
commit c51f7c0b94

77
func.js
View File

@ -2,18 +2,32 @@ var prev_board = []
$(function () { $(function () {
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 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,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] // 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]
const red = "#E83D04" const red = "#E83D04"
const white = "#E8E077" const white = "#E8E077"
const brown = "#4C2E00" const brown = "#4C2E00"
var prev_board = [] var prev_board = []
var color = white; var color = white;
var quad = false;
// var roll = [Math.floor(Math.random() * 6)+1, Math.floor(Math.random() * 6)+1]
var roll = [1,3];
var total_roll = roll[0] + roll[1];
if (roll[0] == roll[1]) {
total_roll = roll[0] * 4;
roll = [roll[0], roll[0], roll[0], roll[0]];
quad = true;
}
console.log("Initial roll:");
console.log(roll);
// Build pins // Build pins
for (var i = 0; i < 26; i++) { for (var i = 0; i < 26; i++) {
if (i < 13 && 0 < i) { if (i < 13 && 0 < i) {
@ -98,35 +112,51 @@ $(function () {
} }
} }
// console.log("lol");
// console.log(prev_board[fromIdx]);
// console.log(prev_board[toIdx]);
board = convertToBoard(); board = convertToBoard();
console.log("board!!!!"); console.log("board!!!!");
console.log(board); console.log(board);
prev_board = handleMove(prev_board, board); var data_stuff = handleMove(prev_board, board, roll, total_roll);
prev_board = data_stuff['board'];
roll = data_stuff['roll'];
total_roll = data_stuff['total_roll'];
convertToPins(prev_board); convertToPins(prev_board);
// if (board != convertToBoard()) {
// prev_board = getBotMove(prev_board); if (total_roll == 0) {
// convertToPins(prev_board); if (board != convertToBoard()) {
// } prev_board = getBotMove(prev_board);
convertToPins(prev_board);
roll = [Math.floor(Math.random() * 6)+1, Math.floor(Math.random() * 6)+1]
total_roll = roll[0] + roll[1]
if (roll[0] == roll[1]) {
total_roll = roll[0] * 4;
roll = [roll[0], roll[0], roll[0], roll[0]]
quad = true;
}
}
}
} }
}); });
}); });
function handleMove(prev_board, board) { function handleMove(prev_board, board, roll, total_roll) {
var new_board = postRequest(prev_board, board); var data = postRequest(prev_board, board, roll, total_roll);
test = []; var new_board = data['board'];
total_roll = data['total_roll'];
roll = data['roll'];
var test = [];
for (var i = 0; i<26; i++) { for (var i = 0; i<26; i++) {
test.push(parseInt(new_board[i])); test.push(parseInt(new_board[i]));
} }
return test;
return {'board': test, 'total_roll': total_roll, 'roll': roll};
}; };
@ -223,10 +253,10 @@ function onPost(data) {
}; };
function postRequest(prev_board, board) { function postRequest(prev_board, board, roll, total_roll) {
console.log(prev_board); console.log(prev_board);
var result = ""; var result = "";
var data = {'board' : board.toString(), 'prev_board': prev_board.toString()} var data = {'board' : board.toString(), 'prev_board': prev_board.toString(), 'roll': roll.toString(), 'total_roll': total_roll.toString()}
$.ajax({ $.ajax({
type : "POST", type : "POST",
async: false, async: false,
@ -237,5 +267,6 @@ function postRequest(prev_board, board) {
} }
}); });
console.log(result); console.log(result);
return result.split(','); var split_result = result.split("#");
return {'board': split_result[0].split(","),'total_roll': split_result[1], 'roll': split_result[2].split(",")};
}; };