Added easy extension of custom merge results. Example fib merge rules added. Code altered to make use of this new style
parent
375cdeb7da
commit
6d247f282f
13 changed files with 129 additions and 84 deletions
@ -0,0 +1,30 @@ |
||||
#include "assert.h" |
||||
#include "merge.h" |
||||
|
||||
#define MERGE_GOAL 11 |
||||
|
||||
:a |
||||
const long merge_values[] = { |
||||
0, 2, 4, 8, 16, 32, 64, 128, 256, 512, |
||||
1024, 2048 |
||||
}; |
||||
|
||||
inline long merge_value(const int v1) |
||||
{ |
||||
return v1 <= MERGE_GOAL ? merge_values[v1] : -1; |
||||
} |
||||
|
||||
inline long merge_goal(void) |
||||
{ |
||||
return MERGE_GOAL; |
||||
} |
||||
|
||||
inline int merge_possible(const int v1, const int v2) |
||||
{ |
||||
return v1 == v2; |
||||
} |
||||
|
||||
inline int merge_result(const int v1, const int v2) |
||||
{ |
||||
return merge_possible(v1, v2) ? v1 + 1 : -1; |
||||
} |
@ -0,0 +1,21 @@ |
||||
#ifndef MERGE_H |
||||
#define MERGE_H |
||||
|
||||
/* When defining a new set of rules, remember that the grid values (and the
|
||||
* therefore the arguments v1, v2) are index values and should be treated as |
||||
* such */ |
||||
|
||||
/* This should return the value of a given index. 0 should default to 0 (empty) */ |
||||
long merge_value(const int v1); |
||||
|
||||
/* This should return the goal value */ |
||||
long merge_goal(void); |
||||
|
||||
/* Return if a merge is possible between two indices */ |
||||
int merge_possible(const int v1, const int v2); |
||||
|
||||
/* The result of a merge of two values. If the two parameters are not mergeable,
|
||||
* then a -1 error code is the suggested return value */ |
||||
int merge_result(const int v1, const int v2); |
||||
|
||||
#endif |
@ -0,0 +1,30 @@ |
||||
#include "assert.h" |
||||
#include "merge.h" |
||||
|
||||
#define MERGE_GOAL 17 |
||||
|
||||
const long merge_values[] = { |
||||
0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, |
||||
233, 377, 610, 987, 1597, 2584 |
||||
}; |
||||
|
||||
inline long merge_value(const int v1) |
||||
{ |
||||
return v1 <= MERGE_GOAL ? merge_values[v1] : -1; |
||||
} |
||||
|
||||
inline long merge_goal(void) |
||||
{ |
||||
return MERGE_GOAL; |
||||
} |
||||
|
||||
inline int merge_possible(const int v1, const int v2) |
||||
{ |
||||
return v1 == v2 - 1 || v2 == v1 - 1; |
||||
} |
||||
|
||||
inline int merge_result(const int v1, const int v2) |
||||
{ |
||||
int max = v1 > v2 ? v1 : v2; |
||||
return merge_possible(v1, v2) ? max + 1 : -1; |
||||
} |
Loading…
Reference in new issue