Files
sor/Server/src/SOR.Game/Grid/BoardShapeGenerator.cs
T
356e1a50e3 Phase 3 & 4: Team colors, player_eliminated push, board shapes, lobby team UI
Phase 3 – Team modes:
- GameState: TeamColors dictionary (int → hex color string) populated from palette
- GameMessages: PlayerEliminatedPush, PlayerOutcomeDto, extended GameOverPush
- GameHandlerBase: FinalizeMultiPlayerGameAsync (pairwise ELO), BroadcastPlayerEliminatedAsync, GetAllPlayerIds for N-player push
- GameAttackHandler: snapshot pre-attack elimination state; use FinalizeMultiPlayerGameAsync; push PlayerEliminatedPush for mid-game knockouts
- game_screen.gd: turn order strip chips carry team-color left border from state.teamColors
- lobby_room_screen.gd: _team_label() helper; team badges on host/guest/additional slots

Phase 4 – Board shape library:
- BoardShapeGenerator: AutoShape() picks shape by player count; adds Triangular (3p), RadialStar (5p, 5-arm star), HexRing (6p, hex perimeter zones); CoordsInRect helper
- GameLobbyReadyHandler: auto-selects board shape at game start; passes BoardShape+PlayerCount into rules snapshot

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-03-26 05:50:55 -04:00

