Created
September 6, 2013 18:42
-
-
Save macbaszii/6468151 to your computer and use it in GitHub Desktop.
** Code นี้ใช้ Underscore Module ด้วยนะ ใช้ node รันแล้วเกิด error /Users/iMacbaszii/Desktop/atkAlgor.js:59 if (opponentArray[monster.position - 1 + i].isDead) { ^
TypeError: Cannot read property 'isDead' of undefined ก็เลยลอง Debug ดูปรากฎว่าแม้ position จะเป็น 4, 5, 6 มันเข้า if (isFrontMonster(monster)) ตลอดเลย = =' ซึ่งจริงแล้วมันไม่ควรเข้าไ…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _ = require("underscore") | |
| monsterArray = [{ type: "Attacker", position: 1, isDead: false}, | |
| { type: "Attacker", position: 2, isDead: false}, | |
| { type: "Attacker", position: 3, isDead: false}, | |
| { type: "Attacker", position: 4, isDead: false}, | |
| { type: "Attacker", position: 5, isDead: false}, | |
| { type: "Attacker", position: 6, isDead: false}]; | |
| opponentArray = [{ type: "Attacker", position: 1, isDead: false}, | |
| { type: "Attacker", position: 2, isDead: false}, | |
| { type: "Attacker", position: 3, isDead: false}, | |
| { type: "Attacker", position: 4, isDead: false}, | |
| { type: "Attacker", position: 5, isDead: false}, | |
| { type: "Attacker", position: 6, isDead: false}]; | |
| var turns = []; | |
| // var monsterType = filteredTypeMonArray[0].type; | |
| function monsterType(monster) { | |
| return monster.type; | |
| } | |
| var possibleDefender = []; | |
| (function simulateBattle() { | |
| var dupMonsterArray = monsterArray; | |
| var dupOpponentArray = opponentArray; | |
| // console.log(calculateTurn(monsterArray[0])); | |
| // calculateTurn(monsterArray[0]); | |
| monsterArray.forEach(function(monster) { | |
| turns.push(calculateTurn(monster)); | |
| possibleDefender = []; | |
| }); | |
| // | |
| // console.log(turns); | |
| // | |
| // turns.forEach(function(turn) { | |
| // console.log(turn); | |
| // }); | |
| // | |
| // monsterArray = dupMonsterArray; | |
| // opponentArray = dupOpponentArray; | |
| // | |
| // opponentArray.forEach(function(monster){ | |
| // turns.push(calculateTurn(monster)); | |
| // possibleDefender = []; | |
| // }); | |
| // console.log("Hello, world"); | |
| })(); | |
| function calculateTurn(monster) { | |
| if (monsterType(monster) == "Attacker") { | |
| console.log("Monster is Attacker"); | |
| if (isFrontMonster(monster)) { | |
| console.log("Monster at Front Row"); | |
| for (var i = 0; i <= 2; ++i) { | |
| if (opponentArray[monster.position - 1 + i].isDead) { | |
| if (!opponentArray[monster.position - 1 + 3 + i].isDead) { | |
| possibleDefender.push(opponentArray[monster.position - 1 + 3 + i]); | |
| } | |
| } else { | |
| possibleDefender.push(opponentArray[monster.position - 1 + i]); | |
| } | |
| } | |
| if (sizeof(possibleDefender) != 0) { | |
| randomDefender = shuffleAndGiveRandomElement(possibleDefender); | |
| var turnTmp = { | |
| atk: monster.position, | |
| act: "attack", | |
| def: randomDefender.position | |
| } | |
| removeSpecific(opponentArray, randomDefender); | |
| return turnTmp; | |
| } else { | |
| return {}; | |
| } | |
| } else if (isBackMonster(monster)) { | |
| console.log("Monster at Back Row"); | |
| // if (frontMonster(monster, monsterArray).isDead) { | |
| // if (opponentArray[monster.position - 1 - 3].isDead) { | |
| // if (!opponentArray[monster.position - 1].isDead) { | |
| // possibleDefender.push(opponentArray[monster.position - 1]); | |
| // } | |
| // } else { | |
| // possibleDefender.push(opponentArray[monster.position - 1 - 3]); | |
| // } | |
| // } | |
| // | |
| // if (sizeof(possibleDefender) != 0) { | |
| // randomDefender = shuffleAndGiveRandomElement(possibleDefender); | |
| // | |
| // var turnTmp = { | |
| // atk: monster.position, | |
| // act: "attack", | |
| // def: randomDefender.position | |
| // } | |
| // | |
| // removeSpecific(opponentArray, randomDefender); | |
| // return turnTmp; | |
| // } else { | |
| // return {}; | |
| // } | |
| } | |
| } | |
| } | |
| function shuffleAndGiveRandomElement(array) { | |
| var shuffleArray = _.shuffle(array); | |
| var randomNumber = randomBetween(1, 3); | |
| if (randomNumber === 1) { | |
| return firstElement(shuffleArray); | |
| } else if (randomNumber === 2) { | |
| return lastElement(shuffleArray); | |
| } else if (randomNumber === 3) { | |
| return shuffleArray[randomBetween(0, sizeof(array))]; | |
| } | |
| } | |
| function firstElement(array) { | |
| return array[0]; | |
| } | |
| function lastElement(array) { | |
| return array[sizeof(array) - 1]; | |
| } | |
| function sizeof(array) { | |
| return array.length; | |
| } | |
| function randomBetween(a, b) { | |
| return Math.floor(Math.random() * b + a); | |
| } | |
| function isFrontMonster(monster) { | |
| if (monster.position >= 1 && monster.position <= 3) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| function isBackMonster(monster) { | |
| if (monster.position >= 4 && monster.position <= 6) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| function frontMonster(monster, monsterArray) { | |
| if (monster.position >= 4 && monster.position <= 6) { | |
| return monsterArray[monster.position - 1 - 3]; | |
| } | |
| return undefined; | |
| } | |
| function backMonster(monster, monsterArray) { | |
| if (monster.position >= 1 && monster.position <= 3) { | |
| return monsterArray[monster.position - 1 + 3]; | |
| } | |
| return undefined; | |
| } | |
| function removeSpecific(array, specificElement) { | |
| return array.splice(array.indexOf(specificElement), 1); | |
| } | |
| // if (monsterType == "Attacker") { | |
| // deadFilteredMonArray.forEach(function(monster){ | |
| // if (monster.position >= 1 && monster.position <= 3) { | |
| // var possibleDefender = []; | |
| // deadFilteredOpponentMonArray.forEach(function(monster){ | |
| // var backMonPosition = 4; | |
| // if (monster.position >= backMonPosition) { | |
| // // var filteredPosition = _.filter(deadFilteredMonArray, function(monster){ | |
| // // monster.position === (backMonPosition - 3); | |
| // // }); | |
| // // if (filteredPosition.length === 0) { | |
| // // possibleDefender.push(monster); | |
| // // deadFilteredMonArray = removeSpecific(deadFilteredMonArray, monster); | |
| // // } | |
| // // } else { | |
| // // possibleDefender.push(monster); | |
| // // deadFilteredMonArray = removeSpecific(deadFilteredMonArray, monster); | |
| // } | |
| // }); | |
| // // possibleDefender = _.shuffle(possibleDefender); | |
| // // deadFilteredMonArray.forEach(function(monster){ | |
| // // turnTmp = { | |
| // // atk: monster.position, | |
| // // act: "attack", | |
| // // def: possibleDefender[0].position | |
| // // } | |
| // // possibleDefender.pop(); | |
| // // turns.push(turnTmp); | |
| // // }); | |
| // } | |
| // }); | |
| // // | |
| // // turns.forEach(function(turn){ | |
| // // console.log(turn); | |
| // // }); | |
| // } | |
| // function monsterCanAttack(fromPos, toPos, fromArray, toArray, monType) { | |
| // var step = 0; | |
| // | |
| // if (fromPos == 1 || fromPos == 2 || fromPos == 3) { | |
| // if (toPos == 1 || toPos == 2 || toPos == 3) { | |
| // step = 1; | |
| // } else { | |
| // var monster = toArray[toPos - 1]; | |
| // step = (monster.isDead) ? 1 : 2; | |
| // } | |
| // } else { | |
| // if (toPos == 1 || toPos == 2 || toPos == 3) { | |
| // var monster = fromArray[toPos - 1]; | |
| // step = (monster.isDead) ? 1 : 2; | |
| // } else { | |
| // var monster = fromArray[toPos - 1]; | |
| // step = (monster.isDead) ? 1 : 2; | |
| // | |
| // var monsterBack = toArray[toPos - 3]; | |
| // if (!monsterBack.isDead) { | |
| // step++; | |
| // } | |
| // } | |
| // } | |
| // | |
| // if (monType == "attacker" || monType == "defender") { | |
| // if (step <= 1) return true; | |
| // } else if (monType == "magicAttacker" || monType == "ranger") { | |
| // if (step <= 2) return true; | |
| // } else if (monType == "healer") { | |
| // if (step <= 3) return true; | |
| // } | |
| // return false; | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment