Remove() function doesn't remove the element from BST - javascript

I'm trying to add a remove method to my BST But I just can't figure out what is wrong. I have defined the pointer variable using let but it's still not working. It seems like it doesn't update the root object.
const Node = (data, left = null, right = null) => {
return {data, left, right};
};
const Tree = array => {
const remDupsAndSort = array => {
const mergeSort = array => {
if(array.length <= 1) return array;
let leftArr = array.slice(0, array.length / 2);
let rightArr = array.slice(array.length / 2);
return merge(mergeSort(rightArr), mergeSort(leftArr))
};
const merge = (leftArr, rightArr) => {
let sorted = [];
while(leftArr.length && rightArr.length){
if(leftArr[0] < rightArr[0]){
sorted.push(leftArr.shift());
}else{
sorted.push(rightArr.shift());
}
};
return [...sorted, ...leftArr, ...rightArr]
};
return mergeSort([... new Set(array)])
};
array = remDupsAndSort(array);
const buildTree = (array, start, end) => {
if(start > end) return null;
let mid = Math.floor((start + end) / 2);
let node = Node(array[mid]);
node.left = buildTree(array, start, mid - 1);
node.right = buildTree(array, mid + 1, end);
return node;
};
const remove = value => {
if(!root) return root;
let pointer = root;
while(pointer){
if(value < pointer.data){
pointer = pointer.left
}
else if(value > pointer.data){
pointer = pointer.right;
}else{
if(!pointer.right && !pointer.left){
return null;
}
if(!pointer.left){
return pointer.right;
}else if(!pointer.right){
return pointer.left;
}else{
let nextBiggest = pointer.right;
while(nextBiggest.left){
nextBiggest = nextBiggest.left;
}
return pointer = nextBiggest;
}
}
}
};
let root = buildTree(array, 0, array.length - 1);
return {root, remove}
};
When I run the code it doesn't remove anything from the tree. What am I missing?

