i need help programming a new variable that adds a option that lets you run in my new game here is the code so far
use std::io;
use std::io::Write;
fn main() {
enum AlienType {
SNAKEALIEN,
OGREALIEN,
MARSHMALLOWMANALIEN,
}
struct Alien {
_type: AlienType, // stores one of the three above types
health: i32, // 0=dead 100=full strength
name: String
}
impl Alien {
fn new(t: AlienType, h: i32, n: String) -> Alien {
Alien {
_type: t,
health: h,
name: n,
}
}
}
struct AlienPack {
aliens: Vec<Alien>,
}
impl AlienPack {
fn new(num_aliens: usize) -> AlienPack {
AlienPack {
aliens: Vec::with_capacity(num_aliens),
}
}
fn add_alien(&mut self, new_alien: Alien) {
self.aliens.push(new_alien);
}
fn get_aliens(&self) -> &Vec<Alien> {
&self.aliens
}
fn calculate_damage(&self) -> i32 {
let mut damage = 0;
for alien in &self.aliens {
match alien._type {
AlienType::SNAKEALIEN => damage += 10, // Snake does 10 damage
AlienType::OGREALIEN => damage += 20, // Ogre does 20 damage
AlienType::MARSHMALLOWMANALIEN => damage += 5, // Marshmallow Man does 5 damage
}
}
damage
}
}
let mut alien_pack = AlienPack::new(3);
alien_pack.add_alien(Alien::new(AlienType::SNAKEALIEN, 100, String::from("Snake Alien")));
alien_pack.add_alien(Alien::new(AlienType::OGREALIEN, 100, String::from("Ogre Alien")));
alien_pack.add_alien(Alien::new(AlienType::MARSHMALLOWMANALIEN, 100, String::from("Marshmallow Man Alien")));
for alien in alien_pack.get_aliens() {
println!("{} - Health: {}", alien.name, alien.health);
}
println!("Total damage caused by the aliens: {}", alien_pack.calculate_damage());
struct Player {
health: i32,
}
impl Player {
fn new(health: i32) -> Player {
Player { health }
}
fn add_health(&mut self, amount: i32) {
self.health += amount;
}
fn display_health(&self) {
println!("Player 1's Health: {}", self.health);
}
}
let mut player1 = Player::new(100);
player1.display_health();
println!("Adding 20 health to Player 1...");
player1.add_health(20);
player1.display_health();
println!("Type 1 to attack aliens:");
let mut input = String::new();
print!("Enter your choice: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).expect("Failed to read line");
if let Some('\n') = input.chars().next_back() {
input.pop();
}
if let Some('\r') = input.chars().next_back() {
input.pop();
}
if input == "1" {
// Attack logic goes here
println!("Attacking aliens!");
} else {
println!("Invalid input. Please type 1 to attack aliens.");
}
}
contact me with the new code at mason.joshuah@marblefallsisd.org if you can help me
What's the question?
Could mean a bunch of things: perhaps you want something along these lines?
let mut running = false;
...
match input.as_str() {
"1" => {
// Attack logic goes here
println!("Attacking aliens!");
}
"2" => {
running = true;
println!("You start running!");
}
_ => {
println!("Invalid input. Please type 1 to attack aliens or 2 to run.");
}
}
Or do you mean how to read input until the game is finished? Or something else?
i am having trouble puting in a rust variable that lets you run when you type 2, thank you for your help
Well, presumably whether or not you're running is a property of the player (If you ever have multiple players, they don't necessarily start and stop running together). So, you probably want to define it something like this:
struct Player {
health: i32,
is_running: bool,
}
impl Player {
fn new(health: i32) -> Player {
Player { health, is_running = false }
}
fn run(&mut self) {
self.is_running = true;
}
// ...
}
// ...
if input == "1" {
// Attack logic goes here
println!("Attacking aliens!");
} else if input == "2" {
player1.run();
println!("Running away...");
} else {
println!("Invalid input. Please type 1 to attack aliens.");
}
i managed to fix it with your people's help thanks to you all here is the finished code
use std::io;
use std::io::Write;
fn main() {
enum AlienType {
SNAKEALIEN,
OGREALIEN,
MARSHMALLOWMANALIEN,
}
struct Alien {
_type: AlienType,
health: i32,
name: String
}
impl Alien {
fn new(t: AlienType, h: i32, n: String) -> Alien {
Alien {
_type: t,
health: h,
name: n,
}
}
}
struct AlienPack {
aliens: Vec<Alien>,
}
impl AlienPack {
fn new(num_aliens: usize) -> AlienPack {
AlienPack {
aliens: Vec::with_capacity(num_aliens),
}
}
fn add_alien(&mut self, new_alien: Alien) {
self.aliens.push(new_alien);
}
fn get_aliens(&self) -> &Vec<Alien> {
&self.aliens
}
fn calculate_damage(&self) -> i32 {
let mut damage = 0;
for alien in &self.aliens {
match alien._type {
AlienType::SNAKEALIEN => damage += 10,
AlienType::OGREALIEN => damage += 20,
AlienType::MARSHMALLOWMANALIEN => damage += 5,
}
}
damage
}
}
let mut alien_pack = AlienPack::new(3);
alien_pack.add_alien(Alien::new(AlienType::SNAKEALIEN, 100, String::from("Snake Alien")));
alien_pack.add_alien(Alien::new(AlienType::OGREALIEN, 100, String::from("Ogre Alien")));
alien_pack.add_alien(Alien::new(AlienType::MARSHMALLOWMANALIEN, 100, String::from("Marshmallow Man Alien")));
for alien in alien_pack.get_aliens() {
println!("{} - Health: {}", alien.name, alien.health);
}
println!("Total damage caused by the aliens: {}", alien_pack.calculate_damage());
struct Player {
health: i32,
}
impl Player {
fn new(health: i32) -> Player {
Player { health }
}
fn add_health(&mut self, amount: i32) {
self.health += amount;
}
fn display_health(&self) {
println!("Player's Health: {}", self.health);
}
fn run(&self) {
println!("Player is running away...");
}
}
let mut player2 = Player::new(80);
player2.display_health();
println!("Adding 15 health to Player 2...");
player2.add_health(15);
player2.display_health();
println!("Type 1 to attack aliens or 2 to run away:");
let mut input = String::new();
print!("Enter your choice: ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).expect("Failed to read line");
if let Some('\n') = input.chars().next_back() {
input.pop();
}
if let Some('\r') = input.chars().next_back() {
input.pop();
}
if input == "1" {
println!("Attacking aliens!");
} else if input == "2" {
player2.run();
} else {
println!("Invalid input. Please type 1 to attack aliens or 2 to run away.");
}
}
system
Closed
July 4, 2024, 2:53pm
7
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.