def test_sort_letters(self): m = Move() m.add_letter('a', (0, 1)) m.add_letter('b', (0, 0)) m.sort_letters() self.assertEqual('b', m.positions[0].letter) self.assertEqual((0, 0), m.positions[0].pos) self.assertEqual('a', m.positions[1].letter) self.assertEqual((0, 1), m.positions[1].pos) m = Move() m.horizontal = False m.add_letter('a', (1, 0)) m.add_letter('b', (0, 0)) m.sort_letters() self.assertEqual('b', m.positions[0].letter) self.assertEqual((0, 0), m.positions[0].pos) self.assertEqual('a', m.positions[1].letter) self.assertEqual((1, 0), m.positions[1].pos)
def test_hash(self): a = Move() a.positions = ['A', 'B', 'C'] b = Move() b.positions = ['A', 'B', 'C'] self.assert_(hash(a) == hash(b)) b.horizontal = False self.assert_(not hash(a) == hash(b)) b.positions = ['A', 'B', 'D'] self.assert_(not hash(a) == hash(b)) b.horizontal = True self.assert_(not hash(a) == hash(b)) b.positions = ['B', 'C', 'A'] self.assert_(hash(a) == hash(b))
def test_equality(self): a = Move() a.positions = ['A', (1, 2, 3)] b = Move() b.positions = ['A', (1, 2, 3)] self.assert_(a == b) b.positions = [(1, 2, 3), 'A'] self.assert_(a == b) b.horizontal = False self.assert_(not a == b) b.positions = ['A', (1, 2)] self.assert_(not a == b) b.horizontal = True self.assert_(not a == b)
def parse_scenario(filename): with open(filename, 'r') as f: lines = map(lambda s: s.rstrip(), f.readlines()) lines = filter(lambda s: len(s) > 0 and not s.startswith('!:'), lines) i = 0 while i < len(lines): line = lines[i] board = [] if line != 'BEGIN BOARD': raise IOError( 'Improperly formatted scenario file, line %d: expected BEGIN BOARD.' % i) i += 1 line = lines[i] while line != 'END BOARD': board.append(list(line)) i += 1 line = lines[i] if len(board) == 0: board = deepcopy(default_board) i += 1 candidate_letters = lines[i] i += 1 r_match = re.match(r'^(\d+), (\d+)$', lines[i]) if not r_match: raise IOError( 'Improperly formatted scenario file, line %d: candidate position.' % i) candidate_pos = tuple(map(int, r_match.groups())) i += 1 if lines[i] == 'H': candidate_dir = True elif lines[i] == 'V': candidate_dir = False else: raise IOError( 'Improperly formatted scenario file, line %d: candidate direction.' % i) i += 1 j = int(lines[i]) if j == 1: exp_result = True elif j == 0: exp_result = False else: raise IOError( 'Improperly formatted scenario file, line %d: expected result.' % i) exp_score = -1 if exp_result: i += 1 exp_score = int(lines[i]) i += 1 # Increment before beginning of next iteration candidate = Move() offset = 0 for j, letter in enumerate(candidate_letters): if candidate_dir: x, y = candidate_pos[0], candidate_pos[1] + j + offset while y < len(board[x]) and board[x][y] not in empty_locations: offset += 1 y += 1 candidate.positions.append(BoardPosition(letter, (x, y))) else: x, y = candidate_pos[0] + j + offset, candidate_pos[1] while x < len(board) and board[x][y] not in empty_locations: offset += 1 x += 1 candidate.positions.append(BoardPosition(letter, (x, y))) candidate.horizontal = candidate_dir yield { 'board': board, 'candidate': candidate, 'result': exp_result, 'score': exp_score }
def parse_scenario(filename): with open(filename, 'r') as f: lines = map(lambda s: s.rstrip(), f.readlines()) lines = filter(lambda s: len(s) > 0 and not s.startswith('!:'), lines) i = 0 while i < len(lines): line = lines[i] board = [] if line != 'BEGIN BOARD': raise IOError('Improperly formatted scenario file, line %d: expected BEGIN BOARD.' % i) i += 1 line = lines[i] while line != 'END BOARD': board.append(list(line)) i += 1 line = lines[i] if len(board) == 0: board = deepcopy(default_board) i += 1 candidate_letters = lines[i] i += 1 r_match = re.match(r'^(\d+), (\d+)$', lines[i]) if not r_match: raise IOError('Improperly formatted scenario file, line %d: candidate position.' % i) candidate_pos = tuple(map(int, r_match.groups())) i += 1 if lines[i] == 'H': candidate_dir = True elif lines[i] == 'V': candidate_dir = False else: raise IOError('Improperly formatted scenario file, line %d: candidate direction.' % i) i += 1 j = int(lines[i]) if j == 1: exp_result = True elif j == 0: exp_result = False else: raise IOError('Improperly formatted scenario file, line %d: expected result.' % i) exp_score = -1 if exp_result: i += 1 exp_score = int(lines[i]) i += 1 # Increment before beginning of next iteration candidate = Move() offset = 0 for j, letter in enumerate(candidate_letters): if candidate_dir: x, y = candidate_pos[0], candidate_pos[1] + j + offset while y < len(board[x]) and board[x][y] not in empty_locations: offset += 1 y += 1 candidate.positions.append(BoardPosition(letter, (x, y))) else: x, y = candidate_pos[0] + j + offset, candidate_pos[1] while x < len(board) and board[x][y] not in empty_locations: offset += 1 x += 1 candidate.positions.append(BoardPosition(letter, (x, y))) candidate.horizontal = candidate_dir yield {'board': board, 'candidate': candidate, 'result': exp_result, 'score': exp_score}
def parse_cross_set(filename): with open(filename, 'r') as f: lines = map(lambda s: s.rstrip(), f.readlines()) lines = filter(lambda s: len(s) > 0 and not s.startswith('!:'), lines) i = 0 while i < len(lines): line = lines[i] board = [] if line != 'BEGIN BOARD': raise IOError('Improperly formatted scenario file, line %d: expected BEGIN BOARD.' % i) i += 1 line = lines[i] while line != 'END BOARD': board.append(list(line)) i += 1 line = lines[i] if len(board) == 0: board = deepcopy(default_board) i += 1 candidate_letters = lines[i] i += 1 r_match = re.match(r'^(\d+), (\d+)$', lines[i]) if not r_match: raise IOError('Improperly formatted scenario file, line %d: candidate position.' % i) candidate_pos = tuple(map(int, r_match.groups())) i += 1 if lines[i] == 'H': candidate_dir = True elif lines[i] == 'V': candidate_dir = False else: raise IOError('Improperly formatted scenario file, line %d: candidate direction.' % i) i += 1 # Increment before beginning of next iteration line = lines[i] if line != 'BEGIN CROSSES': raise IOError('Improperly formatted scenario file, line %d: expected BEGIN CROSSES.' % i) i += 1 line = lines[i] crosses = [] while line != 'END CROSSES': cross = {} r_match = re.match(r'^(\d+), (\d+)$', line) if not r_match: raise IOError('Improperly formatted scenario file, line %d: cross position.' % i) cross['x'], cross['y'] = tuple(map(int, r_match.groups())) i += 1 line = lines[i] if line[0] == '-': cross['horizontal'] = True elif line[0] == '|': cross['horizontal'] = False else: raise IOError('Improperly formatted scenario file, line %d: cross direction.' % i) i += 1 line = lines[i] if line == '/': cross['letters'] = set() else: cross['letters'] = set(line) crosses.append(cross) i += 1 line = lines[i] candidate = Move() offset = 0 for j, letter in enumerate(candidate_letters): if candidate_dir: x, y = candidate_pos[0], candidate_pos[1] + j + offset while y < len(board[x]) and board[x][y] not in empty_locations: offset += 1 y += 1 candidate.positions.append(BoardPosition(letter, (x, y))) else: x, y = candidate_pos[0] + j + offset, candidate_pos[1] while x < len(board) and board[x][y] not in empty_locations: offset += 1 x += 1 candidate.positions.append(BoardPosition(letter, (x, y))) candidate.horizontal = candidate_dir yield {'board': board, 'candidate': candidate, 'crosses': crosses} i += 1