Skip to content

Commit

Permalink
Added few optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
AnasImloul committed Oct 31, 2023
1 parent b1ec64f commit aa3136d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 74 deletions.
106 changes: 47 additions & 59 deletions grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ grid::grid(const grid &g) {
empty = g.empty;
}

grid::grid(int8_t tiles[SIZE][SIZE], int64_t score) {
grid::grid(int8_t tiles[SIZE * SIZE], int64_t score) {
memcpy(this->tiles, tiles, sizeof(this->tiles));
this->score = score;
empty = 0;
for (int i = 0; i < SIZE; i++) {
for (int j : tiles[i]) {
if (j == 0) empty++;
}
for (int i = 0; i < SIZE * SIZE; i++) {
if (tiles[i] == 0) empty++;
}
}

Expand All @@ -42,24 +40,21 @@ inline int length(long long n) {
return count;
}

inline int maxLength(int8_t tiles[SIZE][SIZE]) {
inline int maxLength(int8_t tiles[SIZE * SIZE]) {
int result = 0;
for (int i = 0; i < SIZE; i++) {
for (int tile : tiles[i]) {
result = max(result, length(1 << tile));
}
for (int i = 0; i < SIZE * SIZE; i++) {
result = max(result, length(1 << tiles[i]));
}
return result;
}

void grid::show() {
int maxLength1 = maxLength(tiles);
for (auto & tile : tiles) {
for (int j : tile) {
long long value = (j != 0 ? (1 << j) : 0);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
long long value = (tiles[i * SIZE + j] != 0 ? (1 << tiles[i * SIZE + j]) : 0);
std::cout << value << std::string(maxLength1 - length(value), ' ') << " ";
}

for (int i = 0; i < 1 + maxLength1 / 3; i++)
std::cout << std::endl;
}
Expand All @@ -75,27 +70,26 @@ bool grid::up() {
bool merged = false;
for (int row = 0; row < SIZE; row++) {
// if the tile is empty, skip it
if (tiles[row][col] == 0) {
blocked = false;
if (tiles[row * SIZE + col] == 0) {
continue;
}

// if this is the first tile, move it to the first row
if (last == -1) {
last = 0; // set the tile to the first one
if (row != 0) {
tiles[last][col] = tiles[row][col];
tiles[row][col] = 0;
tiles[last * SIZE + col] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
}
continue;
}

// if the tile is the same as the last one, merge them
if (tiles[last][col] == tiles[row][col] && !merged) {
tiles[last][col]++;
tiles[row][col] = 0;
score += (1 << tiles[last][col]);
if (tiles[last * SIZE + col] == tiles[row * SIZE + col] && !merged) {
tiles[last * SIZE + col]++;
tiles[row * SIZE + col] = 0;
score += (1 << tiles[last * SIZE + col]);
empty++;
blocked = false;
merged = true;
Expand All @@ -104,8 +98,8 @@ bool grid::up() {

// if the tile is different from the last one, move it
if (last + 1 != row) {
tiles[++last][col] = tiles[row][col];
tiles[row][col] = 0;
tiles[++last * SIZE + col] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
merged = false;
continue;
Expand All @@ -126,27 +120,26 @@ bool grid::down() {
bool merged = false;
for (int row = SIZE - 1; row >= 0; row--) {
// if the tile is empty, skip it
if (tiles[row][col] == 0) {
blocked = false;
if (tiles[row * SIZE + col] == 0) {
continue;
}

// if this is the first tile, move it to the first row
if (last == -1) {
last = SIZE - 1; // set the tile to the first one
if (row != SIZE - 1) {
tiles[last][col] = tiles[row][col];
tiles[row][col] = 0;
tiles[last * SIZE + col] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
}
continue;
}

// if the tile is the same as the last one, merge them
if (tiles[last][col] == tiles[row][col] && !merged) {
tiles[last][col]++;
tiles[row][col] = 0;
score += (1 << tiles[last][col]);
if (tiles[last * SIZE + col] == tiles[row * SIZE + col] && !merged) {
tiles[last * SIZE + col]++;
tiles[row * SIZE + col] = 0;
score += (1 << tiles[last * SIZE + col]);
empty++;
blocked = false;
merged = true;
Expand All @@ -155,8 +148,8 @@ bool grid::down() {

// if the tile is different from the last one, move it
if (last - 1 != row) {
tiles[--last][col] = tiles[row][col];
tiles[row][col] = 0;
tiles[--last * SIZE + col] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
merged = false;
continue;
Expand All @@ -177,27 +170,26 @@ bool grid::left() {
bool merged = false;
for (int col = 0; col < SIZE; col++) {
// if the tile is empty, skip it
if (tiles[row][col] == 0) {
blocked = false;
if (tiles[row * SIZE + col] == 0) {
continue;
}

// if this is the first tile, move it to the first row
if (last == -1) {
last = 0; // set the tile to the first one
if (col != 0) {
tiles[row][last] = tiles[row][col];
tiles[row][col] = 0;
tiles[row * SIZE + last] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
}
continue;
}

// if the tile is the same as the last one, merge them
if (tiles[row][last] == tiles[row][col] && !merged) {
tiles[row][last]++;
tiles[row][col] = 0;
score += (1 << tiles[last][col]);
if (tiles[row * SIZE + last] == tiles[row * SIZE + col] && !merged) {
tiles[row * SIZE + last]++;
tiles[row * SIZE + col] = 0;
score += (1 << tiles[row * SIZE + last]);
empty++;
blocked = false;
merged = true;
Expand All @@ -206,8 +198,8 @@ bool grid::left() {

// if the tile is different from the last one, move it
if (last + 1 != col) {
tiles[row][++last] = tiles[row][col];
tiles[row][col] = 0;
tiles[row * SIZE + ++last] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
merged = false;
continue;
Expand All @@ -228,27 +220,26 @@ bool grid::right() {
bool merged = false;
for (int col = SIZE - 1; col >= 0; col--) {
// if the tile is empty, skip it
if (tiles[row][col] == 0) {
blocked = false;
if (tiles[row * SIZE + col] == 0) {
continue;
}

// if this is the first tile, move it to the first row
if (last == -1) {
last = SIZE - 1; // set the tile to the first one
if (col != SIZE - 1) {
tiles[row][last] = tiles[row][col];
tiles[row][col] = 0;
tiles[row * SIZE + last] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
}
continue;
}

// if the tile is the same as the last one, merge them
if (tiles[row][last] == tiles[row][col] && !merged) {
tiles[row][last]++;
tiles[row][col] = 0;
score += (1 << tiles[last][col]);
if (tiles[row * SIZE + last] == tiles[row * SIZE + col] && !merged) {
tiles[row * SIZE + last]++;
tiles[row * SIZE + col] = 0;
score += (1 << tiles[row * SIZE + last]);
empty++;
blocked = false;
merged = true;
Expand All @@ -257,8 +248,8 @@ bool grid::right() {

// if the tile is different from the last one, move it
if (last - 1 != col) {
tiles[row][--last] = tiles[row][col];
tiles[row][col] = 0;
tiles[row * SIZE + --last] = tiles[row * SIZE + col];
tiles[row * SIZE + col] = 0;
blocked = false;
merged = false;
continue;
Expand All @@ -278,11 +269,8 @@ bool grid::add() {
int8_t value = (int8_t)((rand() % 10 < 9) ? 1 : 2);

for (int i = 0; i < SIZE * SIZE; i++) {
row = index / SIZE;
col = index % SIZE;

if (tiles[row][col] == 0) {
tiles[row][col] = value;
if (tiles[index] == 0) {
tiles[index] = value;
empty--;
return true;
}
Expand Down
6 changes: 2 additions & 4 deletions grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class grid {
public:
grid();

grid(int8_t tiles[SIZE][SIZE], int64_t score);
grid(int8_t tiles[SIZE * SIZE], int64_t score);

grid(const grid &g);

Expand All @@ -38,16 +38,14 @@ class grid {

bool move(int direction);

inline int8_t getTile(int i, int j) { return tiles[i][j]; }

inline int64_t getScore() const { return score; }
inline int16_t getEmpty() const { return empty; }

private:

bool add();

int8_t tiles[SIZE][SIZE]; // tiles on the grid (0 = empty, 2^x = tile with value x)
int8_t tiles[SIZE * SIZE]; // tiles on the grid (0 = empty, 2^x = tile with value x)
int64_t score; // current score
int16_t empty; // number of empty tiles

Expand Down
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "grid.h"
#include "move.h"

#pragma GCC optimize("O7")

void fastIO() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
Expand All @@ -17,8 +19,7 @@ int main() {

int move;
do {
int a = 0;
move = move::bestMove(g, 6);
move = move::bestMove(g, 8);
g.move(move);
g.show();
std::cout << "Score: " << g.getScore() << std::endl;
Expand Down
12 changes: 6 additions & 6 deletions move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#define max(a, b) ((a) > (b) ? (a) : (b))

namespace move {
int bestMove(const grid& g, int depth) {
int bestScore = 0, bestDir = -1;
bestMoveHelper(g, depth, bestScore, bestDir, -1);
int bestMove(grid& g, int depth) {
int64_t bestScore = 0;
int bestDir = -1, calls = 0;
bestMoveHelper(g, depth, bestScore, bestDir, -1, calls);
std::cout << "calls: " << calls << std::endl;
return bestDir;
}

inline int evaluate(const grid& g) {
return (g.getEmpty() << 16) + g.getScore();
}
inline int64_t evaluate(grid& g) { return (g.getEmpty() << 16) + g.getScore(); }

namespace {
int bestMoveHelper(const grid& g, int depth, int &bestScore, int &bestDir, int move) {
Expand Down
6 changes: 3 additions & 3 deletions move.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include "grid.h"

namespace move {
int bestMove(const grid &g, int depth);
int bestMove(grid &g, int depth);

inline int evaluate(const grid &g);
inline int64_t evaluate(grid &g);

namespace {
int bestMoveHelper(const grid& g, int depth, int &bestScore, int &bestDir, int move);
void bestMoveHelper(grid& g, int depth, int64_t &bestScore, int &bestDir, int move, int& calls);
}

}

0 comments on commit aa3136d

Please sign in to comment.