Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in rust language. need the 2nd part and extra credit. Intent The purpose of this lab is to give students experience implementing a finite state

in rust language. need the 2nd part and extra credit.

Intent The purpose of this lab is to give students experience implementing a finite state machine in Rust. Finite state machines are used in a variety of applications in programs. They are especially useful in communications protocols, where each communicating side is expecting a set of symbols in a specific order. This case requires the implementation of two interacting state machines. Another good example of the use of a state machine in game development where a character can be in one of a limited number of states at one time and be limited from moving between certain states. For example, a character who is sitting cannot start running without first standing.

Process For this exercise the student must first define the transition diagram and draw a finite state machine for a game character with at least the following states: laying, sitting, standing, walking, running, jumping, falling, dead, where dead is a final state. The inputs are the arrow keys, or the characters {h,j,k,l} which represent {left, down, up, right}. All states must be reachable using the input language. Explain your choices of transitions.

Implement your finite state machine in Rust, and include a main program which allows the user to input a string in the language of the state machine. The program should with each transition display a representation of the character in the state. If the string is accepted, complete a final display and exit. (hint: infinite loop -> read input, perform action(s), display)

Design a second state machine which maintains the direction the character if facing. Include the design in your report.

Implement the second state machine into your code. Include with the display of the representation of the character an indication of the direction of the character.

(Extra credit) Extend your program to track the location of your character. Assume the character starts as some location (0,0). Track the location based on orientation, momentum associated with a state (note: if you can jump while walking, or running, you may need to adjust your original design to account for this), and previous location. Include with your representation of your characters state and orientation, a representation of their location.

(Extra credit) Add a character state crash which is entered if a character enters a location already occupied by an object, or obstacle. The location of the character upon crash should be just prior to the crash point. (Extra credit) Make your representations graphical.

enum State {

Laying,

Sitting,

Standing,

Walking,

Running,

Jumping,

Falling,

Dead,

}

enum Input {

Left,

Down,

Up,

Right,

}

struct Character {

state: State,

direction: Direction,

location: Location,

}

enum Direction {

Left,

Right,

}

struct Location {

x: i32,

y: i32,

}

impl Character {

fn new() -> Self {

Character {

state: State::Standing,

direction: Direction::Right,

location: Location { x: 0, y: 0 },

}

}

fn transition(&mut self, input: Input) -> bool {

match (self.state, input) {

(State::Laying, _) => {

// Laying is a final state, no transitions

false

}

(State::Sitting, Input::Up) => {

self.state = State::Standing;

true

}

(State::Sitting, _) => {

// Can't move while sitting

false

}

(State::Standing, Input::Left) => {

self.direction = Direction::Left;

true

}

(State::Standing, Input::Right) => {

self.direction = Direction::Right;

true

}

(State::Standing, Input::Up) => {

self.state = State::Jumping;

true

}

(State::Standing, Input::Down) => {

self.state = State::Sitting;

true

}

(State::Walking, Input::Left) => {

self.direction = Direction::Left;

true

}

(State::Walking, Input::Right) => {

self.direction = Direction::Right;

true

}

(State::Walking, Input::Up) => {

self.state = State::Jumping;

true

}

(State::Walking, Input::Down) => {

self.state = State::Sitting;

true

}

(State::Running, Input::Left) => {

self.direction = Direction::Left;

true

}

(State::Running, Input::Right) => {

self.direction = Direction::Right;

true

}

(State::Running, Input::Up) => {

self.state = State::Jumping;

true

}

(State::Running, Input::Down) => {

self.state = State::Sitting;

true

}

(State::Jumping, _) => {

self.state = State::Falling;

true

}

(State::Falling, _) => {

self.state = State::Dead;

true

}

(State::Dead, _) => {

// Dead is a final state, no transitions

false

}

}

}

fn display(&self) {

println!(

"Character is {:?} {} facing {:?} at ({}, {})",

self.state, self.direction, self.location.x, self.location.y

);

}

}

fn main() {

let mut character = Character::new();

character.display();

loop {

let input = get_input();

if !character.transition(input) {

break;

}

character.display();

}

character.display();

}

fn get_input() -> Input {

// read input from

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What is computer neworking ?

Answered: 1 week ago

Question

=+4 Develop and deliver the CCT program.

Answered: 1 week ago