//helper function for part 2 public static Player Part2Test() { Player player = PlayerOperations.RandomPlayer(); Item item = PlayerItemOperations.RandomItem(); player.Items.Add(item); item = PlayerItemOperations.RandomItem(); player.Items.Add(item); item = PlayerItemOperations.RandomItem(); player.Items.Add(item); item = PlayerItemOperations.RandomItem(); player.Items.Add(item); Console.WriteLine("highest level item was " + player.GetHighestLevelItem().Level); return(player); }
//Kohta2 käyttää tätä public static void CreateItems() { Player player = new Player(); player.Items = new List <Item>(); player.Items.Add(new Item() { Level = 165555, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 1, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 5, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 200, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 7, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 9, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 12, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 165555, Id = Guid.NewGuid() }); player.Items.Add(new Item() { Level = 16555, Id = Guid.NewGuid() }); Item highestItem = player.GetHighestLevelItem(); Console.WriteLine(highestItem.Level); }
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); } }
static void Main(string[] args) { // 1. Instantiate 1 000 000 players // Create list of Iplayer because we will use the InstantiatePlayers fucntion later for creating Players for antoher game (kohta 7) List <IPlayer> players = new List <IPlayer>(); // Create list for the 1 000 000 players List <IPlayer> playersForAnotherGame = new List <IPlayer>(); // Create list for the 1 000 000 players for antoher game List <Guid> guids = new List <Guid>(); // Get the player GUIDS in a seperate list o check for duplicates List <Guid> playersForAnotherGameGuids = new List <Guid>(); try { InstantiatePlayers(players, guids, false); // Create 1 000 000 players for one game InstantiatePlayers(playersForAnotherGame, playersForAnotherGameGuids, true); // Create 1 000 000 players for another game } catch (Exception) { Console.WriteLine("Duplicates found!\n"); } // 2. Generates a test player and 10 items with random stats Player testPlayer = new Player(); testPlayer.Items = new List <Item>(); Random rnd = new Random(); for (int i = 0; i < 10; i++) { Item item = new Item(); item.Id = Guid.NewGuid(); //item.Level = i; item.Level = rnd.Next(1, 51); testPlayer.Items.Add(item); } Console.WriteLine("Test player highest level item: " + testPlayer.GetHighestLevelItem().Level + "\n"); // 3. Calls GetItems() & GetItemsWithLinq() for the same test player that we created in previous exercise Item[] itemsArray1 = GetItems(testPlayer.Items); Item[] itemsArray2 = GetItemsWithLinq(testPlayer.Items); // Test-loop for the GetItems functions /* for (int i = 0; i < itemsArray1.Length; i++) * { * Console.WriteLine(itemsArray1[i].Level); * } */ // 4. Calls FirstItem() & FirstItemWithLinq() for the same test player that we created in previous exercise Item firstItem1 = FirstItem(testPlayer); Item firstItem2 = FirstItemWithLinq(testPlayer); // Test Console.WriteLine("First item was item with level: " + firstItem1.Level + "\n"); // 5. Delegates Del handler = ProcessEachItem; handler(testPlayer, PrintItem); Console.WriteLine("\n"); // 6. Lambda Action <Item> delegateInstance = (Item item) => Console.WriteLine("Item ID: " + item.Id.ToString() + " & level: " + item.Level.ToString()); handler(testPlayer, delegateInstance); // 7. Let's create Game & PlayerForAnotherGame instances and try to find their top players var game = new Game <IPlayer>(players); // Use the 1 000 000 players we created earlier IPlayer[] top10Players = game.GetTop10Players(); Console.WriteLine("\nTop 10 players are:\n"); for (int i = 0; i < top10Players.Length; i++) { Console.WriteLine((i + 1) + ". Player ID: " + top10Players[i].Id + " and score: " + top10Players[i].Score); } var anotherGame = new Game <IPlayer>(playersForAnotherGame); // Use the 1 000 000 players for another game we created earlier IPlayer[] top10PlayersForAnotherGame = anotherGame.GetTop10Players(); Console.WriteLine("\nTop 10 players for another game are:\n"); for (int i = 0; i < top10PlayersForAnotherGame.Length; i++) { Console.WriteLine((i + 1) + ". Player ID: " + top10PlayersForAnotherGame[i].Id + " and score: " + top10PlayersForAnotherGame[i].Score); } }