This Gist was automatically created by Carbide, a free online programming environment.
Created
September 16, 2022 19:25
-
-
Save carbide-public/7e098e212d199d85862191cc20e4dfd3 to your computer and use it in GitHub Desktop.
Delta Math Robots Type 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import robot from './robot.js' | |
let i = 1 | |
for (let index = 1; index <= 4; index++) { | |
robot.rotate.left() | |
robot.move.forward() | |
robot.move.forward() | |
i += 1 | |
prompt(`${i} ${index}\n${robot.moves.join(', ')}`) | |
} | |
robot.move.forward() | |
robot.move.forward() | |
robot.moves |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import robot from './robot.js' | |
function ComboMove(x) { | |
if (x >= 2) { | |
robot.move.forward() | |
} else { | |
if (x === 1) { | |
robot.rotate.right() | |
} else { | |
robot.move.forward() | |
} | |
} | |
} | |
let i = 4 | |
for (let index = 1; index <= 4; index++) { | |
robot.move.forward() | |
robot.rotate.right() | |
ComboMove(i) | |
i -= 1 | |
} | |
robot.moves |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Robot { | |
x = 0 | |
y = 0 | |
rot = 0 // 0: up, 1: right, etc. | |
moves = [] | |
constructor(x, y, rot) { | |
this.x = x | |
this.y = y | |
this.rot = rot | |
} | |
static rotate = { | |
left() { | |
this.moves.push('counterclockwise') | |
this.rot -= 1 | |
if (this.rot === -1) this.rot = 3 | |
}, | |
right() { | |
this.moves.push('clockwise') | |
this.rot += 1 | |
if (this.rot === 4) this.rot = 0 | |
}, | |
} | |
static move = { | |
forward() { | |
this.moves.push('forward') | |
switch (this.rot) { | |
case 0: | |
this.y += 1 | |
break | |
case 1: | |
this.x += 1 | |
break | |
case 2: | |
this.y -= 1 | |
break | |
case 3: | |
this.x -= 1 | |
break | |
} | |
}, | |
} | |
} | |
export default Robot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment