예제 #1
0
        private eResponseCode executeMove(Cell.Location i_Location)
        {
            eResponseCode moveResult;
            List <Cell>   cellsToFlip = getListOfCellsToFlip(i_Location);

            if (m_GameBoard.Matrix[i_Location.X, i_Location.Y].CellType == Cell.eType.Empty)
            {
                Cell.eType cellType = getCurrentCellType(m_CurrentPlayer.PlayerID); // player2 can be also the computer
                if (cellsToFlip.Count == 0)
                {
                    moveResult = eResponseCode.InvalidMove;
                }
                else
                {
                    flipCells(cellsToFlip, cellType);
                    updateScore(m_Player1);
                    updateScore(m_Player2);
                    moveResult = eResponseCode.OK;
                }
            }
            else
            {
                moveResult = eResponseCode.NotEmpty;
            }

            return(moveResult);
        }
예제 #2
0
        private Cell.Location parseLocation(int i_X, int i_Y)
        {
            Cell.Location chosenLocation = new Cell.Location();
            chosenLocation.Y = i_X;
            chosenLocation.X = i_Y;

            return(chosenLocation);
        }
예제 #3
0
        private List <Cell> getListOfCellsToFlip(Cell.Location i_Location)
        {
            List <Cell> cellsToFlip = new List <Cell>();

            Cell.eType cellType = getCurrentCellType(m_CurrentPlayer.PlayerID);
            cellsToFlip = findCellsToFlip(i_Location, cellType);

            return(cellsToFlip);
        }
예제 #4
0
        private List <Cell> findCellsToFlip(Cell.Location i_Cell, Cell.eType i_CellType)
        {
            List <Cell> cellsToFlip = new List <Cell>();

            Cell.eType otherCellType = i_CellType == Cell.eType.Player1 ? Cell.eType.Player2 : Cell.eType.Player1;
            int[,] directionArr = new int[8, 2] {
                { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, 1 }
            };

            for (int i = 0; i < directionArr.GetLength(0); i++)
            {
                int x          = i_Cell.X;
                int y          = i_Cell.Y;
                int xDirection = directionArr[i, 0];
                int yDirection = directionArr[i, 1];
                x += xDirection;
                y += yDirection;
                if (isOnMatrix(x, y) && m_GameBoard.Matrix[x, y].CellType == otherCellType)
                {
                    x += xDirection;
                    y += yDirection;
                    if (!isOnMatrix(x, y))
                    {
                        continue;
                    }

                    while (m_GameBoard.Matrix[x, y].CellType == otherCellType)
                    {
                        x += xDirection;
                        y += yDirection;
                        if (!isOnMatrix(x, y))
                        {
                            break;
                        }
                    }

                    if (!isOnMatrix(x, y))
                    {
                        continue;
                    }

                    if (m_GameBoard.Matrix[x, y].CellType == i_CellType)
                    { // there are cells to flip - go in reverse to find them
                        while (x != i_Cell.X || y != i_Cell.Y)
                        {
                            x -= xDirection;
                            y -= yDirection;
                            addToList <Cell>(ref cellsToFlip, m_GameBoard.Matrix[x, y]);
                        }
                    }
                }
            }

            return(cellsToFlip);
        }
예제 #5
0
        public eResponseCode PlayTurn(int i_X, int i_Y)
        {
            eResponseCode response = eResponseCode.OK;

            Cell.Location location = parseLocation(i_X, i_Y);
            executeMove(location);
            changeCurrentPlayer();
            response = CheckValidCellsForBothPlayers();
            if (response == eResponseCode.OK)
            {
                if (m_CurrentPlayer.PlayerID == Player.ePlayerID.Computer)
                {
                    response = makeAiMove();
                    changeCurrentPlayer();
                }
            }

            return(response);
        }