using System; using S = System.String; using System.Collections.Generic; partial class M { Dictionary d = new Dictionary(); List e = new List(); int p = 9, m = 999, x, y, z, u, v, i, j, k, h; char a = '@', w = '#', f = '.', q = '$', c; Random r = new Random(); M() { x = y = h = p; //Add enemies for (i = 0; i < m; i++) { e.Add(K(r.Next(m) - 499, r.Next(m) - 499)); d[e[e.Count - 1]] = 'M'; } //set player start d[X] = a; //get keyboard input k = I; while (k > 0) { //save player co-ords u = x; v = y; //handle keyboard return code x = k > 2 && k < 20 ? x - 1 : k > 12 ? x + 1 : x; y = k % 2 > 0 && k % 10 != 0 ? y - 1 : k % 2 == 0 && k % 10 != 0 ? y + 1 : y; //get char at new location c = T(X); //if player hits an enemy if (e.Contains(X)) { //kill the enemy e.Remove(X); d[X] = f; } //reset the cursor to top left R(); //win condition met? if (c == q) return; //if player is somewhere other than floor reset co-ords if (c != f) { x = u; y = v; } //set floor at player's old location and player at new location d[K(u, v)] = f; d[X] = a; //iterate through enemies for (z = 0; z < e.Count; z++) { //store enemy's co-ords i = U(e[z], 0); j = U(e[z], 1); //move towards player u = i < x ? i + 1 : i > x ? i - 1 : i; v = j < y ? j + 1 : j > y ? j - 1 : j; //get char at new co-ord c = T(K(u, v)); //move to new co-ord if it's a floor tile if (c == f) { e[z] = K(u, v); d[e[z]] = d[K(i, j)]; d[K(i, j)] = f; } //if hitting player, deduct player health if (c == a) h--; } //player dead if (h < 1) return; //calculate viewport size from p z = p * 2 - 1; //draw current state to viewport for (j = 0; j < z; j++) { for (i = 0; i < z; i++) { W(T(K(i - p + x, j - p + y))); } L(); } //draw health L(h); //get keyboard input k = I; } } //converts an x y co-ord to a dictionary key S K(int x, int y) { return S.Format("{0}_{1}", x, y); } //returns string for most commonly used key S X { get { return K(x, y); } } //generate a map tile char T(S s) { return d.ContainsKey(s) ? d[s] : d[s] = r.Next(m * p) < 2 ? q : r.Next(9) < 8 ? f : w; } //converts a key to an x or y co-ord int U(S s, int l) { return Int32.Parse((s.Split('_'))[l]); } static void Main() { new M(); } }