From 42ddab80a36d65957ef6a5bc6ac5195b2b51cd48 Mon Sep 17 00:00:00 2001 From: Anas_Imloul <76872415+AnasImloul@users.noreply.github.com> Date: Fri, 3 Jun 2022 15:15:10 +0100 Subject: [PATCH] Update dynamic.cpp For some not so obvious reason, the maxsearch algorithm only works when depth == 1 otherwise it gives a random result. --- dynamic.cpp | 151 ++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 81 deletions(-) diff --git a/dynamic.cpp b/dynamic.cpp index 2cba10d..b126c8d 100644 --- a/dynamic.cpp +++ b/dynamic.cpp @@ -1,6 +1,7 @@ #include "move.cpp" #include #include +#include #include @@ -8,19 +9,14 @@ typedef Move (*Moves) (long* grid, const int &rows, const int &columns); -void copy(const long* src, long* dest){ - long size = sizeof(src)/sizeof(src[0]); - for (int i = 0; i= 0; row--){ if (grid[columns * row + col] == 0){ - blocked = false; + continue; } @@ -126,7 +122,7 @@ Move Down(long* grid, const int &rows, const int &columns){ } } } - return Move(merged,score,blocked,grid,1); + return Move(merged,score,blocked,grid); } @@ -142,7 +138,7 @@ Move Left(long* grid, const int &rows, const int &columns){ for (int row = 0; row < rows; row++){ for (int col = 0; col < columns; col++){ if (grid[columns * row + col] == 0){ - blocked = false; + continue; } @@ -181,7 +177,7 @@ Move Left(long* grid, const int &rows, const int &columns){ } } } - return Move(merged,score,blocked,grid,2); + return Move(merged,score,blocked,grid); } @@ -197,7 +193,7 @@ Move Right(long* grid, const int &rows, const int &columns){ for (int row = 0; row < rows; row++){ for (int col = columns - 1; col >= 0; col--){ if (grid[columns * row + col] == 0){ - blocked = false; + continue; } @@ -234,127 +230,120 @@ Move Right(long* grid, const int &rows, const int &columns){ } } } - return Move(merged,score,blocked,grid,3); + return Move(merged,score,blocked,grid); } -void random_add(long* grid, int rows, int columns, int &slots, long value){ - srand(time(0)); +void random_add(long* grid, int rows, int columns, long value){ - if (slots>0){ int size = rows*columns; - - - int* empty_slots = (int*) malloc(slots*sizeof(int)); - - + int start = rand()%size; - int index = 0; for (int i = 0; i max_score) max_score = move_tracker; } + + //std::cout << std::endl; + //std::cout << max_score.blocked << " " << max_score.merged << " " << max_score.score << std::endl; + //std::cout << std::endl; } else { - std::vector next_states; - bool move_blocked = false; + bool move_blocked = true; for (int i = 0; i()); - - - if (!move_blocked) next_states.pop_back(); - - - - - for (const auto &state : next_states){ - merged = state.merged; - score = state.score; - blocked = state.blocked; + move_blocked = move_blocked && blocked; + + merged = next_move.merged; + score = next_move.score; + blocked = next_move.blocked; + - if (!blocked){ - //random_add(state.grid, rows, columns, slots, 2); - MoveTracker next_depth = maxsearch(state.grid, rows, columns, slots, moves, moves_count, depth - 1); + if (!blocked){ + + random_add(next_grid, rows, columns, 2); + + + MoveTracker next_depth = maxsearch(next_grid, rows, columns, moves, moves_count, depth - 1); + next_depth.merged += merged; next_depth.score += score; - next_depth.path.push_back(state.path[0]); + next_depth.blocked = blocked; + next_depth.path.push_back(i); - max_score = std::max(max_score, next_depth); + if (next_depth > max_score) max_score = next_depth; } + } + if (move_blocked){max_score.path.push_back(rand()%moves_count);} } + + return max_score; }