Skip to content

Instantly share code, notes, and snippets.

@pranithan-kang
Last active January 4, 2024 08:49
Show Gist options
  • Save pranithan-kang/38da818a411ae25773708db62c4b6f1e to your computer and use it in GitHub Desktop.
Save pranithan-kang/38da818a411ae25773708db62c4b6f1e to your computer and use it in GitHub Desktop.
number-block generate game
"""
this program generates a random number block and prints it
"""
import random
class NumberBlock:
# 1 - 2 - 3
# | | |
# 4 - 5 - 6
# | | |
# 7 - 8 - 0
links = (
(1, 3),
(0, 2, 4),
(1, 5),
(0, 4, 6),
(1, 3, 5, 7),
(2, 4, 8),
(3, 7),
(4, 6, 8),
(5, 7)
)
def print_block(self):
pos = self.num_pos
print(f"{pos[0]} - {pos[1]} - {pos[2]}")
print(f"| | |")
print(f"{pos[3]} - {pos[4]} - {pos[5]}")
print(f"| | |")
print(f"{pos[6]} - {pos[7]} - {pos[8]}")
print(f"End=>Start: {[ n + 1 for n, d in self.moves]}")
print(f"Start=>End: {[ d + 1 for n, d in self.moves]}")
print(f"Current nul: {self.current_nul + 1}")
def __init__(self):
self.num_pos = [1, 2, 3, 4, 5, 6, 7, 8, 0]
self.current_nul = 8
self.moves = []
def move(self, nul_pos: int, dest_pos: int):
pos = self.num_pos
pos[nul_pos], pos[dest_pos] = pos[dest_pos], pos[nul_pos]
self.current_nul = dest_pos
self.moves.insert(0, (nul_pos, dest_pos))
def random_block(self, move_cnt: int):
for _ in range(move_cnt):
move_choice = self.links[self.current_nul]
if len(self.moves) > 0:
move_choice = [m for m in move_choice if m != self.moves[0][0]]
self.move(self.current_nul, random.choice(move_choice))
nb = NumberBlock()
nb.random_block(299)
nb.print_block()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment