forked from kflu/2048
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from VladShyrokyi/refactoring
Refactoring
- Loading branch information
Showing
14 changed files
with
304 additions
and
501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.