Skip to main content
added 16 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

What is the best approach to add Adding more responsibility to this endgame-analyzer class?

Would it violate the SRPSingle Responsibility Principle if I will add more responsibilityresponsibilities to this class to not only decide if someone has won the game, but who has won (X or O)? What is the best way to add responsibility to this?

    public interface IGameEndAnalyzer
{
    bool IsGameEnd(IBoard board);
}
public class GameEndAnalyzer : IGameEndAnalyzer
{        
    public bool IsGameEnd(IBoard board)
    {
        return IsAnyRowHasAllSameValue(board) ||
               IsAnyColumnHasAllSameValue(board) ||
               IsLeftDiagonalHasAllSameValue(board) ||
               IsRightDiagonalHasAllSameValue(board);
    }

    private bool IsAnyRowHasAllSameValue(IBoard board)
    {           
        for (int rowIndex = Board.Row.Top; rowIndex <= Board.Row.Bottom; rowIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(rowIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsAnyColumnHasAllSameValue(IBoard board)
    {
        for (int columnIndex = Board.Column.Left; columnIndex <= Board.Column.Right; columnIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(columnIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsLeftDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalLeft()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }

    private bool IsRightDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalRight()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }
}

What is the best approach to add more responsibility to this class?

Would it violate the SRP Principle if I will add more responsibility to this class to not only decide if someone has won the game, but who has won (X or O)? What is the best way to add responsibility to this?

    public interface IGameEndAnalyzer
{
    bool IsGameEnd(IBoard board);
}
public class GameEndAnalyzer : IGameEndAnalyzer
{        
    public bool IsGameEnd(IBoard board)
    {
        return IsAnyRowHasAllSameValue(board) ||
               IsAnyColumnHasAllSameValue(board) ||
               IsLeftDiagonalHasAllSameValue(board) ||
               IsRightDiagonalHasAllSameValue(board);
    }

    private bool IsAnyRowHasAllSameValue(IBoard board)
    {           
        for (int rowIndex = Board.Row.Top; rowIndex <= Board.Row.Bottom; rowIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(rowIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsAnyColumnHasAllSameValue(IBoard board)
    {
        for (int columnIndex = Board.Column.Left; columnIndex <= Board.Column.Right; columnIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(columnIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsLeftDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalLeft()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }

    private bool IsRightDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalRight()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }
}

Adding more responsibility to this endgame-analyzer class

Would it violate the Single Responsibility Principle if I will add more responsibilities to this class to not only decide if someone has won the game, but who has won (X or O)? What is the best way to add responsibility to this?

public interface IGameEndAnalyzer
{
    bool IsGameEnd(IBoard board);
}
public class GameEndAnalyzer : IGameEndAnalyzer
{        
    public bool IsGameEnd(IBoard board)
    {
        return IsAnyRowHasAllSameValue(board) ||
               IsAnyColumnHasAllSameValue(board) ||
               IsLeftDiagonalHasAllSameValue(board) ||
               IsRightDiagonalHasAllSameValue(board);
    }

    private bool IsAnyRowHasAllSameValue(IBoard board)
    {           
        for (int rowIndex = Board.Row.Top; rowIndex <= Board.Row.Bottom; rowIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(rowIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsAnyColumnHasAllSameValue(IBoard board)
    {
        for (int columnIndex = Board.Column.Left; columnIndex <= Board.Column.Right; columnIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(columnIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsLeftDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalLeft()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }

    private bool IsRightDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalRight()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }
}
added 10 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

What is the best approach to add more responsibility to this class?

I have written a ticTic-tacTac-toeToe game. I I have a class called GameEndAnalyzerGameEndAnalyzer that will check if the game ishas already endended or not. Would

Would it violate the SRP Principle if I will add more responbilityresponsibility to this class to not only decide if someone has won the game, but who ishas won too X(X or O. What)? What is the best way to add responsibility to this.?

What is the best approach to add more responsibility to this class

I have written a tic-tac-toe game. I have a class called GameEndAnalyzer that will check if the game is already end or not. Would it violate the SRP Principle if I will add more responbility to this class to not only decide if someone has won the game but who is won too X or O. What is the best way to add responsibility to this.

What is the best approach to add more responsibility to this class?

I have written a Tic-Tac-Toe game. I have a class called GameEndAnalyzer that will check if the game has already ended or not.

Would it violate the SRP Principle if I will add more responsibility to this class to not only decide if someone has won the game, but who has won (X or O)? What is the best way to add responsibility to this?

Source Link

What is the best approach to add more responsibility to this class

I have written a tic-tac-toe game. I have a class called GameEndAnalyzer that will check if the game is already end or not. Would it violate the SRP Principle if I will add more responbility to this class to not only decide if someone has won the game but who is won too X or O. What is the best way to add responsibility to this.

    public interface IGameEndAnalyzer
{
    bool IsGameEnd(IBoard board);
}
public class GameEndAnalyzer : IGameEndAnalyzer
{        
    public bool IsGameEnd(IBoard board)
    {
        return IsAnyRowHasAllSameValue(board) ||
               IsAnyColumnHasAllSameValue(board) ||
               IsLeftDiagonalHasAllSameValue(board) ||
               IsRightDiagonalHasAllSameValue(board);
    }

    private bool IsAnyRowHasAllSameValue(IBoard board)
    {           
        for (int rowIndex = Board.Row.Top; rowIndex <= Board.Row.Bottom; rowIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(rowIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsAnyColumnHasAllSameValue(IBoard board)
    {
        for (int columnIndex = Board.Column.Left; columnIndex <= Board.Column.Right; columnIndex++)
        {
            var isSameValue = board
                       .Cells
                       .WhereInRow(columnIndex)
                       .AllCellIs_O_Or_X();

            if (isSameValue)
            {
                return true;
            }
        }

        return false;
    }

    private bool IsLeftDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalLeft()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }

    private bool IsRightDiagonalHasAllSameValue(IBoard board)
    {
        var isSameValue = board
                          .Cells
                          .WhereInDiagonalRight()
                          .AllCellIs_O_Or_X();

        return isSameValue;
    }
}