Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
intro-rust-game
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
Package registry
Model registry
Operate
Environments
Terraform modules
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
Michael Hauspie
intro-rust-game
Commits
c678d964
Commit
c678d964
authored
2 years ago
by
Michael Hauspie
Browse files
Options
Downloads
Patches
Plain Diff
Add board module
parent
d6aa69ae
Branches
Branches containing commit
Tags
09-modules
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/board.rs
+90
-0
90 additions, 0 deletions
src/board.rs
src/main.rs
+3
-85
3 additions, 85 deletions
src/main.rs
with
93 additions
and
85 deletions
src/board.rs
0 → 100644
+
90
−
0
View file @
c678d964
#[derive(Clone,
Copy,
Debug,
PartialEq)]
pub
enum
Square
{
Empty
,
Cross
,
Circle
,
}
impl
std
::
fmt
::
Display
for
Square
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
match
self
{
Square
::
Empty
=>
write!
(
f
,
"."
),
Square
::
Cross
=>
write!
(
f
,
"X"
),
Square
::
Circle
=>
write!
(
f
,
"O"
),
}
}
}
pub
struct
Board
{
board
:
[
Square
;
9
],
}
impl
Board
{
/// Creates a new empty board
pub
fn
new
()
->
Self
{
Self
{
board
:
[
Square
::
Empty
;
9
],
}
}
/// Display the board to standard output
pub
fn
display
(
&
self
)
{
println!
(
"{}"
,
self
);
}
}
impl
std
::
fmt
::
Display
for
Board
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
for
row
in
0
..
3
{
writeln!
(
f
,
" {} | {} | {}"
,
self
.board
[
row
*
3
],
self
.board
[
row
*
3
+
1
],
self
.board
[
row
*
3
+
2
],
)
?
;
if
row
==
0
||
row
==
1
{
writeln!
(
f
,
"---+---+---"
)
?
;
}
}
Ok
(())
}
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
new_board_squares_are_empty
()
{
let
board
=
Board
::
new
();
for
square
in
board
.board
{
assert_eq!
(
square
,
Square
::
Empty
);
}
}
#[test]
fn
empty_board_to_string
()
{
let
expected
=
" . | . | .
---+---+---
. | . | .
---+---+---
. | . | .
"
;
assert_eq!
(
expected
,
format!
(
"{}"
,
Board
::
new
()));
}
#[test]
fn
empty_square_to_string_dot
()
{
assert_eq!
(
"."
,
format!
(
"{}"
,
Square
::
Empty
));
}
#[test]
fn
cross_square_to_string_x
()
{
assert_eq!
(
"X"
,
format!
(
"{}"
,
Square
::
Cross
));
}
#[test]
fn
circle_square_to_string_big_o
()
{
assert_eq!
(
"O"
,
format!
(
"{}"
,
Square
::
Circle
));
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
3
−
85
View file @
c678d964
#[derive(Clone,
Copy,
Debug,
PartialEq)]
enum
Square
{
Empty
,
Cross
,
Circle
,
}
impl
std
::
fmt
::
Display
for
Square
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
match
self
{
Square
::
Empty
=>
write!
(
f
,
"."
),
Square
::
Cross
=>
write!
(
f
,
"X"
),
Square
::
Circle
=>
write!
(
f
,
"O"
),
}
}
}
mod
board
;
struct
Board
{
board
:
[
Square
;
9
],
}
impl
Board
{
/// Creates a new empty board
fn
new
()
->
Self
{
Self
{
board
:
[
Square
::
Empty
;
9
],
}
}
/// Display the board to standard output
fn
display
(
&
self
)
{
println!
(
"{}"
,
self
);
}
}
impl
std
::
fmt
::
Display
for
Board
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
for
row
in
0
..
3
{
writeln!
(
f
,
" {} | {} | {}"
,
self
.board
[
row
*
3
],
self
.board
[
row
*
3
+
1
],
self
.board
[
row
*
3
+
2
],
)
?
;
if
row
==
0
||
row
==
1
{
writeln!
(
f
,
"---+---+---"
)
?
;
}
}
Ok
(())
}
}
use
board
::
Board
;
fn
main
()
{
let
board
=
Board
::
new
();
...
...
@@ -59,8 +12,6 @@ fn main() {
#[cfg(test)]
// A module where the tests are located, more on modules later
mod
tests
{
use
super
::
*
;
#[test]
fn
should_pass
()
{
assert!
(
true
);
...
...
@@ -70,37 +21,4 @@ mod tests {
fn
should_panic
()
{
panic!
(
"Doing wrong things on purpose here!"
);
}
#[test]
fn
new_board_squares_are_empty
()
{
let
board
=
Board
::
new
();
for
square
in
board
.board
{
assert_eq!
(
square
,
Square
::
Empty
);
}
}
#[test]
fn
empty_board_to_string
()
{
let
expected
=
" . | . | .
---+---+---
. | . | .
---+---+---
. | . | .
"
;
assert_eq!
(
expected
,
format!
(
"{}"
,
Board
::
new
()));
}
#[test]
fn
empty_square_to_string_dot
()
{
assert_eq!
(
"."
,
format!
(
"{}"
,
Square
::
Empty
));
}
#[test]
fn
cross_square_to_string_x
()
{
assert_eq!
(
"X"
,
format!
(
"{}"
,
Square
::
Cross
));
}
#[test]
fn
circle_square_to_string_big_o
()
{
assert_eq!
(
"O"
,
format!
(
"{}"
,
Square
::
Circle
));
}
}
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