added sudoku logic, trying to get board to print

This commit is contained in:
kated
2026-06-02 10:25:31 -04:00
parent a9bedb4cde
commit 8b907c3571
11 changed files with 279 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
source .venv/bin/activate

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

89
board.py Normal file
View File

@@ -0,0 +1,89 @@
import random
from cell import Cell
class Board:
def __init__(self):
self.grid = [[0 for _ in range(9)] for _ in range(9)]
self.cells = []
def generate(self):
self._fill_board()
self.create_cells()
def create_cells(self):
self.cells = []
for row in range(9):
for col in range(9):
self.cells.append(
Cell(
row=row,
col=col,
value=self.grid[row][col]
)
)
def _find_empty(self):
for row in range(9):
for col in range(9):
if self.grid[row][col] == 0:
return row, col
return None
def _valid(self, row, col, num):
if num in self.grid[row]:
return False
for r in range(9):
if self.grid[r][col] == num:
return False
box_row = (row // 3) * 3
box_col = (col // 3) * 3
for r in range(box_row, box_row + 3):
for c in range(box_col, box_col + 3):
if self.grid[r][c] == num:
return False
return True
def _fill_board(self):
empty = self._find_empty()
if empty is None:
return True
row, col = empty
nums = list(range(1, 10))
random.shuffle(nums)
for num in nums:
if self._valid(row, col, num):
self.grid[row][col] = num
if self._fill_board():
return True
self.grid[row][col] = 0
return False

55
border.py Normal file
View File

@@ -0,0 +1,55 @@
class Border:
color = "cyan"
def __init__(self, x, y, char):
self.position = (x, y)
self.character = char
def play_turn(self, game):
pass
def create_borders():
agents = []
left = 1
right = 21
top = 1
bottom = 19
width = 19
height = 10
# outer border
for x in range(left, left + width):
agents.append(Border(x, top, ""))
agents.append(Border(x, top + height, ""))
for y in range(top, top + height + 1):
agents.append(Border(left, y, ""))
agents.append(Border(left + width, y, ""))
# vertical box separators
for y in range(top + 1, top + height):
agents.append(Border(left + 6, y, ""))
agents.append(Border(left + 12, y, ""))
# horizontal box separators
for x in range(left + 1, left + width):
agents.append(Border(x, top + 3, ""))
agents.append(Border(x, top + 6, ""))
return agents

25
cell.py Normal file
View File

@@ -0,0 +1,25 @@
class Cell:
color = "white"
def __init__(self, row, col, value):
self.row = row
self.col = col
self.value = value
board_x = 4
board_y = 2
board_x = 3
board_y = 2
x = board_x + (col * 2)
y = board_y + (row * 2)
self.position = (x, y)
self.character = str(value)
def play_turn(self, game):
pass

81
poetry.lock generated Normal file
View File

@@ -0,0 +1,81 @@
# This file is automatically @generated by Poetry 2.3.1 and should not be changed by hand.
[[package]]
name = "ansicon"
version = "1.89.0"
description = "Python wrapper for loading Jason Hood's ANSICON"
optional = false
python-versions = "*"
groups = ["main"]
markers = "platform_system == \"Windows\""
files = [
{file = "ansicon-1.89.0-py2.py3-none-any.whl", hash = "sha256:f1def52d17f65c2c9682cf8370c03f541f410c1752d6a14029f97318e4b9dfec"},
{file = "ansicon-1.89.0.tar.gz", hash = "sha256:e4d039def5768a47e4afec8e89e83ec3ae5a26bf00ad851f914d1240b444d2b1"},
]
[[package]]
name = "blessed"
version = "1.27.0"
description = "Easy, practical library for making terminal apps, by providing an elegant, well-documented interface to Colors, Keyboard input, and screen Positioning capabilities."
optional = false
python-versions = ">=3.7"
groups = ["main"]
files = [
{file = "blessed-1.27.0-py3-none-any.whl", hash = "sha256:1c599969acc993bb5842bf3f638b0691e335277a9d9058cd079463a346988714"},
{file = "blessed-1.27.0.tar.gz", hash = "sha256:e3064559388bd532ab6460d9b6c7d6dd699c4e0cf54d28ed6e2cab12feda13bb"},
]
[package.dependencies]
jinxed = {version = ">=1.1.0", markers = "platform_system == \"Windows\""}
wcwidth = ">=0.2.14"
[package.extras]
docs = ["Pillow", "Sphinx (>3)", "sphinx-paramlinks", "sphinx_rtd_theme", "sphinxcontrib-manpage"]
[[package]]
name = "jinxed"
version = "1.3.0"
description = "Jinxed Terminal Library"
optional = false
python-versions = "*"
groups = ["main"]
markers = "platform_system == \"Windows\""
files = [
{file = "jinxed-1.3.0-py2.py3-none-any.whl", hash = "sha256:b993189f39dc2d7504d802152671535b06d380b26d78070559551cbf92df4fc5"},
{file = "jinxed-1.3.0.tar.gz", hash = "sha256:1593124b18a41b7a3da3b078471442e51dbad3d77b4d4f2b0c26ab6f7d660dbf"},
]
[package.dependencies]
ansicon = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "retro-games"
version = "1.1.3"
description = "A simple framework for Terminal-based games"
optional = false
python-versions = "<4.0,>=3.10"
groups = ["main"]
files = [
{file = "retro_games-1.1.3-py3-none-any.whl", hash = "sha256:4bdd27241b5cb3ee72e69a042d301ff58df2a2ade7e3c29400a538fa54e30148"},
{file = "retro_games-1.1.3.tar.gz", hash = "sha256:4f91ff725e551820aa4e30c12c0264e2da41967ed34252122b7136bc2a8ed311"},
]
[package.dependencies]
blessed = ">=1.20.0,<2.0.0"
[[package]]
name = "wcwidth"
version = "0.3.5"
description = "Measures the displayed width of unicode strings in a terminal"
optional = false
python-versions = ">=3.8"
groups = ["main"]
files = [
{file = "wcwidth-0.3.5-py3-none-any.whl", hash = "sha256:b0a0245130566939a24ab8432e625b38272fbc62ecbe5aecbdcb50b8f02ce993"},
{file = "wcwidth-0.3.5.tar.gz", hash = "sha256:7c3463f312540cf21ddd527ea34f3ae95c057fa191aa7a9e043898d20d636e59"},
]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<4.0"
content-hash = "03cc38c17964eb2c920ecf014cbfcf966c0c719418a127947b33382f086a0a6e"

28
sudoku.py Normal file
View File

@@ -0,0 +1,28 @@
from retro.game import Game
from board import Board
from border import Border
def main():
board = Board()
board.generate()
agents = []
agents.extend(Border.create_borders())
agents.extend(board.cells)
game = Game(
agents,
{"score":0},
board_size=(15, 25)
)
game.play()
if __name__ == "__main__":
main()