Consider a grid represented by an array = ["....X..", "X......"]
. means the cell is empty, and X means there is a wall. A person is moving from the top-left location towards the right, and if any obstacle is encountered or there is no valid square ahead, it takes a right turn and starts moving
I am trying to represent the location like [0,0,"right"] The will be the initial position and then it will move to [0,1,"right"]->[0,2,"right"]->[0,3,"right"]->[0,4,"right"] now there is an obstacle so it will change direction and reach [1,4,"down"]. I am looking to write a function in javascript given a position [x,y,direction] returns the next location
It's getting clumsier with a lot of if-else conditions. Is there an efficient way to write it, or may I change how I represent the positions?