@@ -32,10 +32,13 @@ use board::Resign;
3232use config:: Config ;
3333use game:: Game ;
3434use playout:: Playout ;
35- use std :: old_io :: Writer ;
35+ use playout :: PlayoutResult ;
3636
37- use rand:: { Rng , weak_rng} ;
37+ use rand:: Rng ;
38+ use rand:: weak_rng;
3839use std:: marker:: MarkerTrait ;
40+ use std:: old_io:: Writer ;
41+ use std:: sync:: Arc ;
3942use std:: sync:: mpsc:: Receiver ;
4043use std:: sync:: mpsc:: Sender ;
4144use std:: sync:: mpsc:: channel;
@@ -46,11 +49,11 @@ mod simple;
4649
4750pub trait McEngine : MarkerTrait {
4851
49- fn record_playout ( & mut MoveStats , & Playout , bool ) ;
52+ fn record_playout ( & mut MoveStats , & PlayoutResult , bool ) ;
5053
5154}
5255
53- fn gen_move < T : McEngine > ( config : Config , color : Color , game : & Game , sender : Sender < Move > , receiver : Receiver < ( ) > ) {
56+ fn gen_move < T : McEngine > ( config : Arc < Config > , color : Color , game : & Game , sender : Sender < Move > , receiver : Receiver < ( ) > ) {
5457 let moves = game. legal_moves_without_eyes ( ) ;
5558 if moves. is_empty ( ) {
5659 if config. log {
@@ -62,7 +65,7 @@ fn gen_move<T: McEngine>(config: Config, color: Color, game: &Game, sender: Send
6265 let mut stats = MoveStats :: new ( & moves, color) ;
6366 let mut counter = 0 ;
6467 let ( send_result, receive_result) = channel :: < ( MoveStats , usize ) > ( ) ;
65- let ( guards, halt_senders) = spin_up :: < T > ( color, config. threads , & moves, game, send_result) ;
68+ let ( guards, halt_senders) = spin_up :: < T > ( color, config. clone ( ) , & moves, game, send_result) ;
6669 loop {
6770 select ! (
6871 result = receive_result. recv( ) => {
@@ -100,30 +103,31 @@ fn finish(color: Color, game: &Game, stats: MoveStats, sender: Sender<Move>, hal
100103 }
101104}
102105
103- fn spin_up < ' a , T : McEngine > ( color : Color , threads : usize , moves : & ' a Vec < Move > , game : & Game , send_result : Sender < ( MoveStats < ' a > , usize ) > ) -> ( Vec < thread:: JoinGuard < ' a , ( ) > > , Vec < Sender < ( ) > > ) {
106+ fn spin_up < ' a , T : McEngine > ( color : Color , config : Arc < Config > , moves : & ' a Vec < Move > , game : & Game , send_result : Sender < ( MoveStats < ' a > , usize ) > ) -> ( Vec < thread:: JoinGuard < ' a , ( ) > > , Vec < Sender < ( ) > > ) {
104107 let mut guards = Vec :: new ( ) ;
105108 let mut halt_senders = Vec :: new ( ) ;
106- for _ in 0 ..threads {
109+ for _ in 0 ..config . threads {
107110 let ( send_halt, receive_halt) = channel :: < ( ) > ( ) ;
108111 halt_senders. push ( send_halt) ;
109112 let send_result = send_result. clone ( ) ;
110- let guard = spin_up_worker :: < T > ( color, receive_halt, moves, game. board ( ) , send_result) ;
113+ let config = config. clone ( ) ;
114+ let guard = spin_up_worker :: < T > ( color, receive_halt, moves, game. board ( ) , config, send_result) ;
111115 guards. push ( guard) ;
112116 }
113117 ( guards, halt_senders)
114118}
115119
116- fn spin_up_worker < ' a , T : McEngine > ( color : Color , recv_halt : Receiver < ( ) > , moves : & ' a Vec < Move > , board : Board , send_result : Sender < ( MoveStats < ' a > , usize ) > ) -> thread:: JoinGuard < ' a , ( ) > {
120+ fn spin_up_worker < ' a , T : McEngine > ( color : Color , recv_halt : Receiver < ( ) > , moves : & ' a Vec < Move > , board : Board , config : Arc < Config > , send_result : Sender < ( MoveStats < ' a > , usize ) > ) -> thread:: JoinGuard < ' a , ( ) > {
117121 thread:: scoped ( move || {
118122 let runs = 100 ;
119123 let mut rng = weak_rng ( ) ;
120124 let mut stats = MoveStats :: new ( moves, color) ;
121125 loop {
122126 for _ in 0 ..runs {
123127 let m = moves[ rng. gen :: < usize > ( ) % moves. len ( ) ] ;
124- let playout = Playout :: run ( & board, & m, & mut rng ) ;
125- let winner = playout . winner ( ) ;
126- T :: record_playout ( & mut stats, & playout , winner == color) ;
128+ let playout_result = config . playout . run ( & board, & m) ;
129+ let winner = playout_result . winner ( ) ;
130+ T :: record_playout ( & mut stats, & playout_result , winner == color) ;
127131 }
128132 if recv_halt. try_recv ( ) . is_ok ( ) {
129133 break ;
0 commit comments