示例#1
0
        public override Cell TakeShot()
        {
            while (true)
            {
                Console.WriteLine("Your turn. Please enter your guess in the format A5 (valid chars are [{0} - {1}]) and valid numbers are [{2} - {3}]", Board.ValidCharacters.First(), Board.ValidCharacters.Last(), Board.ValidNumbers.First(), Board.ValidNumbers.Last());
                var playerInput = Console.ReadLine();

                if (playerInput.Length == 2)
                {
                    playerInput = playerInput.ToUpper();
                    char horizontalGuess;
                    int verticalGuess;
                    if (Char.TryParse(playerInput[0].ToString(), out horizontalGuess) && int.TryParse(playerInput[1].ToString(), out verticalGuess))
                    {
                        if (Board.ValidCharacters.Contains(horizontalGuess) && Board.ValidNumbers.Contains(verticalGuess))
                        {
                            if (previousGuesses.Any(x => x.Horizontal == horizontalGuess && x.Vertical == verticalGuess))
                            {
                                Console.WriteLine("You have already guess there before, please try again.");
                            }
                            else
                            {
                                var cell = new Cell(horizontalGuess, verticalGuess);
                                this.previousGuesses.Add(cell);
                                return cell;
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        public Board()
        {
            this.grid = new Cell[ValidCharacters.Length, ValidNumbers.Length];
            this.placedShips = new List<Ship>();

            for (var i = 0; i < ValidCharacters.Length; i++)
            {
                for (var j = 0; j < ValidNumbers.Length; j++)
                {
                    grid[i, j] = new Cell(ValidCharacters[i], ValidNumbers[j]);
                }
            }
        }
示例#3
0
        public bool IsHit(Cell shot)
        {
            if (cells == null)
            {
                throw new InvalidOperationException("Ship has not yet been placed on the board.");
            }

            if (cells.Contains(shot))
            {
                this.hitCells.Add(shot);

                return true;
            }

            return false;
        }
示例#4
0
        // this could be improved by holding a collection of previous hit cells instead of 1
        // and then figuring out the direction the ship is facing to improve on the selection of the next cell
        private Cell SelectCellNearPreviousChoice()
        {
            Cell selectedCell;

            var horizontalPosition = Array.IndexOf(Board.ValidCharacters, this.previousChosenCellWithoutSinkingShip.Value.Horizontal);
            var verticalPosition = this.previousChosenCellWithoutSinkingShip.Value.Vertical;

            if (horizontalPosition > 0)
            {
                selectedCell = new Cell(Board.ValidCharacters[horizontalPosition - 1], verticalPosition);

                if (!this.previousGuesses.Contains(selectedCell))
                {
                    return selectedCell;
                }
            }
            if (horizontalPosition < Board.ValidCharacters.Length)
            {
                selectedCell = new Cell(Board.ValidCharacters[horizontalPosition + 1], verticalPosition);

                if (!this.previousGuesses.Contains(selectedCell))
                {
                    return selectedCell;
                }
            }
            if (verticalPosition > 0)
            {
                selectedCell = new Cell(Board.ValidCharacters[horizontalPosition], verticalPosition - 1);

                if (!this.previousGuesses.Contains(selectedCell))
                {
                    return selectedCell;
                }
            }
            if (verticalPosition < Board.ValidNumbers.Length)
            {
                selectedCell = new Cell(Board.ValidCharacters[horizontalPosition], verticalPosition + 1);

                if (!this.previousGuesses.Contains(selectedCell))
                {
                    return selectedCell;
                }
            }

            return new Cell('A', 0);
        }
示例#5
0
        public virtual bool CheckForHit(Cell guessedCell)
        {
            var ship = this.ships.FirstOrDefault(x => x.IsHit(guessedCell));

            if (ship == null)
            {
                Console.WriteLine(this.Name + ": YOU HAVE MISSED!!");
                return false;
            }

            if (ship.IsSunk())
            {
                Console.WriteLine(this.Name + ": YOU HAVE SUNK MY: " + ship.ShipType);
                return true;
            }

            Console.WriteLine(this.Name + "YOU HAVE SCORED A HIT!!");
            return true;
        }
示例#6
0
 public override bool CheckForHit(Cell guessedCell)
 {
     var wasHit base.CheckForHit(guessedCell);
 }
示例#7
0
 public bool Occupies(Cell cell)
 {
     return this.cells.Contains(cell);
 }