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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael Hauspie
intro-rust-game
Compare revisions
8d759cce4662b692edda9ac66f46146a80d01762 to 0c1b7cecba084397f937a2a7b213198040bc48cf
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
michael.hauspie/intro-rust-game
Select target project
No results found
0c1b7cecba084397f937a2a7b213198040bc48cf
Select Git revision
Branches
main
Tags
01-simple-board
02-typed-squares
03-board-struct
04-impl-for-board
05-unit-tests
06-trait
07-trait-derive
08-Display
09-modules
09.5-before-main-generic
10-generics
11-flip-coin
13 results
Swap
Target
michael.hauspie/intro-rust-game
Select target project
michael.hauspie/intro-rust-game
1 result
8d759cce4662b692edda9ac66f46146a80d01762
Select Git revision
Branches
main
Tags
01-simple-board
02-typed-squares
03-board-struct
04-impl-for-board
05-unit-tests
06-trait
07-trait-derive
08-Display
09-modules
09.5-before-main-generic
10-generics
11-flip-coin
13 results
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Add ConvertToString trait
· 59c16f17
Michael Hauspie
authored
2 years ago
59c16f17
Implement Clone, Copy, Debug, PartialEq for square
· 0c1b7cec
Michael Hauspie
authored
2 years ago
0c1b7cec
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main.rs
+59
-34
59 additions, 34 deletions
src/main.rs
with
59 additions
and
34 deletions
src/main.rs
View file @
0c1b7cec
trait
ConvertToString
{
/// Returns a string representation of self
fn
to_string
(
&
self
)
->
String
;
}
#[derive(Clone,
Copy,
Debug,
PartialEq)]
enum
Square
{
enum
Square
{
Empty
,
Empty
,
Cross
,
Cross
,
Circle
,
Circle
,
}
}
impl
ConvertToString
for
Square
{
fn
to_string
(
&
self
)
->
String
{
match
self
{
Square
::
Empty
=>
String
::
from
(
"."
),
Square
::
Cross
=>
String
::
from
(
"X"
),
Square
::
Circle
=>
String
::
from
(
"O"
),
}
}
}
struct
Board
{
struct
Board
{
board
:
[
Square
;
9
],
board
:
[
Square
;
9
],
}
}
...
@@ -12,41 +28,34 @@ impl Board {
...
@@ -12,41 +28,34 @@ impl Board {
/// Creates a new empty board
/// Creates a new empty board
fn
new
()
->
Self
{
fn
new
()
->
Self
{
Self
{
Self
{
board
:
[
board
:
[
Square
::
Empty
;
9
],
Square
::
Empty
,
Square
::
Cross
,
Square
::
Circle
,
Square
::
Empty
,
Square
::
Empty
,
Square
::
Empty
,
Square
::
Empty
,
Square
::
Empty
,
Square
::
Empty
,
],
}
}
}
}
/// Display the board to standard output
/// Display the board to standard output
fn
display
(
&
self
)
{
fn
display
(
&
self
)
{
println!
(
"{}"
,
self
.to_string
());
}
}
impl
ConvertToString
for
Board
{
fn
to_string
(
&
self
)
->
String
{
let
mut
out
=
String
::
new
();
for
row
in
0
..
3
{
for
row
in
0
..
3
{
println!
(
out
.push_str
(
" {} | {} | {}"
,
format!
(
square_to_char
(
&
self
.board
[
row
*
3
]),
" {} | {} | {}"
,
square_to_char
(
&
self
.board
[
row
*
3
+
1
]),
self
.board
[
row
*
3
]
.to_string
(),
square_to_char
(
&
self
.board
[
row
*
3
+
2
])
self
.board
[
row
*
3
+
1
]
.to_string
(),
self
.board
[
row
*
3
+
2
]
.to_string
()
)
.as_str
(),
);
);
if
row
==
0
||
row
==
1
{
if
row
==
0
||
row
==
1
{
println!
(
"
---+---+---"
);
out
.push_str
(
"
\n
---+---+---
\n
"
);
}
}
}
}
}
out
}
fn
square_to_char
(
s
:
&
Square
)
->
char
{
match
s
{
Square
::
Empty
=>
'.'
,
Square
::
Cross
=>
'X'
,
Square
::
Circle
=>
'O'
,
}
}
}
}
...
@@ -61,7 +70,7 @@ fn main() {
...
@@ -61,7 +70,7 @@ fn main() {
// A module where the tests are located, more on modules later
// A module where the tests are located, more on modules later
mod
tests
{
mod
tests
{
use
super
::
*
;
use
super
::
*
;
#[test]
#[test]
fn
should_pass
()
{
fn
should_pass
()
{
assert!
(
true
);
assert!
(
true
);
...
@@ -77,14 +86,30 @@ mod tests {
...
@@ -77,14 +86,30 @@ mod tests {
let
board
=
Board
::
new
();
let
board
=
Board
::
new
();
for
square
in
board
.board
{
for
square
in
board
.board
{
assert_eq!
(
square
,
Square
::
Empty
);
// Cannot do that due to 'trait' issues, more on that
// later
// assert_eq!(square, Square::Empty);
match
square
{
Square
::
Empty
=>
(),
Square
::
Cross
|
Square
::
Circle
=>
panic!
(
"Square is not empty"
),
}
}
}
}
}
#[test]
fn
empty_board_to_string
()
{
let
expected
=
" . | . | .
---+---+---
. | . | .
---+---+---
. | . | ."
;
assert_eq!
(
expected
,
Board
::
new
()
.to_string
());
}
#[test]
fn
empty_square_to_string_dot
()
{
assert_eq!
(
"."
,
Square
::
Empty
.to_string
());
}
#[test]
fn
cross_square_to_string_x
()
{
assert_eq!
(
"X"
,
Square
::
Cross
.to_string
());
}
#[test]
fn
circle_square_to_string_big_o
()
{
assert_eq!
(
"O"
,
Square
::
Circle
.to_string
());
}
}
}
This diff is collapsed.
Click to expand it.