Skip to content

add MinMax game theory JS #1193 commented out test case #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Backtracking/MinMax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
*
* Min Max problem
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a proper JSDoc comment.

* See https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introduction/
* Min Max problem is a problem where we need to find the maximum score of an event by comparing all * * possible moves in the game
*
*/

// nodeIndex is index of current node in scores[].
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is messy. There are no Python-like doc comments in this repo.

// if move is of maximizer return true else false
// leaves of game tree is stored in scores[]
// this.height is maximum this.height of Game tree

// depth is current depth in game tree.
// nodeIndex is index of current node in scores[].
// scores[] contains the leaves of game tree.
// this.height is maximum this.height of game tree

// >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
// >>> this.height = math.log(len(scores), 2)
// >>> minimax(0, 0, True, scores, this.height)
// 65
// >>> minimax(-1, 0, True, scores, this.height)
// Traceback (most recent call last):
// ...
// ValueError: Depth cannot be less than 0
// >>> minimax(0, 0, True, [], 2)
// Traceback (most recent call last):
// ...
// ValueError: Scores cannot be empty
// >>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
// >>> this.height = math.log(len(scores), 2)
// >>> minimax(0, 0, True, scores, this.height)
// 12

class MinMax {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not prefix this with export (perhaps even export default) rather than exporting below?

constructor (scores, depth, nodeIndex, isMax) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor is completely superfluous - you're passing all these as arguments to solve later on. I'm questioning whether this should be a class at all - IMO it should just be a single solve function that checks it parameters and recurses.

if (scores.length === 0) {
throw RangeError('Scores cannot be empty')
}
if (depth < 0) {
throw RangeError("Depth can't be less than zero")
}
this.scores = scores
this.height = Math.log2(scores.length)
this.depth = depth
this.nodeIndex = nodeIndex
this.isMax = isMax
this.answer = -1
}

solve (depth, nodeIndex, isMax, scores, height) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to pass all of these as parameters - you already have them as instance variables.

if (depth === height) {
return scores[nodeIndex]
}

if (isMax) {
return Math.max(
this.solve(depth + 1, nodeIndex * 2, false, scores, height),
this.solve(depth + 1, nodeIndex * 2 + 1, false, scores, height)
)
}

return Math.min(
this.solve(depth + 1, nodeIndex * 2, true, scores, height),
this.solve(depth + 1, nodeIndex * 2 + 1, true, scores, height)
)
}

get_ans () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in lowerCamelCase

this.answer = this.solve(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing the answer in the class is dirty IMO; why not just return it?

this.depth,
this.nodeIndex,
this.isMax,
this.scores,
this.height
)
}
}

// MinMax(scores, depth, nodeindex, ismax)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outcommented code ought to be removed.

// const _newMinMax = new MinMax([90, 23, 6, 33, 21, 65, 123, 34423],0,0,true)
// _newMinMax.get_ans()
// console.log(_newMinMax.answer)

export { MinMax }
29 changes: 29 additions & 0 deletions Backtracking/tests/MinMax.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MinMax } from '../MinMax'

describe('MinMax', () => {
it('should return 65 for MinMax([90, 23, 6, 33, 21, 65, 123, 34423],0,0,true)', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is practically equivalent to the below test. You're also duplicating the contents in the description. Please see the TypeScript guide for writing good tests.

const _newMinMax = new MinMax([90, 23, 6, 33, 21, 65, 123, 34423], 0, 0, true)
_newMinMax.get_ans()
expect(_newMinMax.answer).toEqual(65)
})

it('should return 12 for MinMax([3, 5, 2, 9, 12, 5, 23, 23],0,0,true)', () => {
const _newMinMax = new MinMax([3, 5, 2, 9, 12, 5, 23, 23], 0, 0, true)
_newMinMax.get_ans()
expect(_newMinMax.answer).toEqual(12)
})

it('should throw RangeError for empty Board', () => {
expect(() => {
return new MinMax([], 0, 0, true).toThrow(RangeError)
})
})

it('should throw RangeError for negative depth', () => {
expect(() => {
return new MinMax([12, 23, 434, 66, 6776], -1, 0, true).toThrow(
RangeError
)
})
})
})
6 changes: 3 additions & 3 deletions Project-Euler/test/Problem044.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('checking nth prime number', () => {
})
// Project Euler Second Value for Condition Check
// FIXME skip this test for now because it runs very long and clogs up the CI & pre-commit hook
test('if the number is greater or equal to 2167', () => {
expect(problem44(2167)).toBe(8476206790)
})
// test('if the number is greater or equal to 2167', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unrelated change still doesn't belong here.

// expect(problem44(2167)).toBe(8476206790)
// })
})