forked from marcusbuffett/command-line-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputParser.py
69 lines (54 loc) · 2.48 KB
/
InputParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from Move import Move
from Coordinate import Coordinate as C
class InputParser:
def __init__(self, board, side):
self.board = board
self.side = side
def parse(self, humanInput):
humanInput = humanInput.lower()
regexLongNotation = re.compile('[a-z][1-8][a-z][1-8]')
if regexLongNotation.match(humanInput):
return self.moveForLongNotation(humanInput)
regexShortNotation = re.compile('[rnbkqp][a-z][1-8]')
if regexShortNotation.match(humanInput):
return self.moveForShortNotation(humanInput)
def moveForLongNotation(self, notation):
transTable = str.maketrans('abcdefgh', '01234567')
humanInput = humanInput.translate(transTable)
humanInput = [int(c) for c in humanInput]
oldPos = C(humanInput[0], humanInput[1])
newPos = C(humanInput[2], humanInput[3])
return Move(oldPos, newPos)
def moveForShortNotation(self, notation):
moves = self.getLegalMovesWithShortNotation(self.side)
for move in moves:
if move.notation.lower() == notation.lower():
return move
def notationForMove(self, move):
side = self.board.getSideOfMove(move)
moves = self.getLegalMovesWithShortNotation(side)
for m in moves:
if m == move:
return m.notation
def getLegalMovesWithShortNotation(self, side):
moves = []
for legalMove in self.board.getAllMovesLegal(side):
moves.append(legalMove)
legalMove.notation = self.board.getShortNotationOfMove(legalMove)
duplicateNotationMoves = self.duplicateMovesFromMoves(moves)
for duplicateMove in duplicateNotationMoves:
duplicateMove.notation = \
self.board.getShortNotationOfMoveWithFile(duplicateMove)
duplicateNotationMoves = self.duplicateMovesFromMoves(moves)
for duplicateMove in duplicateNotationMoves:
duplicateMove.notation = \
self.board.getShortNotationOfMoveWithRank(duplicateMove)
duplicateNotationMoves = self.duplicateMovesFromMoves(moves)
for duplicateMove in duplicateNotationMoves:
duplicateMove.notation = \
self.board.getShortNotationOfMoveWithFileAndRank(duplicateMove)
return moves
def duplicateMovesFromMoves(self, moves):
return list(filter(
lambda move:
len([m for m in moves if m.notation == move.notation]) > 1, moves))