示例#1
0
        public GridUpdateEvent GetResetGameResults()
        {
            var clearResults = new List <GridUpdate>();

            for (int i = 0; i < _gridSize; i++)
            {
                for (int j = 0; j < _gridSize; j++)
                {
                    var playerGridUpdate = new GridUpdate
                    {
                        ColumnIndexToUpdate = i,
                        RowIndexToUpdate    = j,
                        IsPlayerGrid        = true,
                        PanelType           = PanelType.Unknown
                    };

                    var computerGridUpdate = new GridUpdate
                    {
                        ColumnIndexToUpdate = i,
                        RowIndexToUpdate    = j,
                        IsPlayerGrid        = true,
                        PanelType           = PanelType.Unknown
                    };

                    clearResults.Add(playerGridUpdate);
                    clearResults.Add(computerGridUpdate);
                }
            }

            _setupMoveCount = 0;

            return(new GridUpdateEvent(clearResults, "Game reset."));
        }
示例#2
0
        public GridUpdateEvent GetMoveResults(bool isPlayerGrid, int columnIndex, int rowIndex)
        {
            PanelType panelResult;

            // TODO: move this logic to separate objects, it's a bit hacky as guard clauses like this
            if (_setupMoveCount < PatrolBoatsPlaced && isPlayerGrid)
            {
                panelResult = PanelType.PatrolBoat;
            }
            else if (_setupMoveCount < CruiserPlaced && isPlayerGrid)
            {
                panelResult = PanelType.Cruiser;
            }
            else if (_setupMoveCount < SubmarinePlaced && isPlayerGrid)
            {
                panelResult = PanelType.Submarine;
            }
            else if (_setupMoveCount < BattleShipPlaced && isPlayerGrid)
            {
                panelResult = PanelType.Battleship;
            }
            else if (_setupMoveCount < AircraftCarrierPlaced && isPlayerGrid)
            {
                panelResult = PanelType.AircraftCarrier;
            }
            else
            {
                return GetAttackMoveResults(isPlayerGrid, columnIndex, rowIndex);
            }

            var updateList = new List<GridUpdate>();
            string statusMessage = String.Empty;

            if (_preceededByStartMove && IsMoveValid(columnIndex, rowIndex, _startMoveColumnIndex, _startMoveRowIndex, panelResult))
            {
                var startingSpaceGridUpdate = new GridUpdate
                {
                    ColumnIndexToUpdate = _startMoveColumnIndex,
                    RowIndexToUpdate = _startMoveRowIndex,
                    IsPlayerGrid = true,
                    PanelType = panelResult
                };

                var endingSpaceGridUpdate = new GridUpdate
                {
                    ColumnIndexToUpdate = columnIndex,
                    RowIndexToUpdate = rowIndex,
                    IsPlayerGrid = true,
                    PanelType = panelResult
                };

                updateList.Add(startingSpaceGridUpdate);

                var horizontalGap = columnIndex - _startMoveColumnIndex;
                var verticalGap = rowIndex - _startMoveRowIndex;

                var sizeOfMove = horizontalGap + verticalGap;

                bool sameRow = (Math.Abs(horizontalGap) > 0);

                for (var i = 1; i < Math.Abs(sizeOfMove); i++)
                {
                    var directionToMoveIn = (sizeOfMove / Math.Abs(sizeOfMove)) * i;

                    var inBetweenGridUpdates = new GridUpdate
                    {
                        IsPlayerGrid = true,
                        PanelType = panelResult,
                        ColumnIndexToUpdate = sameRow ? _startMoveColumnIndex + directionToMoveIn : _startMoveColumnIndex,
                        RowIndexToUpdate = sameRow ? _startMoveRowIndex : _startMoveRowIndex + directionToMoveIn
                    };

                    updateList.Add(inBetweenGridUpdates);
                }

                updateList.Add(endingSpaceGridUpdate);

                statusMessage = String.Empty;
            }
            else
            {
                return GetFirstPlacingMoveResult(columnIndex, rowIndex, panelResult);
            }

            _setupMoveCount += updateList.Count;
            _preceededByStartMove = false;

            return new GridUpdateEvent(updateList, statusMessage);
        }
