Skip to content
This repository has been archived by the owner on Feb 7, 2021. It is now read-only.

Adding Risk Avoidance #5

Merged
merged 5 commits into from Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions TempFunctions.js
@@ -0,0 +1,45 @@
// functions that could be implemented in the future

const checkDiagonals = (opt, index, possibleMoves){
if (possibleMoves[index].direction == 'up') {
console.log('up');
if (
(opt.x - 1 == bodyPos.x || opt.x + 1 == bodyPos.x) &&
opt.y == bodyPos.y
) {
isOther = true;
} else if (opt.y - 1 == bodyPos.y && opt.x == bodyPos.x) {
isOther = true;
}
} else if (possibleMoves[index].direction == 'down') {
console.log('down');
if (
(opt.x - 1 == bodyPos.x || opt.x + 1 == bodyPos.x) &&
opt.y == bodyPos.y
) {
isOther = true;
} else if (opt.y + 1 == bodyPos.y && opt.x == bodyPos.x) {
isOther = true;
}
} else if (possibleMoves[index].direction == 'right') {
console.log('right');
if (
(opt.y - 1 == bodyPos.y || opt.y + 1 == bodyPos.y) &&
opt.x == bodyPos.x
) {
isOther = true;
} else if (opt.x + 1 == bodyPos.x && opt.y == bodyPos.y) {
isOther = true;
}
} else if (possibleMoves[index].direction == 'left') {
console.log('left');
if (
(opt.y - 1 == bodyPos.y || opt.y + 1 == bodyPos.y) &&
opt.x == bodyPos.x
) {
isOther = true;
} else if (opt.x - 1 == bodyPos.x && opt.y == bodyPos.y) {
isOther = true;
}
}
}
7 changes: 0 additions & 7 deletions app.json

This file was deleted.

39 changes: 29 additions & 10 deletions index.js
Expand Up @@ -9,7 +9,11 @@ const {
poweredByHandler,
} = require('./handlers.js');

const { spin, findSafeMoves, closestFood } = require('./pathfinding.js');
const {
findSafeMoves,
findClosestFood,
findRiskyMoves,
} = require('./pathfinding.js');

// For deployment to Heroku, the port needs to be set using ENV, so
// we check for the port number in process.env
Expand All @@ -21,11 +25,12 @@ app.use(logger('dev'));
app.use(bodyParser.json());
app.use(poweredByHandler);

// --- SNAKE LOGIC GOES BELOW THIS LINE ---
const state = {
GameID: '',
snakeID: '',
snakeName: 'FalconSnake',
snakeColor: '#B5BABE',
snakeLength: '',
snakeAvatar: '',
currBody: '',
currBoard: '',
Expand Down Expand Up @@ -56,45 +61,59 @@ app.post('/move', async (request, response) => {
// * Set new state for turn
state.currBody = request.body.you.body;
state.currBoard = request.body.board;
state.snakeID = request.body.you.id;
state.currHead = state.currBody[0];
state.currTail = state.currBody[state.currBody.length - 1];
state.snakeLength = request.body.you.length;
// * Pathfinding AI
const validMoves = findSafeMoves(state);
const foodMoves = closestFood(state);
let foundPreferedMove = false;
let preferedMove;
for (let i = 0; i < validMoves.length && !foundPreferedMove; i++) {
const nonRiskyMoves = findRiskyMoves(state);
const foodMoves = findClosestFood(state);
let foundPreferredMove = false;
let preferredMove;
for (let i = 0; i < validMoves.length && !foundPreferredMove; i++) {
for (let z = 0; z < foodMoves.length; z++) {
if (validMoves[i].direction == foodMoves[z].direction) {
preferedMove = validMoves[i].direction;
foundPreferedMove = true;
preferredMove = validMoves[i].direction;
foundPreferredMove = true;
}
}
}

const fallbackMove =
validMoves[Math.floor(Math.random() * validMoves.length)].direction;

if (preferedMove) {
data.move = preferedMove;
if (preferredMove && nonRiskyMoves.includes(preferredMove)) {
data.move = preferredMove;
} else {
while (!nonRiskyMoves.includes(fallbackMove)) {
fallbackMove =
validMoves[Math.floor(Math.random() * validMoves.length)].direction;
}
data.move = fallbackMove;
}
// * Set Previous Move to Current Move
state.prevMove = data.move;
// * Return Data
let validMovesString = '';
let foodMovesString = '';
let nonRiskyMovesString = '';
validMoves.forEach(o => {
validMovesString += `${o.direction}, `;
});
foodMoves.forEach(o => {
foodMovesString += `${o.direction}, `;
});
nonRiskyMoves.forEach(o => {
nonRiskyMovesString += `${o}, `;
});
console.log(`Current Turn: ${request.body.turn}`);
console.log(`Valid Directions: ${validMovesString}`);

console.log(`Food Directions: ${foodMovesString}`);

console.log(`Non-Risky moves: ${nonRiskyMovesString}`);

return response.json(data);
});

Expand Down
28 changes: 7 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.