Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GroupProject
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Joe Stephenson
GroupProject
Commits
9a62a3cb
Commit
9a62a3cb
authored
Oct 20, 2024
by
Joe Stephenson
Browse files
Options
Downloads
Plain Diff
pull
parents
35b9ba6d
fc12fbc9
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
slidingpuzzle.py
+74
-0
74 additions, 0 deletions
slidingpuzzle.py
with
74 additions
and
0 deletions
slidingpuzzle.py
0 → 100644
+
74
−
0
View file @
9a62a3cb
#sliding puzzle
import
random
class
SlidePuzzle
:
solved_board
=
[[
1
,
2
,
3
],
[
4
,
5
,
6
],
[
7
,
8
,
"
"
]]
# Final solved board
def
__init__
(
self
):
"""
Create a new shuffled board when the object is created
"""
self
.
board
=
self
.
shuffled_board
()
def
print_board
(
self
):
"""
Prints the current state of the board
"""
for
row
in
self
.
board
:
print
(
'
_|_
'
.
join
(
str
(
cell
)
for
cell
in
row
))
def
find_empty_cell
(
self
):
"""
Finds the position of the empty cell in the board
"""
for
i
,
row
in
enumerate
(
self
.
board
):
for
j
,
cell
in
enumerate
(
row
):
if
cell
==
"
"
:
return
i
,
j
def
shuffled_board
(
self
):
"""
Creates a new shuffled board by randomly shuffling numbers
"""
board
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
"
"
]
random
.
shuffle
(
board
)
return
[
board
[
i
:
i
+
3
]
for
i
in
range
(
0
,
len
(
board
),
3
)]
def
is_solved
(
self
):
"""
Checks if the current board is the solved board or not
"""
return
self
.
board
==
SlidePuzzle
.
solved_board
def
move
(
self
,
direction
):
"""
Moves the empty cell in the given direction if it is valid. Otherwise, it prints an error message.
"""
empty_cell
=
self
.
find_empty_cell
()
i
,
j
=
empty_cell
if
direction
==
"
up
"
and
i
>
0
:
self
.
board
[
i
][
j
],
self
.
board
[
i
-
1
][
j
]
=
self
.
board
[
i
-
1
][
j
],
self
.
board
[
i
][
j
]
elif
direction
==
"
down
"
and
i
<
2
:
self
.
board
[
i
][
j
],
self
.
board
[
i
+
1
][
j
]
=
self
.
board
[
i
+
1
][
j
],
self
.
board
[
i
][
j
]
elif
direction
==
"
left
"
and
j
>
0
:
self
.
board
[
i
][
j
],
self
.
board
[
i
][
j
-
1
]
=
self
.
board
[
i
][
j
-
1
],
self
.
board
[
i
][
j
]
elif
direction
==
"
right
"
and
j
<
2
:
self
.
board
[
i
][
j
],
self
.
board
[
i
][
j
+
1
]
=
self
.
board
[
i
][
j
+
1
],
self
.
board
[
i
][
j
]
else
:
print
(
"
Invalid direction! Try again.
"
)
def
play_game
():
"""
The main game loop, asks the player for a move until the puzzle is solved
"""
puzzle
=
SlidePuzzle
()
while
not
puzzle
.
is_solved
():
puzzle
.
print_board
()
move
=
input
(
"
Enter the direction (up, down, left, right):
"
).
lower
()
puzzle
.
move
(
move
)
if
puzzle
.
is_solved
():
print
(
"
Congratulations! You solved the puzzle!
"
)
puzzle
.
print_board
()
play_game
()
\ No newline at end of file
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