-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
executable file
·64 lines (52 loc) · 2.02 KB
/
board.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from enum import Enum
import random
from dijkstra import board_to_tuple
class Direction(Enum):
UP = (-1, 0) # 'up' (-1, 0)
DOWN = (1, 0) # 'down' (1, 0)
LEFT = (0, -1) # 'left' (0, -1)
RIGHT = (0, 1) # 'right' (0, 1)
class Board:
def __init__(self):
self.board = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]
self.empty = [2, 2]
self.shuffle(100)
def flatten(self):
return tuple([*self.board[0], *self.board[1], *self.board[2]])
def get_number(self, row, col):
return self.board[row][col]
def move(self, direction: Direction) -> bool:
x, y = self.empty
if direction == Direction.DOWN and x > 0:
self.board[x][y], self.board[x-1][y] = self.board[x-1][y], self.board[x][y]
self.empty = [x-1, y]
return True
elif direction == Direction.UP and x < 2:
self.board[x][y], self.board[x+1][y] = self.board[x+1][y], self.board[x][y]
self.empty = [x+1, y]
return True
elif direction == Direction.RIGHT and y > 0:
self.board[x][y], self.board[x][y-1] = self.board[x][y-1], self.board[x][y]
self.empty = [x, y-1]
return True
elif direction == Direction.LEFT and y < 2:
self.board[x][y], self.board[x][y+1] = self.board[x][y+1], self.board[x][y]
self.empty = [x, y+1]
return True
return False
def is_solved(self) -> bool:
correct_board = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]
return self.board == correct_board
def tuple(self):
return board_to_tuple(self.board)
def shuffle(self, i=100):
directions = [Direction.UP, Direction.DOWN, Direction.LEFT, Direction.RIGHT]
n = 0
while n < i:
direction = random.choice(directions)
ret = self.move(direction)
n += int(ret)