C# <1kB RogueLike v3

openSUSE Linux

screenshot of C# 1kB RougueLike running in Linux

Windows

screenshot of C# 1kB RougueLike running in Windows

What is it?

A RogueLike game written in less than 1kB of C# source. Written for the rec.games.roguelike.development <1kB RL Challenge:

First <1kb RL Challenge

Note that for the challenge we were allowed to use a separate IO library, also included below.

Features:

Download:

1kRl.exe

Program.cs

IO.cs

Instructions:

Any key to begin; arrow keys, numpad keys or vi keys to move, q or [ESC] to quit. Your health is at the bottom. If your health reaches 0 or you find the $ the game is over.

Code:

using System;using S=System.String;using System.Collections.Generic;partial
class M{Dictionary<S,char>d=new Dictionary<S,char>();List<S>e=new List<S>()
;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;for(i=0;i<m;i++){e.Add(K(r.Next(m)-499,r.Next(m)-
499));d[e[e.Count-1]]='M';}d[S]=a;k=I;while(k!=0){u=x;v=y;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;c=T(S);if(e.Contains(
S)){e.Remove(S);d[S]=f;}R();if(c==q)return;if(c!=f){x=u;y=v;}d[K(u,v)]=f;d[
S]=a;for(z=0;z<e.Count;z++){i=U(e[z],0);j=U(e[z],1);u=i<x?i+1:i>x?i-1:i;v=j
<y?j+1:j>y?j-1:j;c=T(K(u,v));if(c==f){e[z]=K(u,v);d[e[z]]=d[K(i,j)];d[K(i,j
)]=f;}if(c==a)h--;}if(h<1)return;z=p*2-1;for(j=0;j<z;j++){for(i=0;i<z;i++){
W(T(K(i-p+x,j-p+y)));}L();}L(h);k=I;}}S K(int x,int y){return S.Format(
"{0}_{1}",x,y);}S S{get{return K(x,y);}}char T(S s){return d.ContainsKey(s)
?d[s]:d[s]=r.Next(m*p)<2?q:r.Next(9)<8?f:w;}int U(S s,int l){return 
Int32.Parse((s.Split('_'))[l]);}static void Main(){new M();}}

Pretty printed version – more readable! now with (minimal) comments!:

ProgramPretty.cs

Pretty printed code:

using System;
using S = System.String;
using System.Collections.Generic;
partial class M
{
    Dictionary<S, char> d = new Dictionary<S, char>();
    List<S> e = new List<S>();

    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(); }
}