Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure mypy and type annotate the code #41

Merged
merged 6 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[mypy]
files = .
warn_unused_ignores = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
check_untyped_defs = True
no_implicit_optional = True
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
termcolor
termcolor
mypy>=0.981
mypy-extensions>=0.4.3
tomli>=2.0.1
types-setuptools>=65.4.0.0
typing_extensions>=4.3.0
40 changes: 22 additions & 18 deletions src/AI.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

import copy
import random
from multiprocessing import Pool
from typing import no_type_check

from src.Board import Board
from src.InputParser import InputParser
from src.Move import Move
from src.MoveNode import MoveNode

WHITE = True
Expand All @@ -13,20 +17,20 @@
class AI:

depth = 1
board = None
side = None
movesAnalyzed = 0

def __init__(self, board, side, depth):
def __init__(self, board: Board, side: bool, depth: int):
self.board = board
self.side = side
self.depth = depth
self.parser = InputParser(self.board, self.side)

def getFirstMove(self, side):
def getFirstMove(self, side: bool) -> Move:
move = list(self.board.getAllMovesLegal(side))[0]
return move

# TODO this method is never used, remove?
@no_type_check
def getAllMovesLegalConcurrent(self, side):
p = Pool(8)
unfilteredMovesWithBoard = \
Expand All @@ -38,8 +42,8 @@ def getAllMovesLegalConcurrent(self, side):
p.join()
return list(filter(None, legalMoves))

def minChildrenOfNode(self, node):
lowestNodes = []
def minChildrenOfNode(self, node: MoveNode) -> list[MoveNode]:
lowestNodes: list[MoveNode] = []
for child in node.children:
if not lowestNodes:
lowestNodes.append(child)
Expand All @@ -50,8 +54,8 @@ def minChildrenOfNode(self, node):
lowestNodes.append(child)
return lowestNodes

def maxChildrenOfNode(self, node):
highestNodes = []
def maxChildrenOfNode(self, node: MoveNode) -> list[MoveNode]:
highestNodes: list[MoveNode] = []
for child in node.children:
if not highestNodes:
highestNodes.append(child)
Expand All @@ -62,12 +66,12 @@ def maxChildrenOfNode(self, node):
highestNodes.append(child)
return highestNodes

def getRandomMove(self):
def getRandomMove(self) -> Move:
legalMoves = list(self.board.getAllMovesLegal(self.side))
randomMove = random.choice(legalMoves)
return randomMove

def generateMoveTree(self):
def generateMoveTree(self) -> list[MoveNode]:
moveTree = []
for move in self.board.getAllMovesLegal(self.side):
moveTree.append(MoveNode(move, [], None))
Expand All @@ -78,7 +82,7 @@ def generateMoveTree(self):
self.board.undoLastMove()
return moveTree

def populateNodeChildren(self, node):
def populateNodeChildren(self, node: MoveNode) -> None:
node.pointAdvantage = self.board.getPointAdvantageOfSide(self.side)
node.depth = node.getDepth()
if node.depth == self.depth:
Expand All @@ -104,7 +108,7 @@ def populateNodeChildren(self, node):
self.populateNodeChildren(node.children[-1])
self.board.undoLastMove()

def getOptimalPointAdvantageForNode(self, node):
def getOptimalPointAdvantageForNode(self, node: MoveNode) -> int:
if node.children:
for child in node.children:
child.pointAdvantage = \
Expand All @@ -119,18 +123,18 @@ def getOptimalPointAdvantageForNode(self, node):
else:
return node.pointAdvantage

def getBestMove(self):
def getBestMove(self) -> Move:
moveTree = self.generateMoveTree()
bestMoves = self.bestMovesWithMoveTree(moveTree)
randomBestMove = random.choice(bestMoves)
randomBestMove.notation = self.parser.notationForMove(randomBestMove)
return randomBestMove

def makeBestMove(self):
def makeBestMove(self) -> None:
self.board.makeMove(self.getBestMove())

def bestMovesWithMoveTree(self, moveTree):
bestMoveNodes = []
def bestMovesWithMoveTree(self, moveTree: list[MoveNode]) -> list[Move]:
bestMoveNodes: list[MoveNode] = []
for moveNode in moveTree:
moveNode.pointAdvantage = \
self.getOptimalPointAdvantageForNode(moveNode)
Expand All @@ -144,13 +148,13 @@ def bestMovesWithMoveTree(self, moveTree):

return [node.move for node in bestMoveNodes]

def isValidMove(self, move, side):
def isValidMove(self, move: Move, side: bool) -> bool:
for legalMove in self.board.getAllMovesLegal(side):
if move == legalMove:
return True
return False

def makeRandomMove(self):
def makeRandomMove(self) -> None:
moveToMake = self.getRandomMove()
self.board.makeMove(moveToMake)

Expand Down
12 changes: 10 additions & 2 deletions src/Bishop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Iterator
ClasherKasten marked this conversation as resolved.
Show resolved Hide resolved

from src.Coordinate import Coordinate as C
from src.Piece import Piece

if TYPE_CHECKING:
from src.Board import Board
from src.Move import Move

WHITE = True
BLACK = False

Expand All @@ -10,11 +18,11 @@ class Bishop (Piece):
stringRep = 'B'
value = 3

def __init__(self, board, side, position, movesMade=0):
def __init__(self, board: Board, side: bool, position: C, movesMade: int = 0):
super(Bishop, self).__init__(board, side, position)
self.movesMade = movesMade

def getPossibleMoves(self):
def getPossibleMoves(self) -> Iterator[Move]:
currentPosition = self.position
directions = [C(1, 1), C(1, -1), C(-1, 1), C(-1, -1)]
for direction in directions:
Expand Down
Loading