예제 #1
0
 public void IsDirections_Correct(string input, ShipDirections expected)
 {
     Assert.AreEqual(expected, Inputs.getDirection(input));
 }
예제 #2
0
 public void GetDirection(string direction, ShipDirections expected)
 {
     Assert.AreEqual(expected, ControlInput.getDirection(direction));
 }
예제 #3
0
        /// <summary>
        /// Returns locations where the ship can be places. It checks if the ship has enough space on the direction specified.
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="ranPosition"></param>
        /// <param name="shipSize"></param>
        /// <returns></returns>
        public List<KeyValuePair<int, int>> ShipCellPositions(ShipDirections direction, KeyValuePair<int, int> ranPosition, int shipSize)
        {
            var markShipPositions = new List<KeyValuePair<int, int>>();

            int xPos = ranPosition.Key;
            int yPos =  ranPosition.Value;

            switch (direction)
            {
                case ShipDirections.Up:
                    if ((yPos - shipSize) < 0)
                        return null;

                    for (int i = 0; i < shipSize; i++)
                    {
                        if (Board.BoardCells[xPos, yPos - i] != (int)BoardCellStatus.Blank)
                        {
                            return null;
                        }
                        else
                        {
                            markShipPositions.Add(new KeyValuePair<int, int>(xPos, yPos - i));
                        }
                    }
                    break;
                case ShipDirections.Right:
                    if ((xPos + shipSize) >= GameBoard.XSIZE)
                        return null;

                    for (int i = 0; i < shipSize; i++)
                    {
                        if (Board.BoardCells[xPos + i, yPos] != (int)BoardCellStatus.Blank)
                        {
                            return null;
                        } else {
                            markShipPositions.Add(new KeyValuePair<int, int>(xPos + i, yPos));
                        }
                    }
                    break;
                case ShipDirections.Down:
                    if ((yPos + shipSize) >= GameBoard.YSIZE)
                        return null;

                    for (int i = 1; i < shipSize; i++) {
                        if (Board.BoardCells[xPos, yPos + i] != (int)BoardCellStatus.Blank) {
                            return null;
                        } else {
                            markShipPositions.Add(new KeyValuePair<int, int>(xPos, yPos + i));
                        }
                    }
                    break;
                case ShipDirections.Left:
                    if ((xPos - shipSize) < 0)
                        return null;

                    for (int i = 1; i < shipSize; i++) {
                        if (Board.BoardCells[xPos - i, yPos] != (int)BoardCellStatus.Blank) {
                            return null;
                        } else {
                            markShipPositions.Add(new KeyValuePair<int, int>(xPos - i, yPos));
                        }
                    }
                    break;
                default:
                    return null;
            }
            return markShipPositions;
        }
예제 #4
0
        /// <summary>
        /// Returns locations where the ship can be places. It checks if the ship has enough space on the direction specified.
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="ranPosition"></param>
        /// <param name="shipSize"></param>
        /// <returns></returns>
        public List <KeyValuePair <int, int> > ShipCellPositions(ShipDirections direction, KeyValuePair <int, int> ranPosition, int shipSize)
        {
            var markShipPositions = new List <KeyValuePair <int, int> >();

            int xPos = ranPosition.Key;
            int yPos = ranPosition.Value;

            switch (direction)
            {
            case ShipDirections.Up:
                if ((yPos - shipSize) < 0)
                {
                    return(null);
                }

                for (int i = 0; i < shipSize; i++)
                {
                    if (Board.BoardCells[xPos, yPos - i] != (int)BoardCellStatus.Blank)
                    {
                        return(null);
                    }
                    else
                    {
                        markShipPositions.Add(new KeyValuePair <int, int>(xPos, yPos - i));
                    }
                }
                break;

            case ShipDirections.Right:
                if ((xPos + shipSize) >= GameBoard.XSIZE)
                {
                    return(null);
                }

                for (int i = 0; i < shipSize; i++)
                {
                    if (Board.BoardCells[xPos + i, yPos] != (int)BoardCellStatus.Blank)
                    {
                        return(null);
                    }
                    else
                    {
                        markShipPositions.Add(new KeyValuePair <int, int>(xPos + i, yPos));
                    }
                }
                break;

            case ShipDirections.Down:
                if ((yPos + shipSize) >= GameBoard.YSIZE)
                {
                    return(null);
                }

                for (int i = 1; i < shipSize; i++)
                {
                    if (Board.BoardCells[xPos, yPos + i] != (int)BoardCellStatus.Blank)
                    {
                        return(null);
                    }
                    else
                    {
                        markShipPositions.Add(new KeyValuePair <int, int>(xPos, yPos + i));
                    }
                }
                break;

            case ShipDirections.Left:
                if ((xPos - shipSize) < 0)
                {
                    return(null);
                }

                for (int i = 1; i < shipSize; i++)
                {
                    if (Board.BoardCells[xPos - i, yPos] != (int)BoardCellStatus.Blank)
                    {
                        return(null);
                    }
                    else
                    {
                        markShipPositions.Add(new KeyValuePair <int, int>(xPos - i, yPos));
                    }
                }
                break;

            default:
                return(null);
            }
            return(markShipPositions);
        }