Skip to content

Commit f2ba0e0

Browse files
committed
Add smiley face draft
1 parent 247cf3f commit f2ba0e0

5 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Smiley Face
2+
3+
Now then, let's get you writing some code! We'll be making a smiley face generator, capable of outputting smiles such as:
4+
```
5+
** **
6+
** **
7+
8+
** * **
9+
* *** *
10+
```
11+
12+
And:
13+
```
14+
** **
15+
** **
16+
17+
* ***
18+
* ** *
19+
**
20+
```
21+
22+
And even:
23+
```
24+
** **
25+
** **
26+
27+
*
28+
* *
29+
* ** *
30+
* **
31+
```
32+
33+
It does this using a really simple algorithm. First, your starting height is 0. Then, for each vertical column (in this case, there are 10), decide to move up, down, or stay where you are. After that, draw a "*" and move to the right.
34+
35+
First thing's first, we're going to use a random number generator. There's a Rust library for that called [rand](https://docs.rs/rand/latest/rand/), which you can install by running `cargo add rand` in your terminal. Do that now, then take a look through the documentation to learn how to use it.
36+
37+
Second, you're going to need to have a list of strings for each row, and slowly build them up. You won't be able to print anything out until the very end. Hint: the easiest way to do this is to start with a blank grid (filled with spaces), then replace a character with a "*" when drawing. Finally, print out the entire grid at the end.
38+
39+
`main.rs` has some starter code for you, including printing the eyes and that blank grid. All you have to do is implement the smiley!
40+
41+
Here are some hints to help you along:
42+
* Replace a character in a string: ||string.replace_range(x..=x, "*")||
43+
* Random number between -1 and 1: ||rng.random_range(-1..=1)||
44+
* Adding with type casting: ||y = (y as i64 + val) as usize||
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"defaultFile": "src/main.rs",
3+
"source": "https://github.com/Cratecode/rust/tree/master/sections/01_rust_projects/040_smiley_face",
4+
"showShareButton": true
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "lesson",
3+
"id": "les_rust_smiley_face",
4+
"extends": "basic",
5+
"name": "Rust Smiley Face",
6+
"unit": "rust_intro",
7+
"spec": "A Rust program that prints out a random smiley face.",
8+
"class": "project"
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use rand::prelude::*;
2+
3+
fn main() {
4+
let mut rng = rand::rng();
5+
6+
println!(" ** ** ");
7+
println!(" ** ** ");
8+
println!();
9+
10+
// 10 spaces (feel free to change it though!)
11+
let mut mouth = vec![" ".to_string(); 4];
12+
13+
// TODO: your code here
14+
15+
for row in mouth {
16+
println!("{row}");
17+
}
18+
}

sections/01_rust_projects/manifest.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"010_more_rust_concepts",
88
"020_mandelbrot_set_renderer",
99
"021_color_mandelbrot",
10-
"030_chat_app_backend"
10+
"030_chat_app_backend",
11+
"040_smiley_face"
1112
],
1213
"lessons": {
1314
"les_more_rust_concepts": {
@@ -22,6 +23,9 @@
2223
"les_rust_chat_app_backend": {
2324
"previous": ["les_rust_enums"],
2425
"next": ["les_rust_async"]
26+
},
27+
"les_rust_smiley_face": {
28+
"next": ["rust_project_concepts"]
2529
}
2630
}
2731
}

0 commit comments

Comments
 (0)