public BattleViewModel LaunchAttack(string input)
        {
            BattleViewModel vm = InputValidator.Validate(input);

            while (!vm.GameOver)
            {
                if (!vm.InputIsValid)
                {
                    return(vm);
                }

                return(GetAttackResult(vm));
            }

            while (!(input.ToUpper() == "Y") || !(input.ToUpper() == "N"))
            {
                if (input.ToUpper() == "Y")
                {
                    NewGame();
                    return(vm);
                }
                else
                {
                    PlayAgain = false;
                    vm.Messages.Add(GameResources.MESSAGE_USER_EXIT);
                    return(vm);
                }
            }

            vm.Messages.Add(GameResources.MESSAGE_PLAY_AGAIN);
            return(vm);
        }
Ejemplo n.º 2
0
        public BattleViewModel Validate(string input)
        {
            vm = new BattleViewModel();
            vm.InputIsValid = true;

            if (string.IsNullOrWhiteSpace(input) || input.Length != 2)
            {
                vm.Messages.Add(GameResources.MESSAGE_INPUT_ERROR_LENGTH);
                vm.InputIsValid = false;
                return(vm);
            }

            if (!Char.IsLetter(input[0]) || !Char.IsDigit(input[1]))
            {
                vm.Messages.Add(GameResources.MESSAGE_INPUT_ERROR_CHAR);
                vm.InputIsValid = false;
                return(vm);
            }

            Location attLoc = LocationTranslatior.TranslateInputToGameLocation(input);

            if (attLoc.xPos > GameOptions.BoardSize - 1 || attLoc.yPos > GameOptions.BoardSize - 1)
            {
                vm.Messages.Add(String.Format(GameResources.MESSAGE_INPUT_ERROR_OUT_OF_RANGE, GameOptions.BoardSize));
                vm.Messages.Add(GameResources.MESSAGE_ATTACK);
                vm.InputIsValid = false;
            }
            else
            {
                if (LocationHasBeenAttacked(attLoc.xPos, attLoc.yPos))
                {
                    vm.Messages.Add(GameResources.MESSAGE_ALREADY_ATTACKED);
                    vm.AttackedLocations = GameRepository.AttackedLocations;
                    vm.ShipsSunk         = GameRepository.Ships.Where(s => s.Hits == s.Size).ToList().Count;
                    vm.InputIsValid      = false;
                }
                else
                {
                    GameRepository.AttackedLocations.Add(attLoc);
                    vm.AttackLocation    = attLoc;
                    vm.AttackedLocations = GameRepository.AttackedLocations;
                    vm.ShipsSunk         = GameRepository.Ships.Where(s => s.Hits == s.Size).ToList().Count;
                }
            }

            return(vm);
        }
        private BattleViewModel GetAttackResult(BattleViewModel vm)
        {
            Ship damagedShip = GameRepository.GetShipByLocation(vm.AttackLocation.xPos, vm.AttackLocation.yPos);

            if (damagedShip != null)
            {
                damagedShip.Hits++;
                if (damagedShip.Hits == damagedShip.Size)
                {
                    if (CheckGameOver())
                    {
                        vm.GameOver = true;
                        vm.Messages.Add(GameResources.MESSAGE_GAME_OVER_PLAYER_WIN);
                        vm.Messages.Add(GameResources.MESSAGE_PLAY_AGAIN);
                        return(vm);
                    }

                    vm.Messages.Add(GameResources.MESSAGE_SUNK_SHIP);
                    vm.ShipsSunk++;
                    return(vm);
                }
                else
                {
                    vm.Messages.Add(GameResources.MESSAGE_HIT_SHIP);
                    return(vm);
                }
            }

            if (CheckGameOver())
            {
                vm.GameOver = true;
                vm.Messages.Add(GameResources.MESSAGE_GAME_OVER_COMPUTER_WIN);
                vm.Messages.Add(GameResources.MESSAGE_PLAY_AGAIN);
                return(vm);
            }

            vm.Messages.Add(GameResources.MESSAGE_MISSED_SHIP);
            return(vm);
        }