diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 19:41:55 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 19:41:55 -0600 |
commit | 57172b8c3c851497b55b5d4c90ddb0cb5f9b202c (patch) | |
tree | ce7b54366d3efe1c226a756cde700073b02be3c3 | |
parent | 5c888d7a8661e1397e3cab868df2627fed5893e5 (diff) |
add turn_right action
-rw-r--r-- | src/simulation/actions.js | 19 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 32 |
2 files changed, 49 insertions, 2 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index a925867..aaaf1d8 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -60,9 +60,26 @@ const turn_left = { }; +const turn_right = { + size: 1, + propose: (agent, head) => { + if (head[0] > threshold) { + const orientation = { n: 'e', e: 's', s: 'w', w: 'n' }[agent.flags.orientation]; + return [{ + agent_changes: [{ + agent_id: agent.id, + flags: { orientation }, + }], + }]; + } else { + return []; + } + }, +}; + export const actions = [ - move_forward, move_backward, turn_left, + move_forward, move_backward, turn_left, turn_right, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index 457015d..d039f8a 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -3,7 +3,11 @@ import { actions } from './actions.js'; -const [move_forward, move_backward, turn_left, ...rest] = actions; +const [ + move_forward, move_backward, + turn_left, turn_right, + ...rest +] = actions; test("move forward", () => { @@ -84,3 +88,29 @@ test("turn_left", () => { }); +test("turn_right", () => { + 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_right.propose(n, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] }, + ]); + expect(turn_right.propose(s, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] }, + ]); + expect(turn_right.propose(e, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] }, + ]); + expect(turn_right.propose(w, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] }, + ]); + + expect(turn_right.propose(n, [0])).toEqual([]); + expect(turn_right.propose(s, [-1])).toEqual([]); + expect(turn_right.propose(e, [0])).toEqual([]); + expect(turn_right.propose(w, [-1])).toEqual([]); +}); + + |