khetai 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1883bbf3758483b6771f1fce6c7ff1c9bc6625a88035ca57f71efb7e03b25e9d
4
- data.tar.gz: 62a0eb4c822754885c6e92d4ad07a78301e6d566b2f804e4fb03db31f4192a6a
3
+ metadata.gz: d169f4a004e24dd36c20cc0483e97089288c37d9a883102902e57ad90b9da52c
4
+ data.tar.gz: 4220a784bde8231944b7cd856839f651c7b7d0491748e6597a1d6a94c58c468d
5
5
  SHA512:
6
- metadata.gz: bcd3d8db78079c7373a228f53408444341d43f5abbc96402d5775cd3f379554b3790152bd7dc1d17604d956dcf2d07329149098a89f6d54df94222f4a6b6c637
7
- data.tar.gz: c1e1ca8ce28b5e690720de15a32af6938970ad6b86fa759bd8cee1f101f8d607642f0d05b6861f8507794e3950e69a03cd76797121d3038ec19193fda36ba6cf
6
+ metadata.gz: 74a55ef185db9d73c58e9e148a3e124f5850da6304be611d7466d8951e43b6d3165e9fbf5ae98654c12861a49e3281bf6a299f03f0b55e9d63ee51c6b76d2793
7
+ data.tar.gz: 6409ac627699d0daa5a7cacb4ce1da174be6f433228466b7cd0ffd04e7c47d9830f11de41e629313b38b776dd7a41647aa82e405f49dc18d69e03f23a14a8f65
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- khetai (0.2.1)
4
+ khetai (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -51,16 +51,20 @@ The internals of the gem are written in C, located in `ext/khetai`.
51
51
  $ bundle exec rake build
52
52
  $ gem install pkg/khetai-<version>.gem
53
53
 
54
+ Once tested and verified, bump the version number in `lib/khetai/version.rb` and commit changes.
54
55
 
55
56
  To release and push to rubygems.org:
56
57
 
57
58
  $ bundle exec rake release
59
+
60
+ To push a pre-built gem manually:
61
+
58
62
  $ gem push pkg/<gem>
59
63
 
60
64
  ## Manual Testing
61
65
 
62
- There is a GUI test harness written in C++ using FLTK available in the `ext/khetai/dev/fltk-ui` directory.
66
+ There is a GUI test harness written in C++ using FLTK available in the [ext/khetai/dev/fltk-ui](/ext/khetai/dev/fltk-ui) directory.
63
67
 
64
68
  ### Why does this exist?
65
69
 
66
- To learn, build, and have fun.
70
+ As something to work on and learn from.
@@ -52,6 +52,11 @@ void (*AILoader::get_init_zobrist())()
52
52
  return reinterpret_cast<void (*)()>(get_symbol("init_zobrist"));
53
53
  }
54
54
 
55
+ void (*AILoader::get_reset_undo())()
56
+ {
57
+ return reinterpret_cast<void (*)()>(get_symbol("reset_undo"));
58
+ }
59
+
55
60
  void (*AILoader::get_setup_board())(char **)
