mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.3 KiB
4.3 KiB
mobs
Instructions
Create a module named mobs
, containing a structure Mob
which has:
name
:String
boss
:Boss
members
: a vector ofMember
cities
: a vector of tuples containing a city name and au8
wealth
:u32
recruit
: an associated function which adds aMember
to themembers
vector. It should accept aname
, and anage
. The member's role should be set toAssociate
.attack
: an associated function which receives anotherMob
. It will remove the last member from the vector ofMember
from whichever mob has the least power combat score. In the case of a draw, the attacker loses. In the case that one of the mobs is left with zero members, the victorious mob will also take the cities and wealth from the losing mob. The power combat score is calculated from the sum of therole
of each mobmember
:Underboss
: 4Caporegime
: 3Soldier
: 2Associate
: 1
steal
: an associated function which receives aMob
to target, and au32
value to steal. The 'self' mob steals the value from the wealth of the target mob, and adds the value to its own wealth. Only as much money as the target mob has can be stolen.conquer_city
: an associated function which receives a vector ofMob
, a city name and au8
value. The city name andu8
value are added to its list of cities if non of the other mobs in the vector have a city with the same name.
You will also need to create two submodules of mob:
boss
: which should contain:Boss
: a struct which consists of:name
:String
age
:u8
new
: an associated function which accepts aname
andage
, and returns aBoss
.
member
submodule which consists of:Role
: an enum with the variants:Underboss
Caporegime
Soldier
Associate
Member
: a struct which consists of:name
:String
role
:Role
age
:u8
get_promotion
: an associated function which when invoked should promote the member from:Associate
->Soldier
Soldier
->Caporegime
Caporegime
->Underboss
new
: accepts aname
,role
andage
, returning aMember
.
The submodules should be created inside a folder named mobs
. We advise you to create two files for each submodule, but that is up to you.
You must include #[derive(Debug, Clone, PartialEq)]
above every struct and enum.
Expected Function
You'll need to work out the function signatures for yourself.
Usage
Here is a program to test your function:
use mobs::*;
fn main() {
let (mafia1, mafia2) = (
Mob {
name: "Hairy Giants".to_string(),
boss: boss::Boss::new("Louie HaHa", 36),
cities: vec![("San Francisco".to_string(), 7)],
members: vec![
member::Member::new("Benny Eggs", member::Role::Soldier, 28),
member::Member::new("Jhonny", member::Role::Associate, 17),
member::Member::new("Greasy Thumb", member::Role::Soldier, 30),
member::Member::new("No Finger", member::Role::Caporegime, 32),
],
wealth: 100000,
},
Mob {
name: "Red Thorns".to_string(),
boss: boss::Boss::new("Big Tuna", 30),
cities: vec![("San Jose".to_string(), 5)],
members: vec![
member::Member::new("Knuckles", member::Role::Soldier, 25),
member::Member::new("Baldy Dom", member::Role::Caporegime, 36),
member::Member::new("Crazy Joe", member::Role::Underboss, 23),
],
wealth: 70000,
},
);
println!("{:?}\n{:?}", mafia1, mafia2);
}
And its output:
$ cargo run
Mob { name: "Hairy Giants", boss: Boss { name: "Louie HaHa", age: 36 }, members: [Member { name: "Benny Eggs", role: Soldier, age: 28 }, Member { name: "Jhonny", role: Associate, age: 17 }, Member { name: "Greasy Thumb", role: Soldier, age: 30 }, Member { name: "No Finger", role: Caporegime, age: 32 }], cities: [("San Francisco", 7)], wealth: 100000 }
Mob { name: "Red Thorns", boss: Boss { name: "Big Tuna", age: 30 }, members: [Member { name: "Knuckles", role: Soldier, age: 25 }, Member { name: "Baldy Dom", role: Caporegime, age: 36 }, Member { name: "Crazy Joe", role: Underboss, age: 23 }], cities: [("San Jose", 5)], wealth: 70000 }
$