diff --git a/test/tetris.nas b/test/tetris.nas index 1578cb4..6137ddf 100644 --- a/test/tetris.nas +++ b/test/tetris.nas @@ -1,79 +1,6 @@ import("lib.nas"); import("module/libkey.nas"); -var mapgen=func(mapx,mapy){ - if(mapx<1 or mapy<1) - die("map_x or map_y must be greater than 1"); - # use in print - var table="\e[44m "; - for(var i=0;i0.9); - append(map,tmp); - } - - # color print - var front=[ - "31","32","33","34","35","36", - "91","92","93","94","95","96", - ]; - var map_print=func(){ - var s="\e[1;1H"~table; - for(var y=0;y=1;x-=1) - map[y][x]=map[y][x-1]; - map[y][0]=tmp; - } - } - - var rotate=func(block){ - - } - - var setblock=func(block){ - - } - - var checkmap=func(block){ - map_print(map); - } - return { - print:map_print, - moveleft:moveleft, - moveright:moveright, - rotate:rotate, - setblock:setblock, - checkmap:checkmap - }; -} - var blocktype=[ [0,1,2,3], [4,5,6,7], @@ -95,82 +22,239 @@ var blockshape=[ # [] [] [] [] # [] [][] [][][] [[0,0],[1,0],[1,1],[1,2]], - [[0,0],[],[],[]], - [[0,0],[],[],[]], - [[0,0],[],[],[]], + [[0,0],[1,0],[2,0],[0,1]], + [[0,0],[0,1],[0,2],[1,2]], + [[0,0],[0,1],[-1,1],[-2,1]], # [] [] [][][] [] # [][][] [][] [] [][] # [] [] - [[0,0],[],[],[]], - [[0,0],[],[],[]], - [[0,0],[],[],[]], - [[0,0],[],[],[]], + [[0,0],[0,1],[-1,1],[1,1]], + [[0,0],[0,1],[-1,1],[0,2]], + [[0,0],[-1,0],[1,0],[0,1]], + [[0,0],[0,1],[0,2],[1,1]], # [] [][][][] # [] # [] # [] - [[0,0],[],[],[]], - [[0,0],[],[],[]], + [[0,0],[0,1],[0,2],[0,3]], + [[0,0],[1,0],[2,0],[3,0]], # [][] # [][] - [[0,0],[],[],[]], + [[0,0],[1,0],[0,1],[1,1]], # [] [][] # [][] [][] # [] - [[0,0],[],[],[]], - [[0,0],[],[],[]], + [[0,0],[0,1],[-1,1],[-1,2]], + [[0,0],[1,0],[1,1],[2,1]], # [] [][] # [][] [][] # [] - [[0,0],[],[],[]], - [[0,0],[],[],[]] + [[0,0],[0,1],[1,1],[1,2]], + [[0,0],[1,0],[0,1],[-1,1]] ]; var block={ x:0, y:0, + rotate:0, type:nil, shape:nil, new:func(x=0,y=0){ (me.x,me.y)=(x,y); + me.rotate=0; me.type=blocktype[rand()*size(blocktype)]; - me.shape=blockshape[me.type][0]; + me.shape=blockshape[me.type[me.rotate]]; return {parents:[block]}; } }; +var mapgen=func(mapx,mapy){ + var score=0; + var (empty,unset,full)=(0,1,2); + + if(mapx<1 or mapy<1) + die("map_x or map_y must be greater than 1"); + # use in print + var table="\e[44m "; + for(var i=0;i=mapx) + return; + if(map[y+i[1]][x+i[0]]==full) + return; + } + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=empty; + blk.x=x; + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=unset; + map_print(); + } + + var rotate=func(){ + var (r,x,y)=(blk.rotate,blk.x,blk.y); + r=(r+1>=size(blk.type))?0:r+1; + var shape=blockshape[blk.type[r]]; + foreach(var i;shape){ + if(x+i[0]>=mapx or x+i[0]<0 or y+i[1]>=mapy or y+i[1]<0) + return; + if(map[y+i[1]][x+i[0]]==full) + return; + } + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=empty; + blk.rotate=r; + blk.shape=shape; + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=unset; + map_print(); + } + + var fall=func(){ + var (x,y)=(blk.x,blk.y+1); + var sethere=0; + foreach(var i;blk.shape){ + if(y+i[1]>=mapy or map[y+i[1]][x+i[0]]==full){ + sethere=1; + break; + } + } + if(sethere){ + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=full; + checkmap(); + new_block(); + map_print(); + return; + } + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=empty; + blk.y=y; + foreach(var i;blk.shape) + map[blk.y+i[1]][blk.x+i[0]]=unset; + map_print(); + } + + var checkmap=func(){ + for(var y=mapy-1;y>=0;y-=1){ + for(var x=0;x=1;t-=1) + for(var x=0;x