68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
|
|
using OECS;
|
|||
|
|
|
|||
|
|
namespace TicTacToe;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Prints the board to the console.
|
|||
|
|
/// </summary>
|
|||
|
|
public class RenderSystem : ISystem
|
|||
|
|
{
|
|||
|
|
public QueryDescriptor Query { get; }
|
|||
|
|
|
|||
|
|
public RenderSystem(World world)
|
|||
|
|
{
|
|||
|
|
Query = world.Query().With<Cell>().Build();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Run(World world)
|
|||
|
|
{
|
|||
|
|
ref var state = ref world.GetSingleton<GameState>();
|
|||
|
|
|
|||
|
|
// Build a 3×3 grid.
|
|||
|
|
var grid = new char[3, 3];
|
|||
|
|
for (int r = 0; r < 3; r++)
|
|||
|
|
for (int c = 0; c < 3; c++)
|
|||
|
|
grid[r, c] = '.';
|
|||
|
|
|
|||
|
|
// Fill in placed marks.
|
|||
|
|
var markQuery = world.Query().With<Cell>().With<Mark>().Build();
|
|||
|
|
world.ForEach(markQuery, (Entity entity, ref Cell cell, ref Mark mark) =>
|
|||
|
|
{
|
|||
|
|
grid[cell.Row, cell.Col] = mark.Player == Player.X ? 'X' : 'O';
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
Console.WriteLine();
|
|||
|
|
Console.WriteLine(" Tic-Tac-Toe");
|
|||
|
|
Console.WriteLine(" ═══════════");
|
|||
|
|
Console.WriteLine();
|
|||
|
|
Console.WriteLine(" 0 1 2");
|
|||
|
|
Console.WriteLine(" ┌───┬───┬───┐");
|
|||
|
|
for (int r = 0; r < 3; r++)
|
|||
|
|
{
|
|||
|
|
Console.Write($"{r} │");
|
|||
|
|
for (int c = 0; c < 3; c++)
|
|||
|
|
{
|
|||
|
|
Console.Write($" {grid[r, c]} ");
|
|||
|
|
if (c < 2) Console.Write("│");
|
|||
|
|
}
|
|||
|
|
Console.WriteLine("│");
|
|||
|
|
if (r < 2) Console.WriteLine(" ├───┼───┼───┤");
|
|||
|
|
}
|
|||
|
|
Console.WriteLine(" └───┴───┴───┘");
|
|||
|
|
Console.WriteLine();
|
|||
|
|
|
|||
|
|
// Status line.
|
|||
|
|
var statusText = state.Status switch
|
|||
|
|
{
|
|||
|
|
GameStatus.Playing => $"{(state.CurrentPlayer == Player.X ? 'X' : 'O')}'s turn (move {state.MoveCount + 1})",
|
|||
|
|
GameStatus.XWon => "X wins!",
|
|||
|
|
GameStatus.OWon => "O wins!",
|
|||
|
|
GameStatus.Draw => "It's a draw!",
|
|||
|
|
_ => ""
|
|||
|
|
};
|
|||
|
|
Console.WriteLine($" {statusText}");
|
|||
|
|
Console.WriteLine();
|
|||
|
|
Console.Write(" Enter row col (e.g. \"1 2\"): ");
|
|||
|
|
}
|
|||
|
|
}
|