diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 17:10:46 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 17:10:46 -0600 |
commit | c9e241e3ffed2131c240607b8dfb36510217dc88 (patch) | |
tree | 66ebca480c8bff0381a1e14fae139511422f74aa /src/simulation | |
parent | a85f61cef6aef51bf6983f98020f3284aeb5020d (diff) |
implement move_backward and turn_left
Diffstat (limited to 'src/simulation')
-rw-r--r-- | src/simulation/actions.js | 42 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 56 |
2 files changed, 96 insertions, 2 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index 40545c5..a925867 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -22,7 +22,47 @@ const move_forward = { }, }; +const move_backward = { + size: 1, + propose: (agent, head) => { + if (head[0] > threshold) { + const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation]; + const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation]; + return [{ + agent_changes: [{ + agent_id: agent.id, + x: agent.x - dx, + y: agent.y - dy, + }], + }]; + } else { + return []; + } + }, +}; + + +const turn_left = { + size: 1, + propose: (agent, head) => { + if (head[0] > threshold) { + const orientation = { n: 'w', e: 'n', s: 'e', w: 's' }[agent.flags.orientation]; + return [{ + agent_changes: [{ + agent_id: agent.id, + flags: { orientation }, + }], + }]; + } else { + return []; + } + }, +}; + + + + export const actions = [ - move_forward, + move_forward, move_backward, turn_left, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index a870352..457015d 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -3,7 +3,7 @@ import { actions } from './actions.js'; -const [move_forward, ...rest] = actions; +const [move_forward, move_backward, turn_left, ...rest] = actions; test("move forward", () => { @@ -30,3 +30,57 @@ test("move forward", () => { expect(move_forward.propose(e, [0])).toEqual([]); expect(move_forward.propose(w, [-1])).toEqual([]); }); + + +test("move backward", () => { + const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } }; + const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } }; + const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; + const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; + + expect(move_backward.propose(n, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] }, + ]); + expect(move_backward.propose(s, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] }, + ]); + expect(move_backward.propose(e, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] }, + ]); + expect(move_backward.propose(w, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] }, + ]); + + expect(move_backward.propose(n, [0])).toEqual([]); + expect(move_backward.propose(s, [-1])).toEqual([]); + expect(move_backward.propose(e, [0])).toEqual([]); + expect(move_backward.propose(w, [-1])).toEqual([]); +}); + + +test("turn_left", () => { + const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } }; + const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } }; + const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; + const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; + + expect(turn_left.propose(n, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] }, + ]); + expect(turn_left.propose(s, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] }, + ]); + expect(turn_left.propose(e, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] }, + ]); + expect(turn_left.propose(w, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] }, + ]); + + expect(turn_left.propose(n, [0])).toEqual([]); + expect(turn_left.propose(s, [-1])).toEqual([]); + expect(turn_left.propose(e, [0])).toEqual([]); + expect(turn_left.propose(w, [-1])).toEqual([]); +}); + + |