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; S a = "@", w = "#", f = ".", s = ">", c; Random r = new Random( ); M( int k, int l, int h ) { //determine number of enemies u = m * l; //add enemies while ( i++ < u ) { e.Add( ( r.Next( m ) - 499 ) + a + ( r.Next( m ) - 499 ) ); d[ e[ e.Count - 1 ] ] = "M"; } //set player start d[ X ] = a; //k will be 0 if player presses Q or ESC while ( k > 0 ) { //store 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 the string at the new location c = T( X ); //if player is hitting an enemy if ( e.Contains( X ) ) { //kill the enemy e.Remove( X ); d[ X ] = f; } //reset the cursor position R( ); //if the player is standing on stairs if ( c == s ) { //reset the dungeon and enemies d.Clear( ); e.Clear( ); //new dungeon new M( 1, l + 1, h ); //if we get here player has died, exit return; } //if the player tried to move onto something that isn't a floor, put them back where they were if ( c != f ) { x = u; y = v; } //set the last tile that the player was on to floor d[ u + a + v ] = f; //set the tile that the player is currently on to @ d[ X ] = a; //iterate through the remaining enemies for ( z = 0; z < e.Count; z++ ) { //get the current enemy's location and store their X position in i and their Y position in j i = U( e[ z ], 0 ); j = U( e[ z ], 1 ); //move towards the player u = i < x ? i + 1 : i > x ? i - 1 : i; v = j < y ? j + 1 : j > y ? j - 1 : j; //get the dungeon tile at the new enemy coordinate c = T( u + a + v ); //if the dungeon tile is floor, move the enemy onto the new coordinate if ( c == f ) { e[ z ] = u + a + v; d[ e[ z ] ] = d[ i + a + j ]; d[ i + a + j ] = f; } //if the dungeon tile the enemy tried to move to contains the player, decrement the player's health if ( c == a ) h--; } //exit if player is dead if ( h < 1 ) return; //calculate the viewport size using p z = p * 2 - 1; //draw visible dungeon tiles to viewport for ( j = 0; j < z; j++ ) { for ( i = 0; i < z; i++ ) { //get tile at location X = i, Y = j c = T( ( i - p + x ) + a + ( j - p + y ) ); //if it's a wall pass the level through to the IO call for the color, otherwise pass through 0 to use default color W( c, c == w ? l : 0 ); } L( ); } //show level and health L( l + f + h ); //get keyboard input k = I; } } //return the player's coordinates as a string S X { get { return x + a + y; } } //get the dungeon tile at the passed in coordinate. If the tile doesn't exist create it S T( S c ) { return d.ContainsKey( c ) ? d[ c ] : d[ c ] = r.Next( m * p ) < 5 ? s : r.Next( 9 ) < 7 ? f : w; } //convert a string coordinate to an x or a y coordinate - if l is 0, return x, if l is 1 return y int U( S c, int l ) { return int.Parse( ( c.Split( '@' ) )[ l ] ); } //start a new game on level 1 with 9 health static void Main( ) { new M( 1, 1, 9 ); } }