Browse Source

Merge pull request #578 from 01-edu/tron-ai-fix

tron: correcting hard.js
content-update
LEEDASILVA 4 years ago committed by GitHub
parent
commit
44c0efef1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      subjects/forum/forum.audit.en.md
  2. 44
      subjects/tron/ai/hard.js

4
subjects/forum/forum.audit.en.md

@ -112,6 +112,10 @@ cc8f5dcf760f <name of the image> "./server" 6 seconds ag
###### Were you prohibited to create the post? ###### Were you prohibited to create the post?
##### Try creating a post as a registered user and try to choose several categories for that post.
###### Were you able to choose several categories for that post?
##### Try creating a post as a registered user and try to choose a category for that post. ##### Try creating a post as a registered user and try to choose a category for that post.
###### Were you able to choose a category for that post? ###### Were you able to choose a category for that post?

44
subjects/tron/ai/hard.js

@ -13,14 +13,18 @@ const pickRandom = (arr) => arr[Math.floor(Math.random() * arr.length)]
* My functions * My functions
************/ ************/
const isAlley = ({ x, y }) => !isFree({ x, y }) || !isInBounds({ x, y }) const isAlley = ({ x, y }) => !isFree({ x, y }) || !isInBounds({ x, y })
// TODO: could make the ai harder if left an empty space so that it could get out
// this functions will find the best path, so the path that has more empty spaces // this functions will find the best path, so the path that has more empty spaces
// so use `isFree`,
const findBestPath = ({ ai }) => { const findBestPath = ({ ai }) => {
let arr = [] let arr = []
let car = ai.cardinal let car = ai.cardinal
// if it as a block on the symmetric position it must // if it as a block on the symmetric position it must
// simulate the symmetric position and see witch path is the best // simulate the symmetric position and see witch path is the best
// from the symmetric position
// o→
// ←↓oooooooooo
// ↓
if ( if (
(car === 3 || car === 0) && (car === 3 || car === 0) &&
!isFree({ x: ai.x - 1, y: ai.y - 1 }) && !isFree({ x: ai.x - 1, y: ai.y - 1 }) &&
@ -29,17 +33,15 @@ const findBestPath = ({ ai }) => {
) { ) {
let xad = ai.x - 1 let xad = ai.x - 1
let yad = ai.y - 1 let yad = ai.y - 1
// [right, line0, line3, down]
let choose = [ let choose = [
calDistance(xad + 1, yad, 1, 0), calDistance(xad + 1, yad, 1, 0),
calDistance(ai.x, ai.y - 1, car, 0), calDistance(ai.x, ai.y - 1, 0, 0),
calDistance(ai.x - 1, ai.y, car, 0), calDistance(ai.x - 1, ai.y, 3, 0),
calDistance(xad, yad + 1, 2, 0), calDistance(xad, yad + 1, 2, 0),
] ]
let index = choose.indexOf(Math.max(...choose)) let index = choose.indexOf(Math.max(...choose))
return index === 0 || index === 1 return index === 0 || index === 1 ? ai.coords[0] : ai.coords[3]
? ai.coords[0]
: ai.coords[3]
} }
if ( if (
(car === 1 || car === 0) && (car === 1 || car === 0) &&
@ -53,14 +55,12 @@ const findBestPath = ({ ai }) => {
// [ down, line1, line0, left ] // [ down, line1, line0, left ]
let choose = [ let choose = [
calDistance(xad, yad + 1, 2, 0), calDistance(xad, yad + 1, 2, 0),
calDistance(ai.x + 1, ai.y, car, 0), calDistance(ai.x + 1, ai.y, 1, 0),
calDistance(ai.x, ai.y - 1, car, 0), calDistance(ai.x, ai.y - 1, 0, 0),
calDistance(xad - 1, yad, 3, 0), calDistance(xad - 1, yad, 3, 0),
] ]
let index = choose.indexOf(Math.max(...choose)) let index = choose.indexOf(Math.max(...choose))
return index === 0 || index === 1 return index === 0 || index === 1 ? ai.coords[1] : ai.coords[0]
? ai.coords[1]
: ai.coords[0]
} }
if ( if (
(car === 2 || car === 1) && (car === 2 || car === 1) &&
@ -74,14 +74,12 @@ const findBestPath = ({ ai }) => {
// [ left, line2, line1, up ] // [ left, line2, line1, up ]
let choose = [ let choose = [
calDistance(xad - 1, yad, 3, 0), calDistance(xad - 1, yad, 3, 0),
calDistance(ai.x, ai.y + 1, car, 0), calDistance(ai.x, ai.y + 1, 2, 0),
calDistance(ai.x + 1, ai.y, car, 0), calDistance(ai.x + 1, ai.y, 1, 0),
calDistance(xad, yad - 1, 0, 0), calDistance(xad, yad - 1, 0, 0),
] ]
let index = choose.indexOf(Math.max(...choose)) let index = choose.indexOf(Math.max(...choose))
return index === 0 || index === 1 return index === 0 || index === 1 ? ai.coords[2] : ai.coords[1]
? ai.coords[2]
: ai.coords[1]
} }
if ( if (
(car === 2 || car === 3) && (car === 2 || car === 3) &&
@ -95,14 +93,12 @@ const findBestPath = ({ ai }) => {
// [ right, line2, line3, up ] // [ right, line2, line3, up ]
let choose = [ let choose = [
calDistance(xad + 1, yad, 1, 0), calDistance(xad + 1, yad, 1, 0),
calDistance(ai.x - 1, ai.y, car, 0), calDistance(ai.x, ai.y + 1, 2, 0),
calDistance(ai.x, ai.y + 1, car, 0), calDistance(ai.x - 1, ai.y, 3, 0),
calDistance(xad, yad - 1, 0, 0), calDistance(xad, yad - 1, 0, 0),
] ]
let index = choose.indexOf(Math.max(...choose)) let index = choose.indexOf(Math.max(...choose))
return index === 0 || index === 1 return index === 0 || index === 1 ? ai.coords[2] : ai.coords[3]
? ai.coords[3]
: ai.coords[2]
} }
for ({ x, y, cardinal } of ai.coords) { for ({ x, y, cardinal } of ai.coords) {
@ -164,8 +160,8 @@ const calDistance = (x, y, car, count) => {
} }
} }
const addToMap = ({ x, y }) => MAP[y * SIZE + x] = 1 const addToMap = ({ x, y }) => (MAP[y * SIZE + x] = 1)
const update = (state) => { const update = (state) => {
state.ais.forEach(addToMap) state.ais.forEach(addToMap)
findBestPath(state) return findBestPath(state)
} }

Loading…
Cancel
Save