mercredi 19 juillet 2017

Unsure how to handle exception in hot potato game C#

I'm making a hot-potato game in C#. In the code I was trying to handle an exception in the Game() method where it will prompt you to pass the potato to someone (by their player number) it would prevent you from entering your own player number (trying to pass it to yourself.) The only thing is, I'm having trouble trying to handle the exception:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotPotatoe
{
    class Program
    {
        public static void Main()
        {
            Console.Clear();
            Console.Write("How many players are there? (up to 4): ");
            string PlayerCount = Console.ReadLine();
            string PlayerOne = null;
            string PlayerTwo = null;
            string PlayerThree = null;
            string PlayerFour = null;
            if (PlayerCount == "2")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 2);
                Game(PlayerOne, PlayerTwo, PlayerThree, PlayerFour, WhoStarts);
            }
            else if (PlayerCount == "3")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Console.Write("Enter player 3's name: ");
                PlayerThree = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 3);
                Game(PlayerOne, PlayerTwo, PlayerThree, PlayerFour, WhoStarts);
            }
            else if (PlayerCount == "4")
            {
                Console.Write("Enter player 1's name: ");
                PlayerOne = Console.ReadLine();
                Console.Write("Enter player 2's name: ");
                PlayerTwo = Console.ReadLine();
                Console.Write("Enter player 3's name: ");
                PlayerThree = Console.ReadLine();
                Console.Write("Enter player 4's name: ");
                PlayerFour = Console.ReadLine();
                Random ran = new Random();
                int WhoStarts = ran.Next(1, 4);

                Game(PlayerOne, 
                    PlayerTwo, 
                    PlayerThree, 
                    PlayerFour, 
                    WhoStarts);
            }
            else
            {
                Console.WriteLine("Not valid!");
                Console.ReadKey();
                Main();
            }
        }

        private static void Game
           (string player1, 
            string player2, 
            string player3, 
            string player4, 
            int whoStarts)     
        {
            Player Player1 = new Player();  
            Player Player2 = new Player();
            Player Player3 = new Player();
            Player Player4 = new Player();
            Player1.Name = player1;         
            Player2.Name = player2;
            Player3.Name = player3;
            Player4.Name = player4;
            Player1.Number = 1;
            Player2.Number = 2;
            Player3.Number = 3;
            Player4.Number = 4;
            Player1.IsOut = false;   
            Player2.IsOut = false;
            if(Player3.Name == null)
            {
                Player3.IsOut = true;
            }
            else
            {
                Player3.IsOut = false;
            }
            if (Player4.Name == null)
            {
                Player4.IsOut = true;
            }
            else
            {
                Player3.IsOut = false;
            }
            switch (whoStarts)
            {
                case 1:
                    Player1.HasPotatoe = true;
                    break;
                case 2:
                    Player2.HasPotatoe = true;
                    break;
                case 3:
                    Player3.HasPotatoe = true;
                    break;
                default:    
                    Player4.HasPotatoe = true;
                    break;
            }
            List<Player> PlayingList = new List<Player>
            {
                Player1,
                Player2
            };
            if (Player3.IsOut == false)
            {
                PlayingList.Add(Player3);
            }
            if (Player4.IsOut == false)
            {
                PlayingList.Add(Player4);
            }
            Random rand = new Random();
            int NumOfRounds = rand.Next(1, 10);
            do
            {
                foreach (Player p in PlayingList)
                {
                    if (p.HasPotatoe == true)
                    {
                        Console.Write("Player {0} has the potatoe! Who do you want to pass it to? (by number): ", p.Number);
                        string input = Console.ReadLine();
                        if (input == "1")
                        {
                            PlayingList[0].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "2")
                        {
                            PlayingList[1].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "3")
                        {
                            if (Player3.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[2].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if (input == "4")
                        {
                            if (Player4.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[3].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if(input == p.Number.ToString())    // this is the part i'm having trouble with................................
                        {
                            Console.WriteLine("{0}", p.Number.ToString());
                            Console.WriteLine("You can't pass it to yourself! -_- Choose someone else..." + p.Number.ToString());
                            Console.ReadKey();
                        }
                    }
                }
            } while (NumOfRounds != 0);
            if(Player1.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player1.Name);
                Console.ReadKey();
                Main();
            }
            else if(Player2.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player2.Name);
                Console.ReadKey();
                Main();
            }
            else if (Player3.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player3.Name);
                Console.ReadKey();
                Main();
            }
            else if (Player4.HasPotatoe == true)
            {
                Console.WriteLine("{0} is out! Play again!", Player4.Name);
                Console.ReadKey();
                Main();
            }
        }
    }        

    class Player
    {
        public string Name { get; set; }
        public int Number { get; set; }
        public bool HasPotatoe { get; set; }
        public bool IsOut { get; set; }
    }
}

The specific part where the error is:

Random rand = new Random();
            int NumOfRounds = rand.Next(1, 10);
            do
            {
                foreach (Player p in PlayingList)
                {
                    if (p.HasPotatoe == true)
                    {
                        Console.Write("Player {0} has the potatoe! Who do you want to pass it to? (by number): ", p.Number);
                        string input = Console.ReadLine();
                        if (input == "1")
                        {
                            PlayingList[0].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "2")
                        {
                            PlayingList[1].HasPotatoe = true;
                            NumOfRounds--;
                        }
                        else if (input == "3")
                        {
                            if (Player3.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[2].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if (input == "4")
                        {
                            if (Player4.IsOut == true)
                            {
                                Console.WriteLine("That person isn't playing! Choose another player!");
                                Console.ReadKey();
                            }
                            else
                            {
                                PlayingList[3].HasPotatoe = true;
                                NumOfRounds--;
                            }
                        }
                        else if(input == p.Number.ToString())  // this is where the problem is...
                        {
                            Console.WriteLine("{0}", p.Number.ToString());
                            Console.WriteLine("You can't pass it to yourself! -_- Choose someone else..." + p.Number.ToString());
                            Console.ReadKey();
                        }
                    }
                }
            } while (NumOfRounds != 0);

so basically I say: if(input == p.Number().ToString()) // p.Number() == player's number
then throw the exception. But i'm not sure how to go about that. I've tried converting the integer to a string with the .ToString() method. but it doesn't seem to change anything. I've also tried turning the input into a 32 bit integer with the Int32.Parse(input) method, but still no luck. The result just does something random. For example if I was player two, and I prompt to pass the potato to player two, then it will just write to the console that player 2 has the potato over and over. I'm not sure how to do this right. Any suggestions?

Aucun commentaire:

Enregistrer un commentaire