-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.js
More file actions
40 lines (29 loc) · 776 Bytes
/
cpu.js
File metadata and controls
40 lines (29 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { Grid } = require('./grid.js')
class Cpu {
constructor() {
this.cycleCount = 1
this.x = 1
this.grid = new Grid(40, 6)
}
cycle() {
let position = (this.cycleCount-1) % 40
let row = Math.floor( (this.cycleCount-1)/40)
let pixel = '.'
if( this.x === position-1 || this.x === position || this.x === position+1) pixel = '#'
this.grid.setCell( position, row, pixel)
this.cycleCount++
}
execute( command, ...parameters) {
if( command === 'noop') {
this.cycle()
}
if( command === 'addx') {
this.cycle()
this.cycle()
this.x += parameters[0]
}
}
}
module.exports = {
Cpu
}