HIGH - LOW: a simple game (SAS program)
--------------------------------------------------------------------------------
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
This sample is from the SAS Sample Library. For additional information refer to SAS Help and Online Documentation.
--------------------------------------------------------------------------------
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: SUGIHILO */
/* TITLE: HIGH - LOW: A Simple Game */
/* PRODUCT: SAS */
/* SYSTEM: all */
/* KEYS: DUTIL INTER DATASTEP WINDOW DISPLAY DO WHILE */
/* FUNCTION NORMAL */
/* PROCS: */
/* DATA: */
/* */
/* SUPPORT: UPDATE: */
/* REF: */
/* MISC: An enhanced version of HILO, from SUGI85 */
/* THIS SHOULD NOT BE RUN IN BATCH. */
/* UPDATE: */
/****************************************************************/
/* A datastep that plays the bar-game 'hilo': */
/* */
/* Someone (the computer) guesses a number, and the player has */
/* to guess what the computer guessed. */
data _null_ ;
length more $ 1 chilo $ 6;
format tries 3.;
guess=.;number=.;last=.;more='Y';savehi=.;savelo=.;grange=.;
window rules color=blue rows=24 columns=80 irow=1 icolumn=1
group=rules
#1 @32 'The Rules of this game are easy ' color=yellow
#2 @32 '******************************* ' color=red
#3 @32 ' ' color=yellow
#4 @32 'First, I guess a number... ' color=green
#5 @32 ' ' color=yellow
#6 @32 'Then you try to guess what I guessed ' color=yellow
#7 @32 ' while I give you clues as to whether ' color=yellow
#8 @32 ' your previous guess was too HIGH or LOW ' color=yellow
#10 @32 'First Guess?' color=white
+2 guess color=white
;
window cheat color=blue rows=1 columns=10 irow=19 icolumn=1
group=cheat
#1 @1 number protect=yes color=white
;
window high color=black rows=8 columns=28 irow=1 icolumn=1
group=empty ' '
group=guess
#1 @2 '-->' color=yellow
+2 last color=red protect=yes
#2 @2 'IS TOO HIGH' color=yellow attr=blink
#4 @2 'Next Guess?' color=green
+2 guess color=yellow
;
window low color=black rows=8 columns=30 irow=17 icolumn=51
group=empty ' '
group=guess
#1 @2 '-->' color=yellow
+2 last color=red protect=yes
#2 @2 'IS TOO LOW' color=yellow attr=blink
#4 @2 'Next Guess?' color=green
+2 guess color=yellow
;
window youwin color=red rows=9 columns=45 irow=14 icolumn=3
group=youwin
#2 @9 '... but it took you' color=white
+1 tries color=white protect=yes
'attempts,' color=white
#3 @13 'did it not ?' color=white
#5 @9 'Another Game?' color=yellow
+2 more color=yellow
;
window youlose color=yellow rows=11 columns=49 irow=15 icolumn=1
group=range
#3 @10 'You guessed' color=red
+1 last color=red protect=yes
#4 @10 '... but I already told you' color=red
+1 grange color=red protect=yes
#5 @14 'was too' color=red
+1 chilo color=red protect=yes
#7 @10 'Another Game?' color=black
+2 more color=black
;
/* Play the game, again and again. */
again:
number=int(1000*normal(0)); /* Choose a number. */
tries=0;
guess=.;last=guess;
savehi=999999;
savelo=0-999999;
continue=1;
do while (guess=.) ;
display rules.rules blank ;
end;
if guess=27511 then display cheat.cheat blank noinput;
last=guess; guess=.;
/* Yeah, I know that spoils it. */
*put / 'After First Guess' /_all_;
do while ( continue=1 );
tries+1;
if last=number then continue=0;
else if last <= savelo then do;
continue=0-1; grange=savelo; chilo='LOW !'; guess=last;
end;
else if last >= savehi then do;
continue=0-1; grange=savehi; chilo='HIGH !'; guess=last;
end;
else if last < number then do;
savelo=last;
do while (guess=.);
display low.guess;
end;
display low.empty blank noinput;
end;
else if last > number then do;
savehi=last;
do while (guess=.);
display high.guess;
end;
display high.empty blank noinput;
end;
*put / 'After nth guess' / _all_;
if guess=27511 then display cheat.cheat blank noinput;
last=guess; guess=.;
end; /* of do while(continue=1) */
*put / 'After do while continue' / _all_;
if continue=0 then do;
_msg_=' So, you win again.';
display youwin.youwin;
end;
else do;
_msg_=' YOU LOSE !!! (The number was '||
trim(left(put(number,6.)))||')';
display youlose.range;
end;
if more='Y' or more='y' then goto again;
stop;
run /* pgm=hilo */;