Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored game logic #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
282 changes: 122 additions & 160 deletions Game.js
Original file line number Diff line number Diff line change
@@ -1,175 +1,137 @@

/**
* Randomize array element order in-place.
* Using Fisher-Yates shuffle algorithm.
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
function Cell(desc, color, attrs) {
if(attrs === undefined)
attrs = {movable: true}
if(attrs.movable === undefined)
attrs.movable = true

this.desc = desc;
this.color = color;
for(attr in attrs)
this[attr] = attrs[attr]
}

function repeat(n, value ) {
return Array.apply(null, Array(n)).map(function(_) {return value;});
var Cells = {
grass: function() { return new Cell("Трава", "#8DCF54") },
sea: function() { return new Cell("Море", "#82F2E2", {water: true, movable: false}) },
ship: function() { return new Cell("Корабль", "#CD853F", {ship: true}) },
chest: function(money) { return new Cell("Сундук", "#CD853F", {money: money}) }
}

var teams = ["Призрачный", "Красный", "Белый"];
function Field(size) {
this.size = size;

//Game
function grassCell() {
return {desc: "Трава", color: "#8DCF54"}
}
var field = []

function seaCell() {
return {desc: "Море", color: "#82F2E2", noMove: true}
}
function cell(x, y) {
var result = field[y * size + x]
result.x = x
result.y = y

function shipCell() {
return {desc: "Корабль", shipTeam: 1, color: "#CD853F"}
}
return result
}
this.cell = cell

function chestCell(money) {
return {desc: "Сундук", money: money, color: "#CD853F"};
}
function setCell(x, y, what) { field[y * size + x] = what }
this.setCell = setCell

function makePirate(id, i, j) {
return {
desc: "Пират", team: 1, id: id, x: i, y: j,
/*move: function(ti, tj) {
this.x = ti; this.y = tj;
}*/
};
}
var pirates = []
this.addPirate = function (id, x, y) { pirates.push({desc: "Пират", id: id, x: x, y: y}) }

function posIndex(i, j) { return j * fieldSize + i; }

var offsets = [{x: 1, y: 0}, {x: 1, y: -1}, {x: 0, y: -1}, {x: -1, y: -1},
{x: -1, y: 0}, {x: -1, y: 1}, {x: 0, y: 1}, {x: 1, y: 1}];

function emptyBoard(size) {
return {
size: size,
field: [],
cell: function(i, j, what) {
if (what === undefined)
return this.field[size * j + i];
else
this.field[size * j + i] = what;
},

pirates: [],
pirate: function(i, what) {
if (what === undefined)
return this.pirates[i];
else
this.pirates[i] = what;
},

piratesOn: function(i, j) {
var res = [];
for (var p of this.pirates) {
if (p && p.x == i && p.y == j)
res.push(p);
}
return res;
},

possibleActions: function(p) {
var res = [];
for (off of offsets) {
var i = p.x + off.x, j = p.y + off.y;
if (i >= 0 && i < this.size &&
j >= 0 && j < this.size &&
!this.cell(i, j).noMove)
res.push({
action: "move", x: i, y: j,
desc: "Идти", act: function(who, where) {
where.move(who, this.x, this.y);
}
});
}
//Drop the money if the pirate has it
if (p.money)
res.push({
action: "drop", x: p.x, y: p.y,
desc: "Положить", act: function(who, where) {
where.drop(who);
}
});
//Grab the money it he doesn't
else if (this.cell(p.x, p.y).money)
res.push({
action: "grab", x: p.x, y: p.y,
desc: "Взять", act: function(who, where) {
where.grab(who);
}
});
//Drive the ship
if (this.cell(p.x, p.y).shipTeam) {
var driveOffsets = [{x: 1, y: 0}, {x: -1, y: 0}];
for (var off of driveOffsets) {
var i = p.x + off.x, j = p.y + off.y;
res.push({
action: "drive", x: i, y: j,
desc: "Плыть", act: function(who, where) {
var ship = where.cell(who.x, who.y);
where.cell(who.x, who.y, seaCell());
where.cell(this.x, this.y, ship);
for (var p of where.piratesOn(who.x, who.y))
where.move(p, this.x, this.y);
function piratesOn(x, y) {
return pirates.filter(function(pirate) { return pirate.x == x && pirate.y == y })
}
this.piratesOn = piratesOn

function neightbours(p) {
var result = []
for(var x=p.x-1;x<= p.x+1;x++)
for(var y=p.y-1;y<=p.y+1;y++) {
if(x == p.x && y == p.y)
continue;

if(x < 0 || x >= size)
continue;
if(y < 0 || y >= size)
continue;

result.push(cell(x, y));
}
});
}
}
return res;
},

move: function(p, i, j) {
//TODO: check possibility
p.x = i; p.y = j;
},

grab: function(p) {
var c = this.cell(p.x, p.y);
if (c.money && !p.money) {
c.money--;
p.money = 1;
}
},

drop: function(p) {
var c = this.cell(p.x, p.y);
if (p.money) {
if (c.money === undefined)
c.money = 0;
c.money++;
p.money = 0;
}
},
}
}

function defaultBoard(size) {
var board = emptyBoard(size);
for (var j = 0; j < size; ++j) {
for (var i = 0; i < size; ++i) {
if (j == 0 || j == fieldSize - 1 ||
i == 0 || i == fieldSize - 1)
board.cell(i, j, seaCell());
else
board.cell(i, j, grassCell());
return result;
}

function moveActions(p) {
return neightbours(p)
.filter(function(cell) { return cell.movable })
.map(function(cell) {
return { name: "move",
desc: "Идти",
x: cell.x, y: cell.y,
action: function() {
p.x = cell.x
p.y = cell.y
}}})
}

function moneyActions(p) {
if (p.money)
return [{ name: "drop",
desc: "Положить",
x: p.x, y: p.y,
action: function() {
var c = cell(p.x, p.y);
if(c.money === undefined)
c.money = 1
else
c.money++
p.money = null
}}]
else if(cell(p.x, p.y).money && !p.money)
return [{ name: "grab",
desc: "Взять",
x: p.x, y: p.y,
action: function() {
cell(p.x, p.y).money--
p.money = 1
}}]

return []
}

function sailActions(p) {
if (!cell(p.x, p.y).ship)
return []

return [p.x - 1, p.x + 1].map(
function(x) {
return { name: "sail",
x: x, y: p.y,
desc: "плыть",
action: function() {
var ship = cell(p.x, p.y)
setCell(p.x, p.y, Cells.sea())
setCell(x, p.y, ship)
for(var pirate of piratesOn(p.x, p.y))
pirate.x = x
}}})
}
}
var n = 1;
for (var i = 1; i <= 3; ++i) {
for (var j = 1; j <= 3 - i; ++j) {
board.cell(n++, 1, chestCell(j));

this.actions = function(p) { return moveActions(p).concat(moneyActions(p), sailActions(p)); }

for (var j = 0; j < size; ++j) {
for (var i = 0; i < size; ++i) {
if (j == 0 || j == fieldSize - 1 ||
i == 0 || i == fieldSize - 1)
this.setCell(i, j, Cells.sea())
else
this.setCell(i, j, Cells.grass())
}
}
var n = 1;
for (var i = 1; i <= 3; ++i) {
for (var j = 1; j <= 3 - i; ++j) {
this.setCell(n++, 1, Cells.chest(j))
}
}
}
board.cell((size - 1) / 2, 0, shipCell());
return board;
this.setCell((size - 1) / 2, 0, Cells.ship())
}
Loading