2048: Difference between revisions
→{{header|D}}
Line 5,687:
struct G2048 {
addTile;
while (true) {
Line 5,701:
private:
uint val = 0;
bool blocked = false;
Line 5,713:
uint score = 0;
void drawBoard() const
writeln("SCORE: ", score, "\n");
foreach (
write("+------+------+------+------+\n| ");
foreach (
if (board[x][y].val)
writef("%4d", board[x][y].val);
Line 5,726:
writeln;
}
writeln("+------+------+------+------+\n"
}
void waitKey() /*@safe*/ {
moved = false;
write("(W)Up (S)Down (A)Left (D)Right (Q)Quit: "
switch (c) {
Line 5,743:
}
foreach (
foreach (
board[x][y].blocked = false;
}
Line 5,753:
}
void addTile()
foreach (
foreach (
if (!board[x][y].val) {
uint a, b;
Line 5,772:
}
bool canMove() const
foreach (
foreach (
if (!board[x][y].val)
return true;
foreach (
foreach (
if (testAdd(x + 1, y, board[x][y].val) ||
testAdd(x - 1, y, board[x][y].val) ||
Line 5,790:
}
bool testAdd(
if (x > 3 || y > 3)
return false;
Line 5,796:
}
void moveVertically(
if (board[x][y + d].val && board[x][y + d].val == board[x][y].val &&
!board[x][y].blocked && !board[x][y + d].blocked) {
Line 5,819:
}
void moveHorizontally(
if (board[x + d][y].val && board[x + d][y].val == board[x][y].val &&
!board[x][y].blocked && !board[x + d][y].blocked) {
Line 5,842:
}
void move(
final switch (d) with(moveDir) {
case up:
foreach (
foreach (
if (board[x][y].val)
moveVertically(x, y, -1);
break;
case down:
foreach (
foreach_reverse (
if (board[x][y].val)
moveVertically(x, y, 1);
break;
case left:
foreach (
foreach (
if (board[x][y].val)
moveHorizontally(x, y, -1);
break;
case right:
foreach (
foreach_reverse (
if (board[x][y].val)
moveHorizontally(x, y, 1);
Line 5,876:
}</syntaxhighlight>
The output is the same as the C++ version.
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
|