Greed: Difference between revisions

11,230 bytes added ,  1 year ago
m
syntax highlighting fixup automation
m (→‎{{header|REXX}}: added/changed comments and whitespace, ensured scope of HALT.)
m (syntax highlighting fixup automation)
 
(9 intermediate revisions by 7 users not shown)
Line 2:
This task is about making a clone of the game "GREED" by Matthew Day.
 
This game is played on a grid of   '''79'''   column by   '''22'''   rows of random numbers from   '''1'''   to 9. The player location is signified by the  '@''9''' symbol  (inclusive).
 
The player location is signified by the   '''@'''   symbol.
The object of Greed is to erase as much of the screen as possible by moving around (all 8 directions are allowed) in this grid. When you move in a direction, you erase N number of grid squares in that direction, N being the first number in that direction. Your score reflects the total number of squares eaten.
 
The object of Greed is to erase as much of the screen as possible by moving around   (all 8 directions are allowed in this grid).
 
When you move in a direction,   '''N'''   number of grid squares are erased ("eaten") in that direction,   '''N'''   being the first number in that direction.
 
Your score reflects the total number of squares "eaten".
 
You may not make a move that places you off the grid or over a previously eaten square.
Line 11 ⟶ 17:
 
[https://www.youtube.com/watch?v=XQHq6tdxylk&list=PLdvB7n7RN2UDkjHAWCmbQ8okmgSrjWcvE Video on YouTube]
<br><br>
 
 
=={{header|C++}}==
Windows console version.
[[File:newGreedCpp.png|200px|thumb|right]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <iostream>
Line 137 ⟶ 143:
greed g; g.play(); return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
This uses Factor's own user interface vocabularies. Use hjkl-bnyu (vi-keys) to move.
<langsyntaxhighlight lang="factor">USING: accessors arrays colors combinators
combinators.short-circuit fry grouping io io.styles kernel lexer
literals make math math.matrices math.parser math.vectors random
Line 315 ⟶ 321:
] with-ui ;
 
MAIN: greed-window</langsyntaxhighlight>
{{out}}
[https://i.imgur.com/3IEo8cC.png Screenshot of the game after a loss]
Line 327 ⟶ 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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 551 ⟶ 557:
}
}
}</langsyntaxhighlight>
 
=={{header|Kotlin}}==
Line 557 ⟶ 563:
{{works with|Windows 10}}
Note that this version uses the Z key (rather than the Y key) to move diagonally downwards to the left.
<langsyntaxhighlight lang="scala">// Kotlin Native v0.5
 
import kotlinx.cinterop.*
Line 754 ⟶ 760:
srand(time(null).toInt())
Greed().play()
}</langsyntaxhighlight>
 
=={{header|Java}}==
Line 764 ⟶ 770:
=={{header|Julia}}==
GUI version. Click a square adjacent to the "@" symbol to move.
<langsyntaxhighlight lang="julia">using Gtk
 
struct BState
Line 865 ⟶ 871:
# greedapp(22, 79) # This would be per task, though a smaller game board is nicer
greedapp(12, 29)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Julia}}
{{libheader|gintro}}
 
This is a translation of the Julia solution with some modifications. The most important is the way the new position is computed after choosing a direction. Rather than moving to the neighbor tile, we move to the farthest tile in the chosen direction as shown in the video.
 
There is of course a lot of differences due to the way <code>gintro</code> library works which is quite different of Julia GTK API.
 
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]
 
const Rows = 22
const Cols = 79
 
type
 
Board = array[Rows, array[Cols, int]]
 
 
BState = object
board: Board
score: Natural
row, col: int
 
 
Game = ref object
window: Window
score: Natural
won: string
board: Board
buttons: array[Rows, array[Cols, Button]]
myRow, myCol: Natural
pastBoardStates: seq[BState]
 
 
proc update(game: Game) =
## Update game state.
var won = true
for i in 0..<Rows:
for j in 0..<Cols:
let val = game.board[i][j]
won = won and val == 0
game.buttons[i][j].setLabel(if val > 0: $val else: "")
 
