Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# .NET Progran Assignment 4 - Classes and Collections 1. Create a class Player

ID: 3589587 • Letter: C

Question

C# .NET Progran Assignment 4 - Classes and Collections 1. Create a class Player that can keep track of a player's name, wins, and losses. Use private variables and public properties. (Ch. 4, starting page 73) 2. Create an enum Actions with the values ‘Rock,' ‘Paper', and ‘Scissors' (or anything to that effect if you want to get creative... . (Ch. 7, page 175-177) 3. Create a method in the Player class to allow a player to play against another player by having each randomly choose an Action from the enum Actions. If actions are numbered 0, 1, ..., n, assume that action 0 defeats action n; action x defeats action x ; otherwise, the players play again. Appropriately update each players' wins and losses. The function should check to make sure a player is not playing against themselves. 4. Create a function that accepts any number of player names, and returns a List of players with those names. Instantiate a list a players using this function. (Ch. 8, page 234; Ch. 9, page 250-253) 5. Using a loop, have each player play another random player (not themselves), and repeat this for 5 rounds (so for 5 players, there should be a total of 25 games). (Optional: For fun, try using namespace System.Threading, and explore the function Sleep0). 6. After the games, use LINQ to display a leaderboard of the players sorted by wins in descending order. (Ch. 9, page 245-255) 7. Using LINQ, display any players with an equal number of wins and losses.

Explanation / Answer

Solution=============================

First 4 sub parts:

//Problem 2
    enum Moves {Rock,Paper,Scissors };

//Problem 1

class Player
    {
        private string name;
        private int wins;
        private int losses;
        private Random rnd;

        public Player(string name){
            this.name = name;
            wins = 0;
            losses = 0;
            rnd = new Random();
        }

        public string getName()
        {
            return name;
        }

        public void setName(string player)
        {
            name=player;
        }

        public void addWins()
        {
            wins += 1;
        }

        public void addLoss()
        {
            losses += 1;
        }
          
        public int getWins()
        {
            return wins;
        }

        public int getLosses()
        {
            return losses;
        }

        //Get the index at random
        public int generateMove()
        {
            return rnd.Next(0, 2);
        }

        //Playing
        public void playAgainst(Player p)
        {
            if(this == p)
            {
                Console.WriteLine("You cannot challenge yourself..");
                return;
            }

            bool result = false;

            while (!result)
            {
                int myMove = generateMove();
                int oppMove = p.generateMove();

                if(myMove-1 == oppMove || myMove+2 == oppMove)
                {
                    //Opponent lost
                    result = true;
                    addWins();
                    p.addLoss();

                }else if (oppMove - 1 == myMove || oppMove + 2 == myMove)
                {
                    //I lost
                    result = true;
                    addLoss();
                    p.addWins();
                }
            }

        }


        public List<Player> generatePlayers(string[] names)
        {
            List<Player> list = new List<Player>();

            foreach(string player in names)
            {
                Player p = new Player(player);
                list.Add(p);
            }

            return list;
        }
    }