2048: Difference between revisions

Content added Content deleted
Line 5,687: Line 5,687:


struct G2048 {
struct G2048 {
public void gameLoop() /*@safe @nogc*/ {
void gameLoop() {
addTile;
addTile;
while (true) {
while (true) {
Line 5,701: Line 5,701:


private:
private:
static struct Tile {
struct Tile {
uint val = 0;
uint val = 0;
bool blocked = false;
bool blocked = false;
Line 5,713: Line 5,713:
uint score = 0;
uint score = 0;


void drawBoard() const /*@safe @nogc*/ {
void drawBoard() const {
writeln("SCORE: ", score, "\n");
writeln("SCORE: ", score, "\n");
foreach (immutable y; 0 .. side) {
foreach (y; 0 .. side) {
write("+------+------+------+------+\n| ");
write("+------+------+------+------+\n| ");
foreach (immutable x; 0 .. side) {
foreach (x; 0 .. side) {
if (board[x][y].val)
if (board[x][y].val)
writef("%4d", board[x][y].val);
writef("%4d", board[x][y].val);
Line 5,726: Line 5,726:
writeln;
writeln;
}
}
"+------+------+------+------+\n".writeln;
writeln("+------+------+------+------+\n");
}
}


void waitKey() /*@safe*/ {
void waitKey() /*@safe*/ {
moved = false;
moved = false;
"(W)Up (S)Down (A)Left (D)Right (Q)Quit: ".write;
write("(W)Up (S)Down (A)Left (D)Right (Q)Quit: ");
immutable c = readln.strip.toLower;
auto c = readln.strip.toLower;


switch (c) {
switch (c) {
Line 5,743: Line 5,743:
}
}


foreach (immutable y; 0 .. side)
foreach (y; 0 .. side)
foreach (immutable x; 0 .. side)
foreach (x; 0 .. side)
board[x][y].blocked = false;
board[x][y].blocked = false;
}
}
Line 5,753: Line 5,753:
}
}


void addTile() /*nothrow*/ @safe /*@nogc*/ {
void addTile() {
foreach (immutable y; 0 .. side) {
foreach (y; 0 .. side) {
foreach (immutable x; 0 .. side) {
foreach (x; 0 .. side) {
if (!board[x][y].val) {
if (!board[x][y].val) {
uint a, b;
uint a, b;
Line 5,772: Line 5,772:
}
}


bool canMove() const pure nothrow @safe @nogc {
bool canMove() const {
foreach (immutable y; 0 .. side)
foreach (y; 0 .. side)
foreach (immutable x; 0 .. side)
foreach (x; 0 .. side)
if (!board[x][y].val)
if (!board[x][y].val)
return true;
return true;


foreach (immutable y; 0 .. side) {
foreach (y; 0 .. side) {
foreach (immutable x; 0 .. side) {
foreach (x; 0 .. side) {
if (testAdd(x + 1, y, board[x][y].val) ||
if (testAdd(x + 1, y, board[x][y].val) ||
testAdd(x - 1, y, board[x][y].val) ||
testAdd(x - 1, y, board[x][y].val) ||
Line 5,790: Line 5,790:
}
}


bool testAdd(in uint x, in uint y, in uint v) const pure nothrow @safe @nogc {
bool testAdd(uint x, uint y, uint v) const {
if (x > 3 || y > 3)
if (x > 3 || y > 3)
return false;
return false;
Line 5,796: Line 5,796:
}
}


void moveVertically(in uint x, in uint y, in uint d) pure nothrow @safe @nogc {
void moveVertically(uint x, uint y, uint d) {
if (board[x][y + d].val && board[x][y + d].val == board[x][y].val &&
if (board[x][y + d].val && board[x][y + d].val == board[x][y].val &&
!board[x][y].blocked && !board[x][y + d].blocked) {
!board[x][y].blocked && !board[x][y + d].blocked) {
Line 5,819: Line 5,819:
}
}


void moveHorizontally(in uint x, in uint y, in uint d) pure nothrow @safe @nogc {
void moveHorizontally(uint x, uint y, uint d) {
if (board[x + d][y].val && board[x + d][y].val == board[x][y].val &&
if (board[x + d][y].val && board[x + d][y].val == board[x][y].val &&
!board[x][y].blocked && !board[x + d][y].blocked) {
!board[x][y].blocked && !board[x + d][y].blocked) {
Line 5,842: Line 5,842:
}
}


void move(in moveDir d) pure nothrow @safe @nogc {
void move(moveDir d) {
final switch (d) with(moveDir) {
final switch (d) with(moveDir) {
case up:
case up:
foreach (immutable x; 0 .. side)
foreach (x; 0 .. side)
foreach (immutable y; 1 .. side)
foreach (y; 1 .. side)
if (board[x][y].val)
if (board[x][y].val)
moveVertically(x, y, -1);
moveVertically(x, y, -1);
break;
break;
case down:
case down:
foreach (immutable x; 0 .. side)
foreach (x; 0 .. side)
foreach_reverse (immutable y; 0 .. 3)
foreach_reverse (y; 0 .. 3)
if (board[x][y].val)
if (board[x][y].val)
moveVertically(x, y, 1);
moveVertically(x, y, 1);
break;
break;
case left:
case left:
foreach (immutable y; 0 .. side)
foreach (y; 0 .. side)
foreach (immutable x; 1 .. side)
foreach (x; 1 .. side)
if (board[x][y].val)
if (board[x][y].val)
moveHorizontally(x, y, -1);
moveHorizontally(x, y, -1);
break;
break;
case right:
case right:
foreach (immutable y; 0 .. side)
foreach (y; 0 .. side)
foreach_reverse (immutable x; 0 .. 3)
foreach_reverse (x; 0 .. 3)
if (board[x][y].val)
if (board[x][y].val)
moveHorizontally(x, y, 1);
moveHorizontally(x, y, 1);
Line 5,876: Line 5,876:
}</syntaxhighlight>
}</syntaxhighlight>
The output is the same as the C++ version.
The output is the same as the C++ version.

=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}