public void NoWinOne()
        {
            CellModel[,] field =
            {
                { null, null, null },
                { null, null, null },
                { null, null, null }
            };
            bool win = m_analysis.WinnerDefiner(field, out _);

            Assert.IsFalse(win);
        }
예제 #2
0
        public async Task <string> Select(string json)
        {
            SelectOperationRequest request = JsonConvert.DeserializeObject <SelectOperationRequest>(json);

            m_logger.LogInformation($"User {Context.ConnectionId} going to select {request}");
            if (!m_checkableField.IsEmpty(request.X, request.Y))
            {
                return(new SelectOperationResponse($"Invalid values! Filed is not empty at ({request.X},{request.Y})").ToJson());
            }

            //TODO Check if user is active!
            UserModel currentUser = m_usersState[Context.ConnectionId];

            if (!currentUser.Id.Equals(m_activeUserState.Current))
            {
                return(new SelectOperationResponse($"Failed! User {currentUser.Id} active!").ToJson());
            }

            if (currentUser.Symbol != request.Symbol)
            {
                return(new SelectOperationResponse($"Invalid symbol for user {currentUser.Id}!").ToJson());
            }

            //Changing the game field
            m_settableField.Set(request.Symbol, request.X, request.Y);
            await Clients.All.OnStateChanged(new FieldChangedEvent(request.Symbol, request.X, request.Y).ToEvent());

            //Changing the active user
            UserModel otherUser = m_users.FirstOrDefault(u => !u.Id.Equals(currentUser.Id));

            if (otherUser == null)
            {
                return(new SelectOperationResponse("Failed to find other user!").ToJson());
            }

            m_activeUserState.Set(otherUser.Id);
            await Clients.All.OnStateChanged(new ActiveUserChangedEvent(m_activeUserState.Current).ToEvent());

            if (m_analysis.WinnerDefiner(m_fieldElements.Field, out IEnumerable <ValueTuple <int, int> > winPositions))
            {
                if (!winPositions.Any())
                {
                    //draw
                    m_logger.LogInformation($"The game is over. Draw");
                    await Clients.All.OnStateChanged(new GameOverEvent().ToEvent());
                }
                else
                {
                    CellModel cell = m_fieldElements.Field[winPositions.First().Item1, winPositions.First().Item2];
                    m_logger.LogInformation($"The game is over. Winner {cell.Symbol}");
                    await Clients.All.OnStateChanged(new GameOverEvent(cell.Symbol, winPositions).ToEvent());
                }
            }
            return(new SelectOperationResponse().ToJson());
        }