Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 75 additions & 12 deletions src/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,87 @@ const boardWidth = 28;
const boardHeight = 14;

let board = [];
let pawns = [];

function init() {
for (let i = 0; i < boardHeight; i++) {
board[i] = [];
for (let j = 0; j < boardWidth; j++) {
board[i][j] = floor;
for (let i = 0; i < boardHeight; i++) {
board[i] = [];
for (let j = 0; j < boardWidth; j++) {
board[i][j] = floor;
}
}
}
}

function write() {
for (let i = 0; i < boardHeight; i++) {
console.log(board[i].join(''));
}
// clear board
init();
// place pawns
pawns.forEach((pawn) => {
let [i, j] = pawn.location;
board[i][j] = pawn.symbol;
});

for (let i = 0; i < boardHeight; i++) {
console.log(board[i].join(''));
}
}

let randomLocation = function fetchRandomLocation() {
let location = [
Math.floor(Math.random() * boardHeight),
Math.floor(Math.random() * boardWidth)
];
pawns.forEach((pawn) => {
if (pawn.location == location) {
return randomLocation();
}
});

return location;
};

let addPawn = function addPawnWithRandomPosition(pawn) {
const location = randomLocation();
pawns.push(pawn);
return location;
};

// TODO: add decorator for redrawing board after move

let up = function newLocationUp([i, j]) {
const newUp = i - 1 <= 0 ? 0 : i - 1;
return [newUp, j];
};

let down = function newLocationDown([i, j]) {
const newDown = i + 1 >= boardHeight ? boardHeight - 1 : i + 1;
return [newDown, j];
};

let left = function newLocationLeft([i, j]) {
const newLeft = j - 1 <= 0 ? 0 : j - 1;
return [i, newLeft];
};

let right = function newLocationRight([i, j]) {
const newRight = j + 1 >= boardWidth - 1 ? boardWidth : j + 1;
return [i, newRight];
};

let movePawns = function moveAllPawns() {
pawns.forEach((p) => {
p.move();
});
};

module.exports = {
board: board,
init: init,
write: write
};
board: board,
init: init,
write: write,
addPawn: addPawn,
up: up,
down: down,
left: left,
right: right,
movePawns: movePawns
};
93 changes: 80 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,91 @@ const readline = require('readline');
const Board = require('./Board');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> '
input: process.stdin,
output: process.stdout,
prompt: '> '
});

let Pawn = class {
constructor(board, hp, symbol) {
this.hp = hp;
this.board = board;
this.location = this.board.addPawn(this);
this.symbol = symbol;
}

up() {
this.location = this.board.up(this.location);
}
down() {
this.location = this.board.down(this.location);
}
left() {
this.location = this.board.left(this.location);
}
right() {
this.location = this.board.right(this.location);
}
move() {
const moves = ["up", "down", "left", "right"];
let randomMove = moves[Math.floor(Math.random()*moves.length)];
this[randomMove]();
}
};

let Player = class extends Pawn {
constructor(board) {
super(board, 10, "P");
}
move() {
rl.prompt();
}
};

let Snake = class extends Pawn {
constructor(board) {
super(board, 2, "S");
}
};

let Dragon = class extends Pawn {
constructor(board) {
super(board, 25, "D");
}
};

const player = new Player(Board);
const dragon = new Dragon(Board);
const snake = new Snake(Board);

rl.on('line', (line) => {
switch (line.trim()) {
default:
Board.write();
console.log(`Your input was '${line.trim()}'`);
break;
}
rl.prompt();
switch (line.trim()) {
case "up":
player.up();
Board.write();
break;
case "down":
player.down();
Board.write();
break;
case "left":
player.left();
Board.write();
break;
case "right":
player.right();
Board.write();
break;
default:
Board.write();
console.log(`Your input was '${line.trim()}'`);
break;
}
Board.movePawns();
}).on('close', () => {
console.log('Have a great day!');
process.exit(0);
console.log('Have a great day!');
process.exit(0);
});

Board.init();
Board.write();
rl.prompt();