2048: Difference between revisions
Content added Content deleted
Line 5,687: | Line 5,687: | ||
struct G2048 { |
struct G2048 { |
||
void gameLoop() { |
|||
addTile; |
addTile; |
||
while (true) { |
while (true) { |
||
Line 5,701: | Line 5,701: | ||
private: |
private: |
||
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 |
void drawBoard() const { |
||
writeln("SCORE: ", score, "\n"); |
writeln("SCORE: ", score, "\n"); |
||
foreach ( |
foreach (y; 0 .. side) { |
||
write("+------+------+------+------+\n| "); |
write("+------+------+------+------+\n| "); |
||
foreach ( |
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("+------+------+------+------+\n"); |
||
} |
} |
||
void waitKey() /*@safe*/ { |
void waitKey() /*@safe*/ { |
||
moved = false; |
moved = false; |
||
"(W)Up (S)Down (A)Left (D)Right (Q)Quit: " |
write("(W)Up (S)Down (A)Left (D)Right (Q)Quit: "); |
||
auto c = readln.strip.toLower; |
|||
switch (c) { |
switch (c) { |
||
Line 5,743: | Line 5,743: | ||
} |
} |
||
foreach ( |
foreach (y; 0 .. side) |
||
foreach ( |
foreach (x; 0 .. side) |
||
board[x][y].blocked = false; |
board[x][y].blocked = false; |
||
} |
} |
||
Line 5,753: | Line 5,753: | ||
} |
} |
||
void addTile() |
void addTile() { |
||
foreach ( |
foreach (y; 0 .. side) { |
||
foreach ( |
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 |
bool canMove() const { |
||
foreach ( |
foreach (y; 0 .. side) |
||
foreach ( |
foreach (x; 0 .. side) |
||
if (!board[x][y].val) |
if (!board[x][y].val) |
||
return true; |
return true; |
||
foreach ( |
foreach (y; 0 .. side) { |
||
foreach ( |
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( |
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( |
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( |
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( |
void move(moveDir d) { |
||
final switch (d) with(moveDir) { |
final switch (d) with(moveDir) { |
||
case up: |
case up: |
||
foreach ( |
foreach (x; 0 .. side) |
||
foreach ( |
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 ( |
foreach (x; 0 .. side) |
||
foreach_reverse ( |
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 ( |
foreach (y; 0 .. side) |
||
foreach ( |
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 ( |
foreach (y; 0 .. side) |
||
foreach_reverse ( |
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}} |