diff --git a/Controllers/TicTacTocController.cs b/Controllers/TicTacTocController.cs index 8d88e39..a2cd062 100644 --- a/Controllers/TicTacTocController.cs +++ b/Controllers/TicTacTocController.cs @@ -30,7 +30,7 @@ namespace ProjectGrid.Controllers public TicTacTocResponse GetBoard() { _logger.LogTrace("GetBoard called."); - return _manager.GetResponse(); + return _manager.GetBoard(); } [HttpGet] @@ -47,8 +47,7 @@ namespace ProjectGrid.Controllers public TicTacTocResponse PostMove(TicTacTocRequest move) { _logger.LogTrace($"PostMove called. {move.DebugString()}"); - _manager.NextMove(move); - return _manager.GetResponse(move.Player); + return _manager.NextMove(move); } } diff --git a/Logic/TicTacTocBoard.cs b/Logic/TicTacTocBoard.cs index 7285291..2031dd5 100644 --- a/Logic/TicTacTocBoard.cs +++ b/Logic/TicTacTocBoard.cs @@ -72,11 +72,12 @@ namespace ProjectGrid.Models return false; } - public bool ValueWon(int value) + public List ValueWon(int value) { Debug.Assert(value > 0, "ValueWon called for Empty field 0"); - for (int x = 0; x < _winning.GetLength(0); x++) + int x = 0; + for (; x < _winning.GetLength(0); x++) { bool won = true; for (int y = 0; y < _winning.GetLength(1); y++) @@ -89,10 +90,12 @@ namespace ProjectGrid.Models } if (won) - return true; + { + return new List() { _winning[x, 0], _winning[x, 1], _winning[x, 2] }; + } } - return false; + return null; } } } \ No newline at end of file diff --git a/Logic/TicTacTocManager.cs b/Logic/TicTacTocManager.cs index 2fea6b0..1c84386 100644 --- a/Logic/TicTacTocManager.cs +++ b/Logic/TicTacTocManager.cs @@ -5,12 +5,22 @@ namespace ProjectGrid { public interface ITicTacTocManager { - public bool NextMove(TicTacTocRequest move); - public TicTacTocResponse GetResponse(int player = 0); + public void Restart(); + public TicTacTocResponse NextMove(TicTacTocRequest move); + + public TicTacTocResponse GetBoard(); } public class TicTacTocManager : ITicTacTocManager { + private enum State + { + PLAYING, + GAME_OVER + } + + private State _state; + //private ITicTacToctRepository _repo; private int _currentPlayer; @@ -25,21 +35,23 @@ namespace ProjectGrid Restart(); } - public bool NextMove(TicTacTocRequest move) - { - var result = _board.SetFieldValue(_currentPlayer, move.Field); //move.PosX, move.PosY); - // switch between 1 and 2 - _currentPlayer = 3 - _currentPlayer; - - return result; - } - - public TicTacTocResponse GetResponse(int player = 0) + public TicTacTocResponse NextMove(TicTacTocRequest move) { TicTacTocResponse response = new TicTacTocResponse(); - if (player > 0 && _board.ValueWon(player)) - response.PlayerWon = player; + if (_state != State.GAME_OVER) + { + _board.SetFieldValue(_currentPlayer, move.Field); + // switch between 1 and 2 + _currentPlayer = 3 - _currentPlayer; + } + + response.Winning = _board.ValueWon(_currentPlayer); + if (_currentPlayer > 0 && response.Winning != null) + { + response.PlayerWon = _currentPlayer; + _state = State.GAME_OVER; + } response.Board = _board.Field; return response; @@ -49,6 +61,14 @@ namespace ProjectGrid { _currentPlayer = new Random().Next(1, 2); _board = new TicTacTocBoard(); + _state = State.PLAYING; + } + + public TicTacTocResponse GetBoard() + { + TicTacTocResponse response = new TicTacTocResponse(); + response.Board = _board.Field; + return response; } } } \ No newline at end of file diff --git a/Models/TicTacTocResponse.cs b/Models/TicTacTocResponse.cs index f975e36..a2a2536 100644 --- a/Models/TicTacTocResponse.cs +++ b/Models/TicTacTocResponse.cs @@ -5,6 +5,7 @@ namespace ProjectGrid.Models public class TicTacTocResponse { public List Board { get; set; } + public List Winning { get; set; } public int NextPlayer { get; set; } public int PlayerWon { get; set; } }