Skip to content

Commit

Permalink
first implementation for 3 players
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJan00 committed Aug 11, 2011
1 parent 45891d2 commit ad52759
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
68 changes: 52 additions & 16 deletions legend.js
Expand Up @@ -41,10 +41,28 @@ G.Display = function() {
}
}

self.playerNameForIndex = function(pn) {
if (!G.playerManager.isHuman())
return G.strings.thinkingMessage;
switch(pn) {
case 0: return G.strings.firstPlayerName;
case 1: return G.strings.secondPlayerName;
case 2: return G.strings.thirdPlayerName;
case 3: return G.strings.fourthPlayerName;
}
}

self.showPlayerScore = function(pn) {
var size = 50;
var x0 = G.Piles[pn].x0 + G.Piles[pn].width/2 - size/2;
var y0 = G.Piles[pn].y0 / 2 - size/2;
var x0, y0;

if (G.Piles[pn].vertical) {
x0 = G.Piles[pn].x0 + G.Piles[pn].width/2 - size/2;
y0 = G.Piles[pn].y0 / 2 - size/2;
} else {
x0 = G.Piles[pn].x0 / 2 - size/2;
y0 = G.Piles[pn].y0 + G.Piles[pn].height/2 - size/2;
}

var ctxt = G.graphicsManager.messagesContext;
G.graphicsManager.mark(x0, y0, size, size);
Expand Down Expand Up @@ -76,20 +94,18 @@ G.Display = function() {

self.showPlayerScore(0);
self.showPlayerScore(1);
//self.showPlayerScore(2);
self.showPlayerScore(2);
//self.showPlayerScore(3);

ctxt.font = "bold 28px CustomFont, sans-serif";
ctxt.fillStyle = self.colorForPlayerBorder(pn);
var msg = (pn?G.strings.secondPlayerName:G.strings.firstPlayerName);
if (!G.playerManager.isHuman())
msg = G.strings.thinkingMessage;
var msg = self.playerNameForIndex(pn);
var msglen = ctxt.measureText(msg);
ctxt.fillText(msg, data.x0+data.width/2 - msglen.width/2, data.y0+data.height/2+14 );

G.Piles[0].redrawBorder(false);
G.Piles[1].redrawBorder(false);
//G.Piles[2].redrawBorder(false);
G.Piles[2].redrawBorder(false);
//G.Piles[3].redrawBorder(false);
}

Expand Down Expand Up @@ -143,8 +159,22 @@ G.Display = function() {

self.checkVictory = function() {
var data = G.coords.text
var counts = G.counts;
var victory1 = counts[0]>counts[1];

// ToDo: this also doesn't belong here, we should separate logic from display
var victorious = -1;
var victorycount = -1;
for (var i=0; i<4; i++)
if (G.counts[i] > victorycount) {
victorious = i;
victorycount = G.counts[i]
}

var tiecount = 0;
for (var i = 0; i < 4; i++)
if (G.counts[i] == victorycount)
tiecount++;

var tiegame = (tiecount > 1);

// TODO: this does NOT belong here! it is logic!
G.waitingForRestart = true;
Expand All @@ -156,27 +186,33 @@ G.Display = function() {
ctxt.font = "bold 24px CustomFont, sans-serif";

var msg;
if (counts[0]>counts[1]) {
if (tiegame) {
ctxt.fillStyle = G.colors.black;
msg = G.strings.tieGame;
} else if (victorious == 0) {
ctxt.fillStyle = self.colorForPlayerBorder(0);
msg = G.strings.firstVictory;
} else if (counts[0] < counts[1]) {
} else if (victorious == 1) {
ctxt.fillStyle = self.colorForPlayerBorder(1);
msg = G.strings.secondVictory;
} else {
ctxt.fillStyle = G.colors.black;
msg = G.strings.tieGame;
} else if (victorious == 2) {
ctxt.fillStyle = self.colorForPlayerBorder(2);
msg = G.strings.thirdVictory;
} else if (victorious == 3) {
ctxt.fillStyle = self.colorForPlayerBorder(3);
msg = G.strings.fourthVictory;
}
var msglen = ctxt.measureText(msg);
ctxt.fillText(msg, data.x0 + data.width/2 - msglen.width/2, data.y0+data.height/2 );

self.showPlayerScore(0);
self.showPlayerScore(1);
//self.showPlayerScore(2);
self.showPlayerScore(2);
//self.showPlayerScore(3);

G.Piles[0].redrawBorder(true);
G.Piles[1].redrawBorder(true);
//G.Piles[2].redrawBorder(true);
G.Piles[2].redrawBorder(true);
//G.Piles[3].redrawBorder(true);
}
}
Expand Down
2 changes: 2 additions & 0 deletions logic.js
Expand Up @@ -186,6 +186,8 @@ G.FloodCheck = function() {
G.counts = [];
G.counts[0] = 0;
G.counts[1] = 0;
G.counts[2] = 0;
G.counts[3] = 0;
for (var i=0; i<self.board.cols; i++)
for (var j=0; j<self.board.rows; j++) {
if (self.board[i][j]) {
Expand Down
4 changes: 4 additions & 0 deletions main.js
Expand Up @@ -115,6 +115,7 @@ G.Main = function() {
G.graphicsManager.clearBackground();
G.Piles[0].drawFromScratch();
G.Piles[1].drawFromScratch();
G.Piles[2].drawFromScratch();
G.board.drawEmpty();
G.board.drawAllTiles();
G.display.showPlayer();
Expand Down Expand Up @@ -233,6 +234,7 @@ G.Main = function() {

G.Piles[0].redrawBorder(G.playerManager.current == 0);
G.Piles[1].redrawBorder(G.playerManager.current == 1);
G.Piles[2].redrawBorder(G.playerManager.current == 2);

if ((!G.waitingForTurn) && (!G.playerManager.isHuman())) {
G.waitingForTurn = true;
Expand Down Expand Up @@ -260,6 +262,8 @@ G.Main = function() {
G.Piles[0].manageClicked(G.mouse.x, G.mouse.y);
else if (G.Piles[1].isClicked(G.mouse.x, G.mouse.y))
G.Piles[1].manageClicked(G.mouse.x, G.mouse.y);
else if (G.Piles[2].isClicked(G.mouse.x, G.mouse.y))
G.Piles[2].manageClicked(G.mouse.x, G.mouse.y);
else if (G.board.isClicked(G.mouse.x, G.mouse.y))
turnIsReady = G.board.manageClicked(G.mouse.x, G.mouse.y);
}
Expand Down
16 changes: 9 additions & 7 deletions piles.js
@@ -1,10 +1,11 @@
G.PileClass = function(x0,y0, owner) {
G.PileClass = function(x0,y0, owner, vertical) {
this.setDimensions(3, 6, x0, y0);

this.border = 5;
this.totalItems = this.rows * this.cols;
this.owner = owner;
this.selection = false;
this.vertical = typeof(vertical)=="boolean" ? vertical : true;
};

G.PileClass.prototype = new G.StoneHolder;
Expand Down Expand Up @@ -67,12 +68,13 @@ G.PileClass.prototype.chooseTiles = function() {

// this condition is wrong... I have to come up with something else
//if (this.x0 < G.board.x0 || this.x0 > G.board.x0+G.board.width) {
if (this.vertical) {
colCount = Math.ceil(tilesPerPlayer / G.board.rows);
rowCount = Math.ceil(tilesPerPlayer / colCount);
//} else {
// rowCount = Math.ceil(tilesPerPlayer / G.board.cols);
// colCount = Math.ceil(tilesPerPlayer / rowCount);
//}
} else {
rowCount = Math.ceil(tilesPerPlayer / G.board.cols);
colCount = Math.ceil(tilesPerPlayer / rowCount);
}

this.setDimensions(colCount, rowCount, this.x0, this.y0);

Expand Down Expand Up @@ -178,8 +180,8 @@ G.initPiles = function()
G.Piles = [];
G.Piles[0] = new G.PileClass(10, 70, 0);
G.Piles[1] = new G.PileClass(500, 70, 1);
G.Piles[2] = new G.PileClass(10, 70, 2);
G.Piles[3] = new G.PileClass(10, 70, 3);
G.Piles[2] = new G.PileClass(200, 400, 2, false);
G.Piles[3] = new G.PileClass(200, 10, 3, false);

G.Piles[0].cellColor = function(ind) {
return G.colors.purpleBackground;
Expand Down
2 changes: 1 addition & 1 deletion playerManager.js
@@ -1,7 +1,7 @@
G.PlayerManager = function() {
var self = this;

self.types = [G.playerTypes.human, G.playerTypes.computerEasy, G.playerTypes.none, G.playerTypes.none];
self.types = [G.playerTypes.human, G.playerTypes.computerHard, G.playerTypes.computerHard, G.playerTypes.none];
self.current = 0;

self.init = function() {
Expand Down

0 comments on commit ad52759

Please sign in to comment.