let pointer = root; will assign the value of root to the pointer variable. Changing the value of pointer will not change the value of the original variable though. The variable is an object, and changing the key values within that object will cause the original to change.
So you either need to set the value of each key individually (which won't work really in this case because you want to set the whole value to be null sometimes), or you can track the parent value as you traverse through the tree so that you can modify parent.left and parent.right (or go a completely different route make a recursive removal function)

Thanks to #abney317 the problem was solved by tracking the parent nodes. Although I'm not happiest with this code and it's not the most good looking code I've done, but I'm sharing it here just in case some one needed it.
const remove = value => {
if(!root) return root;
let pointer = root;
let parent = null;
while(pointer){
if(value < pointer.data){
parent = pointer;
pointer = pointer.left
}
else if(value > pointer.data){
parent = pointer;
pointer = pointer.right;
}else{
if(!pointer.right && !pointer.left){
if(pointer === parent.left) return parent.left = null;
if(pointer === parent.right) return parent.right = null;
}
if(!pointer.left){
if(pointer === parent.left) return parent.left = pointer.right
if(pointer === parent.right) return parent.right = pointer.right
}else if(!pointer.right){
if(pointer === parent.left) return parent.left = pointer.left
if(pointer === parent.right) return parent.right = pointer.left
}else{
let replacingNode = pointer.right;
let replacingParent = pointer;
while(replacingNode.left){
replacingParent = replacingNode;
replacingNode = replacingNode.left;
}
if(pointer === root){
replacingNode.right = root.right;
replacingNode.left = root.left;
root = replacingNode;
if(replacingNode === replacingParent.left) return replacingParent.left = null;
if(replacingNode === replacingParent.right) return replacingParent.right = null;
}
if(pointer === parent.left){
if(replacingNode === pointer.left){
replacingNode.left = null;
}else{
replacingNode.left = pointer.left;
}
if(replacingNode === pointer.right){
replacingNode.right = null;
}else{
replacingNode.right = pointer.right;
}
parent.left = replacingNode;
if(replacingNode === replacingParent.left) return replacingParent.left = null;
if(replacingNode === replacingParent.right) return replacingParent.right = null;
}
if(pointer === parent.right){
if(replacingNode === pointer.left){
replacingNode.left = null;
}else{
replacingNode.left = pointer.left;
}
if(replacingNode === pointer.right){
replacingNode.right = null;
}else{
replacingNode.right = pointer.right;
}
parent.right = replacingNode;
if(replacingNode === replacingParent.left) return replacingParent.left = null;
if(replacingNode === replacingParent.right) return replacingParent.right = null;
}
}
}
}
};

Related

why is this code taking too much time in lintcode?

Facing Time limit exceeded error for large test cases. Any optimization suggestions would be helpful. if possible please provide links to guide on how to optimize JavaScript code for performance. Thanks in advance
using my own implementation of Queue data structure since JavaScript by default doesn't have one.
wallsAndGates(grid) {
// write your code here
class QNode {
constructor(data){
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
this.length = 0;
}
enqueue(data) {
const newNode = new QNode(data);
if(this.front === null && this.rear === null) {
this.front = newNode;
this.rear = newNode;
}
else {
this.rear.next = newNode;
this.rear = newNode;
}
this.length += 1;
}
dequeue() {
if(this.front === null && this.rear === null)
return null;
else {
const data = this.front.data;
if (this.front === this.rear){
this.front = null;
this.rear = null;
}else {
this.front = this.front.next;
}
this.length -= 1;
return data;
}
}
peek() {
if (this.front === null) return null;
else return this.front.data;
}
}
const noOfRows = grid.length;
const noOfCols = grid[0].length;
const imutCord = (i, j) => `${i},${j}`;
const getCord = cord => cord.split(',').map(n => parseInt(n));
const blankAndGateNodes = new Set();
const gateNodes = new Queue();
for (let i = 0; i < noOfRows; i++){
for (let j = 0; j < noOfCols; j++){
const cord = imutCord(i, j);
if(grid[i][j] >= 0) blankAndGateNodes.add(cord);
if(grid[i][j] === 0) gateNodes.enqueue(cord);
}
}
const multiSourceBFS = (sourceQueue, visitNodes, grid) => {
const tempQueue = new Queue();
let noOfLayer = 0;
while (sourceQueue.length !== 0 || tempQueue.length !== 0){
while (sourceQueue.length !== 0){
const cord = sourceQueue.dequeue();
let i, j;
[i, j] = getCord(cord);
if(i >= 0 && j >= 0 && i < noOfRows && j < noOfCols && visitNodes.has(cord)){
visitNodes.delete(cord);
if(grid[i][j] > 0) grid[i][j] = noOfLayer;
tempQueue.enqueue(imutCord(i+1, j));
tempQueue.enqueue(imutCord(i, j+1));
tempQueue.enqueue(imutCord(i-1, j));
tempQueue.enqueue(imutCord(i, j-1));
}
}
while(tempQueue.length !== 0) sourceQueue.enqueue(tempQueue.dequeue());
noOfLayer++;
}
}
multiSourceBFS(gateNodes, blankAndGateNodes, grid);
}
lintcode link: https://www.lintcode.com/problem/663/

How to get string converted to array?

I am kind of new to this, but this is giving me a headache.
Heres my code what i am trying to solve:
function parseComponents(data: any, excelAttributesArray: string[]) {
let confs = "";
let isValues = false;
let listValues = "";
if (!data) return confs;
if (data["Part"]) {
const part = data["Part"];
excelAttributesArray.forEach((attribute) => {
let attributeValue = part[attribute];
if (attributeValue !== null) isValues = true;
if (attributeValue !== null && attributeValue.Value) {
attributeValue = attributeValue.Value;
}
listValues += attributeValue + ",";
});
const number = part["Number"];
if (isValues) {
confs += `${listValues}${number}`;
}
}
if (data["Components"] && data["Components"].length > 0) {
for (let i = 0; i < data["Components"].length; i++) {
const tmp = parseComponents(
data["Components"][i],
excelAttributesArray
);
if (tmp && tmp.length > 0)
confs += (confs.length > 0 ? "|" : "") + tmp;
}
}
return confs;
}
The confs value is returned as a string. How do i get it return as array?

How can I stop this from writing a value to all dictionaries? (Javascript)

Intro
So I have stumbled across this problem yesterday, and I've been stumped trying to figure this out myself. I only have this problem with JavaScript.
Problem
I've been creating a programming language with JS since the start of this week, and I was enjoying myself the entire week making it. The problem here is that when I carry dictionaries across functions, it has some problems reading and writing into those dictionaries. When I check the console logs, I see that not just the dictionary in the function was written, but all of the instances of that dictionary were written.
Code
Here is the code to graph.js:
function codeToArray(code) {
let codeArray = {commands:[], vars:{x:{value:0, read_only:true},y:{value:0, read_only:false}}};
let commands = code.split("\n");
for (i = 0; i < commands.length; i++) {
let command = commands[i];
let inputs = command.split(" ");
if (inputs[0].toLowerCase() !== ";") {
// Arithmetics
if (inputs[0].toLowerCase() === "+") { // Addition
codeArray.commands.push({name:"add", variable:inputs[1], value:inputs[2], syntax_id:1});
} else if (inputs[0].toLowerCase() === "-") { // Subtraction
codeArray.commands.push({name:"subtract", variable:inputs[1], value:inputs[2], syntax_id:1});
} else if (inputs[0].toLowerCase() === "*") { // Multiplication
codeArray.commands.push({name:"multiply", variable:inputs[1], value:inputs[2], syntax_id:1});
} else if (inputs[0].toLowerCase() === "/") { // Division
codeArray.commands.push({name:"divide", variable:inputs[1], value:inputs[2], syntax_id:1});
}
// I/O
else if (inputs[0].toLowerCase() === ":") { // Set
codeArray.commands.push({name:"set", variable:inputs[1], value:inputs[2], syntax_id:1});
}
// Conditional Statements
else if (inputs[0].toLowerCase() === "if") { // If Statement
let ifCommand = "";
for (j = 4; j < inputs.length; j++) {
if (j > 4) {
ifCommand += " " + inputs[j];
} else {
ifCommand += inputs[j];
}
}
let ifCommandArray = codeToArray(ifCommand).commands[0];
codeArray.commands.push({name:"if", value1:inputs[1], condition:inputs[2], value2:inputs[3], command:ifCommandArray, syntax_id:2});
}
}
}
return codeArray
}
function parseValue(value, variables) {
let return_value = value;
console.log(value);
console.log(variables);
if (value.charAt(0) === "$") {
let variable_name = return_value.substring(1, value.length);
console.log(variable_name);
let variable = variables[variable_name];
console.log(variable);
if (variable === undefined) {
return_value = NaN;
} else {
return_value = variable.value;
}
} else {
return_value = parseFloat(value);
}
console.log(return_value);
return return_value
}
function runCodeArray(commands, variables) {
for (i = 0; i < commands.length; i++) {
let command = commands[i];
if (command.syntax_id === 1) { // Simple Syntax (Variable Value Syntax)
if (command.name === "add") { // Addition
let variable = variables[command.variable];
if (variable === undefined) {
let error_message = `Variable cannot be found (line ${i+1} ignoring comments)`;
return {commands:commands, variables:variables, return_message:error_message};
}
if (variable.read_only === true) {
let error_message = `A read-only variable was trying to be written (line ${i+1} ignoring comments)`;
return {commands:commands, variables:variables, return_message:error_message};
}
let value = parseValue(command.value, variables);
if (value === NaN) {
let error_message = `The value parameter is invalid (line ${i+1} ignoring comments)`;
return {commands:commands, variables:variables, return_message:error_message};
}
variable.value += value;
} else if (command.name === "set") { // Set
let variable = variables[command.variable];
if (variable === undefined) {
variables[command.variable] = {value:0, read_only:false};
variable = variables[command.variable];
}
if (variable.read_only === true) {
let error_message = `A read-only variable was trying to be written (line ${i+1} ignoring comments)`;
return {commands:commands, variables:variables, return_message:error_message};
}
let value = parseValue(command.value, variables);
if (value === NaN) {
let error_message = `The value parameter is invalid (line ${i+1} ignoring comments)`;
return {commands:commands, variables:variables, return_message:error_message};
}
variable.value = value;
}
}
}
return {commands:commands, variables:variables, return_message:true};
}
var url_string = ...graph.html?pattern=%3A+a+42%0D%0A%3A+b+%24a%0D%0A%3A+c+%24b%0D%0A // window.location.href;
var url = new URL(url_string);
var pattern = url.searchParams.get("pattern");
let codeArray = codeToArray(pattern);
// console.log(codeArray);
let codeArrayOut = runCodeArray(codeArray.commands, codeArray.vars); // Will return true in return_message if everything is good, and will return a string in return_message if an error occurs.
// console.log(codeArrayOut);
if (codeArrayOut.return_message !== true) {
alert("Error: " + codeArrayOut.return_message);
}
Sorry if the code is too long, boring or messy for you to read. Here is the function that's causing the most problems:
function parseValue(value, variables) {
let return_value = value;
console.log(value);
console.log(variables);
if (value.charAt(0) === "$") {
let variable_name = return_value.substring(1, value.length);
console.log(variable_name);
let variable = variables[variable_name];
console.log(variable);
if (variable === undefined) {
return_value = NaN;
} else {
return_value = variable.value;
}
} else {
return_value = parseFloat(value);
}
console.log(return_value);
return return_value
}
Outro
I'm still learning in JavaScript, so I hope that you can solve this problem (because I can't).

