Skip to content

Commit cc3b525

Browse files
committed
ref: accept thor param on the thor trait
1 parent dbae892 commit cc3b525

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

crates/engine/src/game.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,12 +1011,12 @@ pub fn generic_compute_move<L: ComputeMoveLogger, Out: ComputeMoveOutput, FE: Fr
10111011
if book_move_found == 0 && play_thor_match_openings != 0 {
10121012
/* Optionally use the Thor database as opening book. */
10131013
let threshold = 2;
1014-
Thor::database_search(&g_state.board.board, side_to_move);
1015-
if Thor::get_match_count() >= threshold {
1014+
Thor::database_search(thor, &g_state.board.board, side_to_move);
1015+
if Thor::get_match_count(thor) >= threshold {
10161016
let game_index =
10171017
((g_state.random.my_random() >> 8) %
1018-
Thor::get_match_count() as i64) as i32;
1019-
curr_move = Thor::get_thor_game_move(game_index, g_state.moves.disks_played) as i8;
1018+
Thor::get_match_count(thor) as i64) as i32;
1019+
curr_move = Thor::get_thor_game_move(thor, game_index, g_state.moves.disks_played) as i8;
10201020
if valid_move(curr_move, side_to_move, &g_state.board.board) != 0 {
10211021
book_eval_info =
10221022
create_eval_info(UNDEFINED_EVAL, UNSOLVED_POSITION,
@@ -1041,7 +1041,7 @@ pub fn generic_compute_move<L: ComputeMoveLogger, Out: ComputeMoveOutput, FE: Fr
10411041
if book_move_found == 0 && g_state.game.play_human_openings != 0 && book != 0 {
10421042
/* Check Thor statistics for a move */
10431043
curr_move =
1044-
Thor::choose_thor_opening_move(&g_state.board.board, side_to_move,
1044+
Thor::choose_thor_opening_move(thor, &g_state.board.board, side_to_move,
10451045
0, &mut g_state.random) as i8;
10461046
if curr_move != -(1) {
10471047
book_eval_info =

crates/engine/src/thordb.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::src::myrandom::MyRandom;
22

33
pub trait ThorDatabase {
4-
fn get_thor_game_move(index: i32, move_number: i32) -> i32;
5-
fn database_search(in_board: &[i32], side_to_move: i32);
6-
fn get_match_count() -> i32;
7-
fn get_black_win_count() -> i32;
8-
fn get_draw_count() -> i32;
9-
fn get_white_win_count() -> i32;
10-
fn get_black_median_score() -> i32;
11-
fn get_black_average_score() -> f64;
12-
fn choose_thor_opening_move(in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32;
4+
fn get_thor_game_move(&self, index: i32, move_number: i32) -> i32;
5+
fn database_search(&self, in_board: &[i32], side_to_move: i32);
6+
fn get_match_count(&self) -> i32;
7+
fn get_black_win_count(&self) -> i32;
8+
fn get_draw_count(&self) -> i32;
9+
fn get_white_win_count(&self) -> i32;
10+
fn get_black_median_score(&self) -> i32;
11+
fn get_black_average_score(&self) -> f64;
12+
fn choose_thor_opening_move(&self, in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32;
1313
}

crates/legacy-zebra/src/thordb.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,39 +118,39 @@ static mut filter: FilterType =
118118
pub struct LegacyThor;
119119

120120
impl ThorDatabase for LegacyThor {
121-
fn get_thor_game_move(index: i32, move_number: i32) -> i32 {
121+
fn get_thor_game_move(&self, index: i32, move_number: i32) -> i32 {
122122
unsafe { get_thor_game_move(index, move_number) }
123123
}
124124

125-
fn database_search(in_board: &[i32], side_to_move: i32) {
125+
fn database_search(&self, in_board: &[i32], side_to_move: i32) {
126126
unsafe { database_search(in_board, side_to_move) }
127127
}
128128

129-
fn get_match_count() -> i32 {
129+
fn get_match_count(&self) -> i32 {
130130
unsafe { thor_search.get_match_count() }
131131
}
132132

133-
fn get_black_win_count() -> i32 {
133+
fn get_black_win_count(&self) -> i32 {
134134
unsafe { thor_search.get_black_win_count() }
135135
}
136136

137-
fn get_draw_count() -> i32 {
137+
fn get_draw_count(&self) -> i32 {
138138
unsafe { thor_search.get_draw_count() }
139139
}
140140

141-
fn get_white_win_count() -> i32 {
141+
fn get_white_win_count(&self) -> i32 {
142142
unsafe { thor_search.get_white_win_count() }
143143
}
144144

145-
fn get_black_median_score() -> i32 {
145+
fn get_black_median_score(&self) -> i32 {
146146
unsafe { thor_search.get_black_median_score() }
147147
}
148148

149-
fn get_black_average_score() -> f64 {
149+
fn get_black_average_score(&self) -> f64 {
150150
unsafe { thor_search.get_black_average_score() }
151151
}
152152

153-
fn choose_thor_opening_move(in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32 {
153+
fn choose_thor_opening_move(&self, in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32 {
154154
unsafe { choose_thor_opening_move(in_board, side_to_move, echo, random) }
155155
}
156156
}

crates/legacy-zebra/src/zebra.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ fn play_game(mut file_name: &str,
747747
type ComputeMoveOut = LibcZebraOutput;
748748
type Learn = LibcLearner;
749749
type FE = LibcFatalError;
750-
type Thor = LegacyThor;
750+
let thor = LegacyThor;
751751
let mut play_state: PlayGame<Source> = PlayGame::new(file_name, move_string, repeat, move_file, g_state);
752752
let mut move_attempt = None;
753753
let mut total_search_time: f64 = 0.;
@@ -756,7 +756,7 @@ fn play_game(mut file_name: &str,
756756
let mut stdin_lock = stdin.lock();
757757
loop {
758758
let state = next_state::<
759-
ZF, Source, BoardSrc, ComputeMoveLog, ComputeMoveOut, FE, Thor
759+
ZF, Source, BoardSrc, ComputeMoveLog, ComputeMoveOut, FE, LegacyThor
760760
>(&mut play_state, move_attempt.take(), &LegacyThor);
761761
match state {
762762
// TODO here in all these branches, we should ideally not need mutable reference to play_state
@@ -799,7 +799,7 @@ fn play_game(mut file_name: &str,
799799
dump_position(play_state.side_to_move, &play_state.g_state.board.board);
800800
dump_game_score(play_state.side_to_move, play_state.g_state.board.score_sheet_row, &play_state.g_state.board.black_moves, &play_state.g_state.board.white_moves);
801801
/* Check what the Thor opening statistics has to say */
802-
Thor::choose_thor_opening_move(&play_state.g_state.board.board, play_state.side_to_move, play_state.g_state.config.echo, &mut play_state.g_state.random);
802+
thor.choose_thor_opening_move(&play_state.g_state.board.board, play_state.side_to_move, play_state.g_state.config.echo, &mut play_state.g_state.random);
803803
}
804804
PlayGameState::CanLearn => {
805805
if play_state.g_state.config.use_learning && play_state.g_state.config.one_position_only == 0 {
@@ -852,22 +852,22 @@ fn deal_with_thor_1(use_thor_: bool, side_to_move: i32,
852852
mut config: &Config, g_timer: &Timer,
853853
board_state: &BoardState, total_search_time: &mut f64) {
854854
type ZF = LibcFrontend;
855-
type Thor = LegacyThor;
855+
let thor = LegacyThor;
856856

857857
if use_thor_ {
858858
let database_start = g_timer.get_real_timer();
859-
Thor::database_search(&board_state.board, side_to_move);
860-
let thor_position_count = Thor::get_match_count();
859+
thor.database_search(&board_state.board, side_to_move);
860+
let thor_position_count = thor.get_match_count();
861861
let database_stop = g_timer.get_real_timer();
862862
let database_time = database_stop - database_start;
863863
*total_search_time += database_time;
864864
ZF::report_thor_matching_games_stats(*total_search_time, thor_position_count, database_time);
865865
if thor_position_count > 0 {
866-
let black_win_count = Thor::get_black_win_count();
867-
let draw_count = Thor::get_draw_count();
868-
let white_win_count = Thor::get_white_win_count();
869-
let black_median_score = Thor::get_black_median_score();
870-
let black_average_score = Thor::get_black_average_score();
866+
let black_win_count = thor.get_black_win_count();
867+
let draw_count = thor.get_draw_count();
868+
let white_win_count = thor.get_white_win_count();
869+
let black_median_score = thor.get_black_median_score();
870+
let black_average_score = thor.get_black_average_score();
871871

872872
ZF::report_thor_stats(black_win_count, draw_count, white_win_count, black_median_score, black_average_score);
873873
}
@@ -879,21 +879,22 @@ fn deal_with_thor_2(use_thor_: bool, side_to_move: i32,
879879
config: &Config, g_timer: &Timer,
880880
board_state: &BoardState, total_search_time: &mut f64){
881881
type ZF = LibcFrontend;
882-
type Thor = LegacyThor;
882+
let thor = LegacyThor{};
883+
use ThorDatabase;
883884
if use_thor_ {
884885
let database_start = g_timer.get_real_timer();
885-
Thor::database_search(&board_state.board, side_to_move);
886-
let thor_position_count = Thor::get_match_count();
886+
thor.database_search(&board_state.board, side_to_move);
887+
let thor_position_count = thor.get_match_count();
887888
let database_stop = g_timer.get_real_timer();
888889
let db_search_time = database_stop - database_start;
889890
*total_search_time += db_search_time;
890891
ZF::report_some_thor_stats(*total_search_time, thor_position_count, db_search_time);
891892
if thor_position_count > 0 {
892-
let black_win_count = Thor::get_black_win_count();
893-
let draw_count = Thor::get_draw_count();
894-
let white_win_count = Thor::get_white_win_count();
895-
let black_median_score = Thor::get_black_median_score();
896-
let black_average_score = Thor::get_black_average_score();
893+
let black_win_count = thor.get_black_win_count();
894+
let draw_count = thor.get_draw_count();
895+
let white_win_count = thor.get_white_win_count();
896+
let black_median_score = thor.get_black_median_score();
897+
let black_average_score = thor.get_black_average_score();
897898
ZF::report_some_thor_scores(black_win_count, draw_count, white_win_count, black_median_score, black_average_score);
898899
}
899900
ZF::print_out_thor_matches(config.thor_max_games);

webzebra/crate/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,40 +468,40 @@ pub enum InteractionRequest {
468468
struct WasmThor;
469469

470470
impl ThorDatabase for WasmThor {
471-
fn get_thor_game_move(index: i32, move_number: i32) -> i32 {
471+
fn get_thor_game_move(&self, index: i32, move_number: i32) -> i32 {
472472
0
473473
}
474474

475-
fn database_search(in_board: &[i32], side_to_move: i32) {
475+
fn database_search(&self, in_board: &[i32], side_to_move: i32) {
476476
//
477477
}
478478

479-
fn get_match_count() -> i32 {
479+
fn get_match_count(&self) -> i32 {
480480
//
481481
0
482482
}
483483

484-
fn get_black_win_count() -> i32 {
484+
fn get_black_win_count(&self) -> i32 {
485485
0
486486
}
487487

488-
fn get_draw_count() -> i32 {
488+
fn get_draw_count(&self) -> i32 {
489489
0
490490
}
491491

492-
fn get_white_win_count() -> i32 {
492+
fn get_white_win_count(&self) -> i32 {
493493
0
494494
}
495495

496-
fn get_black_median_score() -> i32 {
496+
fn get_black_median_score(&self) -> i32 {
497497
0
498498
}
499499

500-
fn get_black_average_score() -> f64 {
500+
fn get_black_average_score(&self) -> f64 {
501501
0.0
502502
}
503503

504-
fn choose_thor_opening_move(in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32 {
504+
fn choose_thor_opening_move(&self, in_board: &[i32], side_to_move: i32, echo: i32, random: &mut MyRandom) -> i32 {
505505
0
506506
}
507507
}

0 commit comments

Comments
 (0)