starfish.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * The code box class
  3. */
  4. class CodeBox {
  5. constructor() {
  6. /**
  7. * Possible vectors the pointer can move in
  8. * @type {Object}
  9. */
  10. this.directions = {
  11. NORTH: [-1, 0],
  12. EAST: [ 0, 1],
  13. SOUTH: [ 1, 0],
  14. WEST: [ 0, -1],
  15. };
  16. /**
  17. * The current vector of the pointer
  18. * @type {int[]}
  19. */
  20. this.curr_direction = this.directions.EAST;
  21. /**
  22. * The Set of instructions to execute
  23. *
  24. * Either a 1 or 2-dimensional array
  25. * @type {Array|Array[]}
  26. */
  27. this.box = [];
  28. /**
  29. * The coordinates of the currently executing instruction inside the code box
  30. * @type {int[]}
  31. */
  32. this.pointer = [0,0];
  33. /**
  34. * Was the instruction last moving in the left direction
  35. *
  36. * Used by the {@link Fisherman}
  37. * @type {boolean}
  38. */
  39. this.dirWasLeft = false;
  40. /**
  41. * Are we currently under the influence of the {@link Fisherman}
  42. * @type {boolean}
  43. */
  44. this.onTheHook = false;
  45. }
  46. /**
  47. * Implement C and .
  48. */
  49. SetPointer(x, y) {
  50. this.pointer = [x, y];
  51. }
  52. /**
  53. * Implement ^
  54. *
  55. * Changes the swim direction upward
  56. */
  57. MoveUp() {
  58. this.curr_direction = this.directions.NORTH;
  59. }
  60. /**
  61. * Implement >
  62. *
  63. * Changes the swim direction rightward
  64. */
  65. MoveRight() {
  66. this.curr_direction = this.directions.EAST;
  67. this.dirWasLeft = false;
  68. }
  69. /**
  70. * Implement v
  71. *
  72. * Changes the swim direction downward
  73. */
  74. MoveDown() {
  75. this.curr_direction = this.directions.SOUTH;
  76. }
  77. /**
  78. * Implement <
  79. *
  80. * Changes the swim direction leftward
  81. */
  82. MoveLeft() {
  83. this.curr_direction = this.directions.WEST;
  84. this.dirWasLeft = true;
  85. }
  86. /**
  87. * Implement /
  88. *
  89. * Reflects the swim direction depending on its starting value
  90. */
  91. ReflectForward() {
  92. if (this.curr_direction == this.directions.NORTH) {
  93. this.MoveRight();
  94. }
  95. else if (this.curr_direction == this.directions.EAST) {
  96. this.MoveUp();
  97. }
  98. else if (this.curr_direction == this.directions.SOUTH) {
  99. this.MoveLeft();
  100. }
  101. else {
  102. this.MoveDown();
  103. }
  104. }
  105. /**
  106. * Implement \
  107. *
  108. * Reflects the swim direction depending on its starting value
  109. */
  110. ReflectBack() {
  111. if (this.curr_direction == this.directions.NORTH) {
  112. this.MoveLeft();
  113. }
  114. else if (this.curr_direction == this.directions.EAST) {
  115. this.MoveDown();
  116. }
  117. else if (this.curr_direction == this.directions.SOUTH) {
  118. this.MoveRight();
  119. }
  120. else {
  121. this.MoveUp();
  122. }
  123. }
  124. /**
  125. * Implement |
  126. *
  127. * Swaps the horizontal swim direction to its opposite
  128. */
  129. HorizontalMirror() {
  130. if (this.curr_direction == this.directions.EAST) {
  131. this.MoveLeft();
  132. }
  133. else {
  134. this.MoveRight();
  135. }
  136. }
  137. /**
  138. * Implement _
  139. *
  140. * Swaps the horizontal swim direction to its opposite
  141. */
  142. VerticalMirror() {
  143. if (this.curr_direction == this.directions.NORTH) {
  144. this.MoveDown();
  145. }
  146. else {
  147. this.MoveUp();
  148. }
  149. }
  150. /**
  151. * Implement #
  152. *
  153. * A combination of the vertical and the horizontal mirror
  154. */
  155. OmniMirror() {
  156. if (this.curr_direction[0]) {
  157. this.VerticalMirror();
  158. }
  159. else {
  160. this.HorizontalMirror();
  161. }
  162. }
  163. /**
  164. * Implement x
  165. *
  166. * Pseudo-randomly switches the swim direction
  167. */
  168. ShuffleDirection() {
  169. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  170. }
  171. /**
  172. * Implement `
  173. *
  174. * Changes the swim direction based on the previous direction
  175. * @see https://esolangs.org/wiki/Starfish#Fisherman
  176. */
  177. Fisherman() {
  178. if (this.curr_direction[0]) {
  179. if (this.dirWasLeft) {
  180. this.MoveLeft();
  181. }
  182. else {
  183. this.MoveRight();
  184. }
  185. }
  186. else {
  187. if (this.onTheHook) {
  188. this.onTheHook = false;
  189. this.MoveUp();
  190. }
  191. else {
  192. this.onTheHook = true;
  193. this.MoveDown();
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * The stack class
  200. */
  201. class Stack {
  202. constructor() {
  203. /**
  204. * The stack
  205. * @type {int[]}
  206. */
  207. this.stack = [];
  208. /**
  209. * A single value saved off the stack
  210. * @type {int}
  211. */
  212. this.register = null;
  213. }
  214. /**
  215. * Wrapper function for Array.prototype.push
  216. * @param {*} newValue
  217. */
  218. Push(newValue) {
  219. this.stack.push(newValue);
  220. }
  221. /**
  222. * Wrapper function for Array.prototype.pop
  223. * @returns {*}
  224. */
  225. Pop() {
  226. return this.stack.pop();
  227. }
  228. /**
  229. * Implement }
  230. *
  231. * Shifts the entire stack leftward by one value
  232. */
  233. ShiftLeft() {
  234. const temp = this.stack.shift();
  235. this.stack.push(temp);
  236. }
  237. /**
  238. * Implement {
  239. *
  240. * Shifts the entire stack rightward by one value
  241. */
  242. ShiftRight() {
  243. const temp = this.stack.pop();
  244. this.stack.unshift(temp);
  245. }
  246. /**
  247. * Implement $
  248. *
  249. * Swaps the top two values of the stack
  250. */
  251. SwapTwo() {
  252. // TODO
  253. }
  254. /**
  255. * Implement :
  256. *
  257. * Duplicates the element on the top of the stack
  258. */
  259. Duplicate() {
  260. this.stack.push(this.stack[this.stack.length-1]);
  261. }
  262. /**
  263. * Implements ~
  264. *
  265. * Removes the element on the top of the stack
  266. */
  267. Remove() {
  268. this.stack.pop();
  269. }
  270. /**
  271. * Implement r
  272. *
  273. * Reverses the entire stack
  274. */
  275. Reverse() {
  276. this.stack.reverse();
  277. }
  278. /**
  279. * Implement l
  280. *
  281. * Pushes the length of the stack onto the top of the stack
  282. */
  283. PushLength() {
  284. this.stack.push(this.stack.length);
  285. }
  286. }
  287. /**
  288. * Get the char code of any character
  289. *
  290. * Can actually take any length of a value, but only returns the
  291. * char code of the first character.
  292. *
  293. * @param {*} value Any character
  294. * @returns {int} The value's char code
  295. */
  296. function dec(value) {
  297. return value.toString().charCodeAt(0);
  298. }