├── .github └── workflows │ ├── audit.yml │ ├── build.yml │ └── rust.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── bindings ├── Cargo.toml ├── build.rs └── src │ └── lib.rs ├── media └── logo.png └── src ├── grid.rs ├── gui.rs ├── main.rs └── minesweeper.rs /.github/workflows/audit.yml: -------------------------------------------------------------------------------- 1 | name: Audit 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | security_audit: 11 | runs-on: windows-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Cache cargo bin 15 | uses: actions/cache@v1 16 | with: 17 | path: ~/.cargo/bin 18 | key: ${{ runner.os }}-cargo-audit 19 | - uses: actions-rs/audit-check@v1 20 | with: 21 | token: ${{ secrets.GITHUB_TOKEN }} 22 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | minesweeper-rs: 11 | runs-on: windows-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: actions-rs/toolchain@v1 15 | with: 16 | toolchain: stable 17 | override: true 18 | - uses: actions-rs/cargo@v1 19 | with: 20 | command: build 21 | args: --workspace --all-targets -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | fmt: 11 | runs-on: windows-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: actions-rs/toolchain@v1 15 | with: 16 | toolchain: stable 17 | override: true 18 | - uses: actions-rs/cargo@v1 19 | with: 20 | command: fmt 21 | args: --all -- --check 22 | 23 | clippy_check: 24 | runs-on: windows-latest 25 | steps: 26 | - uses: actions/checkout@v2 27 | - uses: actions-rs/toolchain@v1 28 | with: 29 | toolchain: stable 30 | components: clippy 31 | override: true 32 | - uses: actions-rs/clippy-check@v1 33 | with: 34 | token: ${{ secrets.GITHUB_TOKEN }} 35 | args: --all-features --all-targets -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | **/target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | 12 | # Added by IntelliJ Rust 13 | .idea 14 | *.iml 15 | .gradle 16 | out/ 17 | bin/ 18 | build/ 19 | build-cache 20 | gen/ 21 | deps/ 22 | exampleProject 23 | testData 24 | .DS_Store 25 | /pretty_printers_tests/target 26 | /native-helper/target -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minesweeper-rs" 3 | version = "0.1.0" 4 | authors = ["Chris Ohk "] 5 | edition = "2018" 6 | description = "A simple minesweeper game using Rust and windows-rs" 7 | 8 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 9 | 10 | [dependencies] 11 | windows = "0.18.0" 12 | bindings = { path = "bindings" } 13 | winit = "0.25.0" 14 | raw-window-handle = "0.3.3" 15 | rand = "0.8.4" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Chris Ohk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minesweeper-rs 2 | 3 | 4 | 5 | [![License](https://img.shields.io/badge/Licence-MIT-blue.svg)](https://github.com/utilForever/minesweeper-rs/blob/main/LICENSE) ![Build](https://github.com/utilForever/minesweeper-rs/workflows/Build/badge.svg) ![Audit](https://github.com/utilForever/minesweeper-rs/workflows/Audit/badge.svg) ![Rust](https://github.com/utilForever/minesweeper-rs/workflows/Rust/badge.svg) 6 | 7 | minesweeper-rs is a simple minesweeper game using Rust and windows-rs. 8 | 9 | ## Key Features 10 | 11 | TBA 12 | 13 | ## Quick Start 14 | 15 | TBA 16 | 17 | ## How To Contribute 18 | 19 | Contributions are always welcome, either reporting issues/bugs or forking the repository and then issuing pull requests when you have completed some additional coding that you feel will be beneficial to the main project. If you are interested in contributing in a more dedicated capacity, then please contact me. 20 | 21 | ## Contact 22 | 23 | You can contact me via e-mail (utilForever at gmail.com). I am always happy to answer questions or help with any issues you might have, and please be sure to share any additional work or your creations with me, I love seeing what other people are making. 24 | 25 | ## License 26 | 27 | 28 | 29 | The class is licensed under the [MIT License](http://opensource.org/licenses/MIT): 30 | 31 | Copyright © 2021 [Chris Ohk](https://github.com/utilForever). 32 | 33 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 34 | 35 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 36 | 37 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 38 | -------------------------------------------------------------------------------- /bindings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "bindings" 3 | version = "0.1.0" 4 | edition = "2018" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | windows = "0.18.0" 10 | 11 | [build-dependencies] 12 | windows = "0.18.0" 13 | -------------------------------------------------------------------------------- /bindings/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | windows::build! { 3 | Windows::Graphics::SizeInt32, 4 | Windows::System::DispatcherQueueController, 5 | Windows::UI::Colors, 6 | Windows::UI::Composition::{Compositor, ContainerVisual, SpriteVisual, VisualCollection, CompositionColorBrush}, 7 | Windows::UI::Composition::Desktop::DesktopWindowTarget, 8 | Windows::Win32::System::Com::CoInitializeEx, 9 | Windows::Win32::System::WinRT::{CreateDispatcherQueueController, ICompositorDesktopInterop}, 10 | }; 11 | } 12 | -------------------------------------------------------------------------------- /bindings/src/lib.rs: -------------------------------------------------------------------------------- 1 | windows::include_bindings!(); 2 | -------------------------------------------------------------------------------- /media/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/utilForever/minesweeper-rs/e9210178a58e00fd76f0967e4a88f2c8fa5bccff/media/logo.png -------------------------------------------------------------------------------- /src/grid.rs: -------------------------------------------------------------------------------- 1 | use bindings::Windows::{ 2 | Foundation::Numerics::{Vector2, Vector3}, 3 | UI::Colors, 4 | UI::Composition::{Compositor, ContainerVisual, SpriteVisual}, 5 | }; 6 | 7 | #[derive(Clone)] 8 | pub struct Grid { 9 | compositor: Compositor, 10 | root: ContainerVisual, 11 | tiles: Vec, 12 | 13 | tile_size: Vector2, 14 | margin: Vector2, 15 | } 16 | 17 | impl Grid { 18 | pub fn new( 19 | compositor: &Compositor, 20 | tile_size: &Vector2, 21 | margin: &Vector2, 22 | ) -> windows::Result { 23 | let compositor = compositor.clone(); 24 | let root = compositor.CreateContainerVisual()?; 25 | 26 | Ok(Self { 27 | compositor, 28 | root, 29 | tiles: Vec::new(), 30 | 31 | tile_size: tile_size.clone(), 32 | margin: margin.clone(), 33 | }) 34 | } 35 | 36 | pub fn draw(&mut self) -> windows::Result<()> { 37 | let children = self.root.Children()?; 38 | children.RemoveAll()?; 39 | self.tiles.clear(); 40 | 41 | self.root.SetSize( 42 | (&self.tile_size + Vector2::new(2.5, 2.5)) * Vector2::new(16.0 as f32, 16.0 as f32), 43 | )?; 44 | 45 | for x in 0..8 { 46 | for y in 0..8 { 47 | let visual = self.compositor.CreateSpriteVisual()?; 48 | visual.SetSize(&self.tile_size)?; 49 | visual.SetCenterPoint(Vector3::new( 50 | self.tile_size.X / 2.0, 51 | self.tile_size.Y / 2.0, 52 | 0.0, 53 | ))?; 54 | visual.SetOffset( 55 | Vector3::new(self.margin.X / 2.0, self.margin.Y / 2.0, 0.0) 56 | + (Vector3::new( 57 | self.tile_size.X + self.margin.X, 58 | self.tile_size.Y + self.margin.Y, 59 | 0.0, 60 | ) * Vector3::new(x as f32, y as f32, 0.0)), 61 | )?; 62 | 63 | children.InsertAtTop(&visual)?; 64 | self.tiles.push(visual); 65 | } 66 | } 67 | 68 | Ok(()) 69 | } 70 | 71 | pub fn root(&self) -> &ContainerVisual { 72 | &self.root 73 | } 74 | 75 | pub fn tiles_iter(&self) -> impl std::iter::Iterator { 76 | self.tiles.iter() 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/gui.rs: -------------------------------------------------------------------------------- 1 | use crate::grid::Grid; 2 | 3 | use bindings::Windows::{ 4 | Foundation::Numerics::{Vector2, Vector3}, 5 | UI::Colors, 6 | UI::Composition::{CompositionBorderMode, Compositor, ContainerVisual}, 7 | }; 8 | 9 | #[derive(Clone)] 10 | pub struct GUI { 11 | compositor: Compositor, 12 | window_size: Vector2, 13 | 14 | game_board: Grid, 15 | } 16 | 17 | impl GUI { 18 | pub fn new(container_visual: &ContainerVisual, window_size: &Vector2) -> windows::Result { 19 | let compositor = container_visual.Compositor()?; 20 | let root = compositor.CreateSpriteVisual()?; 21 | 22 | root.SetRelativeSizeAdjustment(Vector2::new(1.0, 1.0))?; 23 | root.SetBrush(compositor.CreateColorBrushWithColor(Colors::White()?)?)?; 24 | root.SetBorderMode(CompositionBorderMode::Hard)?; 25 | container_visual.Children()?.InsertAtTop(&root)?; 26 | 27 | let tile_size = Vector2::new(25.0, 25.0); 28 | let margin = Vector2::new(2.5, 2.5); 29 | let game_board = Grid::new(&compositor, &tile_size, &margin)?; 30 | 31 | let game_board_visual = game_board.root(); 32 | game_board_visual.SetRelativeOffsetAdjustment(Vector3::new(0.5, 0.5, 0.0))?; 33 | game_board_visual.SetAnchorPoint(Vector2::new(0.5, 0.5))?; 34 | root.Children()?.InsertAtTop(game_board_visual)?; 35 | 36 | let mut result = Self { 37 | compositor, 38 | window_size: window_size.clone(), 39 | game_board, 40 | }; 41 | 42 | result.reset()?; 43 | 44 | Ok(result) 45 | } 46 | 47 | pub fn reset(&mut self) -> windows::Result<()> { 48 | self.game_board.draw()?; 49 | 50 | let color_brush = self.compositor.CreateColorBrushWithColor(Colors::Blue()?)?; 51 | 52 | for tile in self.game_board.tiles_iter() { 53 | tile.SetBrush(&color_brush)?; 54 | } 55 | 56 | Ok(()) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod grid; 2 | mod gui; 3 | mod minesweeper; 4 | 5 | use gui::GUI; 6 | use minesweeper::Minesweeper; 7 | 8 | use winit::{ 9 | event::{Event, WindowEvent}, 10 | event_loop::{ControlFlow, EventLoop}, 11 | window::WindowBuilder, 12 | }; 13 | 14 | use bindings::Windows::{ 15 | Foundation::Numerics::Vector2, 16 | Graphics::SizeInt32, 17 | System::DispatcherQueueController, 18 | Win32::Foundation::HWND, 19 | Win32::System::Com::{CoInitializeEx, COINIT_APARTMENTTHREADED}, 20 | Win32::System::WinRT::{ 21 | CreateDispatcherQueueController, DispatcherQueueOptions, ICompositorDesktopInterop, 22 | DQTAT_COM_NONE, DQTYPE_THREAD_CURRENT, 23 | }, 24 | UI::Composition::Compositor, 25 | }; 26 | 27 | use raw_window_handle::HasRawWindowHandle; 28 | use windows::Interface; 29 | 30 | fn create_dispatcher() -> DispatcherQueueController { 31 | // We need a DispatcherQueue on our thread to properly create a Compositor. Note that since 32 | // we aren't pumping messages, the Compositor won't commit. This is fine for the test for now. 33 | let options = DispatcherQueueOptions { 34 | dwSize: std::mem::size_of::() as u32, 35 | threadType: DQTYPE_THREAD_CURRENT, 36 | apartmentType: DQTAT_COM_NONE, 37 | }; 38 | 39 | unsafe { CreateDispatcherQueueController(options).unwrap() } 40 | } 41 | 42 | fn run() -> windows::Result<()> { 43 | unsafe { CoInitializeEx(std::ptr::null_mut(), COINIT_APARTMENTTHREADED)? }; 44 | let _dispatcher = create_dispatcher(); 45 | 46 | let event_loop = EventLoop::new(); 47 | let window = WindowBuilder::new().build(&event_loop).unwrap(); 48 | window.set_title("Minesweeper"); 49 | 50 | // Create desktop window target. 51 | let compositor = Compositor::new()?; 52 | let window_handle = window.raw_window_handle(); 53 | let window_handle = match window_handle { 54 | raw_window_handle::RawWindowHandle::Windows(window_handle) => window_handle.hwnd, 55 | _ => panic!("Unsupported platform!"), 56 | }; 57 | 58 | let compositor_desktop: ICompositorDesktopInterop = compositor.cast()?; 59 | let target = unsafe { 60 | compositor_desktop.CreateDesktopWindowTarget(HWND(window_handle as isize), false)? 61 | }; 62 | 63 | // Create composition root. 64 | let container_visual = compositor.CreateContainerVisual()?; 65 | container_visual.SetRelativeSizeAdjustment(Vector2 { X: 1.0, Y: 1.0 })?; 66 | target.SetRoot(&container_visual)?; 67 | 68 | // Create GUI. 69 | let window_size = Vector2::new( 70 | window.inner_size().width as f32, 71 | window.inner_size().height as f32, 72 | ); 73 | let gui = GUI::new(&container_visual, &window_size)?; 74 | 75 | // Create Minesweeper game. 76 | let game_board_size = SizeInt32 { 77 | Width: 30, 78 | Height: 16, 79 | }; 80 | let game = Minesweeper::new( 81 | game_board_size.Width as u32, 82 | game_board_size.Height as u32, 83 | 99, 84 | &gui, 85 | ); 86 | 87 | event_loop.run(move |event, _, control_flow| { 88 | *control_flow = ControlFlow::Wait; 89 | 90 | match event { 91 | Event::WindowEvent { 92 | event: WindowEvent::CloseRequested, 93 | window_id, 94 | } if window_id == window.id() => *control_flow = ControlFlow::Exit, 95 | _ => (), 96 | } 97 | }); 98 | } 99 | 100 | fn main() { 101 | let result = run(); 102 | 103 | // We do this for nicer HRESULT printing when errors occur. 104 | if let Err(error) = result { 105 | error.code().unwrap(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/minesweeper.rs: -------------------------------------------------------------------------------- 1 | use crate::gui::GUI; 2 | 3 | use rand::Rng; 4 | 5 | #[derive(Clone)] 6 | pub enum MineState { 7 | Empty, 8 | Flag, 9 | Question, 10 | Revaled, 11 | } 12 | 13 | pub struct Minesweeper { 14 | width: u32, 15 | height: u32, 16 | 17 | mines: Vec, 18 | num_mines: i32, 19 | mine_states: Vec, 20 | 21 | gui: GUI, 22 | } 23 | 24 | impl Minesweeper { 25 | pub fn new(width: u32, height: u32, num_mines: i32, gui: &GUI) -> Self { 26 | let board_size = (width * height) as usize; 27 | 28 | let mut result = Self { 29 | width, 30 | height, 31 | 32 | mines: vec![false; board_size], 33 | num_mines, 34 | mine_states: vec![MineState::Empty; board_size], 35 | 36 | gui: gui.clone(), 37 | }; 38 | 39 | result.start(); 40 | 41 | result 42 | } 43 | 44 | fn start(&mut self) { 45 | for mine_state in self.mine_states.iter_mut() { 46 | *mine_state = MineState::Empty 47 | } 48 | 49 | self.generate_mines(); 50 | 51 | self.gui.reset(); 52 | } 53 | 54 | fn generate_mines(&mut self) { 55 | for mine in self.mines.iter_mut() { 56 | *mine = false 57 | } 58 | 59 | let mut rng = rand::thread_rng(); 60 | let side = rand::distributions::Uniform::new(0, self.mines.len()); 61 | 62 | for _i in 0..self.num_mines { 63 | let mut idx: usize; 64 | 65 | while { 66 | idx = rng.sample(side); 67 | self.mines[idx] 68 | } {} 69 | 70 | self.mines[idx] = true; 71 | } 72 | } 73 | 74 | fn is_mine(&self, x: i32, y: i32) -> bool { 75 | if x < 0 || x >= self.width as i32 || y < 0 || y >= self.height as i32 { 76 | false 77 | } else { 78 | let idx = (y as u32 * self.height + x as u32) as usize; 79 | self.mines[idx] 80 | } 81 | } 82 | 83 | fn get_count_neighbor_mines(&self, x: u32, y: u32) -> u32 { 84 | let mut count: u32 = 0; 85 | 86 | // North West 87 | if self.is_mine(x as i32 - 1, y as i32 - 1) { 88 | count += 1; 89 | } 90 | // North 91 | if self.is_mine(x as i32, y as i32 - 1) { 92 | count += 1; 93 | } 94 | // North East 95 | if self.is_mine(x as i32 + 1, y as i32 - 1) { 96 | count += 1; 97 | } 98 | // East 99 | if self.is_mine(x as i32 + 1, y as i32) { 100 | count += 1; 101 | } 102 | // South East 103 | if self.is_mine(x as i32 + 1, y as i32 + 1) { 104 | count += 1; 105 | } 106 | // South 107 | if self.is_mine(x as i32, y as i32 + 1) { 108 | count += 1; 109 | } 110 | // South West 111 | if self.is_mine(x as i32 - 1, y as i32 + 1) { 112 | count += 1; 113 | } 114 | // West 115 | if self.is_mine(x as i32 - 1, y as i32) { 116 | count += 1; 117 | } 118 | 119 | count 120 | } 121 | 122 | pub fn show(&self) { 123 | for y in 0..self.height { 124 | for x in 0..self.width { 125 | let idx = (y * self.height + x) as usize; 126 | print!("{}", if self.mines[idx] { "*" } else { "." }); 127 | } 128 | println!(); 129 | } 130 | } 131 | 132 | pub fn show_with_mine_count(&self) { 133 | for y in 0..self.height { 134 | for x in 0..self.width { 135 | let idx = (y * self.height + x) as usize; 136 | print!( 137 | "{}", 138 | if self.mines[idx] { 139 | "*".to_string() 140 | } else { 141 | self.get_count_neighbor_mines(x, y).to_string() 142 | } 143 | ); 144 | } 145 | println!(); 146 | } 147 | } 148 | } 149 | --------------------------------------------------------------------------------