Tetris on a Die

Discussion in 'Cadence' started by Gustav Gans, Apr 12, 2007.

  1. Gustav Gans

    Gustav Gans Guest

    Open a new layout Window

    put following code into a file and load it from CIW :)

    figures=list(
    list(" * " "***")
    list(" *" "***")
    list("* " "***")
    list("** " " **")
    list(" **" "** ")
    list("" "****")
    )

    procedure(redraw(picture)
    let((i j line cv)
    cv=geGetEditCellView()
    foreach(shape cv~>shapes
    dbDeleteObject(shape)
    )
    for(j 1 length(picture)
    line=nth(j-1 picture)
    for(i 1 strlen(line)
    layer="";
    if((substring(line,i,1) == "R") then layer=list("pin" "drawing"))
    if((substring(line,i,1) == "G") then layer=list("device" "drawing1"))
    if((substring(line,i,1) == "B") then layer=list("wire" "drawing"))
    if((layer != "")
    then
    dbCreateRect(cv,layer,list(i-1:length(picture)-j i:length(picture)-j+1))
    )
    )
    )
    dbCreateRect(cv,"text",list(0:0 screenwidth:screenheight))
    dbCreateLabel(cv,"text",screenwidth/2:screenheight+1 "Tetris","centerCenter","R0","gothic",1)
    dbCreateLabel(cv,"text",0:-0.5 sprintf(nil,"Score:%d",tetris_score),"centerLeft","R0","stick",0.5)
    hiRedraw()
    )
    )

    procedure(place_figure_sub(screen,color,pos)
    let((newscreen i line)
    newscreen=nil
    if((car(pos) < 0 || car(pos) >= screenwidth || cadr(pos) >= screenheight)
    then
    collision=1
    screen
    else

    for(i 1 length(screen)
    line=nth(i-1 screen)
    if((i == cadr(pos)+1)
    then
    if((substring(line,car(pos)+1,1) != " ")
    then
    collision=1
    )
    line1=strcat(substring(line,1,car(pos)) color)
    if((car(pos)+2 <= screenwidth)
    then
    line=strcat(line1 substring(line,car(pos)+2))
    else
    line=line1
    )
    )
    newscreen=cons(line newscreen)
    )
    reverse(newscreen)
    )
    )
    )


    procedure(place_figure(screen,fignum,color,pos,rot)
    let((i j line figure xpos ypos)
    figure=nth(fignum figures)
    for(j 1 length(figure)
    line=nth(j-1 figure)
    for(i 1 strlen(line)
    if((substring(line,i,1) == "*")
    then
    if((rot == 0)
    then
    xpos=car(pos)+i-2
    ypos=cadr(pos)+j-2
    )
    if((rot == 1)
    then
    xpos=car(pos)-j
    ypos=cadr(pos)+i-2
    )
    if((rot == 2)
    then
    xpos=car(pos)-i
    ypos=cadr(pos)-j
    )
    if((rot == 3)
    then
    xpos=car(pos)+j-2
    ypos=cadr(pos)-i
    )
    screen=place_figure_sub(screen,color,xpos:ypos)
    )
    )
    )
    screen
    )
    )

    procedure(tetris_key(dir)
    let((act_pos_bak act_rot_bak screen_new)
    act_pos_bak=act_pos
    act_rot_bak=act_rot
    if((dir == 0)
    then
    act_pos=car(act_pos)-1:cadr(act_pos)
    )
    if((dir == 1)
    then
    act_pos=car(act_pos)+1:cadr(act_pos)
    )
    if((dir == 2)
    then
    act_pos=car(act_pos):cadr(act_pos)-1
    )
    if((dir == 3)
    then
    act_pos=car(act_pos):cadr(act_pos)+1
    )
    if((dir == 4)
    then
    act_rot=modulo(act_rot+1,4)
    )
    if((act_fignum != -1)
    then
    collision=0
    screen_new=place_figure(screen,act_fignum,act_color,act_pos,act_rot)
    if((collision == 1)
    then
    act_pos=act_pos_bak
    act_rot=act_rot_bak
    else
    screen_placed=screen_new
    redraw(screen_placed);
    )
    )
    )
    )

    procedure(tetris_remove_line()
    let((line i count newscreen)
    newscreen=nil
    foreach(line screen
    count=0
    for(i 1 strlen(line)
    if((substring(line,i,1) == " ")
    then
    count=count+1
    )
    )
    if((count > 0)
    then
    newscreen=cons(line newscreen)

    )
    )
    screen=reverse(newscreen)
    while((length(screen) < screenheight)
    screen=cons(car(screen) screen)
    tetris_score=tetris_score+1
    )
    )
    )

    procedure(tetris_timeout()
    tetris_time=tetris_time+1
    tetris_speed=10-tetris_time/100

    if((act_fignum == -1)
    then
    act_fignum=modulo(random(),length(figures))
    act_color=substring("RGB",modulo(random(),3)+1,1)
    act_pos=screenwidth/2:0
    act_rot=modulo(random(),4)
    )
    tetris_key(3)
    if((collision == 1)
    then
    screen=screen_placed
    tetris_remove_line()
    redraw(screen)

    act_fignum=modulo(random(),length(figures))
    act_color=substring("RGB",modulo(random(),3)+1,1)
    act_pos=screenwidth/2:0
    act_rot=modulo(random(),4)
    tetris_key(3)
    if((collision == 1)
    then
    tetris_exit();
    )
    )
    if((tetris_running == 1)
    then
    hiRegTimer("tetris_timeout()",tetris_speed)
    )
    )

    procedure(tetris_init()
    println("init")
    screenwidth=8
    screenheight=12
    screen=nil

    for(i 1 screenheight
    screen=cons(" " screen)
    )
    if((hiGetBindKey("Layout","<Key>Right") != "tetris_key(1)")
    then
    bk_right=hiGetBindKey("Layout","<Key>Right")
    bk_left=hiGetBindKey("Layout","<Key>Left")
    bk_up=hiGetBindKey("Layout","<Key>Up")
    bk_down=hiGetBindKey("Layout","<Key>Down")
    bk_escape=hiGetBindKey("Layout","<Key>Escape")
    hiSetBindKey("Layout","<Key>Right","tetris_key(1)")
    hiSetBindKey("Layout","<Key>Left","tetris_key(0)")
    hiSetBindKey("Layout","<Key>Up","tetris_key(4)")
    hiSetBindKey("Layout","<Key>Down","tetris_key(3)")
    hiSetBindKey("Layout","<Key>Escape","tetris_exit()")
    )


    act_fignum=-1
    tetris_running=1
    tetris_speed=10
    tetris_score=0
    tetris_time=0
    redraw(screen)
    hiRegTimer("tetris_timeout()",tetris_speed)
    )

    procedure(tetris_exit()
    println("exit")
    tetris_running=0
    if((bk_right != nil) then hiSetBindKey("Layout","<Key>Right",bk_right))
    if((bk_left != nil) then hiSetBindKey("Layout","<Key>Left",bk_left))
    if((bk_up != nil) then hiSetBindKey("Layout","<Key>Up",bk_up))
    if((bk_down != nil) then hiSetBindKey("Layout","<Key>Down",bk_down))
    if((bk_escape != nil) then hiSetBindKey("Layout","<Key>Escape",bk_escape))
    dbCreateLabel(cv,"text",screenwidth/2:screenheight/2 "Game Over","centerCenter","R90","gothic",1)
    )

    tetris_init()


    Have fun!
     
    Gustav Gans, Apr 12, 2007
    #1
  2. Neat! But you really need to get out more ;-)

    Andrew.
     
    Andrew Beckett, Apr 12, 2007
    #2
  3. Gustav Gans

    Gustav Gans Guest

    Have fun!
    I dont intend to lose my job coding the most expensive tetris in the world
     
    Gustav Gans, Apr 12, 2007
    #3
  4. Gustav Gans

    camelot Guest

    very funny!!
    Did you write any other funny code?!?!

    Camelot
     
    camelot, Apr 13, 2007
    #4
  5. Gustav Gans

    S. Badel Guest

    I have noticed my layout editor sometimes slows down when I play tetris. Should we file a PCR ? :)

    Funny piece of code!

    Stéphane
     
    S. Badel, Apr 13, 2007
    #5
  6. Gustav Gans

    Tim Guest

    It doesn't seem to work for me. I am running IC5141 Solaris, on a PC
    using Exceed on Demand to the Unix server.

    Did anyone else have a problem?

    Thanks,
    Tim
     
    Tim, Apr 13, 2007
    #6
  7. Gustav Gans

    Tim Guest

    I got it working (great!).

    It wouldn't work with the PDK I had loaded so I opened Cadance "out of
    the box".

    Thanks,
    Tim
     
    Tim, Apr 13, 2007
    #7
  8. Excellent, now I can finish my manual routing through a maze thingy !
    Was just missing the keyhandling stuff, thanks !


    Robbin
     
    robbin.bonthond, Apr 18, 2007
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.