Greed: Difference between revisions
Content added Content deleted
(Added Wren header.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22: | Line 22: | ||
Windows console version. |
Windows console version. |
||
[[File:newGreedCpp.png|200px|thumb|right]] |
[[File:newGreedCpp.png|200px|thumb|right]] |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <iostream> |
#include <iostream> |
||
Line 143: | Line 143: | ||
greed g; g.play(); return 0; |
greed g; g.play(); return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
This uses Factor's own user interface vocabularies. Use hjkl-bnyu (vi-keys) to move. |
This uses Factor's own user interface vocabularies. Use hjkl-bnyu (vi-keys) to move. |
||
< |
<syntaxhighlight lang="factor">USING: accessors arrays colors combinators |
||
combinators.short-circuit fry grouping io io.styles kernel lexer |
combinators.short-circuit fry grouping io io.styles kernel lexer |
||
literals make math math.matrices math.parser math.vectors random |
literals make math math.matrices math.parser math.vectors random |
||
Line 321: | Line 321: | ||
] with-ui ; |
] with-ui ; |
||
MAIN: greed-window</ |
MAIN: greed-window</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://i.imgur.com/3IEo8cC.png Screenshot of the game after a loss] |
[https://i.imgur.com/3IEo8cC.png Screenshot of the game after a loss] |
||
Line 333: | Line 333: | ||
Note that this version uses the Z key (rather than the Y key) to move diagonally downwards to the left. A leave key, L, has also been added in case one wants to end the game prematurely. |
Note that this version uses the Z key (rather than the Y key) to move diagonally downwards to the left. A leave key, L, has also been added in case one wants to end the game prematurely. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 557: | Line 557: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 563: | Line 563: | ||
{{works with|Windows 10}} |
{{works with|Windows 10}} |
||
Note that this version uses the Z key (rather than the Y key) to move diagonally downwards to the left. |
Note that this version uses the Z key (rather than the Y key) to move diagonally downwards to the left. |
||
< |
<syntaxhighlight lang="scala">// Kotlin Native v0.5 |
||
import kotlinx.cinterop.* |
import kotlinx.cinterop.* |
||
Line 760: | Line 760: | ||
srand(time(null).toInt()) |
srand(time(null).toInt()) |
||
Greed().play() |
Greed().play() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 770: | Line 770: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
GUI version. Click a square adjacent to the "@" symbol to move. |
GUI version. Click a square adjacent to the "@" symbol to move. |
||
< |
<syntaxhighlight lang="julia">using Gtk |
||
struct BState |
struct BState |
||
Line 871: | Line 871: | ||
# greedapp(22, 79) # This would be per task, though a smaller game board is nicer |
# greedapp(22, 79) # This would be per task, though a smaller game board is nicer |
||
greedapp(12, 29) |
greedapp(12, 29) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Line 883: | Line 883: | ||
And we also added a score field to the BState object in order to restore the correct score when undoing a move. |
And we also added a score field to the BState object in order to restore the correct score when undoing a move. |
||
< |
<syntaxhighlight lang="nim">import random, strutils |
||
import gintro/[gobject, glib, gtk, gio] |
import gintro/[gobject, glib, gtk, gio] |
||
Line 1,031: | Line 1,031: | ||
let app = newApplication(Application, "Rosetta.greed") |
let app = newApplication(Application, "Rosetta.greed") |
||
discard app.connect("activate", activate) |
discard app.connect("activate", activate) |
||
discard app.run()</ |
discard app.run()</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,100: | Line 1,100: | ||
if ($c eq 'c') { execute( 1, 0) if $Y < $h } # South |
if ($c eq 'c') { execute( 1, 0) if $Y < $h } # South |
||
if ($c eq 'v') { execute( 1, 1) if $X < $w and $Y < $h } # South-East |
if ($c eq 'v') { execute( 1, 1) if $X < $w and $Y < $h } # South-East |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="phix">constant W = 79, H = 22, NCOUNT = W*H |
||
sequence board |
sequence board |
||
Line 1,209: | Line 1,209: | ||
end while |
end while |
||
end procedure |
end procedure |
||
play()</ |
play()</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Computer play by selecting random road. [https://asciinema.org/a/369181 Demo] is here. |
Computer play by selecting random road. [https://asciinema.org/a/369181 Demo] is here. |
||
<syntaxhighlight lang="picolisp"> |
|||
<lang PicoLisp> |
|||
(load "@lib/simul.l") |
(load "@lib/simul.l") |
||
(seed (in "/dev/urandom" (rd 8))) |
(seed (in "/dev/urandom" (rd 8))) |
||
Line 1,311: | Line 1,311: | ||
(=: A 0) ) ) |
(=: A 0) ) ) |
||
(display) ) ) |
(display) ) ) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
{{trans|Phix}} |
{{trans|Phix}} |
||
<lang |
<syntaxhighlight lang="raku" line># 20200913 added Raku programming solution |
||
srand 123456; |
srand 123456; |
||
Line 1,367: | Line 1,367: | ||
when 'v' { execute( 1, 1) if X < w and Y < h } # South-East |
when 'v' { execute( 1, 1) if X < w and Y < h } # South-East |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,375: | Line 1,375: | ||
Pointers (above and to the right of) the grid are included to help identify where the current location is. |
Pointers (above and to the right of) the grid are included to help identify where the current location is. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program lets a user play the game of GREED (by Matthew Day) from the console. */ |
||
parse arg sw sd @ b ?r . /*obtain optional argumenst from the CL*/ |
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 sw=='' | sw=="," then sw= 79 /*cols specified? Then use the default*/ |
||
Line 1,451: | Line 1,451: | ||
if r==!r then _= _ '◄' /*indicate row of current position. */ |
if r==!r then _= _ '◄' /*indicate row of current position. */ |
||
if tell then say _ /*display a row of grid to screen. */ |
if tell then say _ /*display a row of grid to screen. */ |
||
end /*r*/; say; return # /*SHOW also counts # of blanks (score).*/</ |
end /*r*/; say; return # /*SHOW also counts # of blanks (score).*/</syntaxhighlight> |
||
A note on the OUTPUT sections: each (cleared) screen displayed is shown below as a separate OUTPUT section. |
A note on the OUTPUT sections: each (cleared) screen displayed is shown below as a separate OUTPUT section. |
||