예제 #1
0
        //allows the current player's turn to make a move
        private void PlayerMove(Player player)
        {
            string move;

            player.DrawTrackBoard();

            if (player.PlayerName == "Robot")
            {
                move = player.SelectAIMove();

                if (bships.PlayMove(player, move))
                {
                    Console.WriteLine("\nHIT! \n");
                    rw.Log = (player.PlayerName + " " + move + " Hit");
                }
                else
                {
                    Console.WriteLine("\nMISS! \n");
                    rw.Log = (player.PlayerName + " " + move + " Miss");
                }

                if (player.HitCount == 1)
                {
                    Console.WriteLine("Robot has hit " + player.HitCount + " square!\n\nPress any key to continue: ");
                }
                else
                {
                    Console.WriteLine("Robot has hit " + player.HitCount + " squares!\n\nPress any key to continue: ");
                }
                Console.ReadKey();
                Console.Clear();
            }
            else
            {
                Console.WriteLine("\n" + player.PlayerName + " please choose your move.");
                move = Console.ReadLine();

                if (bships.PlayMove(player, move))
                {
                    Console.WriteLine("\nHIT! \n");
                    rw.Log = (player.PlayerName + " " + move + " Hit");
                }
                else
                {
                    Console.WriteLine("\nMISS! \n");
                    rw.Log = (player.PlayerName + " " + move + " Miss");
                }

                if (player.HitCount == 1)
                {
                    Console.WriteLine("You've hit " + player.HitCount + " square!\n\nPress any key to continue: ");
                }
                else
                {
                    Console.WriteLine("You've hit " + player.HitCount + " squares!\n\nPress any key to continue: ");
                }
                Console.ReadKey();
                Console.Clear();
            }
        }
예제 #2
0
        //checks a move is valid and whether it's a hit or not
        public bool PlayMove(Player player, String move)
        {
            bool          notDouble  = false;
            List <string> movesTaken = player.MovesTaken;

            //While loop to check for an invalid move
            while (!CheckValidMove(move))
            {
                if (player.PlayerName == "Robot")
                {
                    move = player.SelectAIMove();
                }
                else
                {
                    Console.WriteLine("\nInvalid coordinates entered. Please enter a new coordinate to shoot: (A1 to J10)\n");
                    move = Console.ReadLine();
                }
            }

            //while loop to check for a double hit. Does not run on the first move
            while (!notDouble && movesTaken.Count != 0)
            {
                foreach (string moveTaken in movesTaken)
                {
                    if (moveTaken == move)
                    {
                        notDouble = false;

                        if (player.PlayerName == "Robot")
                        {
                            move = player.SelectAIMove();
                        }
                        else
                        {
                            Console.WriteLine("\nCoordinate already shot. Please enter a new coordinate to shoot: (A1 to J10)\n");
                            move = Console.ReadLine();

                            while (!CheckValidMove(move))
                            {
                                Console.WriteLine("\nInvalid coordinates entered. Please enter a new coordinate to shoot: (A1 to J10)\n");
                                move = Console.ReadLine();
                            }
                            break;
                        }
                    }
                    else
                    {
                        notDouble = true;
                    }
                }
            }

            player.MovesTaken.Add(move);

            int x = GetXCoord(move);
            int y = GetYCoord(move);

            if (player.TrackBoard[y, x] != " x ")
            {
                string sunkCheck = player.TrackBoard[y, x];

                player.TrackBoard[y, x] = " o ";

                CheckBoats(sunkCheck, player);

                player.HitCount++;
                return(true);
            }
            else
            {
                player.TrackBoard[y, x] = " m ";
                return(false);
            }
        }