Optimization for Binary Search Tree 'remove' function

I just finished my first binary search tree remove function and it is in major need of optimization. I have spent a lot of time on this and this was the best I could manage. Is there a easier way to do this? Does anyone have any suggestions for optimization? To me, it just seems like it will necessarily be massive code.
For starters...
My Binary Search Tree...
function BST() {
this.root = null;
}
My 'remove' function...
BST.prototype.remove = function(data) {
if(this.root.data === data){
var curr = this.root.left;
while(true){
if(curr.right.left === null && curr.right.right === null){
this.root.data = curr.right.data;
curr.right = null;
break;
}
curr = curr.right;
}
}
var curr = this.root;
var found_data = this.find(data);
if(found_data.left !== null && found_data.right !== null){
var runner = found_data.right;
var runner_prev = found_data;
while(true){
if(runner.left === null && runner.right === null){
found_data.data = runner.data;
if(runner_prev.left === runner){
runner_prev.left = null;
}else{
runner_prev.right = null;
}
break;
}
runner_prev = runner;
runner = runner.left;
}
}else if(found_data.left === null || found_data.right === null){
var prev = this.prev(found_data.data);
if(prev.right === found_data){
if(found_data.left){
prev.right = found_data.left;
}else{
prev.right = found_data.right;
}
}else{
if(found_data.left){
prev.left = found_data.left;
}else{
prev.left = found_data.right;
}
}
}else{
var prev = this.prev(found_data.data);
if(prev.left === found_data){
prev.left = null;
}else{
prev.right = null;
}
}
};
You will notice that I use supporting functions within my remove() function, such as prev() and find() They are apart of my overarching BST() function and can be used anywhere within is by prefacing it using this..
Supporting functions I use within remove() (prev() and find())
BST.prototype.find = function(data) {
if(this.root === null){
return 'wrong';
}
var curr = this.root;
while(true){
if(data > curr.data){
curr = curr.right;
}else if(data < curr.data){
curr = curr.left;
}else{
if(curr.data enter code here=== data){
return curr;
}else{
return 'not here player'
}
}
}
}
BST.prototype.prev = function(data){
if(this.root === null){
return false;
}
var prev = this.root;
var curr = this.root;
while(true){
if(curr.left === null && curr.right === null){
return prev;
}
if(data < curr.data){
prev = curr;
curr = curr.left;
}else if(data > curr.data){
prev = curr;
curr = curr.right;
}else{
return prev;
}
}
}
This algorithm absolutely works, but as you can imagine, this is not the type of monster you would want to be answering a whiteboard interview question with.
It would be more efficient if you either:
Combine prev() and find() by returning both the previous node and the found node from find()
Give each node a parent pointer, and just follow it to find prev

