shows current player
This commit is contained in:
parent
7b345ba39c
commit
271dbf1f1f
@ -19,7 +19,7 @@ public partial class Cell : UserControl
|
|||||||
if(Text.Content != null)
|
if(Text.Content != null)
|
||||||
return;
|
return;
|
||||||
Text.Content = Game.CurrentPlayerSign;
|
Text.Content = Game.CurrentPlayerSign;
|
||||||
Game.Turn(GetValue(Grid.RowProperty), GetValue(Grid.ColumnProperty));
|
Game.DoTurn(GetValue(Grid.RowProperty), GetValue(Grid.ColumnProperty));
|
||||||
IsEnabled = false;
|
IsEnabled = false;
|
||||||
Foreground = Brushes.White;
|
Foreground = Brushes.White;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ public class Game
|
|||||||
public char CurrentPlayerSign => _players[_nextPlayerIndex];
|
public char CurrentPlayerSign => _players[_nextPlayerIndex];
|
||||||
|
|
||||||
public event Action<char>? PlayerWon;
|
public event Action<char>? PlayerWon;
|
||||||
|
public event Action<char>? PlayerChanged;
|
||||||
|
|
||||||
private char[][] _gameField;
|
private char[][] _gameField;
|
||||||
private char[] _players = { 'X', '0' };
|
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')
|
if (_gameField[cell_row][cell_col] != '\0')
|
||||||
throw new Exception("the cell was already used");
|
throw new Exception("the cell was already used");
|
||||||
@ -49,6 +50,7 @@ public class Game
|
|||||||
_nextPlayerIndex++;
|
_nextPlayerIndex++;
|
||||||
if (_nextPlayerIndex >= _players.Length)
|
if (_nextPlayerIndex >= _players.Length)
|
||||||
_nextPlayerIndex = 0;
|
_nextPlayerIndex = 0;
|
||||||
|
PlayerChanged?.Invoke(CurrentPlayerSign);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool CheckWinCombination(int cell_row, int cell_col)
|
private bool CheckWinCombination(int cell_row, int cell_col)
|
||||||
|
|||||||
@ -14,8 +14,10 @@
|
|||||||
<Grid.RowDefinitions>40 *</Grid.RowDefinitions>
|
<Grid.RowDefinitions>40 *</Grid.RowDefinitions>
|
||||||
<StackPanel Orientation="Horizontal" Spacing="20">
|
<StackPanel Orientation="Horizontal" Spacing="20">
|
||||||
<Label Target="SizeTextBox" VerticalContentAlignment="Center">Size:</Label>
|
<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>
|
<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>
|
</StackPanel>
|
||||||
<Grid Grid.Row="1" Name="GameGrid"></Grid>
|
<Grid Grid.Row="1" Name="GameGrid"></Grid>
|
||||||
<StackPanel Grid.Row="1" Name="WinPanel" IsEnabled="False"
|
<StackPanel Grid.Row="1" Name="WinPanel" IsEnabled="False"
|
||||||
|
|||||||
@ -11,15 +11,15 @@ public partial class MainWindow : Window
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
DrawGame();
|
RestartButton_OnClick(null, null);
|
||||||
_game.PlayerWon += ShowWin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RestartButton_OnClick(object? sender, RoutedEventArgs e)
|
private void RestartButton_OnClick(object? sender, RoutedEventArgs? e)
|
||||||
{
|
{
|
||||||
int size = Convert.ToInt32(SizeTextBox.Text);
|
int size = Convert.ToInt32(SizeTextBox.Text);
|
||||||
_game = new Game(size, size);
|
_game = new Game(size, size);
|
||||||
_game.PlayerWon += ShowWin;
|
_game.PlayerWon += ShowWin;
|
||||||
|
_game.PlayerChanged += playerChar => CurrentPlayerText.Text = playerChar.ToString();
|
||||||
DrawGame();
|
DrawGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,10 +45,7 @@ public partial class MainWindow : Window
|
|||||||
for(int y=0; y<size; y++)
|
for(int y=0; y<size; y++)
|
||||||
for (int x = 0; x < size; x++)
|
for (int x = 0; x < size; x++)
|
||||||
{
|
{
|
||||||
var cellView = new Cell
|
var cellView = new Cell { Game = _game };
|
||||||
{
|
|
||||||
Game = _game
|
|
||||||
};
|
|
||||||
cellView.SetValue(Grid.ColumnProperty, x);
|
cellView.SetValue(Grid.ColumnProperty, x);
|
||||||
cellView.SetValue(Grid.RowProperty, y);
|
cellView.SetValue(Grid.RowProperty, y);
|
||||||
char cellValue = _game.GameField[y][x];
|
char cellValue = _game.GameField[y][x];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user