Addedd transition for both merging tiles
* Added hasMoved and isNew attributes to Tile type * Made obsolete arbitrary content in previousPosition * Made code more robust to creation of new tiles * Made some changes for and against the original code
This commit is contained in:
parent
69e8bafec1
commit
f83d3e7785
92
2048.c
92
2048.c
|
@ -35,7 +35,10 @@ typedef struct Tile {
|
||||||
int y;
|
int y;
|
||||||
int value;
|
int value;
|
||||||
bool hasMerged;
|
bool hasMerged;
|
||||||
|
bool hasMoved;
|
||||||
|
bool isNew;
|
||||||
Cell previousPosition;
|
Cell previousPosition;
|
||||||
|
Cell previousMerge;
|
||||||
} Tile;
|
} Tile;
|
||||||
|
|
||||||
typedef struct Grid {
|
typedef struct Grid {
|
||||||
|
@ -95,6 +98,22 @@ int rand_int(int min, int max) {
|
||||||
|
|
||||||
// Grid
|
// Grid
|
||||||
|
|
||||||
|
Tile Grid_getEmptyTile() {
|
||||||
|
Tile emptyTile; Cell emptyCell;
|
||||||
|
emptyCell.x = -1;
|
||||||
|
emptyCell.y = -1;
|
||||||
|
emptyTile.x = -1;
|
||||||
|
emptyTile.y = -1;
|
||||||
|
emptyTile.value = 0;
|
||||||
|
emptyTile.hasMerged = false;
|
||||||
|
emptyTile.hasMoved = false;
|
||||||
|
emptyTile.isNew = false;
|
||||||
|
emptyTile.previousPosition = emptyCell;
|
||||||
|
emptyTile.previousMerge = emptyCell;
|
||||||
|
return emptyTile;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
bool Grid_withinBounds(Cell position) {
|
bool Grid_withinBounds(Cell position) {
|
||||||
return (position.x >= 0 && position.x < 4 && position.y >= 0 && position.y < 4);
|
return (position.x >= 0 && position.x < 4 && position.y >= 0 && position.y < 4);
|
||||||
}
|
}
|
||||||
|
@ -122,17 +141,11 @@ int Grid_insertTile(Tile tile) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int Grid_removeTile(Tile tile) { // TODO use with cellContentEdit
|
int Grid_removeTile(Tile tile) { // TODO use with cellContentEdit
|
||||||
Cell emptyCell;
|
|
||||||
Tile emptyTile;
|
Tile emptyTile;
|
||||||
|
emptyTile = Grid_getEmptyTile();
|
||||||
emptyTile.x = tile.x;
|
emptyTile.x = tile.x;
|
||||||
emptyTile.y = tile.y;
|
emptyTile.y = tile.y;
|
||||||
emptyTile.value = 0;
|
emptyTile.value = 0;
|
||||||
emptyTile.hasMerged = false;
|
|
||||||
emptyCell.x = -1;
|
|
||||||
emptyCell.y = -1;
|
|
||||||
emptyTile.previousPosition = emptyCell;
|
|
||||||
|
|
||||||
Grid_grid.array[tile.x][tile.y] = emptyTile;
|
Grid_grid.array[tile.x][tile.y] = emptyTile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +181,8 @@ void Screen_drawTileCase(Tile tile) {
|
||||||
Screen_drawTile(5+tile.x*14, 5+tile.y*14, tile.value);
|
Screen_drawTile(5+tile.x*14, 5+tile.y*14, tile.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Screen_drawTileMoving(Tile tile, float percentage) {
|
void Screen_drawTileMoving(Tile tile, float percentage) { // TODO Make smaller
|
||||||
|
// Draw moved-tiles and under-merged-tile
|
||||||
int x, y, OTx, OTy, NTx, NTy;
|
int x, y, OTx, OTy, NTx, NTy;
|
||||||
NTx = 5+tile.x*14;
|
NTx = 5+tile.x*14;
|
||||||
NTy = 5+tile.y*14;
|
NTy = 5+tile.y*14;
|
||||||
|
@ -176,7 +190,17 @@ void Screen_drawTileMoving(Tile tile, float percentage) {
|
||||||
OTy = 5+tile.previousPosition.y*14;
|
OTy = 5+tile.previousPosition.y*14;
|
||||||
x = OTx + (NTx - OTx) * percentage;
|
x = OTx + (NTx - OTx) * percentage;
|
||||||
y = OTy + (NTy - OTy) * percentage;
|
y = OTy + (NTy - OTy) * percentage;
|
||||||
Screen_drawTile(x, y, tile.value);
|
// Draw over-merged-tile
|
||||||
|
if (!tile.hasMerged) {
|
||||||
|
Screen_drawTile(x, y, tile.value);
|
||||||
|
} else {
|
||||||
|
Screen_drawTile(x, y, tile.value-1);
|
||||||
|
OTx = 5+tile.previousMerge.x*14;
|
||||||
|
OTy = 5+tile.previousMerge.y*14;
|
||||||
|
x = OTx + (NTx - OTx) * percentage;
|
||||||
|
y = OTy + (NTy - OTy) * percentage;
|
||||||
|
Screen_drawTile(x, y, tile.value-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Screen_drawFixedTiles(bool dontDrawPreviousPositionTiles) {
|
int Screen_drawFixedTiles(bool dontDrawPreviousPositionTiles) {
|
||||||
|
@ -188,7 +212,7 @@ int Screen_drawFixedTiles(bool dontDrawPreviousPositionTiles) {
|
||||||
position.x = x;
|
position.x = x;
|
||||||
position.y = y;
|
position.y = y;
|
||||||
tile = Grid_cellContent(position);
|
tile = Grid_cellContent(position);
|
||||||
if (dontDrawPreviousPositionTiles && ((tile.previousPosition.x >= 0 && tile.previousPosition.y >= 0) || (tile.previousPosition.x == -2 && tile.previousPosition.y == -2))) {
|
if (dontDrawPreviousPositionTiles && (tile.hasMoved || tile.hasMerged || tile.isNew)) {
|
||||||
tile.value = 0;
|
tile.value = 0;
|
||||||
}
|
}
|
||||||
Screen_drawTileCase(tile);
|
Screen_drawTileCase(tile);
|
||||||
|
@ -204,7 +228,7 @@ int Screen_drawMovingTiles(float percentage) {
|
||||||
position.x = x;
|
position.x = x;
|
||||||
position.y = y;
|
position.y = y;
|
||||||
tile = Grid_cellContent(position);
|
tile = Grid_cellContent(position);
|
||||||
if (tile.previousPosition.x >= 0 && tile.previousPosition.y >= 0) {
|
if (tile.hasMoved || tile.hasMerged) {
|
||||||
Screen_drawTileMoving(tile, percentage);
|
Screen_drawTileMoving(tile, percentage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,7 +265,7 @@ void Screen_actuate() {
|
||||||
|
|
||||||
SaveDisp(SAVEDISP_PAGE1);
|
SaveDisp(SAVEDISP_PAGE1);
|
||||||
|
|
||||||
for (i = 0; i <= 1; i += 0.01) {
|
for (i = 0; i <= 1; i += 0.02) {
|
||||||
RestoreDisp(SAVEDISP_PAGE1);
|
RestoreDisp(SAVEDISP_PAGE1);
|
||||||
Screen_drawMovingTiles(i);
|
Screen_drawMovingTiles(i);
|
||||||
ML_display_vram();
|
ML_display_vram();
|
||||||
|
@ -304,14 +328,18 @@ Cell Game_getVector(int direction) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game_prepareTiles() {
|
void Game_prepareTiles() {
|
||||||
Cell previousPosition;
|
|
||||||
int x, y;
|
int x, y;
|
||||||
|
Cell previousPosition;
|
||||||
for (x = 0; x <= 3; x++) {
|
for (x = 0; x <= 3; x++) {
|
||||||
for (y = 0; y <= 3; y++) {
|
for (y = 0; y <= 3; y++) {
|
||||||
Grid_grid.array[x][y].hasMerged = false;
|
previousPosition.x = x;
|
||||||
previousPosition.x = -1;
|
previousPosition.y = y;
|
||||||
previousPosition.y = -1;
|
Grid_grid.array[x][y].hasMerged = false;
|
||||||
Grid_grid.array[x][y].previousPosition = previousPosition;
|
Grid_grid.array[x][y].hasMerged = false;
|
||||||
|
Grid_grid.array[x][y].hasMoved = false;
|
||||||
|
Grid_grid.array[x][y].isNew = false;
|
||||||
|
Grid_grid.array[x][y].previousPosition = previousPosition;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -341,7 +369,8 @@ bool Game_moveTile(Tile tile, Cell cell) {
|
||||||
Grid_removeTile(tile);
|
Grid_removeTile(tile);
|
||||||
previousPosition.x = tile.x;
|
previousPosition.x = tile.x;
|
||||||
previousPosition.y = tile.y;
|
previousPosition.y = tile.y;
|
||||||
tile.previousPosition = previousPosition;
|
// tile.previousPosition = previousPosition;
|
||||||
|
tile.hasMoved = true;
|
||||||
tile.x = cell.x;
|
tile.x = cell.x;
|
||||||
tile.y = cell.y;
|
tile.y = cell.y;
|
||||||
Grid_insertTile(tile);
|
Grid_insertTile(tile);
|
||||||
|
@ -377,24 +406,22 @@ Cell Grid_randomAvaiableCell() {
|
||||||
// Game (O game_manager)
|
// Game (O game_manager)
|
||||||
|
|
||||||
void Game_addRandomTile() {
|
void Game_addRandomTile() {
|
||||||
Tile tile; Cell position, previousPosition;
|
Tile tile; Cell position;
|
||||||
|
|
||||||
if (Grid_avaiableCellsAmount() > 0) {
|
if (Grid_avaiableCellsAmount() > 0) {
|
||||||
position = Grid_randomAvaiableCell();
|
position = Grid_randomAvaiableCell();
|
||||||
|
tile = Grid_getEmptyTile();
|
||||||
tile.value = (rand_int(0, 10) < 9 ? 1 : 2);
|
tile.value = (rand_int(0, 10) < 9 ? 1 : 2);
|
||||||
tile.x = position.x;
|
tile.x = position.x;
|
||||||
tile.y = position.y;
|
tile.y = position.y;
|
||||||
previousPosition.x = -2;
|
tile.isNew = true;
|
||||||
previousPosition.y = -2;
|
|
||||||
tile.previousPosition = previousPosition;
|
|
||||||
tile.hasMerged = false;
|
|
||||||
Grid_insertTile(tile);
|
Grid_insertTile(tile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
|
void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
|
||||||
Cell vector, cell, farthest, next;
|
Cell vector, cell, farthest;
|
||||||
Tile tile, merged;
|
Tile tile, merged, next;
|
||||||
Traversal traversals;
|
Traversal traversals;
|
||||||
findFarthestPosition_return position;
|
findFarthestPosition_return position;
|
||||||
bool moved = false;
|
bool moved = false;
|
||||||
|
@ -411,14 +438,16 @@ void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
|
||||||
tile = Grid_cellContent(cell);
|
tile = Grid_cellContent(cell);
|
||||||
if (tile.value > 0) {
|
if (tile.value > 0) {
|
||||||
position = Game_findFarthestPosition(cell, vector);
|
position = Game_findFarthestPosition(cell, vector);
|
||||||
next = position.next;
|
next = Grid_cellContent(position.next);
|
||||||
farthest = position.farthest;
|
farthest = position.farthest;
|
||||||
if (Grid_cellContent(next).value == tile.value && !tile.hasMerged && !Grid_cellContent(next).hasMerged) { // Merge
|
if (next.value == tile.value && !next.hasMerged) { // Merge
|
||||||
|
merged = Grid_getEmptyTile();
|
||||||
merged.x = next.x;
|
merged.x = next.x;
|
||||||
merged.y = next.y;
|
merged.y = next.y;
|
||||||
merged.value = tile.value + 1;
|
merged.value = tile.value + 1;
|
||||||
merged.hasMerged = true;
|
merged.hasMerged = true;
|
||||||
merged.previousPosition = cell;
|
merged.previousPosition = next.previousPosition;
|
||||||
|
merged.previousMerge = cell;
|
||||||
|
|
||||||
Grid_insertTile(merged);
|
Grid_insertTile(merged);
|
||||||
Grid_removeTile(tile);
|
Grid_removeTile(tile);
|
||||||
|
@ -453,7 +482,6 @@ void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
|
||||||
int initGame() {
|
int initGame() {
|
||||||
// Variables
|
// Variables
|
||||||
int x, y;
|
int x, y;
|
||||||
Cell emptyCell;
|
|
||||||
|
|
||||||
// Reset variables
|
// Reset variables
|
||||||
srand(RTC_getTicks());
|
srand(RTC_getTicks());
|
||||||
|
@ -462,16 +490,12 @@ int initGame() {
|
||||||
Game_won = false;
|
Game_won = false;
|
||||||
Game_keepPlaying = true;
|
Game_keepPlaying = true;
|
||||||
|
|
||||||
emptyCell.x = -1;
|
|
||||||
emptyCell.y = -1;
|
|
||||||
|
|
||||||
for (x = 0; x <= 3; x++) {
|
for (x = 0; x <= 3; x++) {
|
||||||
for (y = 0; y <= 3; y++) {
|
for (y = 0; y <= 3; y++) {
|
||||||
|
Grid_grid.array[x][y] = Grid_getEmptyTile();
|
||||||
Grid_grid.array[x][y].x = x;
|
Grid_grid.array[x][y].x = x;
|
||||||
Grid_grid.array[x][y].y = y;
|
Grid_grid.array[x][y].y = y;
|
||||||
Grid_grid.array[x][y].value = 0;
|
Grid_grid.array[x][y].value = 0;
|
||||||
Grid_grid.array[x][y].hasMerged = false;
|
|
||||||
Grid_grid.array[x][y].previousPosition = emptyCell;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
@echo off
|
@echo off
|
||||||
rem Do not edit! This batch file is created by CASIO fx-9860G SDK.
|
rem Do not edit! This batch file is created by CASIO fx-9860G SDK.
|
||||||
|
|
||||||
if exist debug\*.obj del debug\*.obj
|
|
||||||
if exist 2048.G1A del 2048.G1A
|
if exist 2048.G1A del 2048.G1A
|
||||||
|
|
||||||
cd debug
|
cd debug
|
||||||
|
|
Reference in a new issue