My attempt at writing a go (the board game) engine, in Rust
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: add example

+89
+23
.pre-commit-config.yaml
··· 1 + repos: 2 + - repo: local 3 + hooks: 4 + - id: cargo-fmt 5 + name: cargo fmt 6 + entry: cargo fmt -- --check 7 + language: system 8 + types: [rust] 9 + pass_filenames: false 10 + 11 + - id: cargo-clippy 12 + name: cargo clippy 13 + entry: cargo clippy --all-targets --all-features -- -D warnings 14 + language: system 15 + types: [rust] 16 + pass_filenames: false 17 + 18 + - id: cargo-test 19 + name: cargo test 20 + entry: cargo test 21 + language: system 22 + types: [rust] 23 + pass_filenames: false
+1
Cargo.toml
··· 8 8 9 9 [dev-dependencies] 10 10 proptest = "1.5" 11 +
+65
examples/hello_world.rs
··· 1 + use go_engine::{Color, Coord, Game}; 2 + 3 + fn print_board<const W: usize, const H: usize>(game: &Game<W, H>) { 4 + for y in (0..H).rev() { 5 + for x in 0..W { 6 + let coord: Coord<W, H> = (x as u8, y as u8).try_into().unwrap(); 7 + match game.board().get(coord) { 8 + Some(Color::Black) => print!("X "), 9 + Some(Color::White) => print!("O "), 10 + None => print!(". "), 11 + } 12 + } 13 + println!(); 14 + } 15 + } 16 + 17 + fn main() { 18 + let mut game = Game::<9, 9>::new(); 19 + 20 + println!("Hello from go-engine!"); 21 + println!("Initial board:"); 22 + print_board(&game); 23 + 24 + game.play((4, 4).try_into().unwrap()).unwrap(); 25 + println!("\nAfter White plays E5:"); 26 + print_board(&game); 27 + 28 + // Surround the white stone at E5 to capture it 29 + game.play((4, 3).try_into().unwrap()).unwrap(); // Black E4 30 + println!("\nAfter Black plays E4:"); 31 + print_board(&game); 32 + 33 + game.play((0, 0).try_into().unwrap()).unwrap(); // White A1 (pass-like move elsewhere) 34 + println!("\nAfter White plays A1:"); 35 + print_board(&game); 36 + 37 + game.play((4, 5).try_into().unwrap()).unwrap(); // Black E6 38 + println!("\nAfter Black plays E6:"); 39 + print_board(&game); 40 + 41 + game.play((0, 1).try_into().unwrap()).unwrap(); // White A2 42 + println!("\nAfter White plays A2:"); 43 + print_board(&game); 44 + 45 + game.play((3, 4).try_into().unwrap()).unwrap(); // Black D5 46 + println!("\nAfter Black plays D5:"); 47 + print_board(&game); 48 + 49 + game.play((0, 2).try_into().unwrap()).unwrap(); // White A3 50 + println!("\nAfter White plays A3:"); 51 + print_board(&game); 52 + 53 + // Final move to capture the white stone at E5 54 + game.play((5, 4).try_into().unwrap()).unwrap(); // Black F5 captures! 55 + println!("\nAfter Black plays F5 (captures E5!):"); 56 + print_board(&game); 57 + 58 + println!("\nFinal state:"); 59 + println!("Current player: {:?}", game.to_move()); 60 + println!( 61 + "Captures: Black={}, White={}", 62 + game.captures().0, 63 + game.captures().1 64 + ); 65 + }