ProjectGrid/Logic/TicTacTocManager.cs

82 lines
1.8 KiB
C#

using ProjectGrid.Models;
using System;
using System.Collections.Generic;
namespace ProjectGrid
{
public interface ITicTacTocManager
{
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;
private TicTacTocBoard _board;
public TicTacTocBoard Board => _board;
public int[] _playerScore = new int[] { 0, 0, 0 };
public TicTacTocManager(/*ITicTacToctRepository repo*/)
{
//_repo = repo;
_currentPlayer = 1;
Restart();
}
public TicTacTocResponse NextMove(TicTacTocRequest move)
{
TicTacTocResponse response = new TicTacTocResponse();
if (_state != State.GAME_OVER)
{
_board.SetFieldValue(_currentPlayer, move.Field);
response.Winning = _board.ValueWon(_currentPlayer);
if (_currentPlayer > 0 && response.Winning != null)
{
response.PlayerWon = _currentPlayer;
_state = State.GAME_OVER;
_playerScore[_currentPlayer]++;
}
// switch between 1 and 2
_currentPlayer = 3 - _currentPlayer;
}
response.Board = _board.Field;
response.PlayerScore = new List<int>(_playerScore);
return response;
}
public void Restart()
{
_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;
}
}
}