Skip to content

Commit 847c9ee

Browse files
committed
bounce game initial rust implementation
1 parent 9c13d01 commit 847c9ee

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

13_Bounce/rust/src/main.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,60 +58,58 @@ fn main() {
5858
//180 PRINT
5959
//182 PRINT "FEET"
6060
//184 PRINT
61-
print!("\nFEET\n");
61+
print!("\nFEET\n\n");
6262

63-
//186 S1=INT(70/(V/(16*S2)))
64-
let s1 = (70.0 / (v / (16.0 * s2))).floor() as i32; // verified
63+
//186 S1=INT(70/(V/(16*S2))) // verified
64+
let s1 = (70.0 / (v/(16.0*s2))) as i32;
6565

6666
//190 FOR I=1 TO S1
6767
for i in 1..=s1 {
6868
//200 T(I)=V*C^(I-1)/16
69-
t.push(v * c.powf(i as f32 - 1.0) / 16.0);
69+
t.push(v * c.powf(i as f32 - 1.0) / 16.0); // verified
7070
//210 NEXT I
7171
}
7272

7373
let mut l = 0.0;
7474

7575
//220 FOR H=INT(-16*(V/32)^2+V^2/32+.5) TO 0 STEP -.5
76-
let mut h = (-16.0 * (v / 32.0).powi(2) + (v.powi(2)) / 32.0 + 0.5).floor() as i32; // verified
77-
while h >= 0 {
76+
let mut h = (-16.0 * (v / 32.0).powi(2) + (v.powi(2)) / 32.0 + 0.5).floor();
77+
while h >= 0.0 {
7878
//221 IF INT(H)<>H THEN 225
79-
if h != h {
80-
//225 L=0
81-
l = 0.0;
82-
}
83-
else {
79+
if h.floor() == h {
8480
//222 PRINT H;
85-
println!("{}", h);
86-
//225 L=0
87-
l = 0.0;
81+
print!("{}", h);
8882
}
83+
//225 L=0
84+
l = 0.0;
8985

9086
//230 FOR I=1 TO S1
9187
for i in 1..=s1 {
92-
let mut t2 = 0.0;
88+
let mut T = 0.0;
9389
//240 FOR T=0 TO T(I) STEP S2
94-
while t2 <= t[(i - 1) as usize] {
90+
while T <= t[(i - 1) as usize] {
9591
//245 L=L+S2
9692
l = l + s2;
9793

9894
//250 IF ABS(H-(.5*(-32)*T^2+V*C^(I-1)*T))>.25 THEN 270
99-
let condition = h - (0.5 * (-32.0) * t2.powf(2.0) + v * c.powf((i-1) as f32) * t2);
100-
if condition >= 0.25{
95+
let condition = h - (0.5 * (-32.0) * T.powf(2.0) + v * c.powf((i-1) as f32) * T);
96+
if condition.abs() >= 0.25{
97+
T = T + s2;
10198
continue;
10299
}
103100
//260 PRINT TAB(L/S2);"0";
104-
print!("{}0", " ".repeat((l/s2) as usize));
101+
print!("{}{}", " ".repeat((l / s2) as usize), T);
105102

106103
//270 NEXT T
107-
t2 = t2 + s2;
104+
T = T + s2;
108105
}
109106

110107
//275 T=T(I+1)/2
111-
t2 = t[i as usize] / 2.0;
108+
if i as usize == t.len() { break; }
109+
T = t[i as usize] / 2.0;
112110

113111
//276 IF -16*T^2+V*C^(I-1)*T<H THEN 290
114-
if -16.0*t2.powf(2.0) + v * c.powf(i as f32 -1.0) * t2 <= h {
112+
if -16.0 * T.powf(2.0) + v * c.powf(i as f32 -1.0) * T <= h {
115113
break;
116114
}
117115

@@ -125,11 +123,12 @@ fn main() {
125123
h = h - 0.5;
126124
}
127125

126+
//
128127
//310 PRINT TAB(1);
129128
print!(" ");
130129

131130
//320 FOR I=1 TO INT(L+1)/S2+1
132-
for _ in 1..=((l+1.0).floor() / s2 + 1.0) as i32 {
131+
for _ in 1..=((l+1.0) / s2 + 1.0) as i32 {
133132
//330 PRINT ".";
134133
print!(".");
135134
//340 NEXT I
@@ -149,8 +148,8 @@ fn main() {
149148
//400 PRINT
150149
//410 PRINT TAB(INT(L+1)/(2*S2)-2);"SECONDS"
151150
//420 PRINT
152-
let tabs = ((l+1.0).floor() / (2.0 * s2) - 2.0) as usize;
153-
print!("\n{}SECONDS\n", " ".repeat(tabs));
151+
let tabs = ((l+1.0) / (2.0 * s2) - 2.0) as usize;
152+
println!("\n{}SECONDS\n", " ".repeat(tabs));
154153

155154
//430 GOTO 135
156155
}

0 commit comments

Comments
 (0)