I have some code which moves the users position up, right, down or left depending on the direction entered.
function up(field, player) {
player.column = player.column + 1;
field[player.row][player.column] = 'P';
}
function right(field, player) {
player.row = player.row + 1;
field[player.row][player.column] = 'P';
}
function down(field, player) {
player.column = player.column - 1;
field[player.row][player.column] = 'P';
}
function left(field, player) {
player.row = player.row - 1;
field[player.row][player.column] = 'P';
}
The code works fine for what I need, but I was curious if there is a more efficient way of writing the 4 functions above? As in maybe use one function rather than 4?
note Edge cases will be implemented - if the next position is greater than the dimensions of the field, etc. So I'm not sure if this will impact making it more efficient