This commit is contained in:
parent
709ba026fa
commit
bfcd8f8761
|
|
@ -30,7 +30,7 @@ namespace ProjectGrid.Controllers
|
||||||
public TicTacTocResponse GetBoard()
|
public TicTacTocResponse GetBoard()
|
||||||
{
|
{
|
||||||
_logger.LogTrace("GetBoard called.");
|
_logger.LogTrace("GetBoard called.");
|
||||||
return _manager.GetResponse();
|
return _manager.GetBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
|
|
@ -47,8 +47,7 @@ namespace ProjectGrid.Controllers
|
||||||
public TicTacTocResponse PostMove(TicTacTocRequest move)
|
public TicTacTocResponse PostMove(TicTacTocRequest move)
|
||||||
{
|
{
|
||||||
_logger.LogTrace($"PostMove called. {move.DebugString()}");
|
_logger.LogTrace($"PostMove called. {move.DebugString()}");
|
||||||
_manager.NextMove(move);
|
return _manager.NextMove(move);
|
||||||
return _manager.GetResponse(move.Player);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,11 +72,12 @@ namespace ProjectGrid.Models
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ValueWon(int value)
|
public List<int> ValueWon(int value)
|
||||||
{
|
{
|
||||||
Debug.Assert(value > 0, "ValueWon called for Empty field 0");
|
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;
|
bool won = true;
|
||||||
for (int y = 0; y < _winning.GetLength(1); y++)
|
for (int y = 0; y < _winning.GetLength(1); y++)
|
||||||
|
|
@ -89,10 +90,12 @@ namespace ProjectGrid.Models
|
||||||
}
|
}
|
||||||
|
|
||||||
if (won)
|
if (won)
|
||||||
return true;
|
{
|
||||||
|
return new List<int>() { _winning[x, 0], _winning[x, 1], _winning[x, 2] };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,12 +5,22 @@ namespace ProjectGrid
|
||||||
{
|
{
|
||||||
public interface ITicTacTocManager
|
public interface ITicTacTocManager
|
||||||
{
|
{
|
||||||
public bool NextMove(TicTacTocRequest move);
|
public void Restart();
|
||||||
public TicTacTocResponse GetResponse(int player = 0);
|
public TicTacTocResponse NextMove(TicTacTocRequest move);
|
||||||
|
|
||||||
|
public TicTacTocResponse GetBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TicTacTocManager : ITicTacTocManager
|
public class TicTacTocManager : ITicTacTocManager
|
||||||
{
|
{
|
||||||
|
private enum State
|
||||||
|
{
|
||||||
|
PLAYING,
|
||||||
|
GAME_OVER
|
||||||
|
}
|
||||||
|
|
||||||
|
private State _state;
|
||||||
|
|
||||||
//private ITicTacToctRepository _repo;
|
//private ITicTacToctRepository _repo;
|
||||||
|
|
||||||
private int _currentPlayer;
|
private int _currentPlayer;
|
||||||
|
|
@ -25,21 +35,23 @@ namespace ProjectGrid
|
||||||
Restart();
|
Restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool NextMove(TicTacTocRequest move)
|
public TicTacTocResponse 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)
|
|
||||||
{
|
{
|
||||||
TicTacTocResponse response = new TicTacTocResponse();
|
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;
|
response.Board = _board.Field;
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
|
@ -49,6 +61,14 @@ namespace ProjectGrid
|
||||||
{
|
{
|
||||||
_currentPlayer = new Random().Next(1, 2);
|
_currentPlayer = new Random().Next(1, 2);
|
||||||
_board = new TicTacTocBoard();
|
_board = new TicTacTocBoard();
|
||||||
|
_state = State.PLAYING;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TicTacTocResponse GetBoard()
|
||||||
|
{
|
||||||
|
TicTacTocResponse response = new TicTacTocResponse();
|
||||||
|
response.Board = _board.Field;
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ namespace ProjectGrid.Models
|
||||||
public class TicTacTocResponse
|
public class TicTacTocResponse
|
||||||
{
|
{
|
||||||
public List<int> Board { get; set; }
|
public List<int> Board { get; set; }
|
||||||
|
public List<int> Winning { get; set; }
|
||||||
public int NextPlayer { get; set; }
|
public int NextPlayer { get; set; }
|
||||||
public int PlayerWon { get; set; }
|
public int PlayerWon { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue