Skip to content

Commit

Permalink
repositioned piles for 3 players
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJan00 committed Aug 14, 2011
1 parent a61e5d8 commit aee0267
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
2 changes: 1 addition & 1 deletion legend.js
Expand Up @@ -131,7 +131,7 @@ G.Display = function() {
var y = G.board.y0 + G.board.height

var x0 = G.board.x0
var y0 = y + 20
var y0 = G.coords.legend.y0
var y1 = y + G.imageFire.height/2 + 5
var ctxt = G.graphicsManager.messagesContext;
G.graphicsManager.mark(0, y0, G.graphicsManager.width, 25);
Expand Down
83 changes: 82 additions & 1 deletion main.js
Expand Up @@ -58,6 +58,10 @@ G.Main = function() {
y0: 5,
width: 300,
height: 62
},
legend : {
x0: 0,
y0: 440
}
};

Expand Down Expand Up @@ -124,7 +128,7 @@ G.Main = function() {
self.enableTurn();
}

self.repositionEverything = function() {
self.repositionEverythingOld = function() {
var extraWidth = 60;
var minHorzSpace = G.Piles[0].width + G.Piles[1].width + G.board.width + extraWidth;
if (minHorzSpace < 640) {
Expand Down Expand Up @@ -165,6 +169,83 @@ G.Main = function() {
G.yoffset = G.findAbsoluteY(G.gameCanvas);
}

self.repositionEverything = function() {
var pn;
var extraWidth = 60;
var horzPileCount = Math.max(G.playerManager.count() - 2, 0);
// 90 = 50 player score + 20 on each side
var topSpace = 90;
if (G.playerManager.count() == 4)
topSpace = Math.max(90, G.Piles.heightOfHorizontal() + 30);
var legendSpace = 40;
var extraBottomSpace = G.playerManager.count() >= 3 ? G.Piles.heightOfHorizontal() + 30 : 0;
var horzSpace = G.Piles.widthOfVertical() * 2 + G.board.width + extraWidth;
var vertSpace = topSpace + G.board.height + extraBottomSpace + legendSpace;
var vertDimension = vertSpace;

if (horzSpace < 640)
horzSpace = 640;
if (vertSpace < 480)
vertSpace = 480;

G.graphicsManager.resizeCanvas(horzSpace, vertSpace);

G.board.x0 = Math.floor(G.graphicsManager.width / 2 - G.board.width/2);
if (vertDimension < vertSpace)
G.board.y0 = Math.floor(G.graphicsManager.height / 2 - G.board.height/2);
else
G.board.y0 = topSpace;

// first pile
pn = G.playerManager.idForOrder(0);
G.Piles[pn].setVertical(true);
G.Piles[pn].x0 = Math.floor(G.board.x0 / 2 - G.Piles.widthOfVertical()/2);
if (vertDimension < vertSpace)
G.Piles[pn].y0 = Math.floor(G.graphicsManager.height / 2 - G.Piles.heightOfVertical()/2);
else
G.Piles[pn].y0 = topSpace;

// second pile
pn = G.playerManager.idForOrder(1);
if (G.playerManager.count() == 4)
pn = G.playerManager.idForOrder(2);
G.Piles[pn].setVertical(true);
G.Piles[pn].x0 = G.board.x0 + G.board.width + Math.floor(G.board.x0 / 2 - G.Piles.widthOfVertical()/2);
if (vertDimension < vertSpace)
G.Piles[pn].y0 = Math.floor(G.graphicsManager.height / 2 - G.Piles.heightOfVertical()/2);
else
G.Piles[pn].y0 = topSpace;

// third
if (G.playerManager.count() >= 3) {
pn = G.playerManager.idForOrder(2);
if (G.playerManager.count() == 4)
pn = G.playerManager.idForOrder(3);
G.Piles[pn].setVertical(false);
G.Piles[pn].x0 = Math.floor(G.graphicsManager.width / 2 - G.Piles.widthOfHorizontal()/2);
G.Piles[pn].y0 = Math.floor(G.graphicsManager.height - legendSpace - extraBottomSpace/2 - G.Piles.heightOfHorizontal()/2);
}

// fourth
if (G.playerManager.count() == 4) {
pn = G.playerManager.idForOrder(1);
G.Piles[pn].setVertical(false);
G.Piles[pn].x0 = Math.floor(G.graphicsManager.width / 2 - G.Piles.widthOfHorizontal()/2);
G.Piles[pn].y0 = Math.floor(topSpace/2 - G.Piles.heightOfHorizontal()/2);
}

G.coords.text.x0 = G.board.x0;
G.coords.text.width = G.board.width;
G.optionsButton.x0 = G.graphicsManager.width - G.optionsButton.width - 5;
G.optionsButton.y0 = 10; //G.Piles[1].y0 - G.optionsButton.height - 10;

G.coords.legend.y0 = G.graphicsManager.height - 40;

// probably the graphicsmanager should do this
G.xoffset = G.findAbsoluteX(G.gameCanvas);
G.yoffset = G.findAbsoluteY(G.gameCanvas);
}

self.connectMouse = function() {
G.gameCanvas.addEventListener('mousedown', self.mouseDown, false);
G.gameCanvas.addEventListener('mouseup', self.mouseRaise, false);
Expand Down
57 changes: 57 additions & 0 deletions piles.js
Expand Up @@ -121,6 +121,32 @@ G.PileClass.prototype.chooseTiles = function() {
}
}

G.PileClass.prototype.setVertical = function( vert ) {
if (this.vertical != vert) {
// transpose
var stoneList = [];
for (var y = 0; y < this.rows; y++)
for (var x = 0; x < this.cols; x++)
stoneList.push(this[x][y]);

var tmp = this.cols;
this.cols = this.rows;
this.rows = tmp;
this.vertical = !this.vertical;
for (var x = 0; x < this.cols; x++) {
this[x] = [];
for (var y = 0; y < this.rows; y++) {
var st = stoneList.splice(0,1)[0];
st.ix = x;
st.iy = y;
this.set(st.ix, st.iy, st);
}
}
this.width = this.cols * this.side;
this.height = this.rows * this.side;
}
}

////////////////////////////////

G.PileClass.prototype.drawFromScratch = function() {
Expand Down Expand Up @@ -175,6 +201,7 @@ G.PileClass.prototype.getStoneByElement = function(elem)
return false;
}

// todo: G.Piles should become a PileManager
G.initPiles = function()
{
G.Piles = [];
Expand Down Expand Up @@ -214,6 +241,36 @@ G.initPiles = function()
G.Piles[2].chooseTiles();
G.Piles[3].chooseTiles();
}

G.Piles.widthOfVertical = function() {
var totalTiles = G.board.rows * G.board.cols - G.board.holeCount();
var tilesPerPlayer = Math.floor(totalTiles / G.playerManager.count());

return Math.ceil(tilesPerPlayer / G.board.rows) * G.board.side;
}

G.Piles.widthOfHorizontal = function() {
var totalTiles = G.board.rows * G.board.cols - G.board.holeCount();
var tilesPerPlayer = Math.floor(totalTiles / G.playerManager.count());

var rowCount = Math.ceil(tilesPerPlayer / G.board.cols);
return Math.ceil(tilesPerPlayer / rowCount) * G.board.side;
}

G.Piles.heightOfVertical = function() {
var totalTiles = G.board.rows * G.board.cols - G.board.holeCount();
var tilesPerPlayer = Math.floor(totalTiles / G.playerManager.count());

var colCount = Math.ceil(tilesPerPlayer / G.board.rows);
return Math.ceil(tilesPerPlayer / colCount) * G.board.side;
}

G.Piles.heightOfHorizontal = function() {
var totalTiles = G.board.rows * G.board.cols - G.board.holeCount();
var tilesPerPlayer = Math.floor(totalTiles / G.playerManager.count());

return Math.ceil(tilesPerPlayer / G.board.cols) * G.board.side;
}
}

G.countPiles = function() {
Expand Down
4 changes: 4 additions & 0 deletions playerManager.js
Expand Up @@ -48,6 +48,10 @@ G.PlayerManager = function() {
return self.order[self.current];
}

self.idForOrder = function(id) {
return self.order[id];
}

self.setCurrentId = function(id) {
for (var i = 0; i < self.order.length; i++)
if (self.order[i] == id) {
Expand Down

0 comments on commit aee0267

Please sign in to comment.