PlayerColor colorTurn;
PlayerColor rotatingColor;
int numPlayers;
+ bool validPieces[numPieces];
+ bool over;
+ bool validMove;
void NewGame()
{
for(i = 0; i < bs * bs; i++)
board[i] = none;
+ over = false;
colorTurn = blue;
rotatingColor = blue;
+
+ validMove = CheckValidMoves(colorTurn, validPieces);
}
- bool ValidMove(int selectedPiece, int direction, bool flip, int boardX, int boardY)
+ bool ValidMove(PlayerColor playerColor, int selectedPiece, int direction, bool flip, int boardX, int boardY)
{
Piece * piece = &pieces[selectedPiece];
int x, y;
bool valid = true;
bool touchCorner = false;
+ int w = (direction & 1) ? piece->h : piece->w;
+ int h = (direction & 1) ? piece->w : piece->h;
+
+ if(!playerPieces[playerColor][selectedPiece]) return false;
- // TODO: Add check whether player color has this particular piece
+ if(boardX < 0 || boardY < 0 || boardX + w > bs || boardY + h > bs) return false;
for(y = 0; y < 5; y++)
for(x = 0; x < 5; x++)
{
int bx = x + boardX, by = y + boardY;
if(board[by * bs + bx] ||
- (by > 0 && board[(by-1) * bs + bx] == colorTurn) ||
- (by < bs-1 && board[(by+1) * bs + bx] == colorTurn) ||
- (bx > 0 && board[by * bs + bx - 1] == colorTurn) ||
- (bx < bs-1 && board[by * bs + bx + 1] == colorTurn))
+ (by > 0 && board[(by-1) * bs + bx] == playerColor) ||
+ (by < bs-1 && board[(by+1) * bs + bx] == playerColor) ||
+ (bx > 0 && board[by * bs + bx - 1] == playerColor) ||
+ (bx < bs-1 && board[by * bs + bx + 1] == playerColor))
{
valid = false;
break;
}
- if((by > 0 && bx > 0 && board[(by-1) * bs + (bx-1)] == colorTurn) ||
- (by > 0 && bx < bs-1 && board[(by-1) * bs + (bx+1)] == colorTurn) ||
- (by < bs-1 && bx > 0 && board[(by+1) * bs + (bx-1)] == colorTurn) ||
- (by < bs-1 && bx < bs-1 && board[(by+1) * bs + (bx+1)] == colorTurn))
+ if((by > 0 && bx > 0 && board[(by-1) * bs + (bx-1)] == playerColor) ||
+ (by > 0 && bx < bs-1 && board[(by-1) * bs + (bx+1)] == playerColor) ||
+ (by < bs-1 && bx > 0 && board[(by+1) * bs + (bx-1)] == playerColor) ||
+ (by < bs-1 && bx < bs-1 && board[(by+1) * bs + (bx+1)] == playerColor))
touchCorner = true;
}
- if(valid && firstPiece[colorTurn])
+ if(valid && firstPiece[playerColor])
{
for(y = 0; y < 5; y++)
for(x = 0; x < 5; x++)
if(PieceBlock(selectedPiece, x, y, direction, flip))
{
int bx = x + boardX, by = y + boardY;
- if(bx == corners[colorTurn].x && by == corners[colorTurn].y)
+ if(bx == corners[playerColor].x && by == corners[playerColor].y)
{
touchCorner = true;
break;
return valid && touchCorner;
}
+ bool CheckValidMoves(PlayerColor playerColor, bool validPieces[21])
+ {
+ bool result = false;
+ int p;
+ for(p = 0; p < numPieces; p++)
+ {
+ bool validMove = false;
+ if(playerPieces[playerColor][p])
+ {
+ int x, y;
+ for(y = 0; y < bs && !validMove; y++)
+ {
+ for(x = 0; x < bs && !validMove; x++)
+ {
+ int flip;
+ int direction;
+ for(direction = 0; direction < 4 && !validMove; direction++)
+ {
+ for(flip = 0; flip <=1 && !validMove; flip++)
+ {
+ if(ValidMove(playerColor, p, direction, flip, x, y))
+ result = validMove = true;
+ }
+ }
+ }
+ }
+ }
+ if(validPieces) validPieces[p] = validMove;
+ }
+ return result;
+ }
+
void PlayMove(int pieceType, int direction, bool flip, int boardX, int boardY)
{
int y, x;
if(++rotatingColor == green) rotatingColor = 0;
}
if(++colorTurn > green) colorTurn = 0;
+
+ validMove = CheckValidMoves(colorTurn, validPieces);
+ if(!validMove)
+ CheckGameOver();
}
void Pass()
if(++rotatingColor == green) rotatingColor = 0;
}
if(++colorTurn > green) colorTurn = 0;
+
+ validMove = CheckValidMoves(colorTurn, validPieces);
+ if(!validMove)
+ CheckGameOver();
+ }
+
+ void CheckGameOver()
+ {
+ PlayerColor turn = colorTurn;
+ int c;
+ for(c = 0; c < 3; c++)
+ {
+ if(++turn > green) turn = 0;
+ if(CheckValidMoves(turn, null))
+ break;
+ }
+ if(c == 3)
+ over = true;
}
};
Point boardPos;
int direction;
bool flip;
+ bool passed[4];
void NextColorPlayed()
{
}
}
- void DrawSquare(Surface surface, int x, int y, BlokusColor color)
+ void DrawSquare(Surface surface, int x, int y, BlokusColor color, int shade)
{
- surface.background = colors[blokus.gameStarted][color];
+ surface.background = colors[shade][color];
surface.Area(x+1, y+1, x + squareWidth-1, y + squareWidth-1);
surface.foreground = lightGray;
surface.VLine(y+5, y + 15, x + 5);
if(gameStarted && colorPlayed == gameState.colorTurn)
{
Piece * piece = &pieces[selectedPiece];
- if(gameState.ValidMove(selectedPiece, direction, flip, boardPos.x, boardPos.y))
+ if(gameState.ValidMove(gameState.colorTurn, selectedPiece, direction, flip, boardPos.x, boardPos.y))
panel.server.PlayPiece(selectedPiece, direction, flip, boardPos.x, boardPos.y);
}
}
dragging = true;
OnMouseMove(mx, my, mods);
Capture();
-
}
}
return true;
char * s;
int len, tw;
Color turnLight = white;
- if(gameStarted)
+ if(gameStarted && !gameState.over)
{
ColorRGB empty = colors[1][gameState.colorTurn] /*gray*/, full = white;
turnLight = ColorRGB
surface.WriteText(x + 15, y, s, len);
}
- if(gameState.numPlayers > 1 && colorPlayed == gameState.colorTurn)
+ if(gameState.over)
+ {
+ surface.font = yourTurnFont.font;
+ surface.foreground = crimson;
+ surface.CenterTextf(x + bs*squareWidth/2, y + 3, "Game Over");
+ }
+ else if(gameState.numPlayers > 1 && colorPlayed == gameState.colorTurn)
{
surface.font = yourTurnFont.font;
surface.foreground = tomato;
BlokusColor color = gameState.board[y * bs + x];
if(color)
{
- DrawSquare(surface, bx + x * squareWidth, by + y * squareWidth, color);
+ DrawSquare(surface, bx + x * squareWidth, by + y * squareWidth, color, blokus.gameStarted);
}
}
}
if(PieceBlock(c, x, y, 0, false))
{
if(!dragging || selectedPiece != c)
- DrawSquare(surface, bx + x * squareWidth, by + y * squareWidth, colorPlayed);
+ DrawSquare(surface, bx + x * squareWidth, by + y * squareWidth, colorPlayed, gameStarted && (gameState.colorTurn != colorPlayed || gameState.validPieces[c]));
}
}
}
{
if(PieceBlock(selectedPiece, x, y, direction, flip))
{
- DrawSquare(surface, drag.x + x * squareWidth, drag.y + y * squareWidth, colorPlayed);
+ DrawSquare(surface, drag.x + x * squareWidth, drag.y + y * squareWidth, colorPlayed, gameStarted && gameState.colorTurn == colorPlayed);
if(x == 0 || !PieceBlock(selectedPiece, x-1, y, direction, flip))
{
}
};
- Button button1
+ Button btnPass
{
- this, text = "Pass", anchor = { right = 5, bottom = 5 };
+ this, text = "No Move Available! Pass...", anchor = { right = 5, bottom = 5 };
inactive = true;
+ visible = false;
bool NotifyClicked(Button button, int x, int y, Modifiers mods)
{
+ btnPass.visible = false;
+ blokus.passed[blokus.gameState.colorTurn] = true;
panel.server.Pass();
return true;
}
strcpy(panel.playerNames[np++], gameInfo.players[c]);
}
+ blokus.btnPass.visible = false;
blokus.gameState.NewGame();
+ blokus.passed[0] = false;
+ blokus.passed[1] = false;
+ blokus.passed[2] = false;
+ blokus.passed[3] = false;
blokus.gameStarted = true;
blokus.lightValue = 1;
blokus.lightDir = -.1f;
if(color == blokus.colorPlayed)
blokus.NextColorPlayed();
+ if(blokus.colorPlayed == blokus.gameState.colorTurn &&
+ !blokus.gameState.validMove && !blokus.gameState.over)
+ {
+ if(!blokus.passed[blokus.gameState.colorTurn])
+ blokus.btnPass.visible = true;
+ else
+ panel.server.Pass();
+ }
+
blokus.UpdatePlayerNames();
blokus.Update(null);
}
blokus.gameState.Pass();
if(color == blokus.colorPlayed)
blokus.NextColorPlayed();
+
+ if(blokus.colorPlayed == blokus.gameState.colorTurn &&
+ !blokus.gameState.validMove && !blokus.gameState.over)
+ {
+ if(!blokus.passed[blokus.gameState.colorTurn])
+ blokus.btnPass.visible = true;
+ else
+ panel.server.Pass();
+ }
+
+ blokus.UpdatePlayerNames();
blokus.Update(null);
}