Esempio n. 1
0
        static void Main(string[] args)
        {
            //new: causes 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;                    //no physical instance, storage is null
            List <Turn> rounds = new List <Turn>(); //create the physical List<T>

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

            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":
                    {
                        //Turn is a non-static class
                        //therefore you need to create a new instance to use
                        theTurn = new Turn();           //create the physical instance

                        //generate a new FaceValue
                        Player1.Roll();
                        //generate a new FaceValue
                        Player2.Roll();

                        // save the roll
                        //     set      =      get
                        theTurn.Player1 = Player1.FaceValue;
                        theTurn.Player2 = Player2.FaceValue;

                        //display the round results
                        Console.WriteLine("Player1 rolled {0}", Player1.FaceValue);
                        Console.WriteLine("Player2 rolled {0}", Player2.FaceValue);
                        if (Player1.FaceValue > Player2.FaceValue)
                        {
                            Console.WriteLine("Player one wins.");
                        }
                        else if (Player1.FaceValue < Player2.FaceValue)
                        {
                            Console.WriteLine("Player two wins.");
                        }
                        else
                        {
                            Console.WriteLine("Round is 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 = Player2.Sides = 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
                        DisplayCurrentPlayerStats(rounds);
                        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
Esempio n. 2
0
        static void Main(string[] args)
        {
            /*
             * dice game
             * 2 player
             * record wins
             * record moves
             * display moves
             * higher number wins
             *
             * Die character (Object) DONE
             * Size (Int)
             * Color (String)
             * Face (Int)
             * Roll Method
             * Random number, Assign face
             */

            //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

            TurnEX 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");


            //tack the game plays

            List <TurnEX> rounds = new List <TurnEX>();

            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

                        //generate a new FaceValue
                        player1.Roll();
                        //generate a new FaceValue
                        player2.Roll();

                        // save the roll
                        //method a) default constructor and individual setting

                        TheTurn             = new TurnEX();
                        TheTurn.Player1Roll = player1.FaceValue;
                        TheTurn.Player2Roll = player2.FaceValue;

                        //method b) greedy constructor

                        //TheTurn = new TurnEX(player1.FaceValue, player2.FaceValue);

                        //display the round results


                        Console.WriteLine("Player 1 Rolled {0} Player 2 Rolled {1}", TheTurn.Player1Roll, TheTurn.Player2Roll);

                        if (TheTurn.Player1Roll > TheTurn.Player2Roll)
                        {
                            Console.WriteLine("Player 1 Wins");
                        }
                        else if (TheTurn.Player2Roll > TheTurn.Player1Roll)
                        {
                            Console.WriteLine("Player 2 Wins");
                        }
                        else
                        {
                            Console.WriteLine("Player 1 & 2 Tie");
                        }

                        //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
                        DisplayCurrentPlayerStats(rounds);
                        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
Esempio n. 3
0
        static void Main(string[] args)
        {
            //new: causes 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;                           //will not work because instead of creating a physical instance, you just created an area in memory that is null.
            //where the address OF an instance can be place.
            List <Turn> Rounds = new List <Turn>(); //Create the physical List<T>

            //new: causes the constructor of a class to execute
            //   and a physical instance to be created
            Die Player1 = new Die();
            //It takes the address of where the object sits in memory and saves the pointer.
            //Here, the default constructor is used because there's nothing in the brackets. If there was anything, the overloaded
            //constructor would be used. It knows by the signature what you want to call.
            Die Player2 = new Die(6, "green");
            //This grabs the greedy constructor.

            //Player1.Sides = 6;
            //Player1.Colour = "white";
            //This would be the same as declaring it empty, in that you're directly changing the value.

            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":
                    {
                        //Turn is a non-static class
                        //Since it's non static, we need to create a new instance to use to store data in.
                        theTurn = new Turn();         //creates the physical instance

                        //generate a new FaceValue
                        Player1.Roll();
                        //generate a new FaceValue
                        Player2.Roll();
                        // save the roll
                        //Lefthand side of the equal sign is the set, right is get.
                        //      set     =       get
                        theTurn.Player1 = Player1.FaceValue;
                        theTurn.Player2 = Player2.FaceValue;

                        //display the round results
                        Console.WriteLine("Player 1 rolled {0}, player 2 rolled {2}.", Player1.FaceValue, theTurn.Player2);
                        //This will be the same as saying Player2.FaceValue.
                        if (Player1.FaceValue > Player2.FaceValue)
                        {
                            Console.WriteLine("Player 1 wins!");
                        }
                        else if (Player2.FaceValue > Player1.FaceValue)
                        {
                            Console.WriteLine("Player 2 wins!");
                        }
                        else
                        {
                            Console.WriteLine("It's a draw!");
                        }
                        //track the round
                        //Remember, create lists outside the loop so we can keep the values for later!
                        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))         //TryParse checks if it's true or false, then will put the value into sides
                        {
                            //validation of the incoming value
                            if (sides > 1)
                            {
                                //set the die instance Sides
                                //Player1.Sides = sides;
                                //Player2.Sides = sides;
                                //OR
                                Player2.Sides = Player1.Sides = sides;
                                //Values are assigned right to left.
                            }
                            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
                        DisplayCurrentPlayerStats(Rounds);
                        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