refactor: update iterator access and simplify loops

Refactor code to use `Val1` and `Val2` instead of `Item1` and `Item2`
for
iterator access. Simplify `PlaceMarkCommand` and `WinCheckSystem` by
replacing `while` loops with `foreach` and removing redundant comments.
This commit is contained in:
2026-07-21 11:09:55 +08:00
parent 52e1e8fdc9
commit 4cdfe9c957
5 changed files with 20 additions and 38 deletions
+4 -12
View File
@@ -8,24 +8,18 @@ namespace Game.TicTacToe;
/// </summary>
public class WinCheckSystem : ISystem
{
public void Run(World world)
public void RunImpl(World world)
{
// Check game status before doing any work. Use ReadSingleton
// to avoid auto-marking GameState as modified when we bail early.
if (world.ReadSingleton<GameState>().Status != GameStatus.Playing)
return;
// Get singleton ref inside iteration so all mutations are auto-marked
// by EndIteration — no MarkModified calls needed.
using var iter = world.Select<Cell, Mark>();
ref var state = ref world.GetSingleton<GameState>();
// Build a 3×3 grid of marks.
var grid = new Player[3, 3];
while (iter.MoveNext())
foreach (var iter in world.Select<Cell, Mark>())
{
grid[iter.Item1.Row, iter.Item1.Col] = iter.Item2.Player;
grid[iter.Val1.Row, iter.Val1.Col] = iter.Val2.Player;
}
// Check rows.
@@ -62,9 +56,7 @@ public class WinCheckSystem : ISystem
// Check draw.
if (state.MoveCount >= 9)
{
state.Status = GameStatus.Draw;
}
}
private static bool TryGetWinner(Player a, Player b, Player c, out Player winner)
@@ -82,4 +74,4 @@ public class WinCheckSystem : ISystem
{
state.Status = winner == Player.X ? GameStatus.XWon : GameStatus.OWon;
}
}
}