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
025d6e85
Commit
025d6e85
authored
2 years ago
by
Michael Hauspie
Browse files
Options
Downloads
Patches
Plain Diff
Add a move type and refactor a bit
parent
4cb8ec44
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/board/mod.rs
+12
-0
12 additions, 0 deletions
src/board/mod.rs
src/board/tictactoe.rs
+47
-15
47 additions, 15 deletions
src/board/tictactoe.rs
src/main.rs
+1
-1
1 addition, 1 deletion
src/main.rs
with
60 additions
and
16 deletions
src/board/mod.rs
+
12
−
0
View file @
025d6e85
//! A module that implements several board games
pub
mod
tictactoe
;
/// A trait that defines a Board for a game where we can make a player
/// play a move and check if a player has won
pub
trait
Board
<
Move
,
Player
>
{
/// Apply a move to the board
fn
do_move
(
&
mut
self
,
turn
:
Player
,
m
:
&
Move
);
/// Returns if a given player has won the game considering this
/// board
fn
has_won
(
&
self
,
player
:
Player
)
->
bool
;
}
This diff is collapsed.
Click to expand it.
src/board/tictactoe.rs
+
47
−
15
View file @
025d6e85
#[derive(Clone,
Copy,
Debug,
PartialEq)]
/// A Tictactoe square
pub
enum
Square
{
Empty
,
pub
enum
Player
{
Cross
,
Circle
,
}
impl
std
::
fmt
::
Display
for
Player
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
match
self
{
Player
::
Cross
=>
write!
(
f
,
"X"
),
Player
::
Circle
=>
write!
(
f
,
"O"
),
}
}
}
/// A Tictactoe square is either empty (None) or Player
#[derive(Clone,
Copy,
Debug,
PartialEq)]
enum
Square
{
Empty
,
Full
(
Player
),
}
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"
),
Square
::
Full
(
p
)
=>
write!
(
f
,
"{}"
,
p
),
}
}
}
/// A TicTacToe board
pub
struct
TicTacToe
{
pub
struct
Board
{
board
:
[
Square
;
9
],
}
impl
TicTacToe
{
/// A TicTacToe Move
pub
enum
Move
{
A1
=
0
,
B1
=
1
,
C1
=
2
,
A2
=
3
,
B2
=
4
,
C2
=
5
,
A3
=
6
,
B3
=
7
,
C3
=
8
,
}
impl
Board
{
/// Creates a new empty board
pub
fn
new
()
->
Self
{
Self
{
board
:
[
Square
::
Empty
;
9
],
}
Self
{
board
:
[
Square
::
Empty
;
9
]
}
}
/// Display the board to standard output
...
...
@@ -35,7 +59,7 @@ impl TicTacToe {
}
}
impl
std
::
fmt
::
Display
for
TicTacToe
{
impl
std
::
fmt
::
Display
for
Board
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
writeln!
(
f
,
" A B C"
)
?
;
for
row
in
0
..
3
{
...
...
@@ -61,7 +85,7 @@ mod tests {
#[test]
fn
new_board_squares_are_empty
()
{
let
board
=
TicTacToe
::
new
();
let
board
=
Board
::
new
();
for
square
in
board
.board
{
assert_eq!
(
square
,
Square
::
Empty
);
...
...
@@ -77,7 +101,7 @@ mod tests {
---+---+---
. | . | . 3
"
;
assert_eq!
(
expected
,
format!
(
"{}"
,
TicTacToe
::
new
()));
assert_eq!
(
expected
,
format!
(
"{}"
,
Board
::
new
()));
}
#[test]
...
...
@@ -85,11 +109,19 @@ mod tests {
assert_eq!
(
"."
,
format!
(
"{}"
,
Square
::
Empty
));
}
#[test]
fn
cross_player_to_string_x
()
{
assert_eq!
(
"X"
,
format!
(
"{}"
,
Player
::
Cross
));
}
#[test]
fn
circle_player_to_string_big_o
()
{
assert_eq!
(
"O"
,
format!
(
"{}"
,
Player
::
Circle
));
}
#[test]
fn
cross_square_to_string_x
()
{
assert_eq!
(
"X"
,
format!
(
"{}"
,
Square
::
Cross
));
assert_eq!
(
"X"
,
format!
(
"{}"
,
Square
::
Full
(
Player
::
Cross
))
)
;
}
#[test]
fn
circle_square_to_string_big_o
()
{
assert_eq!
(
"O"
,
format!
(
"{}"
,
Square
::
Circle
));
assert_eq!
(
"O"
,
format!
(
"{}"
,
Square
::
Full
(
Player
::
Circle
))
)
;
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
1
−
1
View file @
025d6e85
mod
board
;
use
board
::
tictactoe
::
TicTacToe
;
use
board
::
tictactoe
::
Board
as
TicTacToe
;
fn
main
()
{
let
board
=
TicTacToe
::
new
();
...
...
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