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
Commits
4cb8ec44
Commit
4cb8ec44
authored
2 years ago
by
Michael Hauspie
Browse files
Options
Downloads
Patches
Plain Diff
Refactor to make a board module with a tictactoe submodule
parent
c678d964
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/board/mod.rs
+3
-0
3 additions, 0 deletions
src/board/mod.rs
src/board/tictactoe.rs
+21
-16
21 additions, 16 deletions
src/board/tictactoe.rs
src/main.rs
+2
-2
2 additions, 2 deletions
src/main.rs
with
26 additions
and
18 deletions
src/board/mod.rs
0 → 100644
+
3
−
0
View file @
4cb8ec44
//! A module that implements several board games
pub
mod
tictactoe
;
This diff is collapsed.
Click to expand it.
src/board.rs
→
src/board
/tictactoe
.rs
+
21
−
16
View file @
4cb8ec44
#[derive(Clone,
Copy,
Debug,
PartialEq)]
/// A Tictactoe square
pub
enum
Square
{
Empty
,
Cross
,
...
...
@@ -16,11 +16,12 @@ impl std::fmt::Display for Square {
}
}
pub
struct
Board
{
/// A TicTacToe board
pub
struct
TicTacToe
{
board
:
[
Square
;
9
],
}
impl
Board
{
impl
TicTacToe
{
/// Creates a new empty board
pub
fn
new
()
->
Self
{
Self
{
...
...
@@ -34,14 +35,18 @@ impl Board {
}
}
impl
std
::
fmt
::
Display
for
Board
{
impl
std
::
fmt
::
Display
for
TicTacToe
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
writeln!
(
f
,
" A B C"
)
?
;
for
row
in
0
..
3
{
writeln!
(
f
,
" {} | {} | {}"
,
self
.board
[
row
*
3
],
self
.board
[
row
*
3
+
1
],
self
.board
[
row
*
3
+
2
],
)
?
;
writeln!
(
f
,
" {} | {} | {} {}"
,
self
.board
[
row
*
3
],
self
.board
[
row
*
3
+
1
],
self
.board
[
row
*
3
+
2
],
row
+
1
,
)
?
;
if
row
==
0
||
row
==
1
{
writeln!
(
f
,
"---+---+---"
)
?
;
}
...
...
@@ -53,10 +58,10 @@ impl std::fmt::Display for Board {
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
new_board_squares_are_empty
()
{
let
board
=
Board
::
new
();
let
board
=
TicTacToe
::
new
();
for
square
in
board
.board
{
assert_eq!
(
square
,
Square
::
Empty
);
...
...
@@ -65,13 +70,14 @@ mod tests {
#[test]
fn
empty_board_to_string
()
{
let
expected
=
" . | . | .
let
expected
=
" A B C
. | . | . 1
---+---+---
. | . | .
. | . | .
2
---+---+---
. | . | .
. | . | .
3
"
;
assert_eq!
(
expected
,
format!
(
"{}"
,
Board
::
new
()));
assert_eq!
(
expected
,
format!
(
"{}"
,
TicTacToe
::
new
()));
}
#[test]
...
...
@@ -86,5 +92,4 @@ mod tests {
fn
circle_square_to_string_big_o
()
{
assert_eq!
(
"O"
,
format!
(
"{}"
,
Square
::
Circle
));
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
2
−
2
View file @
4cb8ec44
mod
board
;
use
board
::
Board
;
use
board
::
tictactoe
::
TicTacToe
;
fn
main
()
{
let
board
=
Board
::
new
();
let
board
=
TicTacToe
::
new
();
board
.display
();
}
...
...
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