January 2018
Beginner to intermediate
454 pages
10h 8m
English
In order to do so, we'll need to import another crate—rand. This crate is used to generate random numbers and that is exactly what we need here.
First, add the following line to your Cargo.toml file (in the [dependencies] section):
rand = "0.3"
Next, add the following line to your main.rs file:
extern crate rand;
And we're done! Now we can write the generation function of the tetrimino:
fn create_new_tetrimino() -> Tetrimino { let rand_nb = rand::random::<u8>() % 7; match rand_nb { 0 => TetriminoI::new(), 1 => TetriminoJ::new(), 2 => TetriminoL::new(), 3 => TetriminoO::new(), 4 => TetriminoS::new(), 5 => TetriminoZ::new(), 6 => TetriminoT::new(), _ => unreachable!(), } }
Pretty easy, right? Though, please note that ...
Read now
Unlock full access