Exemplo n.º 1
0
        public static void InstantiatePlayers(List <IPlayer> players, List <Guid> ids, bool playerForAntoherGame)
        {
            Random  rnd = new Random();
            IPlayer player;

            for (int i = 0; i < 1000000; i++)
            {
                if (playerForAntoherGame)
                {
                    player = new Player();
                }
                else
                {
                    player = new PlayerForAnotherGame();
                }

                player.Id    = Guid.NewGuid();
                player.Score = rnd.Next(1, 10000000);
                ids.Add(player.Id);
                players.Add(player);
            }

            // Check if any players have same GUID

            if (ids.Count != ids.Distinct().Count())
            {
                throw new Exception();
            }
        }
Exemplo n.º 2
0
        public static PlayerForAnotherGame RandomPlayerOtherGame()
        {
            PlayerForAnotherGame player = new PlayerForAnotherGame();

            player.Id = Guid.NewGuid();
            Random rnd = new Random();

            player.Score = rnd.Next(1, 100);
            player.Items = new List <Item>();
            return(player);
        }
Exemplo n.º 3
0
        public static Item GetHighestLevelItem(this PlayerForAnotherGame player)
        {
            List <Item> playersItems = player.Items;
            int         maxval       = int.MinValue;
            int         theIndex     = 0;

            for (int i = 0; i < playersItems.Count; i++)
            {
                if (maxval < playersItems[i].Level)
                {
                    maxval   = playersItems[i].Level;
                    theIndex = i;
                }
            }
            return(player.Items[theIndex]);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            // 1. Guid
            List <Player> players = InstantiatePlayers();

            // 2. Extension method
            Console.WriteLine("2. Extension method\n");
            Player pl = new Player();

            Item boots = new Item();

            boots.Id    = Guid.NewGuid();
            boots.Level = 3;

            Item hat = new Item();

            hat.Id    = Guid.NewGuid();
            hat.Level = 6;

            Item shirt = new Item();

            shirt.Id    = Guid.NewGuid();
            shirt.Level = 7;

            List <Item> playerItems = new List <Item>();

            playerItems.Add(boots);
            playerItems.Add(hat);
            playerItems.Add(shirt);

            pl.Items = playerItems;


            if (pl.GetHighestLevelItem() != null)
            {
                Console.WriteLine("Highest item level: " + pl.GetHighestLevelItem().Level + "\n");
            }

            // 3. LINQ
            Console.WriteLine("3. LINQ\n");
            Console.WriteLine("Normal version");
            Item[] items = GetItems(pl);

            // Print item levels to test
            foreach (Item i in items)
            {
                Console.WriteLine(i.Level);
            }

            Console.WriteLine("LINQ version");

            items = GetItemsWithLinq(pl);

            // Print item levels to test
            foreach (Item i in items)
            {
                Console.WriteLine(i.Level);
            }
            Console.WriteLine();

            // 4. LINQ 2
            Console.WriteLine("4. LINQ 2\n");
            Console.WriteLine("First item level: " + FirstItem(pl).Level);
            Console.WriteLine("First item level with LINQ: " + FirstItemWithLinq(pl).Level + "\n");

            // 5.Delegates
            Console.WriteLine("5. Delegates\n");
            ProcessEachItem(pl, PrintItem);

            // 6. Lambda
            Console.WriteLine("6. Lambda\n");
            Console.WriteLine("Lambda version: \n");

            ProcessEachItem(pl, it =>
            {
                Console.WriteLine("Id: " + it.Id);
                Console.WriteLine("Level: " + it.Level + "\n");
            });

            // 7. Generics
            Console.WriteLine("7. Generics\n");
            List <Player> players1 = new List <Player>();
            List <PlayerForAnotherGame> playersAnother = new List <PlayerForAnotherGame>();

            Random r = new Random();

            // Generate 100 players with random score between(0,99) and add them to the player list
            for (int i = 0; i < 100; i++)
            {
                Player p = new Player();
                p.Score = r.Next(0, 100);
                players1.Add(p);
            }

            // Generate 100 players with random score between(0,99) and add them to the another player list
            for (int i = 0; i < 100; i++)
            {
                PlayerForAnotherGame p = new PlayerForAnotherGame();
                p.Score = r.Next(0, 100);
                playersAnother.Add(p);
            }


            Game <Player> game = new Game <Player>(players1);
            Game <PlayerForAnotherGame> anotherGame = new Game <PlayerForAnotherGame>(playersAnother);

            Player[] top10 = game.GetTop10Players();
            PlayerForAnotherGame[] top10Another = anotherGame.GetTop10Players();

            Console.WriteLine("Top10");

            for (int i = 0; i < top10.Length; i++)
            {
                Console.WriteLine(i + 1 + ": " + top10[i].Score);
            }

            Console.WriteLine("\nTop10 another game");

            for (int i = 0; i < top10Another.Length; i++)
            {
                Console.WriteLine(i + 1 + ": " + top10Another[i].Score);
            }
        }