Greed: Difference between revisions
Content deleted Content added
Added Kotlin |
→{{header|REXX}}: added the REXX computer programming language. |
||
Line 345: | Line 345: | ||
See [[Greed/Java]]. |
See [[Greed/Java]]. |
||
<br><br> |
|||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program lets a user play the game of GREED from the console (terminal). */ |
|||
parse arg sw sd @ b ?r . /*obtain optional argumenst from the CL*/ |
|||
if sw=='' | sw=="," then sw=79 /*cols specified? Then use the default*/ |
|||
if sd=='' | sd=="," then sd=22 /*rows " " " " " */ |
|||
if @=='' | @=="," then @= '@' /*here " " " " " */ |
|||
if b=='' | b=="," then b= ' ' /*blank " " " " " */ |
|||
if datatype(?r, 'W') then call random ,,?r /*maybe use a seed for the RANDOM BIF*/ |
|||
if length(@)==2 & datatype(@,'X') then @=x2c(@) /*maybe use @ char for current pos. */ |
|||
if length(b)==2 & datatype(b,'X') then b=x2c(b) /* " " B char for background. */ |
|||
call init |
|||
do until # == sw*sd /*keep playing until the grid is blank.*/ |
|||
call show 1 /*show the playing field (grid) to term*/ |
|||
call ask /*obtain user's move, validate, or quit*/ |
|||
if \move() then leave /*perform the user's move as per @ loc.*/ |
|||
end /*until*/ /* [↑] Also, if out─of─bounds, LEAVE. */ |
|||
if show(1)==sw*sd then say ____ "You've won, the grid is blank, your score is: " # |
|||
if @.!r.!c==@. then say ____ "Game over (out─of─bounds), your score is: " # |
|||
if @.!r.!c==b then say ____ "Game over (a blank location), your score is: " # |
|||
exit 2 |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
ask: do forever /*play 'til done, or no possible moves.*/ |
|||
say ____ 'moves:' ____ ' Q= ◄↑ W= ↑ E= ►↑' |
|||
say ____ 'moves:' ____ ' A= ◄ D= ►' |
|||
say ____ 'moves:' ____ ' Z= ◄↓ X= ↓ C= ►↓' |
|||
say ____ |
|||
say ____ 'enter a move ──or── enter QUIT to quit. (score is: ' #")" |
|||
parse pull $ 2 1 quit . 1 o$; upper $ quit; |
|||
if abbrev('QUIT', quit, 2) | abbrev('QQUIT', quit, 2) then leave |
|||
if length( space(o$) )==1 & pos($, 'QWEADZXC')\==0 then return |
|||
say ____ '***error*** invalid move:' space(o$); say |
|||
end /*forever*/ |
|||
halt: say; say ____ 'quitting.'; exit 1 |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
init: @.= 'Ω'; ____= copies('─', 8) /*out─of─bounds literal; fence for SAYs*/ |
|||
signal on halt /*handle pressing of Ctrl-Break key. */ |
|||
do r=1 for sd |
|||
do c=1 for sw; @.r.c=random(1, 9) /*assign grid area to random digs (1►9)*/ |
|||
end /*c*/ |
|||
end /*r*/ |
|||
!r=random(1, sd); !c=random(1, sw); @.!r.!c=@; return /*assign 1st position*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
move: @.!r.!c=b /*blank out this "start" position. */ |
|||
@@=. /*nullify the count of move positions. */ |
|||
do until @@==0; select |
|||
when $== 'Q' then do; !r=!r - 1; !c=!c - 1; end |
|||
when $== 'W' then !r=!r - 1 |
|||
when $== 'E' then do; !r=!r - 1; !c=!c + 1; end |
|||
when $== 'A' then !c=!c - 1 |
|||
when $== 'D' then !c=!c + 1 |
|||
when $== 'Z' then do; !r=!r + 1; !c=!c - 1; end |
|||
when $== 'X' then !r=!r + 1 |
|||
when $== 'C' then do; !r=!r + 1; !c=!c + 1; end |
|||
end /*select*/ |
|||
?=@.!r.!c; if ?==@. | ?==b then return 0 /*cease if out─of─bounds or blank*/ |
|||
if @@==. then @@=?; if datatype(@@, 'W') then @@=@@ - 1 /*diminish cnt.*/ |
|||
@.!r.!c=b /*blank out a single grid position. */ |
|||
end /*until*/ |
|||
@.!r.!c=@; return 1 /*signify current grid position with @ */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
show: arg tell; #=0; if tell then do; 'CLS'; say left('',max(0,!c-1))"↓"; end |
|||
do r=1 for sd; _= /* [↑] DOS cmd CLS clears the screen*/ |
|||
do c=1 for sw; ?=@.r.c; _=_ || ? /*construct row of the grid for display*/ |
|||
if ?==b | ?==@ then #=# + 1 /*Position==b ─or─ @? Then bump score.*/ |
|||
end /*c*/ |
|||
if r==!r then _=_ '◄' /*indicate row of current position. */ |
|||
if tell then say _ /*display a row of grid to screen. */ |
|||
end /*r*/; say; return # /*SHOW also counts # of blanks (score).*/</lang> |
|||
A note on the OUTPUT sections: each screen displayed is shown below as a separate OUTPUT section. |
|||
The following are the screen shots when inputs used (size of the grid) are: <tt> 22 10 </tt> |
|||
{{out|output|text=: the 1<sup>st</sup> screen shown.}} |
|||
<pre> |
|||
↓ |
|||
1636166561333644938615925878672969839136949348125385742112849651343354296271245 |
|||
4935939188413836477495369151362748736256329449564639583731265554747438655579797 |
|||
2761827294343258918258167935625127433626644177165772453435474591949917695547965 |
|||
5336784646373682676398688475989972451499776252164989899239191733912697265898925 |
|||
4952948995581413589577455233495962736898536553933712711747529619371573895413265 |
|||
5328643745672485468516645326176482571162377128958669252244431799914145324756787 |
|||
9682648416475828434376154259111596818112819626518754715385939211764235211148126 |
|||
4771918124154627513339665771138169237888886368882335865655526894655352121961215 |
|||
794644718989445262471866768299551827168758297323537929749@815519895387457566428 ◄ |
|||
9347969832617624113866732722842121521854745888458198852913265875445986923272597 |
|||
──────── moves: ──────── Q= ◄↑ W= ↑ E= ►↑ |
|||
──────── moves: ──────── A= ◄ D= ► |
|||
──────── moves: ──────── Z= ◄↓ X= ↓ C= ►↓ |
|||
──────── |
|||
──────── enter a move ──or── enter QUIT to quit. (score is: 1) |
|||
e |
|||
</pre> |
|||
{{out|output|text=: the 2<sup>nd</sup> screen shown.}} |
|||
<pre> |
|||
↓ |
|||
1636166561333644938615925878672969839136949348125385742112849651343354296271245 |
|||
4935939188413836477495369151362748736256329449564639583731265554747438655579797 |
|||
2761827294343258918258167935625127433626644177165772453435474591949917695547965 |
|||
53367846463736826763986884759899724514997762521649898992391917@3912697265898925 ◄ |
|||
4952948995581413589577455233495962736898536553933712711747529 19371573895413265 |
|||
532864374567248546851664532617648257116237712895866925224443 799914145324756787 |
|||
96826484164758284343761542591115968181128196265187547153859 9211764235211148126 |
|||
4771918124154627513339665771138169237888886368882335865655 26894655352121961215 |
|||
794644718989445262471866768299551827168758297323537929749 815519895387457566428 |
|||
9347969832617624113866732722842121521854745888458198852913265875445986923272597 |
|||
──────── moves: ──────── Q= ◄↑ W= ↑ E= ►↑ |
|||
──────── moves: ──────── A= ◄ D= ► |
|||
──────── moves: ──────── Z= ◄↓ X= ↓ C= ►↓ |
|||
──────── |
|||
──────── enter a move ──or── enter QUIT to quit. (score is: 6) |
|||
c |
|||
</pre> |
|||
{{out|output|text=: the 3<sup>rd</sup> screen shown.}} |
|||
<pre> |
|||
↓ |
|||
1636166561333644938615925878672969839136949348125385742112849651343354296271245 |
|||
4935939188413836477495369151362748736256329449564639583731265554747438655579797 |
|||
2761827294343258918258167935625127433626644177165772453435474591949917695547965 |
|||
53367846463736826763986884759899724514997762521649898992391917 3912697265898925 |
|||
4952948995581413589577455233495962736898536553933712711747529 1 371573895413265 |
|||
532864374567248546851664532617648257116237712895866925224443 799 14145324756787 |
|||
96826484164758284343761542591115968181128196265187547153859 92117 4235211148126 |
|||
4771918124154627513339665771138169237888886368882335865655 2689465 352121961215 |
|||
794644718989445262471866768299551827168758297323537929749 815519895 87457566428 |
|||
93479698326176241138667327228421215218547458884581988529132658754459 6923272597 |
|||
──────── Game over (out─of─bounds), your score is: 12 |
|||
<pre> |