예제 #1
0
        //Method to validate the shots from players to be in the board range and of length of 2.
        //It will check if the shot is good or not and print out relative message on console
        private void ValidateShot(List <string> shipSlots)
        {
            while (true)
            {
                Console.WriteLine("Player1_ fire at a slot on player2's board:");
                string player1Shot = Console.ReadLine();

                //Check if the shot is in range of the board
                if (_board.IsShipInRange(player1Shot))
                {
                    //check if the shot is in the other player ship slots if yes removes it from the list
                    //if no print missed message and exits the loop
                    if (shipSlots.IndexOf(player1Shot) >= 0)
                    {
                        shipSlots.Remove(player1Shot);
                        Console.WriteLine("GoodShot Player1");
                    }
                    else
                    {
                        Console.WriteLine("Ooops, You missed");
                    }

                    break;
                }
                //if the shot is not in the board range chek if the length of the shot is not 2 characters
                //and ask the player to correct their answer
                else if (player1Shot.Count() != 2)
                {
                    Console.WriteLine("please type exactly 2 characters for your shot:");

                    //if the shot is not in the board range but the length of the shot is  2 characters
                    // check the order of the characters and ask the player to correct their answer
                }
                else if (Regex.IsMatch(player1Shot.Substring(0, 1), @"^[a-zA-Z]+$") && Regex.IsMatch(player1Shot.Substring(1, 1), @"^\d$"))
                {
                    Console.WriteLine("please place your shot in the range of the board(width:a-h, height:1-8)");
                }
                //If non of the above cases are valid the shot is out of the board range.Ask the player to correct their answer.
                else
                {
                    Console.WriteLine("please type in a strin of 2 characters in the order of letter and number Ex:b2");
                }
            }
        }