78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
from django.db import models
|
|
from .utils import split_word
|
|
|
|
|
|
class Level(models.Model):
|
|
level_number = models.IntegerField()
|
|
level_string = models.CharField(max_length=50)
|
|
|
|
|
|
class Word(models.Model):
|
|
word = models.CharField(max_length=50)
|
|
definition = models.CharField(max_length=255)
|
|
level = models.ForeignKey(Level, on_delete=models.CASCADE, related_name="words")
|
|
|
|
|
|
def place_word(width, height, placements, words):
|
|
# Base case
|
|
if placements == []:
|
|
return
|
|
|
|
# Other cases
|
|
else:
|
|
return
|
|
|
|
|
|
# Helper functions
|
|
def get_case(width, height, placements, x, y):
|
|
# todo
|
|
return None
|
|
|
|
|
|
def print_grid(width, height, placements):
|
|
for i in range(0, height):
|
|
for j in range(0, width):
|
|
print(i, j)
|
|
print
|
|
|
|
|
|
class Grid(models.Model):
|
|
level = models.ForeignKey(Level, on_delete=models.CASCADE, related_name="grids")
|
|
words = models.ManyToManyField(Word, through="Placement")
|
|
width = models.IntegerField(default=1)
|
|
height = models.IntegerField(default=1)
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not hasattr(self, "level") or self.level is None:
|
|
self.level = Level.objects.first()
|
|
super(Grid, self).save(*args, **kwargs)
|
|
|
|
def populate(self):
|
|
print(self.width)
|
|
print(self.height)
|
|
|
|
tmp_placements = []
|
|
|
|
place_word(self.width, self.height, tmp_placements, Word.objects.all())
|
|
|
|
|
|
class Direction:
|
|
RIGHT = 1
|
|
BOTTOM = 2
|
|
RIGHT_BOTTOM = 3
|
|
BOTTOM_RIGHT = 4
|
|
|
|
|
|
class Placement(models.Model):
|
|
word = models.ForeignKey(Word, on_delete=models.CASCADE)
|
|
grid = models.ForeignKey(Grid, on_delete=models.CASCADE)
|
|
coordinates_first_letter = models.CharField(max_length=10)
|
|
direction = models.SmallIntegerField(
|
|
default=Direction.RIGHT,
|
|
choices=[(Direction.RIGHT, "Right"), (Direction.BOTTOM, "Bottom")],
|
|
)
|
|
|
|
@property
|
|
def length(self):
|
|
return len(split_word(self))
|