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:
@@ -15,34 +15,24 @@ public struct PlaceMarkCommand : ICommand
|
||||
|
||||
public void Execute(World world)
|
||||
{
|
||||
// Read-only check before iteration — never auto-marks.
|
||||
if (world.ReadSingleton<GameState>().Status != GameStatus.Playing)
|
||||
return;
|
||||
|
||||
// Find the cell entity at (Row, Col) that has no Mark.
|
||||
var query = new Query<Cell>().Without<Mark>();
|
||||
Entity? target = null;
|
||||
|
||||
// Fetch singleton ref inside iteration so mutations are auto-marked.
|
||||
using var iter = world.Select(query);
|
||||
ref var state = ref world.GetSingleton<GameState>();
|
||||
|
||||
while (iter.MoveNext())
|
||||
{
|
||||
if (iter.Item1.Row == Row && iter.Item1.Col == Col)
|
||||
{
|
||||
target = iter.Entity;
|
||||
break;
|
||||
}
|
||||
foreach(var iter in world.Select(new Query<Cell>().Without<Mark>())){
|
||||
if (iter.Val1.Row != Row || iter.Val1.Col != Col) continue;
|
||||
target = iter.Entity;
|
||||
break;
|
||||
}
|
||||
|
||||
if (target == null)
|
||||
return; // Cell already occupied or invalid position.
|
||||
return;
|
||||
|
||||
// Place the mark.
|
||||
world.AddComponent(target.Value, new Mark { Player = state.CurrentPlayer });
|
||||
|
||||
// Advance turn. Mutations auto-marked via EndIteration — no MarkModified needed.
|
||||
state.MoveCount++;
|
||||
state.CurrentPlayer = state.CurrentPlayer == Player.X ? Player.O : Player.X;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user