I have a hashtable and I have tried inputting all the values as either objects created from a class or as arrays. the arrays/objects are meant to be stand ins for cards and I want to use a random number generator to choose the keys of the hashtable and then to read out all the values associated to that key. Please can someone help?
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
namespace CardGame
{
internal class Program
{
static Hashtable monsters;
static void Main(string[] args)
{
monsters = new Hashtable();
int[] mon1 = new int[2] { 1, 2 };
int[] mon2 = new int[2] { 1, 2 };
int[] mon3 = new int[2] { 1, 2 };
int[] mon4 = new int[2] { 1, 2 };
int[] mon5 = new int[2] { 1, 2 };
int[] mon6 = new int[2] { 1, 2 };
int[] mon7 = new int[2] { 1, 2 };
int[] mon8 = new int[2] { 1, 2 };
int[] mon9 = new int[2] { 1, 2 };
int[] mon10 = new int[2] { 1, 4 };
int[] mon11 = new int[2] { 2, 4 };
int[] mon12 = new int[2] { 2, 4 };
int[] mon13 = new int[2] { 2, 4 };
int[] mon14 = new int[2] { 2, 4 };
int[] mon15 = new int[2] { 2, 4 };
int[] mon16 = new int[2] { 2, 4 };
int[] mon17 = new int[2] { 2, 4 };
int[] mon18 = new int[2] { 2, 4 };
int[] mon19 = new int[2] { 2, 4 };
int[] mon20 = new int[2] { 2, 4 };
int[] mon21 = new int[2] { 3, 6 };
int[] mon22 = new int[2] { 3, 6 };
int[] mon23 = new int[2] { 3, 6 };
int[] mon24 = new int[2] { 3, 6 };
int[] mon25 = new int[2] { 3, 6 };
int[] mon26 = new int[2] { 3, 6 };
int[] mon27 = new int[2] { 3, 6 };
int[] mon28 = new int[2] { 3, 6 };
int[] mon29 = new int[2] { 3, 6 };
int[] mon30 = new int[2] { 3, 6 };
int[] mon31 = new int[2] { 4, 8 };
int[] mon32 = new int[2] { 4, 8 };
int[] mon33 = new int[2] { 4, 8 };
int[] mon34 = new int[2] { 4, 8 };
int[] mon35 = new int[2] { 4, 8 };
int[] mon36 = new int[2] { 4, 8 };
int[] mon37 = new int[2] { 4, 8 };
int[] mon38 = new int[2] { 4, 8 };
int[] mon39 = new int[2] { 4, 8 };
int[] mon40 = new int[2] { 4, 8 };
int[] mon41 = new int[2] { 5, 10 };
int[] mon42 = new int[2] { 5, 10 };
int[] mon43 = new int[2] { 5, 10 };
int[] mon44 = new int[2] { 5, 10 };
int[] mon45 = new int[2] { 5, 10 };
int[] mon46 = new int[2] { 5, 10 };
int[] mon47 = new int[2] { 5, 10 };
int[] mon48 = new int[2] { 5, 10 };
int[] mon49 = new int[2] { 5, 10 };
int[] mon50 = new int[2] { 5, 10 };
monsters.Add(1, mon1);
monsters.Add(2, mon2);
monsters.Add(3, mon3);
monsters.Add(4, mon4);
monsters.Add(5, mon5);
monsters.Add(6, mon6);
monsters.Add(7, mon7);
monsters.Add(8, mon8);
monsters.Add(9, mon9);
monsters.Add(10, mon10);
monsters.Add(11, mon11);
monsters.Add(12, mon12);
monsters.Add(13, mon13);
monsters.Add(14, mon14);
monsters.Add(15, mon15);
monsters.Add(16, mon16);
monsters.Add(17, mon17);
monsters.Add(18, mon18);
monsters.Add(19, mon19);
monsters.Add(20, mon20);
monsters.Add(21, mon21);
monsters.Add(22, mon22);
monsters.Add(23, mon23);
monsters.Add(24, mon24);
monsters.Add(25, mon25);
monsters.Add(26, mon26);
monsters.Add(27, mon27);
monsters.Add(28, mon28);
monsters.Add(29, mon29);
monsters.Add(30, mon30);
monsters.Add(31, mon31);
monsters.Add(32, mon32);
monsters.Add(33, mon33);
monsters.Add(34, mon34);
monsters.Add(35, mon35);
monsters.Add(36, mon36);
monsters.Add(37, mon37);
monsters.Add(38, mon38);
monsters.Add(39, mon39);
monsters.Add(40, mon40);
monsters.Add(41, mon41);
monsters.Add(42, mon42);
monsters.Add(43, mon43);
monsters.Add(44, mon44);
monsters.Add(45, mon45);
monsters.Add(46, mon46);
monsters.Add(47, mon47);
monsters.Add(48, mon48);
monsters.Add(49, mon49);
monsters.Add(50, mon50);
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
foreach (DictionaryEntry mons in monsters)
{
Console.WriteLine($"monster number {monsters.Keys}");
foreach (var item in mons)
{
Console.WriteLine(item);
}
}
I just cant seem to be able to access each value pairing from the hashtable. P.S. there's a lot of commented out dead code at the bottom of things that I've tried.
CodePudding user response:
The class Hashtable
is not typed. The compiler doesn't know what types are in the table at runtime. So when you take things from the table, you have to specify the type you expect:
foreach (DictionaryEntry mons in monsters)
{
// Use mons.key, not monsters.key to access the number
// Its type is object. Outputting it works, but
// to actually work with it, you have to cast it back to int.
Console.WriteLine($"monster number {mons.Key}");
// To work with the int array inside the table, use
// a cast
foreach (var item in mons.Value as int[])
{
Console.WriteLine(item);
}
}
Instead of using outdated Hashtable
, use a generic Dictionary<>
, that supports specifying types at compile time:
internal class Program
{
static Dictionary<int, int[]> monsters = new Dictionary<int, int[]>();
static void Main(string[] args)
{
int[] mon1 = new int[2] { 1, 2 };
int[] mon2 = new int[2] { 1, 2 };
int[] mon3 = new int[2] { 1, 2 };
int[] mon4 = new int[2] { 1, 2 };
int[] mon5 = new int[2] { 1, 2 };
int[] mon6 = new int[2] { 1, 2 };
int[] mon7 = new int[2] { 1, 2 };
int[] mon8 = new int[2] { 1, 2 };
int[] mon9 = new int[2] { 1, 2 };
int[] mon10 = new int[2] { 1, 4 };
int[] mon11 = new int[2] { 2, 4 };
int[] mon12 = new int[2] { 2, 4 };
int[] mon13 = new int[2] { 2, 4 };
int[] mon14 = new int[2] { 2, 4 };
int[] mon15 = new int[2] { 2, 4 };
int[] mon16 = new int[2] { 2, 4 };
int[] mon17 = new int[2] { 2, 4 };
int[] mon18 = new int[2] { 2, 4 };
int[] mon19 = new int[2] { 2, 4 };
int[] mon20 = new int[2] { 2, 4 };
int[] mon21 = new int[2] { 3, 6 };
int[] mon22 = new int[2] { 3, 6 };
int[] mon23 = new int[2] { 3, 6 };
int[] mon24 = new int[2] { 3, 6 };
int[] mon25 = new int[2] { 3, 6 };
int[] mon26 = new int[2] { 3, 6 };
int[] mon27 = new int[2] { 3, 6 };
int[] mon28 = new int[2] { 3, 6 };
int[] mon29 = new int[2] { 3, 6 };
int[] mon30 = new int[2] { 3, 6 };
int[] mon31 = new int[2] { 4, 8 };
int[] mon32 = new int[2] { 4, 8 };
int[] mon33 = new int[2] { 4, 8 };
int[] mon34 = new int[2] { 4, 8 };
int[] mon35 = new int[2] { 4, 8 };
int[] mon36 = new int[2] { 4, 8 };
int[] mon37 = new int[2] { 4, 8 };
int[] mon38 = new int[2] { 4, 8 };
int[] mon39 = new int[2] { 4, 8 };
int[] mon40 = new int[2] { 4, 8 };
int[] mon41 = new int[2] { 5, 10 };
int[] mon42 = new int[2] { 5, 10 };
int[] mon43 = new int[2] { 5, 10 };
int[] mon44 = new int[2] { 5, 10 };
int[] mon45 = new int[2] { 5, 10 };
int[] mon46 = new int[2] { 5, 10 };
int[] mon47 = new int[2] { 5, 10 };
int[] mon48 = new int[2] { 5, 10 };
int[] mon49 = new int[2] { 5, 10 };
int[] mon50 = new int[2] { 5, 10 };
monsters.Add(1, mon1);
monsters.Add(2, mon2);
monsters.Add(3, mon3);
monsters.Add(4, mon4);
monsters.Add(5, mon5);
monsters.Add(6, mon6);
monsters.Add(7, mon7);
monsters.Add(8, mon8);
monsters.Add(9, mon9);
monsters.Add(10, mon10);
monsters.Add(11, mon11);
monsters.Add(12, mon12);
monsters.Add(13, mon13);
monsters.Add(14, mon14);
monsters.Add(15, mon15);
monsters.Add(16, mon16);
monsters.Add(17, mon17);
monsters.Add(18, mon18);
monsters.Add(19, mon19);
monsters.Add(20, mon20);
monsters.Add(21, mon21);
monsters.Add(22, mon22);
monsters.Add(23, mon23);
monsters.Add(24, mon24);
monsters.Add(25, mon25);
monsters.Add(26, mon26);
monsters.Add(27, mon27);
monsters.Add(28, mon28);
monsters.Add(29, mon29);
monsters.Add(30, mon30);
monsters.Add(31, mon31);
monsters.Add(32, mon32);
monsters.Add(33, mon33);
monsters.Add(34, mon34);
monsters.Add(35, mon35);
monsters.Add(36, mon36);
monsters.Add(37, mon37);
monsters.Add(38, mon38);
monsters.Add(39, mon39);
monsters.Add(40, mon40);
monsters.Add(41, mon41);
monsters.Add(42, mon42);
monsters.Add(43, mon43);
monsters.Add(44, mon44);
monsters.Add(45, mon45);
monsters.Add(46, mon46);
monsters.Add(47, mon47);
monsters.Add(48, mon48);
monsters.Add(49, mon49);
monsters.Add(50, mon50);
// mons is type KeyValuePair<int, int[]>
foreach (var mons in monsters)
{
Console.WriteLine($"monster number {mons.Key}");
foreach (var item in mons.Value)
{
Console.WriteLine(item);
}
}
}
}
How to get the random selection from the dictionary:
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
foreach (var monId in selection)
{
Console.WriteLine($"monster number {monId}");
int[] monster = monsters[monId];
foreach (var item in monster)
{
Console.WriteLine(item);
}
}
Or a shorter form if you need the random monsters in a list
Random rand = new Random();
var selection = Enumerable.Range(1, 50).OrderBy(x => rand.Next()).Take(10).ToList();
var selectedMonsters = selection.Select(i => monsters[i]);
foreach (var monster in selectedMonsters)
{
Console.WriteLine("monster");
foreach (var item in monster)
{
Console.WriteLine(item);
}
}