Skip to content

Commit

Permalink
refactored the code & added namespace 'move'
Browse files Browse the repository at this point in the history
  • Loading branch information
AnasImloul committed Oct 30, 2023
1 parent 2e50a04 commit 4a28d32
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ grid::grid() {
memset(tiles, 0, sizeof(tiles));
score = 0;
empty = SIZE * SIZE;
blocked = false;
add();
add();
}
Expand Down
6 changes: 2 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ int main() {

int move;
do {
int bestScore = 0;
int bestDir = -1;
bestMove(g, 12, bestScore, bestDir);
move = bestDir;
int a = 0;
move = move::bestMove(g, 6);
g.move(move);
g.show();
std::cout << "Score: " << g.getScore() << std::endl;
Expand Down
52 changes: 31 additions & 21 deletions move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,42 @@

#define max(a, b) ((a) > (b) ? (a) : (b))

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

int bestMove(const grid& g, int depth, int &bestScore, int &bestDir, int move) {

if (depth == 0 || g.isBlocked()) {
return evaluate(g);
namespace move {
int bestMove(const grid& g, int depth) {
int bestScore = 0, bestDir = -1;
bestMoveHelper(g, depth, bestScore, bestDir, -1);
return bestDir;
}

if (move == -1) {
// if the move is -1, we are at the root of the tree
// we will have dynamic depth based on the number of empty tiles
// the less empty tiles, the more depth
depth = max(depth * (SIZE - g.getEmpty() / SIZE) / SIZE, 4);
inline int evaluate(const grid& g) {
return (g.getEmpty() << 16) + g.getScore();
}

for (int dir : DIRECTIONS) {
grid g1(g);
g1.move(dir);
int score = bestMove(g1, depth - 1, bestScore, bestDir, (move == -1 ? dir : move));
if (score > bestScore) {
bestScore = score;
bestDir = move;
namespace {
int bestMoveHelper(const grid& g, int depth, int &bestScore, int &bestDir, int move) {
if (depth == 0 || g.isBlocked()) {
return evaluate(g);
}

if (move == -1) {
// if the move is -1, we are at the root of the tree
// we will have dynamic depth based on the number of empty tiles
// the less empty tiles, the more depth
depth = max(depth * (SIZE - g.getEmpty() / SIZE) / SIZE, 4);
}

for (int dir : DIRECTIONS) {
grid g1(g);
g1.move(dir);
int score = bestMoveHelper(g1, depth - 1, bestScore, bestDir, (move == -1 ? dir : move));
if (score > bestScore) {
bestScore = score;
bestDir = move;
}
}

return bestScore;
}
}

return bestScore;
}
11 changes: 9 additions & 2 deletions move.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
#include "move.h"
#include "grid.h"

int bestMove(const grid& g, int depth, int &bestScore, int &bestDir, int move=-1);
namespace move {
int bestMove(const grid &g, int depth);

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

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

}

0 comments on commit 4a28d32

Please sign in to comment.