optimised o-x-rust
This commit is contained in:
		
							
								
								
									
										2
									
								
								wasm/o-x-rust/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								wasm/o-x-rust/Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -25,7 +25,7 @@ dependencies = [ | |||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "o-x-rust" | name = "o-x-rust" | ||||||
| version = "0.0.5" | version = "0.0.6" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "wasm-bindgen", |  "wasm-bindgen", | ||||||
| ] | ] | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ description = "Noughts and crosses WASM algorithms" | |||||||
| repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/o-x-rust" | repository = "https://git.koval.net/cyclane/game-algorithms/src/branch/main/wasm/o-x-rust" | ||||||
| license = "GNU GPLv3" | license = "GNU GPLv3" | ||||||
| readme = "README.md" | readme = "README.md" | ||||||
| version = "0.0.5" | version = "0.0.6" | ||||||
| edition = "2021" | edition = "2021" | ||||||
|  |  | ||||||
| [lib] | [lib] | ||||||
|   | |||||||
| @@ -35,16 +35,23 @@ pub fn get_turn(me: u8, other: u8, first: bool, empty: usize) -> u8 { | |||||||
| } | } | ||||||
|  |  | ||||||
| #[wasm_bindgen] | #[wasm_bindgen] | ||||||
| pub fn get_score(me: u8, other: u8, first: bool, board: &[u8]) -> i32 { | pub fn get_score(me: u8, other: u8, first: bool, board: &[u8]) -> f64 { | ||||||
|  | 	let (outcomes, score) = sub_get_score(me, other, first, board); | ||||||
|  | 	score as f64 / outcomes as f64 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // outcomes*2, winning outcomes (win = 2, draw = 1, loose = 0) | ||||||
|  | pub fn sub_get_score(me: u8, other: u8, first: bool, board: &[u8]) -> (i32, i32) { | ||||||
| 	let winner = find_winner(board); | 	let winner = find_winner(board); | ||||||
| 	if winner != 0 { | 	if winner != 0 { | ||||||
| 		return if winner == me { 1 } else { -1 }; | 		return if winner == me { (2, 2) } else { (2, 0) }; | ||||||
| 	} | 	} | ||||||
| 	let empty = count_empty(board); | 	let empty = count_empty(board); | ||||||
| 	if empty == 0 { | 	if empty == 0 { | ||||||
| 		return 0; | 		return (2, 1); | ||||||
| 	} | 	} | ||||||
| 	let mut score = 0; | 	let mut score = 0; | ||||||
|  | 	let mut outcomes = 0; | ||||||
| 	for i in 0..9 { | 	for i in 0..9 { | ||||||
| 		if board[i] != 0 { | 		if board[i] != 0 { | ||||||
| 			continue; | 			continue; | ||||||
| @@ -52,14 +59,16 @@ pub fn get_score(me: u8, other: u8, first: bool, board: &[u8]) -> i32 { | |||||||
| 		let copy = &mut [0u8; 9]; | 		let copy = &mut [0u8; 9]; | ||||||
| 		copy.copy_from_slice(board); | 		copy.copy_from_slice(board); | ||||||
| 		copy[i] = get_turn(me, other, first, empty); | 		copy[i] = get_turn(me, other, first, empty); | ||||||
| 		score += get_score(me, other, first, copy); | 		let (sub_outcomes, sub_score) = sub_get_score(me, other, first, copy); | ||||||
|  | 		outcomes += sub_outcomes; | ||||||
|  | 		score += sub_score; | ||||||
| 	} | 	} | ||||||
| 	score | 	(outcomes, score) | ||||||
| } | } | ||||||
|  |  | ||||||
| #[wasm_bindgen] | #[wasm_bindgen] | ||||||
| pub fn predict(me: u8, other: u8, first: bool, board: &[u8]) -> usize { | pub fn predict(me: u8, other: u8, first: bool, board: &[u8]) -> usize { | ||||||
| 	let (mut max_p, mut max_s) = (0, i32::MIN); | 	let (mut max_p, mut max_s) = (0, f64::MIN); | ||||||
| 	let empty = count_empty(board); | 	let empty = count_empty(board); | ||||||
| 	if empty == 0 { | 	if empty == 0 { | ||||||
| 		return 0; | 		return 0; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user