game.buttons[game.myRow][game.myCol].setLabel("@")
game.won = if won: "WINNING " else: ""
game.window.setTitle(game.won & "Greed game (score: $#)".format(game.score))
 
 
proc eraseFromTile(game: Game; moveRow, moveCol: Natural) =
## Erase tile values from the given tile.
let xdir = moveRow - game.myRow
let ydir = moveCol - game.myCol
if abs(xdir) > 1 or abs(ydir) > 1 or xdir == 0 and ydir == 0 or game.board[moveRow][moveCol] == 0:
return
game.pastBoardStates.add BState(board: game.board, score: game.score,
row: game.myRow, col: game.myCol)
let count = game.board[moveRow][moveCol]
var x: int = game.myRow
var y: int = game.myCol
for i in 1..count:
inc x, xdir
inc y, ydir
if x in 0..<Rows and y in 0..<Cols:
game.board[x][y] = 0
inc game.score
else:
# Restore to last valid position.
dec x, xdir
dec y, ydir
game.board[game.myRow][game.myCol] = 0
game.myRow = x
game.myCol = y
game.update()
 
 
proc clicked(b: Button; game: Game) =
## Callback tp process a click in the grid.
var row, col = -1
for i in 0..<Rows:
for j in 0..<Cols:
if game.buttons[i][j] == b:
(row, col) = (i, j)
break
if row < 0: return
game.eraseFromTile(row, col)
 
 
proc undo(b: ToolButton; game: Game) =
## Callback to undo last move.
if game.won.len == 0 and game.pastBoardStates.len > 0:
let bst = game.pastBoardStates.pop()
game.board = bst.board
game.score = bst.score
game.myRow = bst.row
game.myCol = bst.col
game.update()
 
 
proc initialize(game: Game) =
## Initialize the game.
const PossibleVals = {1..9}
game.score = 0
game.won = ""
for i in 0..<Rows:
for j in 0..<Cols:
game.board[i][j] = PossibleVals.sample()
game.buttons[i][j].connect("clicked", clicked, game)
game.myRow = rand(Rows - 1)
game.myCol = rand(Cols - 1)
game.update()
 
 
proc initGame(b: ToolButton; game: Game) =
## Callback to start a new game.
game.initialize()
 
 
proc activate(app: Application) =
## Activate the application.
 
let game = new(Game)
let window = app.newApplicationWindow()
window.setSizeRequest(1600, 800)
window.setTitle("Greed game")
game.window = window
 
let box = newBox(Orientation.vertical, 0)
window.add box
let toolbar = newToolbar()
box.add toolbar
let newGame = newToolButton(label = "New game")
let undoMove = newToolButton(label = "Undo move")
toolbar.add(newGame)
toolbar.add(undoMove)
 
let srcWindow = newScrolledWindow()
box.packEnd(srcWindow, true, true, 0)
let grid = newGrid()
srcWindow.add grid
 
for i in 0..<Rows:
for j in 0..<Cols:
let b = newButton()
game.buttons[i][j] = b
grid.attach(b, j, i, 1, 1)
 
game.initialize()
newGame.connect("clicked", initGame, game)
undoMove.connect("clicked", undo, game)
game.window.showAll()
 
 
randomize()
let app = newApplication(Application, "Rosetta.greed")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
my @board;
my $w = 79;
my $h = 22;
for (1..$h) {
my @row;
push @row, int 1 + rand 9 for 1..$w;
push @board, [@row];
}
my $X = int 0.5 + rand $w;
my $Y = int 0.5 + rand $h;
$board[$Y][$X] = '@';
 
my $score = 0;
 
sub execute {
my($y,$x) = @_;
my $i = $board[$Y+$y][$X+$x];
if (countSteps($i, $x, $y)) {
$score += $i;
$board[ $Y + $y*$_ ][ $X + $x*$_ ] = ' ' for 0..$i;
$board[ $Y += $y*$i ][ $X += $x*$i ] = '@';
}
}
 
sub countSteps {
my($i, $x, $y) = @_;
my $tX = $X;
my $tY = $Y;
for (0..$i) {
$tX += $x;
$tY += $y;
return 0 if $tX < 0 or $tY < 0 or $tX >= $w or $tY >= $h or $board[$tY][$tX] eq ' '
}
return 1
}
 
sub existsMoves {
for ([-1,-1], [-1,0], [-1,1], [0,-1], [0,0], [0,1], [1,-1], [1,0], [1,1]) {
my($x,$y) = @$_;
next if $x == 0 and $y == 0;
next if $X+$x < 0 or $X+$x > $w or $Y+$y < 0 or $Y+$y > $h ;
my $i = $board[$Y+$y][$X+$x];
return 1 if ( $i ne ' ' and countSteps($i, $x, $y) )
}
return 0;
}
 
while () {
say join '', @$_ for @board;
say "Game over." and last unless existsMoves();
print "Current score : " . $score . "\n";
my $c = <> ; chomp $c;
if ($c eq 'q') { say "So long." and last}
if ($c eq 'e') { execute(-1,-1) if $X > 0 and $Y > 0 } # North-West
if ($c eq 'r') { execute(-1, 0) if $Y > 0 } # North
if ($c eq 't') { execute(-1, 1) if $X < $w and $Y > 0 } # North-East
if ($c eq 'd') { execute( 0,-1) if $X > 0 } # West
if ($c eq 'g') { execute( 0, 1) if $X < $w } # East
if ($c eq 'x') { execute( 1,-1) if $X > 0 and $Y < $h } # South-West
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
}</syntaxhighlight>
 
=={{header|Phix}}==
{{trans|C++}}
<langsyntaxhighlight Phixlang="phix">constant W = 79, H = 22, NCOUNT = W*H
 