56
61
  {
57
62
  return reinterpret_cast<void (*)(char **)>(get_symbol("setup_board"));
@@ -13,6 +13,7 @@ public:
13
13
  ~AILoader();
14
14
 
15
15
  void (*get_init_zobrist())();
16
+ void (*get_reset_undo())();
16
17
  void (*get_setup_board())(char **);
17
18
  void (*get_print_board())();
18
19
  void (*get_set_time_parameters())(int, time_t);
@@ -54,6 +54,7 @@ void free_c_array(char **c_array, size_t size)
54
54
  Move call_ai_move(AILoader &ai_loader, const std::vector<std::vector<std::string>> &board_pieces, Player player, int max_depth, int max_time)
55
55
  {
56
56
  auto init_zobrist = ai_loader.get_init_zobrist();
57
+ auto reset_undo = ai_loader.get_reset_undo();
57
58
  auto setup_board = ai_loader.get_setup_board();
58
59
  auto print_board = ai_loader.get_print_board();
59
60
  auto set_time_parameters = ai_loader.get_set_time_parameters();
@@ -65,6 +66,7 @@ Move call_ai_move(AILoader &ai_loader, const std::vector<std::vector<std::string
65
66
 
66
67
  char **c_board = vector_to_c_array(flatten_2d_vector_with_buffer(board_pieces));
67
68
 
69
+ reset_undo();
68
70
  init_zobrist();
69
71
  srand((unsigned)time(NULL));
70
72
 
data/ext/khetai/khetai.c CHANGED
@@ -40,7 +40,7 @@ VALUE move(VALUE self, VALUE board_array, VALUE _player, VALUE _max_depth, VALUE
40
40
  VALUE element = rb_ary_entry(board_array, i);
41
41
  if (!RB_TYPE_P(element, T_STRING) || RSTRING_LEN(element) != 2)
42
42
  {
43
- rb_raise(rb_eArgError, "each element in the board_array must be 2 characters");
43
+ rb_raise(rb_eArgError, "each element in board_array must be 2 characters");
44
44
  }
45
45
 
46
46
  // check if element in board_array is a valid string
@@ -56,7 +56,7 @@ VALUE move(VALUE self, VALUE board_array, VALUE _player, VALUE _max_depth, VALUE
56
56
  }
57
57
  if (!is_valid)
58
58
  {
59
- rb_raise(rb_eArgError, "each element in the board_array must be a valid piece (example: 'p1') or empty ('--')");
59
+ rb_raise(rb_eArgError, "each element in board_array must be a valid piece (example: 'p1') or empty ('--')");
60
60
  }
61
61
  }
62
62
 
@@ -11,6 +11,7 @@ Square board[120] = {0};
11
11
  int pharaoh_loc[2] = {0};
12
12
  enum Player whose_turn;
13
13
  enum Player starter;
14
+ int initial_depth = 0;
14
15
 
15
16
  Move undo_moves[MAX_DEPTH] = {0};
16
17
  int undo_capture_indices[MAX_DEPTH] = {0};
@@ -29,6 +30,7 @@ Move alphabeta_root(int depth, enum Player player)
29
30
  {
30
31
  whose_turn = player;
31
32
  starter = player;
33
+ initial_depth = depth;
32
34
  int best_score = -MAX_SCORE;
33
35
  Move best_move = (Move)0;
34
36
  int alpha = -MAX_SCORE;
@@ -71,8 +73,10 @@ int alphabeta(int depth, enum Player player, int alpha, int beta)
71
73
  int alpha_orig = alpha;
72
74
  Move valid_moves[NUM_VALID_MOVES] = {0};
73
75
  int vi = 0;
76
+
77
+ int table_depth = initial_depth - depth;
74
78
  HashEntry *entry = search_table(hashes[move_num]);
75
- if (entry->key == hashes[move_num] && entry->depth >= depth)
79
+ if (entry->key == hashes[move_num] && entry->depth > table_depth)
76
80
  {
77
81
  if (entry->flag == EXACT)
78
82
  return entry->score;
@@ -123,19 +127,19 @@ int alphabeta(int depth, enum Player player, int alpha, int beta)
123
127
  else if (best_score >= beta)
124
128
  flag = ALPHA;
125
129
 
126
- insert_table(hashes[move_num], depth, flag, best_score, best_move);
130
+
131
+ insert_table(entry, hashes[move_num], table_depth, flag, best_score, best_move);
127
132
  return best_score;
128
133
  }
129
134
 
130
- void insert_table(uint64_t key, int depth, int flag, int score, Move move)
135
+ void insert_table(HashEntry *entry, uint64_t key, int table_depth, int flag, int score, Move move)
131
136
  {
132
- HashEntry *entry = search_table(key);
133
137
  if (entry->key != 0)
134
138
  {
135
- if (depth < entry->depth)
139
+ if (table_depth > entry->depth)
136
140
  {
137
141
  entry->key = key;
138
- entry->depth = depth;
142
+ entry->depth = table_depth;
139
143
  entry->flag = flag;
140
144
  entry->score = score;
141
145
  entry->move = move;
@@ -144,7 +148,7 @@ void insert_table(uint64_t key, int depth, int flag, int score, Move move)
144
148
  else
145
149
  {
146
150
  entry->key = key;
147
- entry->depth = depth;
151
+ entry->depth = table_depth;
148
152
  entry->flag = flag;
149
153
  entry->score = score;
150
154
  entry->move = move;
@@ -154,7 +158,7 @@ void insert_table(uint64_t key, int depth, int flag, int score, Move move)
154
158
  int calculate_score()
155
159
  {
156
160
  int score = 0;
157
- int anubis_score = 500;
161
+ int anubis_score = 800;
158
162
  int pyramid_score = 1000;
159
163
  int pharaoh_score = 100000;
160
164
  for (int i = 0; i < 120; i++)
@@ -467,6 +471,7 @@ void reset_undo()
467
471
 
468
472
  void setup_board(char *init_board[120])
469
473
  {
474
+ move_num = 0;
470
475
  uint64_t hash = 0;
471
476
  for (int i = 0; i < 120; i++)
472
477
  {
@@ -38,6 +38,7 @@ enum Orientation
38
38
 
39
39
  extern enum Player whose_turn;
40
40
  extern enum Player starter;
41
+ extern int initial_depth;
41
42
 
42
43
  // north, east, south, west, diagonals
43
44
  static const int directions[8] = {-12, 1, 12, -1, (12 + 1), (12 - 1), (-12 + 1), (-12 - 1)};
@@ -223,6 +224,6 @@ typedef struct HashEntry
223
224
 
224
225
  extern HashEntry table[TABLE_SIZE];
225
226
  static inline HashEntry *search_table(uint64_t key) { return &table[key % TABLE_SIZE]; };
226
- void insert_table(uint64_t key, int depth, int flag, int score, Move move);
227
+ void insert_table(HashEntry *entry, uint64_t key, int depth, int flag, int score, Move move);
227
228
 
228
229
  #endif // KHET_LIB_H_INCLUDED
@@ -1,3 +1,3 @@
1
1
  module KhetAI
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: khetai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - jkugs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-17 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: