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
b556719b
Commit
b556719b
authored
2 years ago
by
Michael Hauspie
Browse files
Options
Downloads
Patches
Plain Diff
Implement all needed trait and generic play
parent
9e981954
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/board/tictactoe.rs
+35
-3
35 additions, 3 deletions
src/board/tictactoe.rs
src/main.rs
+43
-3
43 additions, 3 deletions
src/main.rs
with
78 additions
and
6 deletions
src/board/tictactoe.rs
+
35
−
3
View file @
b556719b
...
...
@@ -6,6 +6,23 @@ pub enum Player {
Circle
,
}
impl
Default
for
Player
{
fn
default
()
->
Self
{
Player
::
Cross
}
}
impl
std
::
ops
::
Not
for
Player
{
type
Output
=
Player
;
fn
not
(
self
)
->
Self
::
Output
{
match
self
{
Player
::
Cross
=>
Player
::
Circle
,
Player
::
Circle
=>
Player
::
Cross
,
}
}
}
impl
std
::
fmt
::
Display
for
Player
{
fn
fmt
(
&
self
,
f
:
&
mut
std
::
fmt
::
Formatter
<
'_
>
)
->
std
::
fmt
::
Result
{
match
self
{
...
...
@@ -50,9 +67,24 @@ pub enum Move {
C3
=
8
,
}
// impl std::fmt::FromStr for Move {
// }
impl
FromStr
for
Move
{
type
Err
=
String
;
fn
from_str
(
s
:
&
str
)
->
Result
<
Self
,
Self
::
Err
>
{
match
s
{
"A1"
=>
Ok
(
Move
::
A1
),
"B1"
=>
Ok
(
Move
::
B1
),
"C1"
=>
Ok
(
Move
::
C1
),
"A2"
=>
Ok
(
Move
::
A2
),
"B2"
=>
Ok
(
Move
::
B2
),
"C2"
=>
Ok
(
Move
::
C2
),
"A3"
=>
Ok
(
Move
::
A3
),
"B3"
=>
Ok
(
Move
::
B3
),
"C3"
=>
Ok
(
Move
::
C3
),
_
=>
Err
(
format!
(
"Illegal move {}"
,
s
))
}
}
}
impl
Board
{
/// Creates a new empty board
...
...
This diff is collapsed.
Click to expand it.
src/main.rs
+
43
−
3
View file @
b556719b
mod
board
;
use
board
::
tictactoe
::
Board
as
TicTacToe
;
use
board
::
Board
;
use
std
::
io
;
use
std
::
io
::
prelude
::
*
;
fn
prompt
<
Player
>
(
turn
:
Player
)
where
Player
:
std
::
fmt
::
Display
,
{
print!
(
"{}>"
,
turn
);
io
::
stdout
()
.flush
()
.unwrap
();
}
fn
play
<
Game
,
Move
,
Player
>
(
mut
board
:
Game
)
where
Game
:
Board
<
Move
,
Player
>
+
std
::
fmt
::
Display
,
Player
:
Copy
+
std
::
fmt
::
Display
+
Default
+
std
::
ops
::
Not
<
Output
=
Player
>
,
Move
:
std
::
str
::
FromStr
,
{
let
mut
stdin
=
std
::
io
::
stdin
()
.lock
();
let
mut
player
=
Player
::
default
();
println!
(
"Let start"
);
println!
(
"{board}"
);
prompt
(
player
);
loop
{
let
mut
input
=
String
::
default
();
stdin
.read_line
(
&
mut
input
)
.unwrap
();
let
str_move
=
input
.trim_end
();
if
let
Ok
(
m
)
=
str_move
.parse
()
{
board
.do_move
(
player
,
&
m
);
println!
(
"{board}"
);
if
board
.has_won
(
player
)
{
println!
(
"Victory!"
);
return
;
}
player
=
!
player
;
}
prompt
(
player
);
}
}
fn
main
()
{
let
board
=
TicTacToe
::
new
();
board
.display
();
play
(
board
);
}
// Will only be build in 'test' configuration
...
...
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