| 23 | // let chanceOfFullJump = 0.2; |
| 24 | |
| 25 | class Brain { |
| 26 | |
| 27 | constructor(size, randomiseInstructions = true) { |
| 28 | this.instructions = []; |
| 29 | this.currentInstructionNumber = 0; |
| 30 | if (randomiseInstructions) |
| 31 | this.randomize(size); |
| 32 | this.parentReachedBestLevelAtActionNo = 0; |
| 33 | } |
| 34 | |
| 35 | randomize(size) { |
| 36 | for (let i = 0; i < size; i++) { |
| 37 | this.instructions[i] = this.getRandomAction(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | getRandomAction() { |
| 42 | let isJump = false; |
| 43 | |
| 44 | if (random() > jumpChance) { |
| 45 | isJump = true; |
| 46 | } |
| 47 | |
| 48 | let holdTime = random(0.1, 1); |
| 49 | if(random()<chanceOfFullJump){ |
| 50 | holdTime = 1; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | let directions = [-1, -1, -1, 0, 1, 1, 1] |
| 55 | let xDirection = random(directions) |
| 56 | |
| 57 | |
| 58 | return new AIAction(isJump, holdTime, xDirection) |
| 59 | } |
| 60 | |
| 61 | getNextAction() { |
| 62 | if(this.currentInstructionNumber >= this.instructions.length){ |
| 63 | return null; |
| 64 | } |
| 65 | this.currentInstructionNumber += 1; |
| 66 | return this.instructions[this.currentInstructionNumber - 1]; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | clone() { |
| 71 | let clone = new Brain(this.size, false); |
| 72 | clone.instructions = []; |
| 73 | for (let i = 0; i < this.instructions.length; i++) { |
| 74 | clone.instructions.push(this.instructions[i].clone()) |
| 75 | } |
| 76 | return clone; |
| 77 | } |
| 78 | |
| 79 | mutate() { |
| 80 | let mutationRate = 0.1; |
| 81 | let chanceOfNewInstruction = 0.02; |
| 82 | for (let i = this.parentReachedBestLevelAtActionNo; i < this.instructions.length; i++) { |
nothing calls this directly
no outgoing calls
no test coverage detected