home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / litesout.zip / litesout.pas < prev    next >
Pascal/Delphi Source File  |  1995-11-04  |  6KB  |  352 lines

  1. program litesout;
  2.  
  3. {$IFNDEF VIRTUALPASCAL}  { DOS version: Use Turbo Pascal 6.0+ to compile }
  4.  
  5. uses
  6.  crt,drivers;
  7.  
  8. {$ELSE}                 { OS/2 version: use Virtual Pascal }
  9.  
  10. uses
  11.  crt,drivers,os2base;
  12.  
  13. {$ENDIF}
  14.  
  15. const
  16.  xelems=5;
  17.  yelems=5;
  18.  xwide=10;
  19.  ywide=4;
  20.  stoffset=15;
  21.  
  22.  toomany=500;
  23.  deltime=3000;
  24.  
  25.  restx1=-1;
  26.  restx2=0;
  27.  resty1=0;
  28.  resty2=(yelems div 2)-1;
  29.  colx1=-1;
  30.  colx2=0;
  31.  coly1=(yelems div 2)+1;
  32.  coly2=yelems-1;
  33.  quitx1=xelems;
  34.  quitx2=10;
  35.  quity1=2;
  36.  quity2=yelems-3;
  37.  
  38.  qwide=xwide-3;
  39.  qdeep=ywide-1;
  40.  xgap=(80-(xelems*xwide)) div 2;
  41.  ygap=(25-(yelems*ywide)) div 2+1;
  42.  
  43.  plotit:array[false..true] of string[qwide]=('       ','███████');
  44.  games:integer=-1;
  45.  wins:word=0;
  46.  
  47.  version='1.0';
  48.  
  49. var
  50.  lites:array[0..xelems-1,0..yelems-1] of boolean;
  51.  x,y,z,q:integer;
  52.  lights:byte;
  53.  
  54.  clicks:word;
  55.  totclicks:longint;
  56.  blockcol:byte;
  57.  
  58.  c:char;
  59.  
  60. {$IFNDEF VIRTUALPASCAL}  { DOS version: Use Turbo Pascal 6.0+ to compile }
  61.  
  62. procedure slice; assembler;
  63.  asm
  64.             int $28
  65.  end;
  66.  
  67. {$ELSE}                 { OS/2 version: use Virtual Pascal }
  68.  
  69. procedure slice;
  70.  var
  71.   foo:word;
  72.  begin
  73.   foo:=dossleep(10)
  74.  end;
  75.  
  76. {$ENDIF}
  77.  
  78. function mousecheck:boolean;
  79.  begin
  80.   if buttoncount<>0 then
  81.    begin
  82.     initvideo;
  83.     initevents;
  84.     mousecheck:=true
  85.    end
  86.   else mousecheck:=false
  87.  end;
  88.  
  89. function mouseg:boolean;
  90.  var
  91.   mvent:tevent;
  92.  begin
  93.   getmouseevent(mvent);
  94.   mouseg:=(mousebuttons=mbleftbutton)
  95.  end;
  96.  
  97. procedure display(x,y:integer);
  98.  begin
  99.   textcolor(blockcol);
  100.   for z:=1 to qdeep do
  101.    begin
  102.     gotoxy(x*xwide+xgap+1,y*ywide+ygap+z);
  103.     write(plotit[lites[x,y]])
  104.    end
  105.  end;
  106.  
  107. procedure toggle(x,y:integer);
  108.  begin
  109.   if ((x>=0) and (x<xelems)) and ((y>=0) and (y<yelems)) then
  110.    begin
  111.     lites[x,y]:=not(lites[x,y]);
  112.     display(x,y)
  113.    end
  114.  end;
  115.  
  116. procedure hitone(x,y:integer);
  117.  begin  
  118.   toggle(x,y);
  119.   toggle(x-1,y);
  120.   toggle(x,y-1);
  121.   toggle(x,y+1);
  122.   toggle(x+1,y)
  123.  end;
  124.  
  125. procedure refresh;
  126.  begin
  127.   for x:=0 to xelems-1 do
  128.    for y:=0 to yelems-1 do
  129.     display(x,y)
  130.  end;
  131.  
  132. function victory:boolean;
  133.  var
  134.   none:boolean;
  135.  begin
  136.   none:=true;
  137.   lights:=0;
  138.   for x:=0 to xelems-1 do
  139.    for y:=0 to yelems-1 do
  140.     if lites[x,y] then
  141.      begin
  142.       inc(lights);
  143.       none:=false
  144.      end;
  145.   victory:=none
  146.  end;
  147.  
  148. function within(a,b,c,d:integer):boolean;
  149.  begin
  150.   within:=((x>=a) and (x<=b)) and ((y>=c) and (y<=d))
  151.  end;
  152.  
  153. function inrange:boolean;
  154.  begin
  155.   inrange:=within(0,xelems-1,0,yelems-1)
  156.  end;
  157.  
  158. procedure init;  { Junk init -- randomize }
  159.  begin
  160.   lights:=0;
  161.   for x:=0 to xelems-1 do
  162.    for y:=0 to yelems-1 do
  163.     begin
  164.      lites[x,y]:=(random(2)=1);
  165.      if lites[x,y] then inc(lights)
  166.     end
  167.  end;
  168.  
  169. procedure box;
  170.  begin
  171.   gotoxy(xgap-1,ygap);
  172.   write('╔');
  173.   for x:=1 to xelems do
  174.    begin
  175.     for y:=1 to xwide-1 do write('═');
  176.     if x<>xelems then write('╤')
  177.    end;
  178.   write('╗');
  179.   for x:=1 to yelems do
  180.    begin
  181.     for y:=1 to ywide-1 do
  182.      for z:=0 to xelems do
  183.       begin
  184.        gotoxy(xgap+z*xwide-1,ygap+(x-1)*ywide+y);
  185.        if (z=0) or (z=xelems) then write('║')
  186.        else write('│')
  187.       end;
  188.     if x<>yelems then
  189.      begin
  190.       gotoxy(xgap-1,ygap+x*ywide);
  191.       write('╟');
  192.       for z:=1 to xelems do
  193.        begin
  194.         gotoxy(xgap+(z-1)*xwide,ygap+x*ywide);
  195.         for q:=1 to xwide-1 do write('─');
  196.         if z<>xelems then write('┼')
  197.         else write('╢')
  198.        end
  199.      end
  200.    end;
  201.   gotoxy(xgap-1,ygap+yelems*ywide);
  202.   write('╚');
  203.   for x:=1 to xelems do
  204.    begin
  205.     for y:=1 to xwide-1 do write('═');
  206.     if x<>xelems then write('╧')
  207.    end;
  208.   write('╝')
  209.  end;
  210.  
  211. procedure spectext;
  212.  begin
  213.   gotoxy((xgap-7) div 2,6);
  214.   write('Restart');
  215.   gotoxy((xgap-6) div 2,18);
  216.   write('Color');
  217.   gotoxy((xgap-6) div 2,19);
  218.   write('Change');
  219.   gotoxy(xgap+xelems*xwide+((xgap-4) div 2),12);
  220.   write('Quit')
  221.  end;
  222.  
  223. procedure statline;
  224.  begin
  225.   gotoxy(stoffset,ygap+yelems*ywide+2);
  226.   write('Clicks:        Lights:      Games:       Wins:')
  227.  end;
  228.  
  229. procedure statup;
  230.  begin
  231.   textcolor(7);
  232.   gotoxy(stoffset+8,ygap+yelems*ywide+2);
  233.   write(clicks:5);
  234.   gotoxy(stoffset+23,ygap+yelems*ywide+2);
  235.   write(lights:3)
  236.  end;
  237.  
  238. procedure gameup;
  239.  begin
  240.   textcolor(7);
  241.   gotoxy(stoffset+35,ygap+yelems*ywide+2);
  242.   write(games:3);
  243.   gotoxy(stoffset+46,ygap+yelems*ywide+2);
  244.   write(wins:3)
  245.  end;
  246.  
  247. procedure restart;
  248.  begin
  249.   inc(games);
  250.   gameup;
  251.   blockcol:=random(15)+1;
  252.   clicks:=0;
  253.   init;
  254.   refresh
  255.  end;
  256.  
  257. procedure nextcol;
  258.  begin
  259.   inc(blockcol);
  260.   if blockcol=16 then blockcol:=1;
  261.   refresh
  262.  end;
  263.  
  264. function ontarget:boolean;
  265.  begin
  266.   ontarget:=true;
  267.   if not inrange then
  268.    if within(restx1,restx2,resty1,resty2) then restart
  269.    else if within(colx1,colx2,coly1,coly2) then nextcol
  270.         else if within(quitx1,quitx2,quity1,quity2) then ontarget:=false
  271.  end;
  272.  
  273. procedure sinit;
  274.  begin
  275.   clrscr;
  276.   write('      Lights Out! v'+version+
  277.    '  written by William McBrine  wmcbrine@clark.net');
  278.   box;
  279.   spectext;
  280.   statline
  281.  end;
  282.  
  283. procedure warn;
  284.  begin
  285.   gotoxy(1,ygap+yelems*ywide+3);
  286.   textcolor(7);
  287.   writeln;
  288.   writeln;
  289.   writeln('Don''t you think you should rest your arm now?'#7);
  290.   delay(deltime);
  291.   sinit;
  292.   refresh;
  293.   statup;
  294.   gameup
  295.  end;
  296.  
  297. procedure main;
  298.  begin
  299.   restart;
  300.   showmouse;
  301.   repeat
  302.    statup;
  303.    while not(mouseg) do slice;
  304.    repeat until not(mouseg);
  305.    x:=mousewhere.x-xgap+1;
  306.    if x>=0 then x:=x div xwide
  307.    else x:=-1;
  308.    y:=mousewhere.y-ygap+1;
  309.    if y>=0 then y:=y div ywide
  310.    else y:=-1;
  311.    if inrange then 
  312.     begin
  313.      inc(clicks);
  314.      inc(totclicks);
  315.      hidemouse;
  316.      hitone(x,y);
  317.      if (totclicks>0) and ((totclicks mod toomany)=0) then warn;
  318.      showmouse
  319.     end
  320.   until not(ontarget) or victory
  321.  end;
  322.  
  323. begin
  324.  randomize;
  325.  if not(mousecheck) then
  326.   begin
  327.    writeln('Sorry, this program requires a mouse.');
  328.    halt(1)
  329.   end
  330.  else
  331.  repeat
  332.   c:='N';
  333.   sinit;
  334.   main;
  335.   hidemouse;
  336.   statup;
  337.   if victory then
  338.    begin
  339.     inc(wins);
  340.     gotoxy(1,ygap+yelems*ywide+3);
  341.     textcolor(7);
  342.     writeln;
  343.     writeln;
  344.     writeln('Congratulations! You solved it!!'#7);
  345.     write('Play again? (Y/n) ');
  346.     while not keypressed do slice;
  347.     c:=upcase(readkey)
  348.    end
  349.  until c='N';
  350.  donevideo
  351. end.
  352.