Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
TicTacToe-Robot
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
Container 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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lukas Hoffleit
TicTacToe-Robot
Commits
cb8e7cc9
Commit
cb8e7cc9
authored
2 months ago
by
Lukas Hoffleit
Browse files
Options
Downloads
Patches
Plain Diff
Error-Handling und Player-Conversion
parent
06dd6b28
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tictactoe.py
+21
-8
21 additions, 8 deletions
tictactoe.py
with
21 additions
and
8 deletions
tictactoe.py
+
21
−
8
View file @
cb8e7cc9
from
enum
import
Enum
class
Player
(
Enum
):
undefined
=
0
one
=
1
two
=
2
undefined
=
3
class
TicTacToeError
(
Exception
):
"""
TicTacToe Error
"""
class
TicTacToe
():
def
__init__
(
self
)
->
None
:
...
...
@@ -11,6 +14,14 @@ class TicTacToe():
for
i
in
range
(
3
):
self
.
grid
.
append
([
Player
.
undefined
,
Player
.
undefined
,
Player
.
undefined
])
def
_get_player_from_int
(
self
,
player
:
Player
|
int
)
->
Player
:
if
isinstance
(
player
,
int
):
try
:
return
Player
(
player
)
except
ValueError
:
raise
TicTacToeError
(
"
The player number is invalid. Only
'
1
'
and
'
2
'
are accepted
"
)
return
player
def
add
(
self
,
row
:
int
,
col
:
int
,
player
:
Player
|
int
)
->
bool
:
"""
Change the value of a undefined field.
The player can be passed as Player Object or plain Int.
...
...
@@ -18,10 +29,7 @@ class TicTacToe():
Returns False, if the field is already occupied
or the player number is invalid.
"""
if
isinstance
(
player
,
int
):
try
:
player
=
Player
(
player
)
except
ValueError
:
return
False
# invalid player number
player
=
self
.
_get_player_from_int
(
player
)
if
self
.
check_field
(
row
,
col
):
self
.
grid
[
row
][
col
]
=
player
return
True
...
...
@@ -49,6 +57,11 @@ class TicTacToe():
print
(
"
\n
├───┼───┼───┤
"
)
print
(
"
\n
└───┴───┴───┘
\n
"
)
def
get_best_move
(
self
,
player
:
Player
|
int
)
->
tuple
[
int
,
int
]:
return
(
0
,
0
)
if
__name__
==
"
__main__
"
:
ttt
=
TicTacToe
()
...
...
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