public static T RandomElement <T>(this T[,] mat) { if (mat.Length == 0) { return(default(T)); } return(mat[XSSR.Next(mat.GetLength(0)), XSSR.Next(mat.GetLength(1))]); }
public static T RandomElement <T>(this T[] arr) { if (arr.Length == 0) { return(default(T)); } return(arr[XSSR.Next(arr.Length)]); }
private void FinishMove() { H.S.StepsTaken = 0; Entity e = H.S.Entities[H.S.CurrentActor]; List <Position> adj = e.AdjacentToEnemy(e.Pos); if (adj.Count > 0) { Position rpos = adj[XSSR.Next(adj.Count)]; BeingDamaged.Add(H.S.Entities[rpos].Name, true); message = H.S.Entities.Attack(e.Pos, rpos); H.S.CurrentReason = WaitReason.AttackAnimating; } else { message = ""; } }
public static Position RandomMatch <T>(this T[,] mat, T test) where T : IEquatable <T> { if (mat.Length == 0) { return(new Position(-1, -1)); } int frustration = 0; int span0 = mat.GetLength(0), span1 = mat.GetLength(1); int coord0 = XSSR.Next(span0), coord1 = XSSR.Next(span1); while (frustration < 20 && !(test.Equals(mat[coord0, coord1]))) //System.Collections.Generic.EqualityComparer<T>.Default.Equals { coord0 = XSSR.Next(span0); coord1 = XSSR.Next(span1); frustration++; } if (frustration >= 20) { List <int> iii = new List <int>(span0 * span1), jjj = new List <int>(span0 * span1); for (int i = 0; i < span0; i++) { for (int j = 0; j < span1; j++) { if (test.Equals(mat[i, j])) { iii.Add(i); jjj.Add(j); } } } if (iii.Count == 0) { return(new Position(-1, -1)); } else { int idx = XSSR.Next(iii.Count); return(new Position(iii[idx], jjj[idx])); } } return(new Position(coord0, coord1)); }
//private static XSRandom XSSR = new XSRandom(); public static T RandomElement <T>(this IEnumerable <T> coll) { var list = coll.ToList(); if (!list.Any()) { return(default(T)); } int idx = 0, tgt = XSSR.Next(list.Count()); foreach (T t in list) { if (tgt == idx) { return(t); } idx++; } return(default(T)); }
public Entry() { State s = new State(); s.Entities = new EntityDictionary(); s.Initiative = new Schedule(); s.TurnsLeft = 0; s.DungeonStart = new Dungeon(TilesetType.DEFAULT_DUNGEON, 40, 40); //ROUND_ROOMS_DIAGONAL_CORRIDORS var spawnPoint = s.DungeonStart.LogicWorld.RandomMatch(Dungeon.Floor); Seeker = new Dijkstra(s.DungeonStart.LogicWorld); VisualRandom = new XSRandom(); BeingDamaged = new Dictionary <string, bool>(); // Player.Seeker.SetGoal(Player.Pos.Y, Player.Pos.X); // Player.Seeker.Scan(); for (int i = 0; i < 6; i++) { do { if (s.Entities.Contains(spawnPoint)) { spawnPoint = s.DungeonStart.LogicWorld.RandomMatch(Dungeon.Floor); continue; } Entity player = new Entity("Hero " + ((char)(65 + i)), "@" + WeaponGlyphs[i], _playerColors[i], spawnPoint.Y, spawnPoint.X, 5, 3 + XSSR.Next(4), 0).UpdateStats(health: new Gauge(10), damage: 3); // \u1202 player.Seeker = new Dijkstra(s.DungeonStart.LogicWorld); s.Entities.Add(player); spawnPoint = s.DungeonStart.LogicWorld.RandomMatch(Dungeon.Floor); break; } while (true); } for (int i = 0; i < 25; i++) { spawnPoint = s.DungeonStart.LogicWorld.RandomMatch(Dungeon.Floor); if (spawnPoint.Y >= 0 && !s.Entities.Contains(spawnPoint)) { Entity baddie = new Entity("Baddie " + (char)(65 + i), "b" + IconGlyphs.RandomElement(), _bloodRed, spawnPoint.Y, spawnPoint.X, 4, XSSR.Next(1, 5), -1); baddie.Seeker = new Dijkstra(s.DungeonStart.LogicWorld); // baddie.Seeker.SetGoal(baddie.Pos.Y, baddie.Pos.X); // baddie.Seeker.Scan(); s.Entities.Add(baddie); } } foreach (var k in s.Entities.NameToEntity.Keys) { IEnumerable <Position> foes = s.Entities.NameToEntity.Where(kv => kv.Value.Faction != s.Entities[k].Faction) .Select(e => e.Value.Pos); s.Entities[k] = s.Entities[k].AddEnemies(foes); IEnumerable <Position> friends = s.Entities.NameToEntity.Where(kv => kv.Value.Faction == s.Entities[k].Faction) .Select(e => e.Value.Pos); s.Entities[k] = s.Entities[k].AddAllies(friends); } s.StepsTaken = 0; s.XSSRState = XSSR.GetState(); H.S = s; H.ResetInitiative(); Entity first = H.S.Entities[H.S.Initiative.PeekTurn().Actor]; H.S.CurrentActor = first.Name; H.S.Cursor = first.Pos; H.S.Camera = first.Pos; if (first.Faction == 0) { Seeker.SetGoal(first.Pos.Y, first.Pos.X); Seeker.Scan(first); H.S.CurrentReason = WaitReason.Receiving; H.Remember(); } // Player.Seeker.GetPath(Player.Y, Player.X); }