parent
568af65c07
commit
43a0a09314
7 changed files with 188 additions and 99 deletions
@ -1,16 +1,36 @@ |
||||
export const TROGGLE_ACTION = 'TROGGLE_ACTION'; |
||||
export const CREATE = 'CREATE'; |
||||
export const MOVE = 'MOVE'; |
||||
export const FREEZE_ALL = 'FREEZE_ALL'; |
||||
export const UNFREEZE_ALL = 'UNFREEZE_ALL'; |
||||
export const CLEAR_ALL = 'CLEAR_ALL'; |
||||
|
||||
export const create = () => ({ |
||||
export const create = (x, y) => ({ |
||||
type: TROGGLE_ACTION, |
||||
action: CREATE |
||||
action: CREATE, |
||||
x: x, |
||||
y: y |
||||
}); |
||||
|
||||
export const move = (index, muncherX, muncherY) => ({ |
||||
export const move = (index, x, y) => ({ |
||||
type: TROGGLE_ACTION, |
||||
action: MOVE, |
||||
index: index, |
||||
muncherX: muncherX, |
||||
muncherY: muncherY |
||||
x: x, |
||||
y: y |
||||
}); |
||||
|
||||
export const freezeAll = () => ({ |
||||
type: TROGGLE_ACTION, |
||||
action: FREEZE_ALL |
||||
}); |
||||
|
||||
export const unfreezeAll = () => ({ |
||||
type: TROGGLE_ACTION, |
||||
action: UNFREEZE_ALL |
||||
}); |
||||
|
||||
export const clearAll = () => ({ |
||||
type: TROGGLE_ACTION, |
||||
action: CLEAR_ALL |
||||
}); |
||||
|
@ -0,0 +1,74 @@ |
||||
import { SETTINGS } from '../App'; |
||||
|
||||
const TroggleAI = { |
||||
move: function(currX, currY, muncherX, muncherY) { |
||||
// Randomize movement with boolean flags.
|
||||
const moveToAttack = Boolean(Math.round(Math.random())); |
||||
const moveAlongXAxis = Boolean(Math.round(Math.random())); |
||||
const moveInPositiveDirection = Boolean(Math.round(Math.random())); |
||||
|
||||
let newX = currX; |
||||
let newY = currY; |
||||
|
||||
// DO NOT CONSOLIDATE. Attack first, ask questions later.
|
||||
if (moveAlongXAxis === true) { |
||||
if (currX >= SETTINGS.GRID_WIDTH - 1 || |
||||
(moveToAttack && currX > muncherX)) { |
||||
newX = currX - 1; |
||||
} |
||||
else if (currX <= 0 || |
||||
(moveToAttack && currX < muncherX)) { |
||||
newX = currX + 1; |
||||
} |
||||
else if (moveInPositiveDirection) { |
||||
newX = currX + 1; |
||||
} |
||||
else if (!moveInPositiveDirection) { |
||||
newX = currX - 1; |
||||
} |
||||
} |
||||
else { |
||||
if (currY >= SETTINGS.GRID_HEIGHT - 1 || |
||||
(moveToAttack && currY > muncherY)) { |
||||
newY = currY - 1; |
||||
} |
||||
else if (currY <= 0 || |
||||
(moveToAttack && currY < muncherY)) { |
||||
newY = currY + 1; |
||||
} |
||||
else if (moveInPositiveDirection) { |
||||
newY = currY + 1; |
||||
} |
||||
else if (!moveInPositiveDirection) { |
||||
newY = currY - 1; |
||||
} |
||||
} |
||||
|
||||
return { x: newX, y: newY }; |
||||
}, |
||||
|
||||
create: function() { |
||||
// Start outside grid at a randomized location.
|
||||
const enterOnXAxis = Boolean(Math.round(Math.random())); |
||||
const enterFromPositive = Boolean(Math.round(Math.random())); |
||||
|
||||
let x = -1; |
||||
let y = -1; |
||||
|
||||
if (enterFromPositive === true) { |
||||
x = SETTINGS.GRID_WIDTH; |
||||
y = SETTINGS.GRID_HEIGHT; |
||||
} |
||||
|
||||
if (enterOnXAxis === true) { |
||||
y = Math.round(Math.random() * (SETTINGS.GRID_HEIGHT - 1)); |
||||
} |
||||
else { |
||||
x = Math.round(Math.random() * (SETTINGS.GRID_WIDTH - 1)); |
||||
} |
||||
|
||||
return { x: x, y: y } |
||||
} |
||||
}; |
||||
|
||||
export default TroggleAI; |
Loading…
Reference in new issue