khetai 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
1
  #ifndef KHET_LIB_H_INCLUDED
2
2
  #define KHET_LIB_H_INCLUDED
3
3
 
4
- #include <stdint.h>
5
4
  #include <stdbool.h>
5
+ #include <stdint.h>
6
+ #include <time.h>
6
7
 
7
8
  #ifdef __cplusplus
8
9
  #include <ctime>
@@ -15,25 +16,22 @@ typedef uint32_t Move;
15
16
  #define MAX_SCORE 9999999
16
17
  #define MAX_DEPTH 25
17
18
 
18
- enum Player
19
- {
20
- Silver,
21
- Red
19
+ enum Player {
20
+ SILVER,
21
+ RED
22
22
  };
23
- enum Piece
24
- {
25
- Anubis = 1,
26
- Pyramid = 2,
27
- Scarab = 3,
28
- Pharaoh = 4,
29
- Sphinx = 5
23
+ enum Piece {
24
+ ANUBIS = 1,
25
+ PYRAMID = 2,
26
+ SCARAB = 3,
27
+ PHARAOH = 4,
28
+ SPHINX = 5
30
29
  };
31
- enum Orientation
32
- {
33
- North,
34
- East,
35
- South,
36
- West
30
+ enum Orientation {
31
+ NORTH,
32
+ EAST,
33
+ SOUTH,
34
+ WEST
37
35
  };
38
36
 
39
37
  extern enum Player whose_turn;
@@ -92,17 +90,16 @@ extern time_t start_time;
92
90
  extern int max_time;
93
91
 
94
92
  #ifdef __cplusplus
95
- extern "C"
96
- {
93
+ extern "C" {
97
94
  #endif
98
95
 
99
- void set_time_parameters(int _max_time, time_t _start_time);
100
- void reset_undo();
101
- void init_zobrist();
102
- void setup_board(char *board[]);
103
- Move alphabeta_root(int depth, enum Player player);
104
- void make_move(Move move);
105
- void print_board();
96
+ void set_time_parameters(int _max_time, time_t _start_time);
97
+ void reset_undo();
98
+ void init_zobrist();
99
+ void setup_board(char *board[]);
100
+ Move alphabeta_root(int depth, enum Player player);
101
+ void make_move(Move move);
102
+ void print_board();
106
103
 
107
104
  #ifdef __cplusplus
108
105
  }
@@ -137,18 +134,16 @@ static inline int get_end(Move m) { return m >> 8 & 0x7F; }
137
134
  static inline int get_rotation(Move m) { return (m >> 15 & 0x3) - 2; }
138
135
 
139
136
  #ifdef __cplusplus
140
- extern "C"
141
- {
137
+ extern "C" {
142
138
  #endif
143
- int get_start_wrapper(Move move);
144
- int get_end_wrapper(Move move);
145
- int get_rotation_wrapper(Move move);
139
+ int get_start_wrapper(Move move);
140
+ int get_end_wrapper(Move move);
141
+ int get_rotation_wrapper(Move move);
146
142
  #ifdef __cplusplus
147
143
  }
148
144
  #endif
149
145
 
150
- static inline Square rotate(Square s, int rotation)
151
- {
146
+ static inline Square rotate(Square s, int rotation) {
152
147
  int orientation = get_orientation(s);
153
148
  orientation = (orientation + rotation) % 4;
154
149
  if (orientation < 0)
@@ -156,65 +151,62 @@ static inline Square rotate(Square s, int rotation)
156
151
  return (s & 0x1F) + (orientation << 5);
157
152
  }
158
153
 
159
- static inline enum Player opposite_player(enum Player player)
160
- {
161
- return player == Red ? Silver : Red;
154
+ static inline enum Player opposite_player(enum Player player) {
155
+ return player == RED ? SILVER : RED;
162
156
  }
163
157
 
164
- #define Dead -1
165
- #define Absorbed -2
158
+ #define DEAD -1
159
+ #define ABSORBED -2
166
160
 
167
161
  // [laser direciton][piece type][piece orientation] = reflection result
168
162
  // anubis, pyramid, scarab, pharaoh, sphinx
169
163
  static const int reflections[4][5][4] = {
170
164
  {// North
171
- {Dead, Dead, Absorbed, Dead},
172
- {Dead, East, West, Dead},
173
- {West, East, West, East},
174
- {Dead, Dead, Dead, Dead},
175
- {Absorbed, Absorbed, Absorbed, Absorbed}},
165
+ {DEAD, DEAD, ABSORBED, DEAD},
166
+ {DEAD, EAST, WEST, DEAD},
167
+ {WEST, EAST, WEST, EAST},
168
+ {DEAD, DEAD, DEAD, DEAD},
169
+ {ABSORBED, ABSORBED, ABSORBED, ABSORBED}},
176
170
  {// East
177
- {Dead, Dead, Dead, Absorbed},
178
- {Dead, Dead, South, North},
179
- {South, North, South, North},
180
- {Dead, Dead, Dead, Dead},
181
- {Absorbed, Absorbed, Absorbed, Absorbed}},
171
+ {DEAD, DEAD, DEAD, ABSORBED},
172
+ {DEAD, DEAD, SOUTH, NORTH},
173
+ {SOUTH, NORTH, SOUTH, NORTH},
174
+ {DEAD, DEAD, DEAD, DEAD},
175
+ {ABSORBED, ABSORBED, ABSORBED, ABSORBED}},
182
176
  {// South
183
- {Absorbed, Dead, Dead, Dead},
184
- {East, Dead, Dead, West},
185
- {East, West, East, West},
186
- {Dead, Dead, Dead, Dead},
187
- {Absorbed, Absorbed, Absorbed, Absorbed}},
177
+ {ABSORBED, DEAD, DEAD, DEAD},
178
+ {EAST, DEAD, DEAD, WEST},
179
+ {EAST, WEST, EAST, WEST},
180
+ {DEAD, DEAD, DEAD, DEAD},
181
+ {ABSORBED, ABSORBED, ABSORBED, ABSORBED}},
188
182
  {// West
189
- {Dead, Absorbed, Dead, Dead},
190
- {North, South, Dead, Dead},
191
- {North, South, North, South},
192
- {Dead, Dead, Dead, Dead},
193
- {Absorbed, Absorbed, Absorbed, Absorbed}}};
183
+ {DEAD, ABSORBED, DEAD, DEAD},
184
+ {NORTH, SOUTH, DEAD, DEAD},
185
+ {NORTH, SOUTH, NORTH, SOUTH},
186
+ {DEAD, DEAD, DEAD, DEAD},
187
+ {ABSORBED, ABSORBED, ABSORBED, ABSORBED}}};
194
188
 
195
189
  extern uint64_t keys[0xFF][120];
196
190
  extern uint64_t hashes[MAX_DEPTH];
197
191
  extern uint64_t turn_key;
198
- extern int move_num;
192
+ extern int hashes_index;
199
193
  extern bool checkmate;
200
194
 
201
195
  uint64_t get_board_hash();
202
196
  static uint64_t seed = 1070372;
203
- static inline uint64_t random_number()
204
- {
197
+ static inline uint64_t random_number() {
205
198
  seed ^= seed >> 12;
206
199
  seed ^= seed << 25;
207
200
  seed ^= seed >> 27;
208
201
  return seed * 0x2545F4914F6CDD1DLL;
209
202
  }
210
203
 
211
- #define TABLE_SIZE 0x400000
204
+ #define TABLE_SIZE 0x3FFFFF
212
205
 
213
206
  #define EXACT 0
214
- #define ALPHA 1
215
- #define BETA 2
216
- typedef struct HashEntry
217
- {
207
+ #define LOWERBOUND 1
208
+ #define UPPERBOUND 2
209
+ typedef struct HashEntry {
218
210
  uint64_t key;
219
211
  int depth;
220
212
  int flag;
@@ -226,4 +218,33 @@ extern HashEntry table[TABLE_SIZE];
226
218
  static inline HashEntry *search_table(uint64_t key) { return &table[key % TABLE_SIZE]; };
227
219
  void insert_table(HashEntry *entry, uint64_t key, int depth, int flag, int score, Move move);
228
220
 
221
+ #define EPT 0xFF
222
+ typedef struct PieceTracker {
223
+ uint8_t positions[13];
224
+ uint8_t board_idx_position[120];
225
+ } PieceTracker;
226
+ extern PieceTracker piece_trackers[2];
227
+ void init_piece_trackers();
228
+
229
+ static inline uint8_t get_board_index(enum Player player, uint8_t pos_idx) { return piece_trackers[player].positions[pos_idx]; }
230
+ static inline uint8_t get_position_index(enum Player player, uint8_t board_idx) { return piece_trackers[player].board_idx_position[board_idx]; }
231
+ static inline void update_piece_tracker(enum Player player, uint8_t old_board_idx, uint8_t new_board_idx) {
232
+ uint8_t pos_idx = get_position_index(player, old_board_idx);
233
+ piece_trackers[player].positions[pos_idx] = new_board_idx;
234
+ piece_trackers[player].board_idx_position[old_board_idx] = EPT;
235
+ piece_trackers[player].board_idx_position[new_board_idx] = pos_idx;
236
+ }
237
+ static inline void remove_from_piece_tracker(enum Player player, uint8_t board_idx) {
238
+ uint8_t pos_idx = get_position_index(player, board_idx);
239
+ piece_trackers[player].positions[pos_idx] = EPT;
240
+ piece_trackers[player].board_idx_position[board_idx] = EPT;
241
+ }
242
+ static inline void add_to_piece_tracker(enum Player player, uint8_t board_idx) {
243
+ uint8_t pos_idx = 0;
244
+ while (piece_trackers[player].positions[pos_idx] != EPT)
245
+ pos_idx++;
246
+ piece_trackers[player].positions[pos_idx] = board_idx;
247
+ piece_trackers[player].board_idx_position[board_idx] = pos_idx;
248
+ }
249
+
229
250
  #endif // KHET_LIB_H_INCLUDED
@@ -1,3 +1,3 @@
1
1
  module KhetAI
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
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.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jkugs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-05 00:00:00.000000000 Z
11
+ date: 2024-12-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -18,6 +18,7 @@ extensions:
18
18
  - ext/khetai/extconf.rb
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - ".clang-format"
21
22
  - ".gitignore"
22
23
  - Gemfile
23
24
  - Gemfile.lock