Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D7018E-Rust-Course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fredrik Pettersson
D7018E-Rust-Course
Commits
46c428d9
Commit
46c428d9
authored
7 years ago
by
DevDoggo
Browse files
Options
Downloads
Patches
Plain Diff
Added more result-printing functions.
parent
0adf0e71
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
a2_guessing_game/src/main.rs
+55
-3
55 additions, 3 deletions
a2_guessing_game/src/main.rs
with
55 additions
and
3 deletions
a2_guessing_game/src/main.rs
+
55
−
3
View file @
46c428d9
...
@@ -3,6 +3,9 @@ extern crate colored;
...
@@ -3,6 +3,9 @@ extern crate colored;
use
std
::
io
;
//standard in-out
use
std
::
io
;
//standard in-out
use
std
::
cmp
::
Ordering
;
use
std
::
cmp
::
Ordering
;
use
std
::
cmp
::
max
;
use
std
::
collections
::
HashMap
;
use
rand
::
Rng
;
use
rand
::
Rng
;
use
std
::
io
::
Write
;
//seemingly needed to use io::stdout().flush()
use
std
::
io
::
Write
;
//seemingly needed to use io::stdout().flush()
use
colored
::
*
;
use
colored
::
*
;
...
@@ -23,11 +26,48 @@ fn get_input() -> Result<u32, String> {
...
@@ -23,11 +26,48 @@ fn get_input() -> Result<u32, String> {
}
}
fn
print_guesses
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
fn
print_guesses
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
println!
(
"Results:
\n
Attempt | Guess"
);
let
iterations
=
2
;
for
i
in
0
..
iterations
{
println!
(
"
\n
Results: Printing All
\n
Attempt | Guess"
);
for
g
in
v
{
for
g
in
v
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
}
}
}
}
}
fn
print_the_last_three_guesses_in_backwards_order_really_nicely
(
v
:
&
Vec
<
(
u32
,
String
)
>
)
{
let
mut
count
=
0
;
println!
(
"
\n
Results: Printing Last Three
\n
Attempt | Guess"
);
for
g
in
v
.iter
()
.rev
()
{
println!
(
" {} | {}"
,
g
.0
,
g
.1
);
count
+=
1
;
if
count
==
3
{
break
;
}
}
}
fn
print_hashmap
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing HashMap
\n
Attempt | Guess"
);
for
m
in
hm
{
println!
(
" {:?} | {:?}"
,
m
.0
,
m
.1
);
}
}
fn
print_hashmap_last_three
(
hm
:
&
HashMap
<
u32
,
String
>
)
{
println!
(
"
\n
Results: Printing Last Three in Hashmap
\n
Attempt | Guess"
);
let
mut
entries
=
0
;
for
m
in
hm
.iter
()
{
entries
+=
1
;
}
//let ln: u32 = hm.len(); //Give error because hm.len() is usize, not u32
for
i
in
(
max
(
entries
-
2
,
1
)
..
entries
+
1
)
.rev
()
{
let
mut
item
=
hm
.get
(
&
i
);
match
item
{
Some
(
num
)
=>
println!
(
" {} | {}"
,
i
,
num
),
None
=>
println!
(
"Nothing!"
),
};
}
}
fn
main
()
{
fn
main
()
{
println!
(
"{}"
,
"
\n\n
Guess the number or perish. :)"
.bold
());
println!
(
"{}"
,
"
\n\n
Guess the number or perish. :)"
.bold
());
...
@@ -40,6 +80,10 @@ fn main() {
...
@@ -40,6 +80,10 @@ fn main() {
//Vector to hold all guesses, first to last.
//Vector to hold all guesses, first to last.
let
mut
guesses
:
Vec
<
(
u32
,
String
)
>=
Vec
::
new
();
let
mut
guesses
:
Vec
<
(
u32
,
String
)
>=
Vec
::
new
();
let
mut
guess_pair
:
(
u32
,
String
);
let
mut
guess_pair
:
(
u32
,
String
);
//Hashmap
let
mut
guess_hashmap
=
HashMap
::
new
();
let
mut
guess
:
u32
;
let
mut
guess
:
u32
;
let
mut
attempts
=
0
;
let
mut
attempts
=
0
;
...
@@ -56,9 +100,14 @@ fn main() {
...
@@ -56,9 +100,14 @@ fn main() {
};
};
attempts
+=
1
;
attempts
+=
1
;
//Vector
guess_pair
=
(
attempts
,
guess
.to_string
());
guess_pair
=
(
attempts
,
guess
.to_string
());
guesses
.push
(
guess_pair
);
guesses
.push
(
guess_pair
);
//Hashmap
guess_hashmap
.insert
(
attempts
,
guess
.to_string
());
println!
(
"
\n
You guessed: {}!"
,
guess
);
println!
(
"
\n
You guessed: {}!"
,
guess
);
print!
(
"That's "
);
print!
(
"That's "
);
...
@@ -78,7 +127,10 @@ fn main() {
...
@@ -78,7 +127,10 @@ fn main() {
}
}
println!
(
"
\n
You've guessed {} time(s) already!"
,
attempts
.to_string
()
.yellow
())
println!
(
"
\n
You've guessed {} time(s) already!"
,
attempts
.to_string
()
.yellow
())
}
}
//println!("{:?}", guesses);
print_guesses
(
&
guesses
);
print_guesses
(
&
guesses
);
print_the_last_three_guesses_in_backwards_order_really_nicely
(
&
guesses
);
print_hashmap
(
&
guess_hashmap
);
print_hashmap_last_three
(
&
guess_hashmap
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment