Exercism track submissions
0
fork

Configure Feed

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

minesweeper

+401
+37
rust/minesweeper/.exercism/config.json
··· 1 + { 2 + "blurb": "Add the numbers to a minesweeper board", 3 + "authors": [ 4 + "EduardoBautista" 5 + ], 6 + "contributors": [ 7 + "ashleygwilliams", 8 + "coriolinus", 9 + "cwhakes", 10 + "EduardoBautista", 11 + "efx", 12 + "ErikSchierboom", 13 + "ffflorian", 14 + "IanWhitney", 15 + "kytrinyx", 16 + "lutostag", 17 + "mkantor", 18 + "nfiles", 19 + "petertseng", 20 + "rofrol", 21 + "stringparser", 22 + "workingjubilee", 23 + "xakon", 24 + "ZapAnton" 25 + ], 26 + "files": { 27 + "solution": [ 28 + "src/lib.rs" 29 + ], 30 + "test": [ 31 + "tests/minesweeper.rs" 32 + ], 33 + "example": [ 34 + ".meta/example.rs" 35 + ] 36 + } 37 + }
+8
rust/minesweeper/.gitignore
··· 1 + # Generated by Cargo 2 + # will have compiled files and executables 3 + /target/ 4 + **/*.rs.bk 5 + 6 + # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 + # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 8 + Cargo.lock
+4
rust/minesweeper/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "minesweeper" 4 + version = "1.1.0"
+85
rust/minesweeper/HELP.md
··· 1 + # Help 2 + 3 + ## Running the tests 4 + 5 + Execute the tests with: 6 + 7 + ```bash 8 + $ cargo test 9 + ``` 10 + 11 + All but the first test have been ignored. After you get the first test to 12 + pass, open the tests source file which is located in the `tests` directory 13 + and remove the `#[ignore]` flag from the next test and get the tests to pass 14 + again. Each separate test is a function with `#[test]` flag above it. 15 + Continue, until you pass every test. 16 + 17 + If you wish to run _only ignored_ tests without editing the tests source file, use: 18 + 19 + ```bash 20 + $ cargo test -- --ignored 21 + ``` 22 + 23 + If you are using Rust 1.51 or later, you can run _all_ tests with 24 + 25 + ```bash 26 + $ cargo test -- --include-ignored 27 + ``` 28 + 29 + To run a specific test, for example `some_test`, you can use: 30 + 31 + ```bash 32 + $ cargo test some_test 33 + ``` 34 + 35 + If the specific test is ignored, use: 36 + 37 + ```bash 38 + $ cargo test some_test -- --ignored 39 + ``` 40 + 41 + To learn more about Rust tests refer to the online [test documentation][rust-tests]. 42 + 43 + [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html 44 + 45 + ## Submitting your solution 46 + 47 + You can submit your solution using the `exercism submit src/lib.rs` command. 48 + This command will upload your solution to the Exercism website and print the solution page's URL. 49 + 50 + It's possible to submit an incomplete solution which allows you to: 51 + 52 + - See how others have completed the exercise 53 + - Request help from a mentor 54 + 55 + ## Need to get help? 56 + 57 + If you'd like help solving the exercise, check the following pages: 58 + 59 + - The [Rust track's documentation](https://exercism.org/docs/tracks/rust) 60 + - [Exercism's support channel on gitter](https://gitter.im/exercism/support) 61 + - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) 62 + 63 + Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. 64 + 65 + ## Rust Installation 66 + 67 + Refer to the [exercism help page][help-page] for Rust installation and learning 68 + resources. 69 + 70 + ## Submitting the solution 71 + 72 + Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. 73 + 74 + ## Feedback, Issues, Pull Requests 75 + 76 + The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! 77 + 78 + If you want to know more about Exercism, take a look at the [contribution guide]. 79 + 80 + ## Submitting Incomplete Solutions 81 + It's possible to submit an incomplete solution so you can see how others have completed the exercise. 82 + 83 + [help-page]: https://exercism.io/tracks/rust/learning 84 + [github]: https://github.com/exercism/rust 85 + [contribution guide]: https://exercism.io/docs/community/contributors
+69
rust/minesweeper/README.md
··· 1 + # Minesweeper 2 + 3 + Welcome to Minesweeper on Exercism's Rust Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + Add the mine counts to a completed Minesweeper board. 9 + 10 + Minesweeper is a popular game where the user has to find the mines using 11 + numeric hints that indicate how many mines are directly adjacent 12 + (horizontally, vertically, diagonally) to a square. 13 + 14 + In this exercise you have to create some code that counts the number of 15 + mines adjacent to a given empty square and replaces that square with the 16 + count. 17 + 18 + The board is a rectangle composed of blank space (' ') characters. A mine 19 + is represented by an asterisk ('\*') character. 20 + 21 + If a given space has no adjacent mines at all, leave that square blank. 22 + 23 + ## Examples 24 + 25 + For example you may receive a 5 x 4 board like this (empty spaces are 26 + represented here with the '·' character for display on screen): 27 + 28 + ``` 29 + ·*·*· 30 + ··*·· 31 + ··*·· 32 + ····· 33 + ``` 34 + 35 + And your code will transform it into this: 36 + 37 + ``` 38 + 1*3*1 39 + 13*31 40 + ·2*2· 41 + ·111· 42 + ``` 43 + 44 + ## Source 45 + 46 + ### Created by 47 + 48 + - @EduardoBautista 49 + 50 + ### Contributed to by 51 + 52 + - @ashleygwilliams 53 + - @coriolinus 54 + - @cwhakes 55 + - @EduardoBautista 56 + - @efx 57 + - @ErikSchierboom 58 + - @ffflorian 59 + - @IanWhitney 60 + - @kytrinyx 61 + - @lutostag 62 + - @mkantor 63 + - @nfiles 64 + - @petertseng 65 + - @rofrol 66 + - @stringparser 67 + - @workingjubilee 68 + - @xakon 69 + - @ZapAnton
+47
rust/minesweeper/src/lib.rs
··· 1 + fn gett<'a>(xs: &'a [&str]) -> impl Fn((usize, usize)) -> Option<u8> + 'a { 2 + move |(i, j): (usize, usize)| { 3 + xs.get(i)? 4 + .chars() 5 + .nth(j) 6 + .map(|x| if x == '*' { 1 } else { 0 }) 7 + } 8 + } 9 + fn neighbourhood(i: usize, j: usize, xs: &[&str]) -> Vec<Option<u8>> { 10 + let m_range = if i > 0 { i - 1..i + 2 } else { 0..2 }; 11 + let n_range = if j > 0 { j - 1..j + 2 } else { 0..2 }; 12 + let ys: Vec<(usize, usize)> = m_range 13 + .flat_map(|m| n_range.clone().map(move |n| (m, n))) 14 + .filter(|&q| (i, j) != q) 15 + .collect(); 16 + 17 + ys.iter().map(|p| gett(xs)(*p)).collect() 18 + } 19 + 20 + fn count_neighbours(xs: &[&str], i: usize, j: usize) -> String { 21 + let n: u8 = neighbourhood(i, j, xs).iter().map(|n| n.unwrap_or(0)).sum(); 22 + if n == 0 { 23 + String::from(" ") 24 + } else { 25 + n.to_string() 26 + } 27 + } 28 + pub fn annotate(minefield: &[&str]) -> Vec<String> { 29 + Vec::from(minefield) 30 + .iter() 31 + .enumerate() 32 + .map(|(i, x)| { 33 + let l: String = x 34 + .chars() 35 + .enumerate() 36 + .map(|(j, y)| { 37 + if y == ' ' { 38 + count_neighbours(minefield, i, j) 39 + } else { 40 + String::from(y) 41 + } 42 + }) 43 + .collect(); 44 + l 45 + }) 46 + .collect() 47 + }
+151
rust/minesweeper/tests/minesweeper.rs
··· 1 + use minesweeper::annotate; 2 + 3 + fn remove_annotations(board: &[&str]) -> Vec<String> { 4 + board.iter().map(|r| remove_annotations_in_row(r)).collect() 5 + } 6 + 7 + fn remove_annotations_in_row(row: &str) -> String { 8 + row.chars() 9 + .map(|ch| match ch { 10 + '*' => '*', 11 + _ => ' ', 12 + }) 13 + .collect() 14 + } 15 + 16 + fn run_test(test_case: &[&str]) { 17 + let cleaned = remove_annotations(test_case); 18 + let cleaned_strs = cleaned.iter().map(|r| &r[..]).collect::<Vec<_>>(); 19 + let expected = test_case.iter().map(|&r| r.to_string()).collect::<Vec<_>>(); 20 + assert_eq!(expected, annotate(&cleaned_strs)); 21 + } 22 + 23 + #[test] 24 + fn no_rows() { 25 + #[rustfmt::skip] 26 + run_test(&[ 27 + ]); 28 + } 29 + 30 + #[test] 31 + #[ignore] 32 + fn no_columns() { 33 + #[rustfmt::skip] 34 + run_test(&[ 35 + "", 36 + ]); 37 + } 38 + 39 + #[test] 40 + #[ignore] 41 + fn no_mines() { 42 + #[rustfmt::skip] 43 + run_test(&[ 44 + " ", 45 + " ", 46 + " ", 47 + ]); 48 + } 49 + 50 + #[test] 51 + #[ignore] 52 + fn board_with_only_mines() { 53 + #[rustfmt::skip] 54 + run_test(&[ 55 + "***", 56 + "***", 57 + "***", 58 + ]); 59 + } 60 + 61 + #[test] 62 + fn mine_surrounded_by_spaces() { 63 + #[rustfmt::skip] 64 + run_test(&[ 65 + "111", 66 + "1*1", 67 + "111", 68 + ]); 69 + } 70 + 71 + #[test] 72 + #[ignore] 73 + fn space_surrounded_by_mines() { 74 + #[rustfmt::skip] 75 + run_test(&[ 76 + "***", 77 + "*8*", 78 + "***", 79 + ]); 80 + } 81 + 82 + #[test] 83 + #[ignore] 84 + fn horizontal_line() { 85 + #[rustfmt::skip] 86 + run_test(&[ 87 + "1*2*1", 88 + ]); 89 + } 90 + 91 + #[test] 92 + #[ignore] 93 + fn horizontal_line_mines_at_edges() { 94 + #[rustfmt::skip] 95 + run_test(&[ 96 + "*1 1*", 97 + ]); 98 + } 99 + 100 + #[test] 101 + #[ignore] 102 + fn vertical_line() { 103 + #[rustfmt::skip] 104 + run_test(&[ 105 + "1", 106 + "*", 107 + "2", 108 + "*", 109 + "1", 110 + ]); 111 + } 112 + 113 + #[test] 114 + #[ignore] 115 + fn vertical_line_mines_at_edges() { 116 + #[rustfmt::skip] 117 + run_test(&[ 118 + "*", 119 + "1", 120 + " ", 121 + "1", 122 + "*", 123 + ]); 124 + } 125 + 126 + #[test] 127 + #[ignore] 128 + fn cross() { 129 + #[rustfmt::skip] 130 + run_test(&[ 131 + " 2*2 ", 132 + "25*52", 133 + "*****", 134 + "25*52", 135 + " 2*2 ", 136 + ]); 137 + } 138 + 139 + #[test] 140 + #[ignore] 141 + fn large_board() { 142 + #[rustfmt::skip] 143 + run_test(&[ 144 + "1*22*1", 145 + "12*322", 146 + " 123*2", 147 + "112*4*", 148 + "1*22*2", 149 + "111111", 150 + ]); 151 + }