From 271dbf1f1fe2eaa9d0596c5a5fa360df077d0435 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 1 Dec 2023 14:55:42 +0600 Subject: [PATCH] shows current player --- TicTacToe/Cell.axaml.cs | 2 +- TicTacToe/Game.cs | 4 +++- TicTacToe/MainWindow.axaml | 4 +++- TicTacToe/MainWindow.axaml.cs | 11 ++++------- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/TicTacToe/Cell.axaml.cs b/TicTacToe/Cell.axaml.cs index 6429fc5..4dbbedd 100644 --- a/TicTacToe/Cell.axaml.cs +++ b/TicTacToe/Cell.axaml.cs @@ -19,7 +19,7 @@ public partial class Cell : UserControl if(Text.Content != null) return; Text.Content = Game.CurrentPlayerSign; - Game.Turn(GetValue(Grid.RowProperty), GetValue(Grid.ColumnProperty)); + Game.DoTurn(GetValue(Grid.RowProperty), GetValue(Grid.ColumnProperty)); IsEnabled = false; Foreground = Brushes.White; } diff --git a/TicTacToe/Game.cs b/TicTacToe/Game.cs index 23c4c6a..aa865d7 100644 --- a/TicTacToe/Game.cs +++ b/TicTacToe/Game.cs @@ -15,6 +15,7 @@ public class Game public char CurrentPlayerSign => _players[_nextPlayerIndex]; public event Action? PlayerWon; + public event Action? PlayerChanged; private char[][] _gameField; private char[] _players = { 'X', '0' }; @@ -35,7 +36,7 @@ public class Game } } - public void Turn(int cell_row, int cell_col) + public void DoTurn(int cell_row, int cell_col) { if (_gameField[cell_row][cell_col] != '\0') throw new Exception("the cell was already used"); @@ -49,6 +50,7 @@ public class Game _nextPlayerIndex++; if (_nextPlayerIndex >= _players.Length) _nextPlayerIndex = 0; + PlayerChanged?.Invoke(CurrentPlayerSign); } private bool CheckWinCombination(int cell_row, int cell_col) diff --git a/TicTacToe/MainWindow.axaml b/TicTacToe/MainWindow.axaml index e7bbb76..c2fd7f3 100644 --- a/TicTacToe/MainWindow.axaml +++ b/TicTacToe/MainWindow.axaml @@ -14,8 +14,10 @@ 40 * - + 3 + + X CurrentPlayerText.Text = playerChar.ToString(); DrawGame(); } @@ -45,10 +45,7 @@ public partial class MainWindow : Window for(int y=0; y