shows current player

This commit is contained in:
timerix 2023-12-01 14:55:42 +06:00
parent 7b345ba39c
commit 271dbf1f1f
4 changed files with 11 additions and 10 deletions

View File

@ -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;
}

View File

@ -15,6 +15,7 @@ public class Game
public char CurrentPlayerSign => _players[_nextPlayerIndex];
public event Action<char>? PlayerWon;
public event Action<char>? 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)

View File

@ -14,8 +14,10 @@
<Grid.RowDefinitions>40 *</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Spacing="20">
<Label Target="SizeTextBox" VerticalContentAlignment="Center">Size:</Label>
<TextBox Name="SizeTextBox" Width="60"></TextBox>
<TextBox Name="SizeTextBox" Width="60">3</TextBox>
<Button Click="RestartButton_OnClick">Restart</Button>
<Label Target="CurrentPlayerText" Margin="100 0 0 0">Current player:</Label>
<Border BorderThickness="1"><TextBlock Name="CurrentPlayerText" VerticalAlignment="Center">X</TextBlock></Border>
</StackPanel>
<Grid Grid.Row="1" Name="GameGrid"></Grid>
<StackPanel Grid.Row="1" Name="WinPanel" IsEnabled="False"

View File

@ -11,15 +11,15 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
DrawGame();
_game.PlayerWon += ShowWin;
RestartButton_OnClick(null, null);
}
private void RestartButton_OnClick(object? sender, RoutedEventArgs e)
private void RestartButton_OnClick(object? sender, RoutedEventArgs? e)
{
int size = Convert.ToInt32(SizeTextBox.Text);
_game = new Game(size, size);
_game.PlayerWon += ShowWin;
_game.PlayerChanged += playerChar => CurrentPlayerText.Text = playerChar.ToString();
DrawGame();
}
@ -45,10 +45,7 @@ public partial class MainWindow : Window
for(int y=0; y<size; y++)
for (int x = 0; x < size; x++)
{
var cellView = new Cell
{
Game = _game
};
var cellView = new Cell { Game = _game };
cellView.SetValue(Grid.ColumnProperty, x);
cellView.SetValue(Grid.RowProperty, y);
char cellValue = _game.GameField[y][x];