I decided to make a roguelike for fun. Here's the first part, a building generator (in the link at the bottom of the post, each rectangle represents a building):
var mm=millis() //var mm2=0 var rooms=[] var S=63 //What's a better name for S? S+=S%2 //forces S to be even for(var i=0;i<S;i++){ rooms[i]=[] for(var j=0;j<S;j++){ rooms[i][j]=Infinity } } var pick=function(array){ var queue=[] //var mm3=millis() for(var i=1;i<S-1;i++){ for(var j=1;j<S-1;j++){ if(rooms[i][j]==Infinity){ var t = min(min(rooms[i-1][j],rooms[i][j-1]), min(rooms[i+1][j],rooms[i][j+1])) if(t<Infinity){ queue.push([i,j,pow(S,2)]) } } } } //mm2+=millis()-mm3 return queue[~~(Math.random()*queue.length)] } var createRoom=function(pos){ rooms[pos[0]][pos[1]]=pos[2] } var drawRooms=function(){ pushMatrix() //scale(4.0) colorMode(HSB) background(255, 0, 255) noStroke() for(var i=0;i<S;i++){ for(var j=0;j<S;j++){ if(rooms[i][j]!==Infinity){ fill(rooms[i][j]*10,255,255) rect(i*width/S,j*height/S,ceil(width/S),ceil(height/S)) } } } popMatrix() } var redo=function(x,y,t){ if(rooms[x][y]!==Infinity&&rooms[x][y]>t){ rooms[x][y]=t t++ redo(x+1,y,t) redo(x,y+1,t) redo(x-1,y,t) redo(x,y-1,t) } } createRoom([S/2,S/2,1]) for(var i=0;i<pow(S,2)/10;i++){ createRoom(pick()) } redo(S/2,S/2,0) drawRooms() fill(0) var benchmark=millis()-mm text('Took '+benchmark+' milliseconds to run.',10,10)
I would have done it in processing.py, but I had some trouble with matrices. If anyone knows how to use them in vanilla python, feel free to give me an explanation.
Lastly, here's the link I mentioned if you want to have a look at it:
https://www.khanacademy.org/computer-programming/roguelike-room-generator/6424540231008256