Tic Tac Toe with Minimax and Javascript

I am attempting to create a Tic Tac Toe game using Javascript as part of my learning on FreeCodeCamp and after my 5th attempt still haven't managed to get it to work. I think i'm doing the correct thing, but the computer AI is still making very stupid decisions and loosing.
Here is my entire AI function, which can be called using a console log to see the recommended move from the AI. However when i hard code values for moves already taken and ask for the next move, it doesn't pick what i would expect.
/*
Attempt to inplement a Tic Tac Toe minimax game
5th attempt, so hopefully this time it works!
*/
var util = require("util");
//These are all the winning square sequences, as string to make various things easier
var validRows = [
['00','01','02'], // left column
['10','11','12'], // middle column
['20','21','22'], // right column
['00','10','20'], // top row
['01','11','21'], // middle row
['02','12','22'], // bottom row
['00','11','22'], // Diag TL to BR
['20','11','02'] // Diag BL to TR
];
//Our scoring arrays for evaulating moves
var max1 = ['100','010','001'];
var min1 = ['200','020','002'];
var max2 = ['110','011'];
var min2 = ['220','022'];
var max3 = ['111'];
var min3 = ['222'];
//All the valid squares
var validSquaresFactory = ['00','10','20','01','11','21','02','12','22'];
//Store all the moves somewhere
//var computerMoves = ['10','22'];
//var userMoves = ['00','02'];
//Store all the moves somewhere
var computerMoves = ['11','22'];
var userMoves = ['00','01'];
function Board(minOrMax,computerMoves,userMoves){
this.computer = computerMoves;
this.user = userMoves;
this.minOrMax = minOrMax;
this.remaining = this.genRemaining();
this.complete = this.genComplete();
var results = this.getScore();
this.score = results[0];
this.winOrLoose = results[1];
}
Board.prototype.genRemaining = function(){
//Create an array of all moves taken
var takenMoves = this.computer.concat(this.user);
//Calculate all remaining squares
var validSquares = validSquaresFactory.filter(function(object){
return takenMoves.indexOf(object) === -1;
});
return validSquares;
}
Board.prototype.genComplete = function(){
return ((this.computer.length + this.user.length) === 9);
}
Board.prototype.flipMinOrMax = function(){
return (this.minOrMax === 'max') ? 'min' : 'max'
}
Board.prototype.genArrays = function(minOrMax,square){
var tempUser = this.user.slice(0);
var tempComputer = this.computer.slice(0);
if(minOrMax === 'min'){
tempUser.push(square);
} else {
tempComputer.push(square);
}
return [tempComputer,tempUser];
}
Board.prototype.generateBoards = function(minOrMax){
var boards = [];
var that = this;
this.remaining.forEach(function(remainingSquare){
var moves = that.genArrays(minOrMax,remainingSquare);
boards.push(new Board(minOrMax,moves[0],moves[1]));
})
//console.log(boards);
return boards;
}
Board.prototype.getScore = function(){
var that = this;
var winOrLoose = false;
var returnScore = validRows.reduce(function(storage,row,index,array){
var score = row.reduce(function(storage1,square){
if (that.computer.indexOf(square) !== -1) {
storage1 += '1';
} else if (that.user.indexOf(square) !== -1) {
storage1 += '2';
} else {
storage1 += '0';
}
return storage1;
},'')
var finalScore = 0;
if(max1.indexOf(score) != -1){
finalScore = 1;
} else if(min1.indexOf(score) != -1){
finalScore = -1;
} else if(max2.indexOf(score) != -1){
finalScore = 10;
} else if(min2.indexOf(score) != -1){
finalScore = -10;
} else if(max3.indexOf(score) != -1){
winOrLoose = true;
finalScore = 100;
} else if(min3.indexOf(score) != -1){
winOrLoose = false;
finalScore = -100;
}
storage.push(finalScore);
return storage;
},[])
var condensedReturnScore = returnScore.reduce(function(storage,score){
return storage+score;
})
return [condensedReturnScore,winOrLoose];
}
function generateMove(){
var board = new Board('max',computerMoves,userMoves);
var scores = [];
var boards = board.generateBoards('max');
boards.forEach(function(board){
scores.push(testMove(board,4));
});
scores = scores.map(function(score,index){
return [board.remaining[index],score];
});
var returnValue = scores.reduce(function(storage,score){
return (score[1].score > storage[1].score) ? score : storage;
});
return [returnValue[0],returnValue[1].score];
}
function testMove(masterBoard,count){
count --;
var boards = [];
boards = masterBoard.generateBoards(generateMinOrMax(masterBoard.minOrMax));
//console.log('/////////Master Board/////////');
//console.log(masterBoard);
//console.log(boards);
//console.log('//////////////////////////////');
boards = boards.map(function (move) {
if (move.complete === true || count === 0 || move.winOrLoose === true){
return move;
} else {
var returnScore = testMove(move,count);
return returnScore;
}
});
returnBoard = boards.reduce(function(storage,board,index,array){
if(board.minOrMax === 'max'){
return (board.score > storage.score) ? board : storage;
} else {
return (board.score < storage.score) ? board : storage;
}
});
return returnBoard;
}
function generateMinOrMax(minOrMax){
return (minOrMax === 'max') ? 'min' : 'max'
}
I've checked the scoring function and from what i can see it is returning the expected score for any move i try, but because of the shear number of possibilities calculated it's very hard to debug efficiently.
Any help/pointers on this would be most appreciated as i have really hit a brick wall with this, can't see the forrest for the trees e.t.c
If you would like to test this with the GUI, it's on codepen at - http://codepen.io/gazzer82/pen/yYZmvJ?editors=001
So after banging my head against this for day, as soon as i posted this i found the issues. Firstly using the wrong variable for minormax in my reduce function, so it wasn't flipping correctly and not setting the winOrLoose value correctly for a score of -100. Here is the corrected version.
*
Attempt to inplement a Tic Tac Toe minimax game
5th attempt, so hopefully this time it works!
*/
var util = require("util");
//These are all the winning square sequences, as string to make various things easier
var validRows = [
['00','01','02'], // left column
['10','11','12'], // middle column
['20','21','22'], // right column
['00','10','20'], // top row
['01','11','21'], // middle row
['02','12','22'], // bottom row
['00','11','22'], // Diag TL to BR
['20','11','02'] // Diag BL to TR
];
//Our scoring arrays for evaulating moves
var max1 = ['100','010','001'];
var min1 = ['200','020','002'];
var max2 = ['110','011'];
var min2 = ['220','022'];
var max3 = ['111'];
var min3 = ['222'];
//All the valid squares
var validSquaresFactory = ['00','10','20','01','11','21','02','12','22'];
//Store all the moves somewhere
//var computerMoves = ['10','22'];
//var userMoves = ['00','02'];
//Store all the moves somewhere
var computerMoves = ['00','20','01'];
var userMoves = ['10','11','02'];
//01,21,22 - 01//
function Board(minOrMax,computerMoves,userMoves){
this.computer = computerMoves;
this.user = userMoves;
this.minOrMax = minOrMax;
this.remaining = this.genRemaining();
this.complete = this.genComplete();
var results = this.getScore();
this.score = results[0];
this.winOrLoose = results[1];
}
Board.prototype.genRemaining = function(){
//Create an array of all moves taken
var takenMoves = this.computer.concat(this.user);
//Calculate all remaining squares
var validSquares = validSquaresFactory.filter(function(object){
return takenMoves.indexOf(object) === -1;
});
return validSquares;
}
Board.prototype.genComplete = function(){
return ((this.computer.length + this.user.length) === 9);
}
Board.prototype.flipMinOrMax = function(){
return (this.minOrMax === 'max') ? 'min' : 'max'
}
Board.prototype.genArrays = function(minOrMax,square){
var tempUser = this.user.slice(0);
var tempComputer = this.computer.slice(0);
if(minOrMax === 'min'){
tempUser.push(square);
} else {
tempComputer.push(square);
}
return [tempComputer,tempUser];
}
Board.prototype.generateBoards = function(minOrMax){
var boards = [];
var that = this;
this.remaining.forEach(function(remainingSquare){
var moves = that.genArrays(minOrMax,remainingSquare);
boards.push(new Board(minOrMax,moves[0],moves[1]));
})
//console.log(boards);
return boards;
}
Board.prototype.getScore = function(){
var that = this;
var winOrLoose = false;
var returnScore = validRows.reduce(function(storage,row,index,array){
var score = row.reduce(function(storage1,square){
if (that.computer.indexOf(square) !== -1) {
storage1 += '1';
} else if (that.user.indexOf(square) !== -1) {
storage1 += '2';
} else {
storage1 += '0';
}
return storage1;
},'')
var finalScore = 0;
if(max1.indexOf(score) != -1){
finalScore = 1;
} else if(min1.indexOf(score) != -1){
finalScore = -1;
} else if(max2.indexOf(score) != -1){
finalScore = 10;
} else if(min2.indexOf(score) != -1){
finalScore = -10;
} else if(max3.indexOf(score) != -1){
winOrLoose = true;
finalScore = 100;
} else if(min3.indexOf(score) != -1){
winOrLoose = true;
finalScore = -100;
}
storage.push(finalScore);
return storage;
},[])
var condensedReturnScore = returnScore.reduce(function(storage,score){
return storage+score;
})
return [condensedReturnScore,winOrLoose];
}
function generateMove() {
var board = new Board('max', computerMoves, userMoves);
if (board.remaining.length === 1) {
return [board.remaining[0], 0];
} else {
var scores = [];
var boards = board.generateBoards('max');
boards.forEach(function (board) {
scores.push(testMove(board, 4));
});
scores = scores.map(function (score, index) {
return [board.remaining[index], score];
});
var returnValue = scores.reduce(function (storage, score) {
return (score[1].score > storage[1].score) ? score : storage;
});
return [returnValue[0], returnValue[1].score];
}
}
function testMove(masterBoard,count){
count --;
var boards = [];
boards = masterBoard.generateBoards(generateMinOrMax(masterBoard.minOrMax));
boards = boards.map(function (move) {
if (move.complete === true || count <= 0 || move.winOrLoose === true){
return move;
} else {
var returnScore = testMove(move,count);
return returnScore;
}
});
returnBoard = boards.reduce(function(storage,board,index,array){
if(generateMinOrMax(masterBoard.minOrMax) === 'max'){
return (board.score > storage.score) ? board : storage;
} else {
return (board.score < storage.score) ? board : storage;
}
});
return returnBoard;
}
function generateMinOrMax(minOrMax){
return (minOrMax === 'max') ? 'min' : 'max'
}
function getScore(user,computer,minOrMax){
var that = this;
that.computer = computer;
that.user = user;
that.minOrMax = minOrMax;
var returnScore = validRows.reduce(function(storage,row,index,array){
var score = row.reduce(function(storage1,square){
if (that.computer.indexOf(square) !== -1) {
storage1 += '1';
} else if (that.user.indexOf(square) !== -1) {
storage1 += '2';
} else {
storage1 += '0';
}
return storage1;
},'')
var finalScore = 0;
if(max1.indexOf(score) != -1){
finalScore = 1;
} else if(min1.indexOf(score) != -1){
finalScore = -1;
} else if(max2.indexOf(score) != -1){
finalScore = 10;
} else if(min2.indexOf(score) != -1){
finalScore = -10;
} else if(max3.indexOf(score) != -1){
finalScore = 100;
} else if(min3.indexOf(score) != -1){
finalScore = -100;
}
storage.push(finalScore);
return storage;
},[])
var condensedReturnScore = returnScore.reduce(function(storage,score){
if(that.minOrMax === 'max'){
return (score >= storage) ? score : storage;
} else {
return (score <= storage) ? score : storage;
}
})
return condensedReturnScore;
}

Categories

Resources