245 lines
10 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace SOR.Game.Grid;
/// <summary>
/// Generates <see cref="Board"/> instances for different board shapes, populating
/// <see cref="Board.StartingZones"/> for each player seat.
/// </summary>
public static class BoardShapeGenerator
{
/// <summary>
/// Returns the recommended board shape for a given player count when none is explicitly specified.
/// </summary>
public static string AutoShape(int playerCount) => playerCount switch
{
3 => "Triangular",
4 => "Cross",
5 => "RadialStar",
6 => "HexRing",
_ => "Rectangle", // 2 and 7–10
};
/// <summary>Generate a board appropriate for the given shape, rules, and player count.</summary>
public static Board Generate(string shape, GameRules rules, int playerCount) =>
shape switch
{
"Cross" => MakeCross(rules),
"Triangular" => MakeTriangular(rules),
"RadialStar" => MakeRadialStar(rules),
"HexRing" => MakeHexRing(rules),
_ => MakeRectangle(rules, playerCount),
};
/// <summary>
/// Rectangle board. Starting zones per seat:
/// Seat 0 = rows 0–1 (bottom edge)
/// Seat 1 = rows Height-2 .. Height-1 (top edge)
/// Seat 2 = cols 0–1 (left edge)
/// Seat 3 = cols Width-2 .. Width-1 (right edge)
/// </summary>
private static Board MakeRectangle(GameRules rules, int playerCount)
{
var board = Board.Create(rules.BoardWidth, rules.BoardHeight, rules.StartingRandomTilePct);
int w = rules.BoardWidth, h = rules.BoardHeight;
var zones = new Dictionary<int, HashSet<Coord>>();
// Seat 0 = bottom 2 rows
zones[0] = CoordsInRows(0, 1, w);
if (playerCount >= 2)
zones[1] = CoordsInRows(h - 2, h - 1, w);
if (playerCount >= 3)
zones[2] = CoordsInCols(0, 1, h);
if (playerCount >= 4)
zones[3] = CoordsInCols(w - 2, w - 1, h);
// Return a new Board with the zones attached (Board is a reference type with mutable Tiles
// so we create a wrapper that shares the same Tiles dictionary).
return new Board { Width = board.Width, Height = board.Height, Tiles = board.Tiles, StartingZones = zones };
}
/// <summary>
/// Cross (plus-sign) board for 4 players.
/// A square center arena with 4 arms; the tip of each arm is one player's starting zone.
/// </summary>
private static Board MakeCross(GameRules rules)
{
const int arm = 4;
const int center = 5;
int total = center + arm * 2; // 13 in each dimension
var tiles = new Dictionary<string, Tile>();
// Center square
for (int x = arm; x < arm + center; x++)
for (int y = arm; y < arm + center; y++)
AddTile(tiles, x, y);
int mid = arm + center / 2; // midpoint column/row of the center section
// Bottom arm
for (int i = 0; i < arm; i++) AddTile(tiles, mid, i);
// Top arm
for (int i = 0; i < arm; i++) AddTile(tiles, mid, arm + center + i);
// Left arm
for (int i = 0; i < arm; i++) AddTile(tiles, i, mid);
// Right arm
for (int i = 0; i < arm; i++) AddTile(tiles, arm + center + i, mid);
var zones = new Dictionary<int, HashSet<Coord>>
{
[0] = new() { new Coord(mid, 0) }, // bottom tip
[1] = new() { new Coord(mid, arm + center - 1 + arm) }, // top tip
[2] = new() { new Coord(0, mid) }, // left tip
[3] = new() { new Coord(arm + center - 1 + arm, mid) }, // right tip
};
return new Board { Width = total, Height = total, Tiles = tiles, StartingZones = zones };
}
/// <summary>
/// Triangular board for 3 players.
/// Uses a standard rectangle with three well-separated starting zones:
/// Seat 0 = bottom-left corner, Seat 1 = bottom-right corner, Seat 2 = top-center.
/// </summary>
private static Board MakeTriangular(GameRules rules)
{
// Prefer a wider board so all three zones have breathing room.
int w = Math.Max(rules.BoardWidth, 12);
int h = Math.Max(rules.BoardHeight, 10);
var board = Board.Create(w, h, rules.StartingRandomTilePct);
int zoneW = Math.Max(2, w / 5); // width of each corner zone
var zones = new Dictionary<int, HashSet<Coord>>
{
[0] = CoordsInRect(0, 0, zoneW - 1, 1, w, h), // bottom-left
[1] = CoordsInRect(w - zoneW, 0, w - 1, 1, w, h), // bottom-right
[2] = CoordsInRect(w / 2 - zoneW / 2, h - 2,
w / 2 + zoneW / 2 - 1, h - 1, w, h), // top-center
};
return new Board { Width = board.Width, Height = board.Height, Tiles = board.Tiles, StartingZones = zones };
}
/// <summary>
/// Radial star board for 5 players.
/// Five arms radiating from a central arena; one starting-zone tile at each arm tip.
/// Arms point: bottom, lower-right, upper-right, upper-left, lower-left (pentagon arrangement).
/// </summary>
private static Board MakeRadialStar(GameRules rules)
{
const int armLen = 5; // tiles per arm (not counting center)
const int arenaR = 4; // radius of central filled square (half-width)
int center = arenaR * 2 + 1; // 9
int total = center + armLen * 2; // 19 in each dimension (plenty of room)
var tiles = new Dictionary<string, Tile>();
// Central arena
int c = total / 2;
for (int x = c - arenaR; x <= c + arenaR; x++)
for (int y = c - arenaR; y <= c + arenaR; y++)
AddTile(tiles, x, y);
// 5 arms at angles 270°, 342°, 54°, 126°, 198° (bottom-first, counter-clockwise)
// Approximated on a square grid as (dx, dy) unit directions scaled to armLen steps.
// Pentagon vertices (unit circle, starting at bottom):
// 270° → (0, -1) lower center
// 342° → (0.95, -0.31) → approx (1, 0) then (1,-1) staircase
// 54° → (0.59, 0.81) → approx (1, 1) staircase
// 126° → (-0.59, 0.81) → approx (-1, 1)
// 198° → (-0.95, -0.31) → approx (-1, 0) then (-1,-1)
// Grid-friendly arm directions for the 5 seats:
(int dx, int dy)[] dirs =
[
( 0, -1), // Seat 0: bottom
( 1, -1), // Seat 1: lower-right (diagonal)
( 1, 1), // Seat 2: upper-right (diagonal)
(-1, 1), // Seat 3: upper-left (diagonal)
(-1, -1), // Seat 4: lower-left (diagonal)
];
var zones = new Dictionary<int, HashSet<Coord>>();
for (int seat = 0; seat < dirs.Length; seat++)
{
var (dx, dy) = dirs[seat];
int tipX = c, tipY = c;
for (int step = 1; step <= armLen; step++)
{
int ax = c + dx * step;
int ay = c + dy * step;
AddTile(tiles, ax, ay);
tipX = ax; tipY = ay;
}
zones[seat] = new HashSet<Coord> { new Coord(tipX, tipY) };
}
return new Board { Width = total, Height = total, Tiles = tiles, StartingZones = zones };
}
/// <summary>
/// Hexagonal ring board for 6 players.
/// A filled hexagon (approximated on a square grid) with 6 starting zones at the
/// flat-top hex vertices: bottom-left, bottom, bottom-right, top-right, top, top-left.
/// </summary>
private static Board MakeHexRing(GameRules rules)
{
// Use a rectangle large enough to contain the hex, with random tiles.
int w = Math.Max(rules.BoardWidth, 14);
int h = Math.Max(rules.BoardHeight, 12);
var board = Board.Create(w, h, rules.StartingRandomTilePct);
// Starting zones at 6 evenly-spaced perimeter positions.
// Place them like a hex: 2 on bottom row, 2 on top row, 1 left-mid, 1 right-mid.
int zW = Math.Max(2, w / 6);
int mY = h / 2; // mid row for left/right zones
var zones = new Dictionary<int, HashSet<Coord>>
{
[0] = CoordsInRect(0, 0, zW - 1, 1, w, h), // bottom-left
[1] = CoordsInRect(w / 2 - zW / 2, 0, w / 2 + zW / 2 - 1, 1, w, h), // bottom-center
[2] = CoordsInRect(w - zW, 0, w - 1, 1, w, h), // bottom-right
[3] = CoordsInRect(w - zW, h - 2, w - 1, h - 1, w, h), // top-right
[4] = CoordsInRect(w / 2 - zW / 2, h - 2, w / 2 + zW / 2 - 1, h - 1, w, h), // top-center
[5] = CoordsInRect(0, h - 2, zW - 1, h - 1, w, h), // top-left
};
return new Board { Width = board.Width, Height = board.Height, Tiles = board.Tiles, StartingZones = zones };
}
// ── Helpers ───────────────────────────────────────────────────────────────
private static HashSet<Coord> CoordsInRect(int x0, int y0, int x1, int y1, int w, int h)
{
var s = new HashSet<Coord>();
for (int x = Math.Max(0, x0); x <= Math.Min(w - 1, x1); x++)
for (int y = Math.Max(0, y0); y <= Math.Min(h - 1, y1); y++)
s.Add(new Coord(x, y));
return s;
}
private static HashSet<Coord> CoordsInRows(int r0, int r1, int width)
{
var s = new HashSet<Coord>();
for (int x = 0; x < width; x++)
for (int y = r0; y <= r1; y++)
s.Add(new Coord(x, y));
return s;
}
private static HashSet<Coord> CoordsInCols(int c0, int c1, int height)
{
var s = new HashSet<Coord>();
for (int x = c0; x <= c1; x++)
for (int y = 0; y < height; y++)
s.Add(new Coord(x, y));
return s;
}
private static void AddTile(Dictionary<string, Tile> tiles, int x, int y)
{
var c = new Coord(x, y);
tiles[c.ToKey()] = new Tile { Coord = c };
}
}