Created
March 25, 2012 21:40
-
-
Save edma2/2200030 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import re | |
class Board(object): | |
''' | |
tile | |
[a-z] placed tile | |
"_" normal empty square | |
[2-9] multiplier empty square | |
''' | |
def __init__(self, size=15, moves=[]): | |
self.size = size | |
self.grid = [] | |
for i in range(size): | |
row = [] | |
for y in range(size): | |
row.append('_') | |
self.grid.append(row) | |
def __repr__(self): | |
''' | |
Returns a printable representation of myself | |
''' | |
s = '' | |
labels = '0123456789!@#$%' | |
print '', labels | |
for i, row in enumerate(self.grid): | |
s += labels[i] | |
for square in row: | |
s += square | |
s += '\n' | |
return s[:-1] | |
def columns(self): | |
''' | |
Returns all columns in the board, each column is represented by a list | |
of squares | |
''' | |
for i in range(self.size): | |
column = [] | |
for j in range(self.size): | |
column.append(self.grid[j][i]) | |
yield column | |
def rows(self): | |
''' | |
Returns all rows in the board, each row is represented by a list of | |
squares | |
''' | |
for row in self.grid: | |
yield row | |
def valid(self): | |
''' | |
Returns True if I represent a valid gamestate | |
''' | |
def strings(): | |
for col in self.columns(): | |
yield ''.join(col) | |
for row in self.rows(): | |
yield ''.join(row) | |
for s in strings(): | |
# Words to be checked are at least 2 characters long | |
for word in re.findall(r'[a-z][a-z]+', s): | |
if word not in dict_words: | |
return False | |
return True | |
def with_move(self, x, y, letter): | |
''' | |
Returns a copy of self with move made | |
''' | |
new_board = copy.deepcopy(self) | |
new_board.grid[y][x] = letter | |
return new_board | |
def covered(self, x, y): | |
''' | |
Returns True if square (x, y) is covered by a tile | |
''' | |
return self.grid[y][x] in 'abcdefghijklmnopqrstuvwxyz' | |
def cross_checks(self, row_index): | |
''' | |
Returns cross checks for a row | |
''' | |
result = [] | |
for col_index in range(self.size): | |
letters = [] | |
for letter in 'abcdefghijklmnopqrstuvwxyz': | |
if self.with_move(col_index, row_index, letter).valid(): | |
letters.append(letter) | |
result.append(letters) | |
return result | |
def anchors(self, row_index): | |
''' | |
Returns anchors for a row | |
''' | |
result = [] | |
for col_index in range(self.size): | |
if not self.covered(col_index, row_index): | |
for y, x in [(row_index-1, col_index), | |
(row_index+1, col_index), | |
(row_index, col_index-1), | |
(row_index, col_index+1)]: | |
if x in range(self.size) and y in range(self.size): | |
if self.covered(x, y): | |
result.append((col_index)) | |
return result | |
f = open('dict.txt') | |
dict_words = set(f.read().split('\n')) | |
f.close() | |
b = Board().with_move(2, 3, 'f').with_move(3, 3, 'a').with_move(4, 3, 't') | |
print b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment