Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 1 #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Homework 1 #4

wants to merge 7 commits into from

Conversation

Jackliu-miaozi
Copy link
Owner

No description provided.

Comment on lines 24 to 26
let a: u64 =
self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block);
a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create a var:

Suggested change
let a: u64 =
self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block);
a
self.fed - HUNGER_PER_BLOCK * ((exec::block_height() as u64) - self.fed_block)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

impl Tamagotchi {
fn current_fed(&mut self) -> u64 {
let a: u64 =
self.fed - (HUNGER_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.fed_block);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If self.fed is less than HUNGER_PER_BLOCK * (exec::block_height() - self.fed_block), then subtraction results in the underflow.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Comment on lines 48 to 51
let birthdate = exec::block_height() as u64;
let fedblock = exec::block_height() as u64;
let entertainedblock = exec::block_height() as u64;
let sleptblock = exec::block_height() as u64;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to call exec::block_height multiple times as it is quite expensive. Better is to get block height one time and copy it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

tmg.slept = tmg.current_slept();
} else {
let fedblock = exec::block_height() as u64;
tmg.fed = 10000;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use named constants for numbers.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Comment on lines 95 to 96
tmg.entertained = tmg.current_entertained();
tmg.slept = tmg.current_slept();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same code is in both if branches.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

}
fn current_entertained(&mut self) -> u64 {
let b: u64 = self.entertained
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using self.entertained_block is not correct here. We need to save the last processed block height here.

Suggested change
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.entertained_block);
- (BOREDOM_PER_BLOCK as u64) * ((exec::block_height() as u64) - last_processed_block);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it.

Comment on lines 125 to 126
tmg.fed = tmg.current_fed();
tmg.entertained = tmg.current_entertained();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, you can move the fed / entertained / slept decreasing outside this match.


let _result = program.send(1, TmgAction::Sleep);
//how to test the panic result?
//negetive test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to check the Tamagotchi's vital signs after spending various counts of blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants