// Cloned by stephas2 on 5 Nov 2018 from World "ca318 copy Complex World" by test3
// Please leave this clone trail here.
function Mind()
{
const BLOCKPUNISH = 50,
ACTION_LEFT = 0,
ACTION_RIGHT = 1,
ACTION_UP = 2,
ACTION_DOWN = 3,
ACTION_STAYSTILL = 4;
var counter = 0
var move = 0
var board = []
var checked_pts = []
var minor_set = []
let vision = (function(list2d, i, j, row) {
list2d = []
for(i = 0; i < 20; i++) {
row = []
for(j = 0; j < 20; j++) {
row.push({isUnknown: true, isWall: false, isEnemy: false, isMe: false, isFree: false})
}
list2d.push(row)
}
return list2d
})()
let actions = [ACTION_LEFT, ACTION_RIGHT, ACTION_UP, ACTION_DOWN, ACTION_STAYSTILL, BLOCKPUNISH],
getRandomInt = max=>Math.floor(Math.random() * Math.floor(max))
m_pp = {x:0,y:0},
e_pp = {x:0,y:0}
// xy = [x, y]
function notin(lst, xy){
//global minor_set
for(let i=0;i<lst.length;i++)
{
if(lst[i][0] == xy[0] && lst[i][1] == xy[1])
return false
}
return true
}
function adj(x, y)
{
let tmpLst = []
//left
if(x > 1 && notin(minor_set, [x-1, y]))
tmpLst.push([x-1, y])
//right
if(x < 19 && notin(minor_set, [x+1, y]))
tmpLst.push([x+1, y])
//upper
if(y > 1 && notin(minor_set, [x, y-1]))
tmpLst.push([x, y-1])
//bottom
if(y < 19 && notin(minor_set, [x, y+1]))
tmpLst.push([x, y+1])
console.log("RA")
return tmpLst
}
function recSearch(xy)
{
console.log("Rec search running")
let x = xy[0]
let y = xy[1]
if(x < 2 || x > 18 || y < 2 || y > 18 || board[y][x] == "0")
return false
console.log("Up the")
let lst = adj(x,y)
let new_pts = []
console.log("Section 1")
for(let i=0;i<lst.length;i++)
{
let item = lst[i]
console.log("Iter:--> ", lst[i])
if( board[item[1]][item[0]] != "1" && notin(checked_pts, [ item[0], item[1] ]))
new_pts.push(new_pts[i])
}
console.log("1st For loop: OK")
console.log(xy)
//No repeats
if(new_pts.length == 0)
minor_set.push([x,y])
return true
for(let i=0;i<new_pts.length;i++)
{
minor_set.push(new_pts[i])
}
console.log("2nd For loop: OK")
if(new_pts.length == 1)
return true && recSearch(new_pts[0])
if(new_pts.length == 2)
return true && recSearch(new_pts[0]) && recSearch(new_pts[1])
if(new_pts.length == 3)
return true && recSearch(new_pts[0]) && recSearch(new_pts[1]) && recSearch(new_pts[2])
if(new_pts.length == 4)
return true && recSearch(new_pts[0]) && recSearch(new_pts[1]) && recSearch(new_pts[2]) && recSearch(new_pts[3])
}
function findWalls(board)
{
console.log("start")
let sections = []
for(let y=0;y<18;y++)
{
for(let x=0;x<18;x++)
{
if(notin(checked_pts, [x, y]) && board[y][x] == "2")
{
console.log("Check ok")
//global
minor_set = []
//if this is a free wall (x, y)
if(recSearch([x, y]))
{
console.log("Search complete")
sections.push(minor_set)
//record walls for future use
for(let k=0;i<minor_set.length;k++)
checked_pts.push(minor_set[k])
}
}
}
}
console.log("Ran soundly")
return sections
}
function display(List2D)
{
var string = ""
let time = performance.now()
for(let i=1;i<19;i++)
{
//console.log(List2D[i]);
for(let j=1;j<19;j++)
{ //if any case is true..
if(List2D[i][j].isFree)
string += "1";
if(List2D[i][j].isUnknown)
{
if(List2D[i][j].isWall)
string += "2";
else
string += "0";
}
if(List2D[i][j].isWall && List2D[i][j].isFree)
console.log(i, j, " is fucked")
}
string += "\n";
}
console.log(performance.now()-time, "ms")
return string
}
this.newRun = function() {
let debug_box = document.getElementById('ab-runheaderbox'),
mini_map = document.createElement('canvas')
mini_map.width = 200
mini_map.height = 200
mini_map.style.background = 'yellow'
mini_map.style.width = '200px'
mini_map.style.height = '200px'
mini_map.setAttribute('id', 'mini_map')
//draw a 20x20 map surrounded by walls
ctx = mini_map.getContext('2d')
ctx.fillStyle = 'grey'
ctx.fillRect(10, 10, 180, 180)
debug_box.appendChild(mini_map)
};
this.getAction = function (state, e, m, e_vision, m_vision) {
e = {x: state[2], y: state[3]} //enemy position
m = {x: state[0], y: state[1]} //my position
if(e_pp && e_pp.x)
vision[e_pp.y][e_pp.x].isEnemy = false
if(m_pp && m_pp.x)
vision[m_pp.y][m_pp.x].isMe = false
if(m.x == m_pp.x && m.y == m_pp.y)
{
//if block hasn't moved, then obstacle must be in the way
//make sure the block being defined is a wall and not just the enemy
//move =0 left
if(move == 0 && e.x != m.x-1 && e.y != m.y && vision[m.y][m.x-1].isUnknown)
vision[m.y][m.x-1].isWall = true
//vision[m.y][m.x-1].isUnknown = false
//move =1 right
if(move == 1 && e.x != m.x+1 && e.y != m.y && vision[m.y][m.x+1].isUnknown)
vision[m.y][m.x+1].isWall = true
//vision[m.y][m.x+1].isUnknown = false
//move =2 up
if(move == 2 && e.x != m.x && e.y+1 != m.y && vision[m.y+1][m.x].isUnknown)
vision[m.y+1][m.x].isWall = true
//vision[m.y+1][m.x].isWall = false
//move =3 down
if(move == 3 && e.x != m.x && e.y-1 != m.y && vision[m.y-1][m.x].isUnknown)
vision[m.y-1][m.x].isWall = true
//vision[m.y-1][m.x].isWall = false
}
e_pp = e
m_pp = m
e_vision = vision[e.y][e.x]
m_vision = vision[m.y][m.x]
///*
//Do not touch, if removed, code will break. I forget how this works :(
if(e_vision.isUnknown) {
e_vision.isUnknown = false
e_vision.isEnemy = true
e_vision.isFree = true
}
if(m_vision.isUnknown) {
m_vision.isUnknown = false
m_vision.isMe = true
m_vision.isFree = true
}
//*/
let mini_map = document.getElementById('mini_map')
ctx = mini_map.getContext('2d')
for(let i = 1; i < 19; i++) {
for(let j = 1; j < 19; j++) {
if(vision[j][i].isFree) {
ctx.fillStyle = 'lightblue'
ctx.fillRect(i*10, j*10, 10, 10)
}
if(vision[j][i].isWall)
{
ctx.fillStyle = 'green'
ctx.fillRect(i*10, j*10, 10, 10)
}
}
}
ctx.fillStyle = 'blue'
ctx.fillRect(m.x*10, m.y*10, 10, 10)
ctx.fillStyle = 'red'
ctx.fillRect(e.x*10, e.y*10, 10, 10)
if(counter == 10)
console.log("")
counter ++;
if(counter==300)
{
//output is exactly what's needed
board = display(vision).split("\n").slice(0,-1);
//console.log(board)
let ding = findWalls(board)
for(let i=0;i<ding.length;i++)
{
if(ding[i].length == 1)
{
console.log("x: ", ding[i][0], "y: ",ding[i][1])
}
}
}
move = actions[getRandomInt(4)]
return move;
};
/*
this.endRun = function()
{
console.log(enemy_pos)
console.log(my_pos)
};
*/
}