Skip to content

Commit

Permalink
Убрал свойство игрового поля из IGameProcessor
Browse files Browse the repository at this point in the history
Теперь эта штука ещё больше похожа на настоящий MVC :)
Надеюсь, оно компилируется, поскольку у меня нет возможности и желания
писать код на Windows, а на линуксе WPF нет по понятным причинам, и
скомпилировать я этот проект не мог
  • Loading branch information
KriseevM committed Aug 2, 2022
1 parent 07eeed8 commit e63ec9d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
5 changes: 2 additions & 3 deletions IGameProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ public interface IGameProcessor
{
public event EventHandler GameTick;
public event System.EventHandler<GameFinishedEventArgs> GameFinished;

public int FieldSize { get; }
public bool IsCrossesTurn { get; }
public string CurrentPlayer { get; }

public TicTacToeField GameField { get; }

public double Time { get; }

public void SetCell(int x, int y);
Expand Down
9 changes: 4 additions & 5 deletions LocalGameProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
Application.Current.Dispatcher.Invoke(() => GameTick.Invoke(this, EventArgs.Empty));
}

public TicTacToeField GameField => field;

public double Time { get; set; } = 0.0;

protected bool isCrossesTurn = true;
public int FieldSize => field.Size;
public bool IsCrossesTurn => isCrossesTurn;

protected string currentPlayer = "";
Expand All @@ -53,8 +52,8 @@ public CellState GetCellState(int x, int y)

public virtual void SetCell(int x, int y)
{
if (GameField[x, y] != CellState.Empty) return;
GameField[x, y] = IsCrossesTurn ? CellState.Cross : CellState.Nought;
if (field[x, y] != CellState.Empty) return;
field[x, y] = IsCrossesTurn ? CellState.Cross : CellState.Nought;
currentPlayer = isCrossesTurn ? player2Name : player1Name;
GameTick.Invoke(this, EventArgs.Empty);
CheckIfGameFinished();
Expand All @@ -65,7 +64,7 @@ protected virtual void CheckIfGameFinished()
var winner = field.WinnerSide;
if (winner == CellState.Empty)
{
if (GameField.IsFull)
if (field.IsFull)
{
GameFinished.Invoke(this, new GameFinishedEventArgs("", CellState.Empty));
timer.Stop();
Expand Down
22 changes: 11 additions & 11 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MainWindow(IGameProcessor processor)
mainProcessor = processor;
mainProcessor.GameTick += MainProcessor_GameTick;
mainProcessor.GameFinished += MainProcessor_GameFinished;
for (int i = 0; i < mainProcessor.GameField.Size; ++i)
for (int i = 0; i < mainProcessor.FieldSize; ++i)
{
mainField.RowDefinitions.Add(new RowDefinition()
{
Expand All @@ -45,11 +45,11 @@ public MainWindow(IGameProcessor processor)
});
}

fieldRectangles = new Border[mainProcessor.GameField.Size, mainProcessor.GameField.Size];
displayedCellsStates = new CellState[mainProcessor.GameField.Size, mainProcessor.GameField.Size];
for (int i = 0; i < mainProcessor.GameField.Size; ++i)
fieldRectangles = new Border[mainProcessor.FieldSize, mainProcessor.FieldSize];
displayedCellsStates = new CellState[mainProcessor.FieldSize, mainProcessor.FieldSize];
for (int i = 0; i < mainProcessor.FieldSize; ++i)
{
for (int j = 0; j < mainProcessor.GameField.Size; ++j)
for (int j = 0; j < mainProcessor.FieldSize; ++j)
{
Border currentBorder;
fieldRectangles[i, j] = currentBorder = new Border()
Expand Down Expand Up @@ -167,16 +167,16 @@ private void MainProcessor_GameTick(object sender, EventArgs e)
timeLabel.Content = Math.Ceiling(proc.Time) + "с";
currentTurnLabel.Content = proc.CurrentPlayer;
});
for (int i = 0; i < proc.GameField.Size; ++i)
for (int i = 0; i < proc.FieldSize; ++i)
{
for (int j = 0; j < proc.GameField.Size; ++j)
for (int j = 0; j < proc.FieldSize; ++j)
{

Dispatcher.Invoke(() =>
{
if(proc.GameField[i, j] != displayedCellsStates[i, j])
if(proc.GetCellState(i, j) != displayedCellsStates[i, j])
{
displayedCellsStates[i, j] = proc.GameField[i, j];
displayedCellsStates[i, j] = proc.GetCellState(i, j);
UpdateRectangleState((Rectangle)fieldRectangles[i, j].Child, displayedCellsStates[i, j]);
}
});
Expand All @@ -186,8 +186,8 @@ private void MainProcessor_GameTick(object sender, EventArgs e)

private void WindowLoaded(object sender, RoutedEventArgs e)
{
Width = (double)Application.Current.Resources["cellWidth"] * mainProcessor.GameField.Size + 30;
Height = (double)Application.Current.Resources["cellHeight"] * mainProcessor.GameField.Size + 160;
Width = (double)Application.Current.Resources["cellWidth"] * mainProcessor.FieldSize + 30;
Height = (double)Application.Current.Resources["cellHeight"] * mainProcessor.FieldSize + 160;
mainProcessor.SetReady();
}

Expand Down

0 comments on commit e63ec9d

Please sign in to comment.