Skip to content

Instantly share code, notes, and snippets.

@carbide-public
Created September 16, 2022 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbide-public/7e098e212d199d85862191cc20e4dfd3 to your computer and use it in GitHub Desktop.
Save carbide-public/7e098e212d199d85862191cc20e4dfd3 to your computer and use it in GitHub Desktop.
Delta Math Robots Type 1
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
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
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