Skip to content

Commit 6106e03

Browse files
Merge pull request #928 from marquesrs/bounce-rust-implementation
Rust implementation for the game Bounce
2 parents daa961e + 4f46b2f commit 6106e03

File tree

4 files changed

+208
-12
lines changed

4 files changed

+208
-12
lines changed

13_Bounce/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

13_Bounce/rust/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]

13_Bounce/rust/src/main.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/** BOUNCE GAME
2+
* https://github.com/coding-horror/basic-computer-games/blob/main/13_Bounce/bounce.bas
3+
* Direct conversion from BASIC to Rust by Pablo Marques (marquesrs).
4+
* No additional features or improvements were added. As a faithful translation,
5+
* many of the code here are done in an unrecommended way by today's standards.
6+
* 03/03/25
7+
*/
8+
9+
use std::io::Write;
10+
11+
fn input(msg: &str) -> String {
12+
print!("{}", msg);
13+
let _ =std::io::stdout().flush().unwrap();
14+
let mut input = String::new();
15+
std::io::stdin().read_line(&mut input).unwrap();
16+
return input.trim().to_uppercase();
17+
}
18+
19+
fn main() {
20+
//10 PRINT TAB(33);"BOUNCE"
21+
//20 PRINT TAB(15);"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
22+
//30 PRINT:PRINT:PRINT
23+
print!("{}{}\n{}{}\n\n\n",
24+
" ".repeat(33),
25+
"BOUNCE",
26+
" ".repeat(15),
27+
"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
28+
);
29+
30+
//90 DIM T(20)
31+
let mut t: Vec<f32> = Vec::with_capacity(20);
32+
33+
//100 PRINT "THIS SIMULATION LETS YOU SPECIFY THE INITIAL VELOCITY"
34+
//110 PRINT "OF A BALL THROWN STRAIGHT UP, AND THE COEFFICIENT OF"
35+
//120 PRINT "ELASTICITY OF THE BALL. PLEASE USE A DECIMAL FRACTION"
36+
//130 PRINT "COEFFICIENCY (LESS THAN 1)."
37+
//131 PRINT
38+
//132 PRINT "YOU ALSO SPECIFY THE TIME INCREMENT TO BE USED IN"
39+
//133 PRINT "'STROBING' THE BALL'S FLIGHT (TRY .1 INITIALLY)."
40+
//134 PRINT
41+
print!("{}\n{}\n{}\n{}\n\n{}\n{}\n\n",
42+
"THIS SIMULATION LETS YOU SPECIFY THE INITIAL VELOCITY",
43+
"OF A BALL THROWN STRAIGHT UP, AND THE COEFFICIENT OF",
44+
"ELASTICITY OF THE BALL. PLEASE USE A DECIMAL FRACTION",
45+
"COEFFICIENCY (LESS THAN 1).",
46+
"YOU ALSO SPECIFY THE TIME INCREMENT TO BE USED IN",
47+
"'STROBING' THE BALL'S FLIGHT (TRY .1 INITIALLY).",
48+
);
49+
50+
loop {
51+
//135 INPUT "TIME INCREMENT (SEC)";S2
52+
let s2 = input("TIME INCREMENT (SEC): ").parse::<f32>().unwrap();
53+
//let s2 = 0.2f32;
54+
55+
//140 PRINT
56+
println!();
57+
58+
//150 INPUT "VELOCITY (FPS)";V
59+
let v = input("VELOCITY (FPS): ").parse::<f32>().unwrap();
60+
//let v = 20.0f32;
61+
62+
//160 PRINT
63+
println!();
64+
65+
//170 INPUT "COEFFICIENT";C
66+
let c = input("COEFFICIENT: ").parse::<f32>().unwrap();
67+
//let c = 0.6f32;
68+
69+
//180 PRINT
70+
//182 PRINT "FEET"
71+
//184 PRINT
72+
print!("\nFEET\n\n");
73+
74+
//186 S1=INT(70/(V/(16*S2))) // verified
75+
let s1 = (70.0 / (v/(16.0*s2))) as i32;
76+
77+
//190 FOR I=1 TO S1
78+
for i in 1..=s1 {
79+
//200 T(I)=V*C^(I-1)/16
80+
t.push(v * c.powf(i as f32 - 1.0) / 16.0); // verified
81+
//210 NEXT I
82+
}
83+
84+
let mut l = 0.0;
85+
86+
//220 FOR H=INT(-16*(V/32)^2+V^2/32+.5) TO 0 STEP -.5
87+
let mut h = (-16.0 * (v / 32.0).powi(2) + (v.powi(2)) / 32.0 + 0.5).floor();
88+
while h >= 0.0 {
89+
let mut line_content = String::new();
90+
//221 IF INT(H)<>H THEN 225
91+
if h.floor() == h {
92+
//222 PRINT H;
93+
line_content.push_str(h.to_string().as_str());
94+
line_content.push(' ');
95+
}
96+
//225 L=0
97+
l = 0.0;
98+
//230 FOR I=1 TO S1
99+
for i in 1..=s1 {
100+
let mut t_val = 0.0;
101+
//240 FOR T=0 TO T(I) STEP S2
102+
while t_val <= t[(i - 1) as usize] {
103+
//245 L=L+S2
104+
l = l + s2;
105+
106+
//250 IF ABS(H-(.5*(-32)*T^2+V*C^(I-1)*T))>.25 THEN 270
107+
let condition = h - (0.5 * (-32.0) * t_val.powf(2.0) + v * c.powf((i-1) as f32) * t_val);
108+
if condition.abs() >= 0.25{
109+
t_val = t_val + s2;
110+
continue;
111+
}
112+
// TABS ARE NOT SPACES, BUT A TERMINAL POSITION
113+
//260 PRINT TAB(L/S2);"0";
114+
let spaces = ((l / s2) - 1.0) as usize;
115+
while line_content.len() < spaces {
116+
line_content.push(' ');
117+
}
118+
line_content.push('0');
119+
120+
//270 NEXT T
121+
t_val = t_val + s2;
122+
}
123+
124+
//275 T=T(I+1)/2
125+
if i as usize == t.len() { break; }
126+
t_val = t[i as usize] / 2.0;
127+
128+
//276 IF -16*T^2+V*C^(I-1)*T<H THEN 290
129+
if -16.0 * t_val.powf(2.0) + v * c.powf(i as f32 -1.0) * t_val <= h {
130+
break;
131+
}
132+
133+
//280 NEXT I
134+
}
135+
print!("{}", line_content);
136+
//290 PRINT
137+
println!();
138+
139+
//300 NEXT H
140+
h = h - 0.5;
141+
}
142+
143+
let mut line_content = String::from("");
144+
145+
//310 PRINT TAB(1);
146+
print!(" ");
147+
148+
//320 FOR I=1 TO INT(L+1)/S2+1
149+
for _ in 1..=((l+1.0) / s2 + 1.0) as i32 {
150+
//330 PRINT ".";
151+
line_content.push('.');
152+
//340 NEXT I
153+
}
154+
155+
//350 PRINT
156+
//355 PRINT " 0";
157+
println!("{}", line_content);
158+
159+
line_content = String::from(" 0");
160+
161+
//360 FOR I=1 TO INT(L+.9995)
162+
for i in 1..=((l + 0.9995) as i32) {
163+
//380 PRINT TAB(INT(I/S2));I;
164+
while line_content.len() < (i as f32 / s2) as usize {
165+
line_content.push(' ');
166+
}
167+
line_content.push_str(i.to_string().as_str());
168+
//390 NEXT I
169+
}
170+
171+
println!("{}", line_content);
172+
173+
//400 PRINT
174+
//410 PRINT TAB(INT(L+1)/(2*S2)-2);"SECONDS"
175+
//420 PRINT
176+
let tabs = ((l+1.0) / (2.0 * s2) - 2.0) as usize;
177+
println!("{}SECONDS\n", " ".repeat(tabs));
178+
179+
//430 GOTO 135
180+
//break;
181+
}
182+
//440 END
183+
}

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ NOTE: per [the official blog post announcement](https://blog.codinghorror.com/up
8989
| 09_Battle | x | x | x | | | | x | | x | x |
9090
| 10_Blackjack | x | x | x | | | | x | x | x | x |
9191
| 11_Bombardment | x | x | x | | | x | x | x | x | x |
92-
| 12_Bombs_Away | x | x | x | | x | x | x | | | x |
93-
| 13_Bounce | x | x | x | | | x | x | x | | x |
92+
| 12_Bombs_Away | x | x | x | | x | x | x | | x | x |
93+
| 13_Bounce | x | x | x | | | x | x | x | x | x |
9494
| 14_Bowling | x | x | x | | | x | x | | | x |
9595
| 15_Boxing | x | x | x | | | x | x | | | x |
9696
| 16_Bug | x | x | x | | | | x | x | | x |
9797
| 17_Bullfight | x | x | x | x | | | x | | | x |
9898
| 18_Bullseye | x | x | x | | | x | x | | x | x |
99-
| 19_Bunny | x | x | x | | | x | x | x | | x |
99+
| 19_Bunny | x | x | x | | | x | x | x | x | x |
100100
| 20_Buzzword | x | x | x | | x | x | x | x | x | x |
101101
| 21_Calendar | x | x | x | | | x | x | x | x | x |
102102
| 22_Change | x | x | x | | | x | x | | x | x |
103103
| 23_Checkers | x | | x | | | x | x | x | | x |
104104
| 24_Chemist | x | x | x | | | x | x | | x | x |
105-
| 25_Chief | x | x | x | | x | x | x | x | | x |
105+
| 25_Chief | x | x | x | | x | x | x | x | x | x |
106106
| 26_Chomp | x | x | x | | | x | x | | | x |
107107
| 27_Civil_War | x | x | x | | | | x | | | x |
108108
| 28_Combat | x | x | x | | | x | x | | | x |
@@ -120,49 +120,49 @@ NOTE: per [the official blog post announcement](https://blog.codinghorror.com/up
120120
| 40_Gomoko | x | x | x | | | x | x | | | x |
121121
| 41_Guess | x | x | x | | | x | x | x | x | x |
122122
| 42_Gunner | x | x | x | | | x | x | | | x |
123-
| 43_Hammurabi | x | x | x | | | | x | | | x |
123+
| 43_Hammurabi | x | x | x | | | | x | | x | x |
124124
| 44_Hangman | x | x | x | | | x | x | x | | x |
125-
| 45_Hello | x | x | x | | x | x | x | x | | x |
125+
| 45_Hello | x | x | x | | x | x | x | x | x | x |
126126
| 46_Hexapawn | x | | | | | | x | | | x |
127127
| 47_Hi-Lo | x | | x | x | x | x | x | x | x | x |
128128
| 48_High_IQ | x | x | x | | | | x | | | x |
129129
| 49_Hockey | x | | x | | | | x | | | x |
130130
| 50_Horserace | x | x | x | | | | | | x | x |
131131
| 51_Hurkle | x | x | x | | | x | x | x | x | x |
132-
| 52_Kinema | x | x | x | | | x | x | x | | x |
132+
| 52_Kinema | x | x | x | | | x | x | x | x | x |
133133
| 53_King | x | | x | | | | x | | x | x |
134134
| 54_Letter | x | x | x | | | x | x | x | x | x |
135135
| 55_Life | x | x | x | | | x | x | x | x | x |
136136
| 56_Life_for_Two | x | x | x | | | x | x | | | x |
137137
| 57_Literature_Quiz | x | x | x | | | x | x | | x | x |
138-
| 58_Love | x | x | x | | | x | x | x | | x |
138+
| 58_Love | x | x | x | | | x | x | x | x | x |
139139
| 59_Lunar_LEM_Rocket | x | | x | | | | x | | x | x |
140140
| 60_Mastermind | x | x | x | | | x | x | | x | x |
141141
| 61_Math_Dice | x | x | x | | | x | x | x | x | x |
142142
| 62_Mugwump | x | x | x | | | x | x | | x | x |
143-
| 63_Name | x | x | x | x | | x | x | x | | x |
143+
| 63_Name | x | x | x | x | | x | x | x | x | x |
144144
| 64_Nicomachus | x | x | x | | | x | x | | x | x |
145145
| 65_Nim | x | | x | | | | x | x | x | x |
146146
| 66_Number | x | x | x | | | x | x | | x | x |
147147
| 67_One_Check | x | x | x | | | x | x | | | x |
148148
| 68_Orbit | x | x | x | | | x | x | x | x | x |
149-
| 69_Pizza | x | x | x | | | x | x | x | | x |
149+
| 69_Pizza | x | x | x | | | x | x | x | x | x |
150150
| 70_Poetry | x | x | x | | | x | x | x | | x |
151151
| 71_Poker | x | x | x | | | | | | | x |
152152
| 72_Queen | x | | x | | | x | x | | x | x |
153153
| 73_Reverse | x | x | x | | | x | x | x | | x |
154154
| 74_Rock_Scissors_Paper | x | x | x | x | | x | x | x | x | x |
155155
| 75_Roulette | x | x | x | | | x | x | | x | x |
156156
| 76_Russian_Roulette | x | x | x | x | | x | x | x | x | x |
157-
| 77_Salvo | x | | x | | | | x | | | x |
157+
| 77_Salvo | x | | x | | | | x | | x | x |
158158
| 78_Sine_Wave | x | x | x | x | | x | x | x | x | x |
159159
| 79_Slalom | x | | x | | | | x | | | x |
160160
| 80_Slots | x | x | x | | | x | x | x | | x |
161161
| 81_Splat | x | x | x | | | x | x | | x | x |
162162
| 82_Stars | x | x | x | | | x | x | x | x | x |
163163
| 83_Stock_Market | x | x | x | | | | x | | | x |
164164
| 84_Super_Star_Trek | x | x | x | | | | x | | x | x |
165-
| 85_Synonym | x | x | x | | | x | x | x | | x |
165+
| 85_Synonym | x | x | x | | | x | x | x | x | x |
166166
| 86_Target | x | x | x | | | x | x | | | x |
167167
| 87_3-D_Plot | x | x | x | | | x | x | x | | x |
168168
| 88_3-D_Tic-Tac-Toe | x | | x | | | | x | | | x |

0 commit comments

Comments
 (0)