Exemple #1
0
 private void CheckForVictory(ShotResponse response)
 {
     if (response.ShotStatus == Shots.HitAndSunk)
     {
         // did they win?
         if (Ships.All(s => s.IsSunk))
         {
             response.ShotStatus = Shots.Victory;
         }
     }
 }
Exemple #2
0
        public static void ShowShotResult(ShotResponse shotresponse, Coordinate c, string playername)
        {
            String str = "";

            switch (shotresponse.ShotStatus)
            {
            case Shots.Duplicate:
                Console.ForegroundColor = ConsoleColor.Red;
                str = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Duplicate shot location!";
                break;

            case Shots.Hit:
                Console.ForegroundColor = ConsoleColor.Green;
                str = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Hit!";
                break;

            case Shots.HitAndSunk:
                Console.ForegroundColor = ConsoleColor.Green;
                str = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Hit and Sunk, " + shotresponse.ShipImpacted + "!";
                break;

            case Shots.Invalid:
                Console.ForegroundColor = ConsoleColor.Red;
                str = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Invalid hit location!";
                break;

            case Shots.Miss:
                Console.ForegroundColor = ConsoleColor.White;
                str = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Miss!";
                break;

            case Shots.Victory:
                Console.ForegroundColor = ConsoleColor.Green;
                str  = "Shot location: " + GetLetterFromNumber(c.XCoordinate) + c.YCoordinate.ToString() + "\t result: Hit and Sunk, " + shotresponse.ShipImpacted + "! \n\n";
                str += "       ******\n";
                str += "       ******\n";
                str += "        **** \n";
                str += "         **  \n";
                str += "         **  \n";
                str += "       ******\n";
                str += "Game Over, " + playername + " wins!";
                break;
            }
            Console.WriteLine(str);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("");
        }
Exemple #3
0
        private void CheckShipsForHit(Coordinate coordinate, ShotResponse response)
        {
            response.ShotStatus = Shots.Miss;

            foreach (var ship in Ships)
            {
                // no need to check sunk ships
                if (ship.IsSunk)
                {
                    continue;
                }

                Shots status = ship.FireAtShip(coordinate);

                switch (status)
                {
                case Shots.HitAndSunk:
                    response.ShotStatus   = Shots.HitAndSunk;
                    response.ShipImpacted = ship.ShipName;
                    ShotHistory.Add(coordinate, ShotRecord.Hit);
                    break;

                case Shots.Hit:
                    response.ShotStatus   = Shots.Hit;
                    response.ShipImpacted = ship.ShipName;
                    ShotHistory.Add(coordinate, ShotRecord.Hit);
                    break;
                }

                // if they hit something, no need to continue looping
                if (status != Shots.Miss)
                {
                    break;
                }
            }

            if (response.ShotStatus == Shots.Miss)
            {
                ShotHistory.Add(coordinate, ShotRecord.Miss);
            }
        }
Exemple #4
0
        public ShotResponse FireShot(Coordinate coordinate)
        {
            var response = new ShotResponse();

            // is this coordinate on the board?
            if (!IsValidCoordinate(coordinate))
            {
                response.ShotStatus = Shots.Invalid;
                return(response);
            }

            // did they already try this position?
            if (ShotHistory.ContainsKey(coordinate))
            {
                response.ShotStatus = Shots.Duplicate;
                return(response);
            }

            CheckShipsForHit(coordinate, response);
            CheckForVictory(response);

            return(response);
        }