Пример #1
0
        public void Start(TicTacToePlayer player1, TicTacToePlayer player2)
        {
            if (player1 == null)
            {
                throw new ArgumentNullException(nameof(player1));
            }
            if (player2 == null)
            {
                throw new ArgumentNullException(nameof(player2));
            }

            _player1 = player1;
            _player2 = player2;

            _player1.PlayerMoved += OnPlayerMoved;
            _player2.PlayerMoved += OnPlayerMoved;

            _random = new Random((int)DateTime.Now.Ticks);
            var starts1 = _random.Next() % 2 == 0;

            if (starts1)
            {
                MovePlayer(_player1);
            }
            else
            {
                MovePlayer(_player2);
            }
        }
Пример #2
0
        protected void OnPlayerMoved(TicTacToePlayer player, Tuple <int, int> cell)
        {
            player.CanMove = false;

            if (this[cell] == TicTacToeCellType.Empty)
            {
                var newCells = Cells.ToArray();
                newCells[ConvertToIndex(cell.Item1, cell.Item2)] = player.CellType;
                Cells = newCells;

                var endType = CheckWin();
                if (endType != null)
                {
                    PrintSchema();
                    GameEnded?.Invoke(this, endType.Value);
                }
                else
                {
                    if (player == _player1)
                    {
                        MovePlayer(_player2);
                    }
                    else if (player == _player2)
                    {
                        MovePlayer(_player1);
                    }
                }
            }
            else
            {
                MovePlayer(player);
            }
        }
Пример #3
0
        public void MovePlayer(TicTacToePlayer p)
        {
            if (p.NeedsOutput)
            {
                PrintSchema();
            }

            p.CanMove = true;
        }