Пример #1
0
        static void Main(string[] args)
        {
            //new cause an instance (occurance) of the specified
            //   class to be created and placed in the
            //   receiving variable
            //the variable is a pointer address to the actual
            //   physical memory location of the instance



            //declaring an instance (occurance) of the specified
            //   class will not create a physical instance, just a
            //   a pointer which can hold a physical instance
            Turn theTurn;

            //new cause the constructor of a class to execute
            //   and a phyiscal instance to be created
            Die player1 = new Die();
            Die player2 = new Die(6, "Green");

            //track the game plays
            List <Turn> rounds     = new List <Turn>();
            string      menuChoice = "";

            do
            {
                //Console is a static class
                Console.WriteLine("\nMake a choice\n");
                Console.WriteLine("A) Roll");
                Console.WriteLine("B) Set number of dice sides");
                Console.WriteLine("C) Display Current Game Stats");
                Console.WriteLine("X) Exit\n");
                Console.Write("Enter your choice:\t");
                menuChoice = Console.ReadLine();

                //user friendly error handling
                try
                {
                    switch (menuChoice.ToUpper())
                    {
                    case "A":
                    {
                        //Die is a non-static class
                        player1.Roll();        //generate a new FaceValue
                        player2.Roll();        //generate a new FaceValue


                        // save the roll
                        //method a) default constructor and individual setting
                        theTurn = new Turn();
                        theTurn.PlayerOneResult = player1.Face;
                        theTurn.PlayerTwoResult = player2.Face;

                        //method b) greedy constructor
                        //theTurn = new Turn(player1.Face, player2.Face);

                        //display the round results
                        Console.WriteLine("Player 1 rolled " + player1.Face);
                        Console.WriteLine("Player 2 rolled " + player2.Face);
                        if (player1.Face > player2.Face)
                        {
                            Console.WriteLine("Player 1 wins the round.");
                        }
                        else if (player2.Face > player1.Face)
                        {
                            Console.WriteLine("Player 2 wins the round.");
                        }
                        else
                        {
                            Console.WriteLine("This round's a draw.");
                        }
                        //track the round
                        rounds.Add(theTurn);
                        break;
                    }

                    case "B":
                    {
                        string inputSides = "";
                        int    sides      = 0;

                        Console.Write("Enter your number of desired sides (greater than 1):\t");
                        inputSides = Console.ReadLine();

                        //using the conversion try version of parsing
                        // TryParse has two parameters
                        // one: in string to convert
                        // two: the output conversion value if the string can be
                        //      converted
                        // successful conversion returns a true bool
                        // failed conversion returns a false bool
                        if (int.TryParse(inputSides, out sides))
                        {
                            //validation of the incoming value
                            if (sides > 1)
                            {
                                //set the die instance Sides
                                player1.Sides = sides;
                                player2.SetSides(sides);
                            }
                            else
                            {
                                throw new Exception("You did not enter a numeric value greater than 1.");
                            }
                        }
                        else
                        {
                            throw new Exception("You did not enter a numeric value.");
                        }
                        break;
                    }

                    case "C":
                    {
                        //Display the current players' stats
                        DisplayCurrentPlayerStats(rounds);
                        break;
                    }

                    case "X":
                    {
                        //Display the final players' stats
                        Console.WriteLine("\nThank you for playing.");
                        break;
                    }

                    default:
                    {
                        Console.WriteLine("Your choice was invalid. Try again.");
                        break;
                    }
                    }//eos
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error: " + ex.Message);
                    Console.ResetColor();
                }
            } while (menuChoice.ToUpper() != "X");
        }//eomain
Пример #2
0
 public void InsertDie(Die die)
 {
     dice.Add(die);
 }