Skip to content

Commit

Permalink
Merge pull request #2 from VladShyrokyi/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
VladShyrokyi authored Jul 14, 2021
2 parents a505d43 + 2dfb730 commit 2cc40bb
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 501 deletions.
24 changes: 10 additions & 14 deletions C#/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Core_2048
{

public class Board<T> : IEnumerable<Element<T>>
public class Board<T> : IEnumerable<Cell<T>>
{
public delegate void Mapper(T value, int row, int column);

Expand All @@ -23,17 +23,18 @@ public Board(int height, int width, Func<T> valueInitiator)
public int Height { get; }
public int Width { get; }

public IEnumerator<Element<T>> GetEnumerator()
public IEnumerator<Cell<T>> GetEnumerator()
{
for (var row = 0; row < _values.GetLength(0); row++)
{
for (var column = 0; column < _values.GetLength(1); column++)
{
yield return Element<T>.Builder()
.SetRow(row)
.SetColumn(column)
.SetValue(_values[row, column])
.Build();
yield return new Cell<T>()
{
Row = row,
Column = column,
Value = _values[row, column]
};
}
}
}
Expand All @@ -55,9 +56,9 @@ public Board<T> Set(int row, int column, T value)
return this;
}

public Board<T> Set(Element<T> element)
public Board<T> Set(Cell<T> cell)
{
Set(element.Row, element.Column, element.Value);
Set(cell.Row, cell.Column, cell.Value);

return this;
}
Expand Down Expand Up @@ -119,9 +120,4 @@ public override int GetHashCode()
}
}

public class Board : Board<ulong>
{
public Board(int height, int width, ulong initValue) : base(height, width, () => initValue) { }
}

}
73 changes: 73 additions & 0 deletions C#/BoardBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;

namespace Core_2048
{

public partial class BoardBehavior<T>
{
private readonly Board<T> _board;
private readonly ICellBehavior<T> _cellBehavior;
private readonly ICellGenerator<T> _cellGenerator;

private Action<Dictionary<Cell<T>, Cell<T>>> _updated;

public BoardBehavior(Board<T> board, ICellGenerator<T> cellGenerator, ICellBehavior<T> cellBehavior,
Action<Dictionary<Cell<T>, Cell<T>>> updated = null)
{
_updated = updated;
_board = board;
_cellGenerator = cellGenerator;
_cellBehavior = cellBehavior;
}

public void AddUpdatedListener(Action<Dictionary<Cell<T>, Cell<T>>> action)
{
_updated += action;
}

public void RemoveUpdatedListener(Action<Dictionary<Cell<T>, Cell<T>>> action)
{
_updated -= action;
}

public void AddNew()
{
var element = _cellGenerator.GetNewElement(_board);
if (element == null)
{
return;
}

_board.Set(element);
}

public void Update(bool isAlongRow, bool isIncreasing)
{
var changes = CalculateChanges(isAlongRow, isIncreasing);
var updateMap = new Dictionary<Cell<T>, Cell<T>>();
foreach (var changeElementAction in changes)
{
var prev = changeElementAction.Previous;
var next = changeElementAction.Next;
_board.Set(prev.Row, prev.Column, _cellBehavior.GetCellBaseValue())
.Set(next);
updateMap.Add(prev, next);
}

_updated?.Invoke(updateMap);
}

public IEnumerable<ChangeElementAction> CalculateChanges(bool isAlongRow, bool isIncreasing)
{
return new UpdateLoop(_cellBehavior, _board, isAlongRow, isIncreasing);
}

public class ChangeElementAction
{
public Cell<T> Next;
public Cell<T> Previous;
}
}

}
2 changes: 1 addition & 1 deletion C#/BoardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Core_2048

public static class BoardHelper<T>
{
public static IEnumerable<Element<T>> GetEmpties(Board<T> board, Predicate<T> emptyChecker)
public static IEnumerable<Cell<T>> GetEmpties(Board<T> board, Predicate<T> emptyChecker)
{
return board.Where(value => emptyChecker.Invoke(value.Value));
}
Expand Down
11 changes: 11 additions & 0 deletions C#/Cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Core_2048
{

public class Cell<T>
{
public int Column;
public int Row;
public T Value;
}

}
161 changes: 0 additions & 161 deletions C#/Core.cs

This file was deleted.

62 changes: 0 additions & 62 deletions C#/Element.cs

This file was deleted.

12 changes: 12 additions & 0 deletions C#/ICellBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Core_2048
{

public interface ICellBehavior<T>
{
bool IsBaseCell(Cell<T> cell);
bool IsMergeCells(Cell<T> previous, Cell<T> next);
T MergeCells(Cell<T> previous, Cell<T> next);
T GetCellBaseValue();
}

}
10 changes: 10 additions & 0 deletions C#/ICellGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace Core_2048
{

public interface ICellGenerator<T>
{
Cell<T> GetNewElement(Board<T> board);
}

}
Loading

0 comments on commit 2cc40bb

Please sign in to comment.