예제 #1
0
        public static Tuple <bool, char> ComputeGameResult(char[,] board, char emptyChar)
        {
            var boardLines = BoardUtils.GetAllBoardLinesCoordinates(board.GetLength(0));

            foreach (var line in boardLines)
            {
                char c = board[line.First().Item1, line.First().Item2];
                if (c == emptyChar)
                {
                    continue;
                }

                bool win = true;
                foreach (var coord in line)
                {
                    if (c != board[coord.Item1, coord.Item2])
                    {
                        win = false;
                        break;
                    }
                }

                if (win)
                {
                    return(new Tuple <bool, char>(true, c));
                }
            }

            if (BoardUtils.IsDraw(board, emptyChar))
            {
                return(new Tuple <bool, char>(true, emptyChar));
            }

            return(new Tuple <bool, char>(false, emptyChar));
        }
예제 #2
0
        protected Tuple <int, int> findWinningMove(char[,] board, char symbol)
        {
            List <List <Tuple <int, int> > > boardLines = BoardUtils.GetAllBoardLinesCoordinates(boardSize);

            foreach (var line in boardLines)
            {
                int currentLineScore       = 0;
                int movesToWin             = 0;
                Tuple <int, int> winCoords = null;

                foreach (var coord in line)
                {
                    if (board[coord.Item1, coord.Item2] == symbol)
                    {
                        currentLineScore++;
                    }
                    else if (board[coord.Item1, coord.Item2] == Engine.EMPTY_CHAR)
                    {
                        movesToWin++;
                        winCoords = coord;
                    }
                }

                if (currentLineScore == boardSize - 1 && movesToWin == 1)
                {
                    return(winCoords);
                }
            }

            return(null);
        }