Last active
September 13, 2016 04:42
-
-
Save holyshared/054f3f4be16433c77b7b16afa5e0066b to your computer and use it in GitHub Desktop.
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
module.exports = [ | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | |
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1], | |
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1], | |
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1], | |
[1, 0, 1, 1, 1, 1, 0, 1, 0, 1], | |
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 0, 1, 1, 1, 1, 0, 1, 0, 1], | |
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1], | |
[1, 0, 1, 0, 0, 0, 0, 1, 1, 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
'use strict'; | |
const PlayerDirection = require('./player-direction'); | |
class Dungeon { | |
constructor(map) { | |
this.map = map; | |
} | |
isWall(position) { | |
const x = position.x; | |
const y = position.y; | |
if (!this.map[y]) { | |
return true; | |
} | |
const row = this.map[y]; | |
if (row[x] === 1) { | |
return true; | |
} | |
return false; | |
} | |
print(player) { | |
const texts = this.map.map((rows, y) => { | |
return rows.map((col, x) => { | |
if (col === 1) { | |
return 'X'; | |
} else if (x === player.position.x && y === player.position.y) { | |
return player.toString(); | |
} | |
return ' '; | |
}); | |
}); | |
texts.map((rows) => rows.join('')) | |
.forEach((row) => console.log(row)) | |
} | |
} | |
module.exports = Dungeon; |
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
const Dungeon = require('./dungeon'); | |
const Player = require('./player'); | |
const MoveType = require('./move-type'); | |
const dungeonMap = require('./dungeon-map'); | |
const d = new Dungeon(dungeonMap); | |
const p = new Player(d); | |
p.move(MoveType.Front); | |
d.print(p); | |
p.move(MoveType.Front); | |
d.print(p); | |
p.move(MoveType.Front); | |
d.print(p); | |
p.move(MoveType.Right); | |
d.print(p); |
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
'use strict'; | |
module.exports = { | |
Left: 1, | |
Right: 2, | |
Front: 3, | |
Back: 4 | |
}; |
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
'use strict'; | |
class PlayerDirection { | |
constructor(value, symbol) { | |
this._value = value; | |
this._symbol = symbol; | |
} | |
get value() { | |
return this._value; | |
} | |
equals(direction) { | |
return this.value === direction.value; | |
} | |
toString() { | |
return this._symbol; | |
} | |
} | |
module.exports = { | |
Left: new PlayerDirection(1, '←'), | |
Right: new PlayerDirection(2, '→'), | |
Front: new PlayerDirection(3, '↑'), | |
Back: new PlayerDirection(4, '↓') | |
}; |
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
'use strict'; | |
const MoveType = require('./move-type'); | |
const PlayerDirection = require('./player-direction'); | |
const Point = require('./point'); | |
const PointDistance = require('./point-distance'); | |
class Player { | |
constructor(dungeon) { | |
this.direction = PlayerDirection.Front; | |
this.dungeon = dungeon; | |
this.position = new Point(1, 9); | |
} | |
move(moveType) { | |
let distance = null; | |
let direction = null; | |
if (moveType === MoveType.Front) { | |
distance = new PointDistance(0, -1); | |
direction = PlayerDirection.Front; | |
} else if (moveType === MoveType.Back) { | |
distance = new PointDistance(0, 1); | |
direction = PlayerDirection.Back; | |
} else if (moveType === MoveType.Left) { | |
distance = new PointDistance(-1, 0); | |
direction = PlayerDirection.Left; | |
} else if (moveType === MoveType.Right) { | |
distance = new PointDistance(1, 0); | |
direction = PlayerDirection.Right; | |
} | |
const next = this.position.add(distance); | |
console.log(next); | |
if (this.dungeon.isWall(next)) { | |
return; | |
} | |
this.direction = direction; | |
this.position = next; | |
} | |
toString() { | |
return this.direction.toString(); | |
} | |
} | |
module.exports = Player; |
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
'use strict'; | |
class PointDistance { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
module.exports = PointDistance; |
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
'use strict'; | |
class Point { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
add(distance) { | |
return new Point( | |
this.x + distance.x, | |
this.y + distance.y | |
); | |
} | |
} | |
module.exports = Point; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment