Exemple #1
0
        static void Main(string[] args)
        {
            // create instances of our objects to be used in this program

            // you can check for additional namespaces that may me needed to use your objects

            // when you create a project, the default namespace is set to the project name
            // a good way to visualize projects would be as different buildings on campus... the object would be room number... you could have two room 100's,
            // but you can tell them apart because one is in CAT and one is in HP

            // we need to have a structure that will allow us to hold an unknown number of instances of a variable => USE A LIST

            // array is used for a fixed number of items, list is used for an unknown number of items

            // List<T> the T here stands for template, you would want to use the variable name being stored in the list, below using Turn

            // List<T> is an object that holds x number of datatype instances

            // the new List<T> physically creates the instance of List<T> in memory

            // the constructor of List<T> is called

            List <Turn> gameTurns = new List <Turn>(); // FIGURE OUT EXACTLY WHAT HAPPENS HERE

            // create two instances of the Die object

            Die Player1Dice = new Die();                    // default constructor
            Die Player2Dice = new Die(6, "Green");          // greedy constructor

            string menuChoice = "";

            do
            {
                Console.WriteLine("Game Menu: \n");
                Console.WriteLine("A) Set Die side count");
                Console.WriteLine("B) Roll the dice");
                Console.WriteLine("C) Display all game turn results");
                Console.WriteLine("X) Exit");
                Console.Write("Enter menu choice: ");
                menuChoice = Console.ReadLine();

                switch (menuChoice.ToUpper())
                {
                case "A":
                {
                    // logic can de done using a method
                    // the method will need to have the local variables Player1Dice and Player2Dice passed to it
                    // objects are passed as references
                    SetDiceSides(Player1Dice, Player2Dice);
                    break;
                }

                case "B":
                {
                    //logic can be done actually inside the case
                    //one does not have to always call a method

                    // roll the dice for each player, using the roll method
                    // the . operator is used with your instance to access a property or a behaviour

                    Player1Dice.Roll();         // this uses the method Roll on the Object Player1Dice **** CONFIRM IF THIS IS TRUE ****
                    Player2Dice.Roll();

                    // record the result of the roll for this turn
                    // we will need to create a new instance of the Turn class

                    Turn aturn = new Turn();         // this creates a new instance of the class Turn, named aturn **** CONFIRM IF THIS IS TRUE ****

                    //      set                     get
                    aturn.Player1DiceValue = Player1Dice.FaceValue;         // this is a property, you don't have the (), this makes sense because properties don't take in parameters
                    aturn.Player2DiceValue = Player2Dice.FaceValue;

                    // determine dice battle results
                    // it does not matter in this logic weather we use the values from aturn or the values from the Die variables, see below for proof

                    if (aturn.Player1DiceValue > Player2Dice.FaceValue)         // this is just done as an example, we should pick one and use the same for both
                    {
                        aturn.TurnWinner = "Player1";
                    }

                    else if (aturn.Player2DiceValue > aturn.Player1DiceValue)         // here we use aturn values for  both, this is a better idea
                    {
                        aturn.TurnWinner = "Player2";
                    }

                    else
                    {
                        aturn.TurnWinner = "Draw";
                    }



                    Console.WriteLine("Results: Player 1 rolled {0}, Player 2 rolled {1}, winner is {2}", aturn.Player1DiceValue, aturn.Player2DiceValue, aturn.TurnWinner);

                    // add the aturn instance to the List<T>
                    gameTurns.Add(aturn);


                    break;
                }

                case "C":
                {
                    // display the current standing in the game

                    ///////////////
                    //foreach loop
                    ///////////////
                    // this loop will start processing your collection from the first instance to the last instance, moving automatically to the next instance

                    // c# has a datatype called var, var datatype is set at execution time but it still strongly typed based on its first execution

                    foreach (var thisTurn in gameTurns)         // **** WHAT THE F**K DOES "IN" DO ****
                    {
                        Console.WriteLine("Results: Player 1 rolled {0}, Player 2 rolled {1}, winner is {2}", thisTurn.Player1DiceValue, thisTurn.Player2DiceValue, thisTurn.TurnWinner);
                    }

                    Console.Write("\n");
                    break;
                }         // closes case c

                case "X":
                {
                    Console.WriteLine("Thank you for playing. Come again.");

                    // HOMEWORK IS TO DISPLAY HERE, HOW MANY WINS PLAYER 1 HAD, HOW MANY WINS PLAYER 2 HAD, AND HOW MANY DRAWS
                    // TRY USING A FOREACH LOOP THAT WALKS THROUGH THE COLLECTION AND DOES COUNTS FOR ME
                    // USE AN IF STATEMENT WITHIN THE FOREACH LOOP

                    // below is to declare a new array, named counts
                    int[] counts = new int[] { 0, 0, 0 };         // you could have just put 3 inbetween the second set of "[]", that would create an array that stores three values
                                                                  // using { 0, 0, 0 } just initializes the three values to zero, which is the default starting value anyways
                    foreach (var aturn in gameTurns)
                    {
                        if (aturn.TurnWinner.Equals("Player1"))
                        {
                            counts[0]++;         // this increments the value in the array at position zero (increments meaning adds one to the current value)
                        }
                        else if (aturn.TurnWinner.Equals("Player2"))
                        {
                            counts[1]++;
                        }
                        else
                        {
                            counts[2]++;
                        }
                    }

                    Console.WriteLine("Player 1 won {0} times, player 2 won {1} times, and {2} draws", counts[0], counts[1], counts[2]);
                    Console.WriteLine("Thanks for playing, now frig off!");
                    break;
                }         // closes case x

                default:
                {
                    Console.WriteLine("Invalid menu choice. Try again.");
                    break;
                }
                } // closes switch
            } while (menuChoice.ToUpper() != "X");

            Console.ReadKey();
        } // end of main
Exemple #2
0
        static void Main(string[] args)
        {
            // create the instances of our objects to be used
            // in this program

            //you can check for additional namespaces
            //that may be needed to use your objects.

            //we need to have a structure that will allow
            //one to hold an unknown number of instances
            //of a variable
            //List<T> is an object that holds x number of datatype instances
            //The new List<T> phyiscal creates the instance of LIst<T>
            //    in memory. The constructor of List<T> is called
            List <Turn> gameTurns = new List <Turn>();

            //create 2 instances of the Die object
            Die Player1Dice = new Die();            //default constructor
            Die Player2Dice = new Die(6, "Green");  //greedy constructor

            string menuChoice = "";

            do
            {
                Console.WriteLine("Game Menu: \n");
                Console.WriteLine("A) Set Die side count");
                Console.WriteLine("B) Roll the dice");
                Console.WriteLine("C) Display all game turn results");
                Console.WriteLine("X) Exit");
                Console.Write("Enter menu choice: ");
                menuChoice = Console.ReadLine();

                switch (menuChoice.ToUpper())
                {
                case "A":
                {
                    //logic can de done using a method
                    //the method will need to have the
                    //    local variables PLayer1Dice and
                    //    Player2Dice passed to it.
                    //Objects are passed as references
                    SetDiceSides(Player1Dice, Player2Dice);
                    break;
                }

                case "B":
                {
                    //logic can be done actually inside the case
                    //one does not have to always call a method

                    //Roll the dice for each player
                    //the dot operator is used with your instance
                    //  to access a Property or a Behaviour
                    Player1Dice.Roll();
                    Player2Dice.Roll();

                    //record the result of the roll for this turn
                    //we need to create a new instance of the Turn
                    //    class
                    Turn aturn = new Turn();

                    //assign the facevalue of each dice to the Turn
                    //   instance
                    //        set                   get
                    aturn.Player1DiceValue = Player1Dice.FaceValue;
                    aturn.Player2DiceValue = Player2Dice.FaceValue;

                    //determine your battle results
                    //it does not matter in this logic whether we
                    //   use the values from aturn or the Die variables
                    if (aturn.Player1DiceValue > Player2Dice.FaceValue)
                    {
                        aturn.TurnWinner = "Player1";
                    }
                    else if (aturn.Player2DiceValue > aturn.Player1DiceValue)
                    {
                        aturn.TurnWinner = "Player2";
                    }
                    else
                    {
                        aturn.TurnWinner = "Draw";
                    }

                    //display the results to the user
                    Console.WriteLine("Results: Player1 rolled {0}" +
                                      " Player2 rolled {1} " +
                                      " Winner: {2}",
                                      aturn.Player1DiceValue, aturn.Player2DiceValue, aturn.TurnWinner);

                    //add the aturn instance to the List<T>
                    gameTurns.Add(aturn);
                    break;
                }

                case "C":
                {
                    //display the current standing in the game
                    //foreach loop
                    //this loop will start processing your collection
                    //   from the 1st instance to the last instance
                    //   moving automatically to the next instance

                    //C# will strong datatype variable at compile time
                    //   when the datatype is used in declaring the variable
                    //C# also has a datatype called var
                    //var datatype is set at execution time BUT is still
                    //   strongly datatype on its FIRST execution
                    foreach (var thisTurn in gameTurns)
                    {
                        Console.WriteLine("Results: Player1 rolled {0}" +
                                          " Player2 rolled {1} " +
                                          " Winner: {2}",
                                          thisTurn.Player1DiceValue,
                                          thisTurn.Player2DiceValue,
                                          thisTurn.TurnWinner);
                    }
                    Console.WriteLine("\n");
                    break;
                }

                case "X":
                {
                    //display summary results of game
                    int[] counts = new int[] { 0, 0, 0 };
                    foreach (var aturn in gameTurns)
                    {
                        if (aturn.TurnWinner.Equals("Player1"))
                        {
                            counts[0]++;
                        }
                        else if (aturn.TurnWinner.Equals("Player2"))
                        {
                            counts[1]++;
                        }
                        else
                        {
                            counts[2]++;
                        }
                    }
                    Console.WriteLine("Player 1 wins {0} Player 2 wins {1} Draws {2}",
                                      counts[0], counts[1], counts[2]);
                    Console.WriteLine("Thank you for playing. Come again.");
                    break;
                }

                default:
                {
                    Console.WriteLine("Invalid menu choice. Try again.");
                    break;
                }
                }
            } while (menuChoice.ToUpper() != "X");
            Console.ReadKey();
        }//eom