-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoard.cs
66 lines (54 loc) · 1.64 KB
/
Board.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
namespace TicTacToe;
using System.Collections.Generic;
public partial class Board : Node2D
{
[Signal]
public delegate void BoardStateChangedEventHandler();
private BoardState BoardState = new();
private readonly List<Cell> _cells = new();
public override void _Ready()
{
int id = 0;
foreach (var cell in GetNode("Areas").GetChildren().Cast<Cell>())
{
cell.BoardIndex = id;
cell.InputEvent += (Node viewport, InputEvent @event, long shapeId) =>
{
if (@event.IsActionReleased("click"))
OnCellClick(cell);
};
_cells.Add(cell);
id++;
}
}
public void ResetBoard()
{
for (int i = 0; i < 9; i++)
{
BoardState[i] = CellState.FREE;
_cells[i].ChangeState(CellState.FREE);
_cells[i].SetScore(0);
_cells[i].SetHighlight(false);
}
BoardState.CurrentPlayer = BoardState.Player.CROSS;
}
private void OnCellClick(Cell cell)
{
if (BoardState.GameEnded()) return;
Play(cell.BoardIndex);
}
private void Play(int index)
{
if (BoardState[index] != CellState.FREE) return;
BoardState.Play(index);
_cells[index].ChangeState(BoardState[index]);
EmitSignal(SignalName.BoardStateChanged);
}
public void OnBestMove(int bestMove)
{
if (bestMove < 0) return;
_cells.ForEach(cell => cell.SetHighlight(cell.BoardIndex == bestMove));
}
internal void OnScoreCalculated(int index, float score) =>
_cells[index].SetScore(score);
}