示例#3
0
        public GridUpdateEvent GetResetGameResults()
        {
            var clearResults = new List<GridUpdate>();

            for (int i = 0; i < _gridSize; i++)
            {
                for (int j = 0; j < _gridSize; j++)
                {
                    var playerGridUpdate = new GridUpdate
                    {
                        ColumnIndexToUpdate = i,
                        RowIndexToUpdate = j,
                        IsPlayerGrid = true,
                        PanelType = PanelType.Unknown
                    };

                    var computerGridUpdate = new GridUpdate
                    {
                        ColumnIndexToUpdate = i,
                        RowIndexToUpdate = j,
                        IsPlayerGrid = true,
                        PanelType = PanelType.Unknown
                    };

                    clearResults.Add(playerGridUpdate);
                    clearResults.Add(computerGridUpdate);
                }
            }

            _setupMoveCount = 0;

            return new GridUpdateEvent(clearResults, "Game reset.");
        }
示例#4
0
        private void UpdateGrids(GridUpdate result)
        {
            var gridToUpdate = result.IsPlayerGrid
                ? dgPlayerShips
                : dgComputerShips;

            try
            {
                var cellToUpdate = gridToUpdate.Rows[result.RowIndexToUpdate].Cells[result.ColumnIndexToUpdate];
                cellToUpdate.Value = ConvertToDisplayValue(result.PanelType);
            }
            catch (Exception)
            {
                throw new ApplicationException
                    ("The game controller indicated that a certain grid space should be updated, "
                    + "but this space does not exist on the game board.");
            }
        }
示例#5
0
        public GridUpdateEvent GetMoveResults(bool isPlayerGrid, int columnIndex, int rowIndex)
        {
            PanelType panelResult;

            // TODO: move this logic to separate objects, it's a bit hacky as guard clauses like this
            if (_setupMoveCount < PatrolBoatsPlaced && isPlayerGrid)
            {
                panelResult = PanelType.PatrolBoat;
            }
            else if (_setupMoveCount < CruiserPlaced && isPlayerGrid)
            {
                panelResult = PanelType.Cruiser;
            }
            else if (_setupMoveCount < SubmarinePlaced && isPlayerGrid)
            {
                panelResult = PanelType.Submarine;
            }
            else if (_setupMoveCount < BattleShipPlaced && isPlayerGrid)
            {
                panelResult = PanelType.Battleship;
            }
            else if (_setupMoveCount < AircraftCarrierPlaced && isPlayerGrid)
            {
                panelResult = PanelType.AircraftCarrier;
            }
            else
            {
                return(GetAttackMoveResults(isPlayerGrid, columnIndex, rowIndex));
            }

            var    updateList    = new List <GridUpdate>();
            string statusMessage = String.Empty;

            if (_preceededByStartMove && IsMoveValid(columnIndex, rowIndex, _startMoveColumnIndex, _startMoveRowIndex, panelResult))
            {
                var startingSpaceGridUpdate = new GridUpdate
                {
                    ColumnIndexToUpdate = _startMoveColumnIndex,
                    RowIndexToUpdate    = _startMoveRowIndex,
                    IsPlayerGrid        = true,
                    PanelType           = panelResult
                };

                var endingSpaceGridUpdate = new GridUpdate
                {
                    ColumnIndexToUpdate = columnIndex,
                    RowIndexToUpdate    = rowIndex,
                    IsPlayerGrid        = true,
                    PanelType           = panelResult
                };

                updateList.Add(startingSpaceGridUpdate);

                var horizontalGap = columnIndex - _startMoveColumnIndex;
                var verticalGap   = rowIndex - _startMoveRowIndex;

                var sizeOfMove = horizontalGap + verticalGap;

                bool sameRow = (Math.Abs(horizontalGap) > 0);

                for (var i = 1; i < Math.Abs(sizeOfMove); i++)
                {
                    var directionToMoveIn = (sizeOfMove / Math.Abs(sizeOfMove)) * i;

                    var inBetweenGridUpdates = new GridUpdate
                    {
                        IsPlayerGrid        = true,
                        PanelType           = panelResult,
                        ColumnIndexToUpdate = sameRow ? _startMoveColumnIndex + directionToMoveIn : _startMoveColumnIndex,
                        RowIndexToUpdate    = sameRow ? _startMoveRowIndex : _startMoveRowIndex + directionToMoveIn
                    };

                    updateList.Add(inBetweenGridUpdates);
                }

                updateList.Add(endingSpaceGridUpdate);

                statusMessage = String.Empty;
            }
            else
            {
                return(GetFirstPlacingMoveResult(columnIndex, rowIndex, panelResult));
            }

            _setupMoveCount      += updateList.Count;
            _preceededByStartMove = false;

            return(new GridUpdateEvent(updateList, statusMessage));
        }