A Simpler Tetra Master
by Sam Fereday
JavaScript
/*
Step One:
- Each card edge has a defence or weakness
- When two are placed next to each other they do battle
- Whoever loses will lose that card
- 3x3 grid
*/
// Cell
var Cell = function(x, y) {
this.occupied = false;
this.occupiedBy = {};
this.x = x;
this.y = y;
};
Cell.prototype.Occupy = function(card) {
this.SetOccupied();
this.occupiedBy = card;
};
Cell.prototype.GetOccupier = function() {
return this.occupiedBy;
};
Cell.prototype.SetOccupied = function() {
this.occupied = true;
};
Cell.prototype.IsAvailable = function() {
return !this.occupied;
};
Cell.prototype.IsOccupied = function() {
return this.occupied;
};
// Grid
var Grid = function(w, h) {
this.w = w;
this.h = h;
this.cells = [];
};
Grid.prototype.GetCellAt = function(x, y) {
return this.cells[x][y];
};
Grid.prototype.Make = function() {
for (var i = 0; i < this.w; i++) {
let cellRow = [];
for (var j = 0; j < this.h; j++) {
cellRow.push(new Cell(i, j));
}
this.cells.push(cellRow);
}
};
// Cards
var Card = function(name, owner) {
this.name = name;
this.owner = owner;
this.stats = {
att: 1,
def: 1
};
};
Card.prototype.Chown = function(owner) {
this.owner = owner;
};
Card.prototype.InPosession = function(owner) {
return this.owner === owner;
};
// Board Manager
var BoardMan = function(w, h) {
this.grid = new Grid(w, h);
this.opponents = {
a: null,
b: null
}
return this;
};
BoardMan.prototype.Init = function(a, b) {
this.opponents.a = a;
this.opponents.b = b;
this.grid.Make();
return this;
};
BoardMan.prototype.PlaceCard = function(ownerCard, x, y) {
let _cell = this.grid.GetCellAt(x, y);
if (_cell.IsAvailable()) {
_cell.Occupy(ownerCard);
this.OnPlaced(_cell, ownerCard);
}
};
BoardMan.prototype.OnPlaced = function(cell, ownerCard) {
// Find out who's where in relation
let up = this.grid.GetCellAt(cell.x, cell.y),
down =...