sequence board
Line 974 ⟶ 1,209:
end while
end procedure
play()</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Computer play by selecting random road. [https://asciinema.org/a/369181 Demo] is here.
<syntaxhighlight lang="picolisp">
(load "@lib/simul.l")
(seed (in "/dev/urandom" (rd 8)))
(scl 6)
# N - number
# C - Color
# F - flag to draw candidates
# A - @ mark
(de display ()
(let P 0
(wait 500)
(prin "^[[2J")
(for L G
(for This L
# count cleared cells
(and (lt0 (: N)) (inc 'P))
(prin
"^[[0;"
(if (or (: A) (: F)) 100 (: C))
"m"
(cond
((: A) "@")
((lt0 (: N)) " ")
(T (: N)) )
"^[[0m" ) )
(prinl) )
(prinl
"Score: "
S
" "
(round (*/ P 1.0 100.0 1738.0) 2)
"%" ) ) )
(de roads (Lst Flg)
(mapc
'((L)
(with C
(do (car L)
(setq This ((cadr L) This))
(=: F Flg) ) ) )
Lst )
(display) )
(let
(Colors (simul~shuffle (31 32 33 35 91 92 93 94 96))
G (simul~grid 22 79)
C NIL
S 0 )
# set random grid
(for L G
(for This L
(let X (rand 1 9)
(=: N X)
(=: C (get Colors X)) ) ) )
# set random startpoint
(with (get G (rand 1 22) (rand 1 79))
(setq C This)
(=: A 0) )
(display)
(loop
(NIL
(setq Z
(extract
'((D)
(with C
(let? S (with (D This) (: N))
(and
(do S
(NIL (setq This (D This)))
(NIL (gt0 (: N)))
'next )
(list S D This) ) ) ) )
'(simul~west
simul~east
simul~south
simul~north
((X) (simul~south (simul~west X)))
((X) (simul~north (simul~west X)))
((X) (simul~south (simul~east X)))
((X) (simul~north (simul~east X))) ) ) ) )
# XXX
(roads Z T)
(roads Z)
# select road randomly
(let L (get Z (rand 1 (length Z)))
(with C
(inc 'S (: N))
# clear value of "old" Center
(=: N -1)
(=: A)
# clear selected road
(do (car L)
(inc 'S (: N))
(=: N -1)
(setq This ((cadr L) This)) ) )
# set new Center
(with (caddr L)
(setq C This)
(=: A 0) ) )
(display) ) )
(bye)</syntaxhighlight>
 
=={{header|Raku}}==
{{trans|Phix}}
<syntaxhighlight lang="raku" line># 20200913 added Raku programming solution
 
srand 123456;
 
my @board = [ (1..9).roll xx my \w = 79 ] xx my \h = 22 ;
my \X = $ = w.rand.Int ; my \Y = $ = h.rand.Int;
@board[Y;X] = '@';
my \score = $ = 0;
 
sub execute (\y,\x) {
my \i = $ = @board[Y+y;X+x];
if countSteps(i, x, y) {
score += i;
@board[ Y + y*$_ ; X + x*$_ ] = ' ' for ^i;
@board[ Y += y*i ; X += x*i ] = '@';
}
}
 
sub countSteps(\i, \x, \y) {
my \tX = $ = X ; my \tY = $ = Y;
for ^i {
tX += x; tY += y;
return False if tX < 0 or tY < 0 or tX ≥ w or tY ≥ h or @board[tY;tX] eq ' '
}
return True;
}
 
sub existsMoves {
for (-1 .. 1) X (-1 .. 1) -> (\x,\y) {
next if x == 0 and y == 0;
next if X+x < 0 or X+x > w or Y+y < 0 or Y+y > h ;
my \i = @board[Y+y;X+x];
return True if ( i ne ' ' and countSteps(i, x, y) )
}
return False;
}
 
loop {
for @board { .join.print ; print "\r\n" } ;
{ say "Game over." and last } unless existsMoves();
print "Current score : ", score, "\r\n";
given my $c = $*IN.getc {
when 'q' { say "So long." and last}
when 'e' { execute(-1,-1) if X > 0 and Y > 0 } # North-West
when 'r' { execute(-1, 0) if Y > 0 } # North
when 't' { execute(-1, 1) if X < w and Y > 0 } # North-East
when 'd' { execute( 0,-1) if X > 0 } # West
when 'g' { execute( 0, 1) if X < w } # East
when 'x' { execute( 1,-1) if X > 0 and Y < h } # South-West
when 'c' { execute( 1, 0) if Y < h } # South
when 'v' { execute( 1, 1) if X < w and Y < h } # South-East
}
}</syntaxhighlight>
 
=={{header|REXX}}==
Line 982 ⟶ 1,375:
 
Pointers (above and to the right of) the grid are included to help identify where the current location is.
<langsyntaxhighlight 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*/
if sw=='' | sw=="," then sw= 79 /*cols specified? Then use the default*/
Line 1,058 ⟶ 1,451:
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).*/</langsyntaxhighlight>
A note on the OUTPUT sections: &nbsp; each (cleared) screen displayed is shown below as a separate OUTPUT section.
 
Line 1,107 ⟶ 1,500:
──────── quitting.
</pre>
 
 
=={{header|Wren}}==
 
See [[Greed/Wren]].
 
=={{header|Z80 Assembly}}==
 
See [[Greed/Z80 Assembly]].
<br><br>
10,327

edits