diff options
| author | tyropro <[email protected]> | 2025-10-10 00:45:38 +0100 |
|---|---|---|
| committer | tyropro <[email protected]> | 2025-10-10 00:45:38 +0100 |
| commit | b5a78217d39a0127cd9ad62b2fb41119f308cb67 (patch) | |
| tree | 4877047363b70a1a975c85c5ed88bba8cc345373 | |
| parent | dd2a87355574497d249c5809231a26d181d61a92 (diff) | |
optimised code to reference the player object instead of passing and replacing values in the object
| -rw-r--r-- | Cargo.lock | 54 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/main.rs | 177 |
3 files changed, 112 insertions, 123 deletions
@@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "cfg-if" @@ -10,20 +10,21 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "getrandom" -version = "0.2.10" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" dependencies = [ "cfg-if", "libc", + "r-efi", "wasi", ] [[package]] name = "libc" -version = "0.2.147" +version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" [[package]] name = "ppv-lite86" @@ -32,21 +33,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] name = "rand" -version = "0.8.5" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" dependencies = [ - "libc", "rand_chacha", "rand_core", ] [[package]] name = "rand_chacha" -version = "0.3.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" dependencies = [ "ppv-lite86", "rand_core", @@ -54,22 +60,40 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.4" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" dependencies = [ "getrandom", ] [[package]] name = "rs-snakesandladders" -version = "0.2.0" +version = "1.1.0" dependencies = [ "rand", ] [[package]] name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" +version = "0.14.7+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "883478de20367e224c0090af9cf5f9fa85bed63a95c1abf3afc5c083ebc06e8c" +dependencies = [ + "wasip2", +] + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wit-bindgen" +version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" @@ -1,7 +1,7 @@ [package] name = "rs-snakesandladders" -version = "1.0.0" +version = "1.1.0" edition = "2021" [dependencies] -rand = "0.8.5" +rand = "0.9.2" diff --git a/src/main.rs b/src/main.rs index 20a4ad9..dcd00de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,13 @@ const BOARD: [u8; 100] = [ ]; const GOAL: u8 = 100; // goal space -//const NO_PLAYERS: u8 = 2; + //const NO_PLAYERS: u8 = 2; struct Player { current_space: u8, // current space the player is on turns: u8, // number of turns the player has taken won: bool, // whether the player has won the game or not name: String, // name of the player - moves: Vec<u8>, // moves taken by the player } impl Player { @@ -28,116 +27,31 @@ impl Player { turns: 0, won: false, name, - moves: Vec::new(), } } - - fn clone(&self) -> Player { - Player { - current_space: self.current_space, - turns: self.turns, - won: self.won, - name: self.name.clone(), - moves: self.moves.clone(), - } - } -} - -/// Plays a single turn for the given player by rolling the dice, checking for overshooting the goal, handling snakes/ladders, and recording the new position. Returns the updated player state after the turn. -fn turn(mut player: Player) -> Player { - player.turns += 1; - - let name = player.name.clone(); - - // play 1 turn - player.current_space = roll(player.current_space, name); - - // added new goal logic in the main function - // play = player_1.current_space != GOAL; - - player.current_space = check_over_goal(player.current_space); - player.current_space = check_movement(player.current_space); - - player.moves.push(player.current_space); - - player -} - -// Rolls the dice and returns the new space the player is on. -fn roll(mut space: u8, name: String) -> u8 { - let mut rng: rand::rngs::ThreadRng = rand::thread_rng(); - - let roll: u8 = rng.gen_range(1..7); // rolls the dice (random number 1-6) - space += roll; // adds rolls to current space - - // outputs the current space and roll to the user - println!("{} rolled a {}.", name, roll); - println!("You are now on space {}.", space); - - // returns 'space' - space } -// Checks if the player has gone over the goal and returns the new space. -fn check_over_goal(mut space: u8) -> u8 { - if space > 100 { - // checks if the player has gone over the goal - let spaces_over: u8 = space - GOAL; // gets how many spaces the user has gone over 100 - space -= spaces_over * 2; // decreases current spaces by 'spaces over 100' * 2 (*2 is necessary to reduce the space below 100) - - // outputs the current space and how many spaces over 100 to the user - println!("You have rolled {} spaces over 100.", spaces_over); - println!("You are now on space {}.", space); - } - - // returns 'space' - space -} +fn main() { + // print name of game + println!("Snakes & Ladders\n"); -// Checks if the player has moved to a new space and returns the new space. -fn check_movement(mut space: u8) -> u8 { - // converts the space into an index and 'usize' type - let space_index: usize = (space - 1) as usize; + let mut players: Vec<Player> = Vec::new(); - if BOARD[space_index] != 0 { - let movement_object: &str = if BOARD[space_index] > space { - "ladder" - } else { - "snake" - }; + println!("How many players?"); - space = BOARD[space_index]; // sets space to the end of the object (snake or ladder) + let mut str_no_players = String::new(); + std::io::stdin().read_line(&mut str_no_players).expect(""); - // outputs the current space and the object to the user - println!("You have landed on a {}.", movement_object); - println!("You are now on space {}.", space); - } + let raw_no_players: Result<u8, _> = str_no_players.trim().parse(); - // returns 'space' - space -} + let no_players: u8 = match raw_no_players { + Ok(parsed_num) => parsed_num, + Err(_) => { + println!("Please enter a valid unsigned integer"); + return; + } + }; -fn main() { - // print name of game - println!("Snakes & Ladders\n"); - - let mut players: Vec<Player> = Vec::new(); - - println!("How many players?"); - - let mut str_no_players = String::new(); - std::io::stdin().read_line(&mut str_no_players).expect(""); - - let raw_no_players: Result<u8, _> = str_no_players.trim().parse(); - - let no_players: u8 = match raw_no_players { - Ok(parsed_num) => parsed_num, - Err(_) => { - println!("Please enter a valid unsigned integer"); - return; - }, - }; - for i in 0..no_players { // creates new players println!("\nEnter name of player {}:", i + 1); @@ -166,10 +80,7 @@ fn main() { let current_player_index: usize = (current_player - 1) as usize; // logic for turn - let player: Player = players[current_player_index].clone(); - players[current_player_index] = turn(player); - - players[current_player_index].won = players[current_player_index].current_space == GOAL; + turn(&mut players[current_player_index]); if players[current_player_index].won { // tells the user they've won and how many turns it took. @@ -186,3 +97,57 @@ fn main() { } } } + +// Plays a single turn for the given player by rolling the dice, checking for overshooting the goal, handling snakes/ladders, and recording the new position. +fn turn(player: &mut Player) { + player.turns += 1; + + roll(player); + check_over_goal(player); + check_movement(player); + + player.won = player.current_space == GOAL; +} + +// Rolls the dice and returns the new space the player is on. +fn roll(player: &mut Player) { + let mut rng: rand::rngs::ThreadRng = rand::rng(); + + let roll: u8 = rng.random_range(1..7); // rolls the dice (random number 1-6) + player.current_space += roll; + + // outputs the current space and roll to the user + println!("{} rolled a {}.", player.name, roll); + println!("You are now on space {}.", player.current_space); +} + +// Checks if the player has gone over the goal and returns the new space. +fn check_over_goal(player: &mut Player) { + if player.current_space > GOAL { + let spaces_over: u8 = player.current_space - GOAL; + player.current_space -= spaces_over * 2; // decreases current spaces by 'spaces over 100' * 2 (*2 is necessary to reduce the space below 100) + + // outputs the current space and how many spaces over 100 to the user + println!("You have rolled {} spaces over 100.", spaces_over); + println!("You are now on space {}.", player.current_space); + } +} + +// Checks if the player has moved to a new space and returns the new space. +fn check_movement(player: &mut Player) { + let space_index: usize = (player.current_space - 1) as usize; + + if BOARD[space_index] != 0 { + let movement_object: &str = if BOARD[space_index] > player.current_space { + "ladder" + } else { + "snake" + }; + + player.current_space = BOARD[space_index]; // sets space to the end of the object (snake or ladder) + + // outputs the current space and the object to the user + println!("You have landed on a {}.", movement_object); + println!("You are now on space {}.", player.current_space); + } +} |
