Skip to content

Commit

Permalink
Add flake8, black and isort
Browse files Browse the repository at this point in the history
Remove dead code

Make flake8 happy

Add .python-version to .gitignore
  • Loading branch information
aaaandrzej authored and ClasherKasten committed Oct 12, 2022
1 parent e0903dc commit aa09c50
Show file tree
Hide file tree
Showing 21 changed files with 487 additions and 409 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 79
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ Please note we have a code of conduct, please follow it in all your interactions
build.
2. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Make sure that your code follows the formatting rules enforced within this repository. This can be checked by running
`flake8 src`, assuming that you have required tools installed from `requirements-dev.txt`. Also, make sure that
`mypy --show-error-codes --pretty --strict .` still runs happily against the changed code.

**Working on your first Pull Request?** You can learn how from this free series [How to Contribute to an Open Source Project on GitHub](https://kcd.im/pull-request)
5 changes: 4 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
check_untyped_defs = True
no_implicit_optional = True
no_implicit_optional = True

[mypy-colored]
ignore_missing_imports = True
9 changes: 9 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
flake8>=5.0.4
mccabe>=0.7.0
mypy>=0.981
mypy-extensions>=0.4.3
pycodestyle>=2.9.1
pyflakes>=2.5.0
tomli>=2.0.1
types-setuptools>=65.4.0.0
typing_extensions>=4.3.0
5 changes: 0 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
colored
mypy>=0.981
mypy-extensions>=0.4.3
tomli>=2.0.1
types-setuptools>=65.4.0.0
typing_extensions>=4.3.0
16 changes: 5 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import find_packages, setup
from setuptools import setup

setup(
name='cl-chess',
Expand All @@ -8,15 +8,9 @@
author='Marcus Buffett',
author_email='[email protected]',
url='https://github.com/marcusbuffett/command-line-chess',
#download_url='https://github.com/peterldowns/mypackage/tarball/0.1',
entry_points={
'console_scripts': [
'chess = src.main:main',
],
},
install_requires=[
'colored',
],
# download_url='https://github.com/peterldowns/mypackage/tarball/0.1',
entry_points={'console_scripts': ['chess = src.main:main', ], },
install_requires=['colored', ],
keywords=['chess', 'game'],
classifiers=["Programming Language :: Python :: 3"],
classifiers=['Programming Language :: Python :: 3'],
)
71 changes: 9 additions & 62 deletions src/AI.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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
Expand All @@ -15,7 +12,6 @@


class AI:

depth = 1
movesAnalyzed = 0

Expand All @@ -25,47 +21,6 @@ def __init__(self, board: Board, side: bool, depth: int):
self.depth = depth
self.parser = InputParser(self.board, 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 = \
[(move, copy.deepcopy(self.board))
for move in self.board.getAllMovesUnfiltered(side)]
legalMoves = p.starmap(self.returnMoveIfLegal,
unfilteredMovesWithBoard)
p.close()
p.join()
return list(filter(None, legalMoves))

def minChildrenOfNode(self, node: MoveNode) -> list[MoveNode]:
lowestNodes: list[MoveNode] = []
for child in node.children:
if not lowestNodes:
lowestNodes.append(child)
elif child < lowestNodes[0]:
lowestNodes = []
lowestNodes.append(child)
elif child == lowestNodes[0]:
lowestNodes.append(child)
return lowestNodes

def maxChildrenOfNode(self, node: MoveNode) -> list[MoveNode]:
highestNodes: list[MoveNode] = []
for child in node.children:
if not highestNodes:
highestNodes.append(child)
elif child < highestNodes[0]:
highestNodes = []
highestNodes.append(child)
elif child == highestNodes[0]:
highestNodes.append(child)
return highestNodes

def getRandomMove(self) -> Move:
legalMoves = list(self.board.getAllMovesLegal(self.side))
randomMove = random.choice(legalMoves)
Expand Down Expand Up @@ -111,15 +66,16 @@ def populateNodeChildren(self, node: MoveNode) -> None:
def getOptimalPointAdvantageForNode(self, node: MoveNode) -> int:
if node.children:
for child in node.children:
child.pointAdvantage = \
self.getOptimalPointAdvantageForNode(child)
child.pointAdvantage = self.getOptimalPointAdvantageForNode(
child
)

# If the depth is divisible by 2,
# it's a move for the AI's side, so return max
if node.children[0].depth % 2 == 1:
return(max(node.children).pointAdvantage)
return max(node.children).pointAdvantage
else:
return(min(node.children).pointAdvantage)
return min(node.children).pointAdvantage
else:
return node.pointAdvantage

Expand All @@ -136,8 +92,9 @@ def makeBestMove(self) -> None:
def bestMovesWithMoveTree(self, moveTree: list[MoveNode]) -> list[Move]:
bestMoveNodes: list[MoveNode] = []
for moveNode in moveTree:
moveNode.pointAdvantage = \
self.getOptimalPointAdvantageForNode(moveNode)
moveNode.pointAdvantage = self.getOptimalPointAdvantageForNode(
moveNode
)
if not bestMoveNodes:
bestMoveNodes.append(moveNode)
elif moveNode > bestMoveNodes[0]:
Expand All @@ -148,18 +105,8 @@ def bestMovesWithMoveTree(self, moveTree: list[MoveNode]) -> list[Move]:

return [node.move for node in bestMoveNodes]

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) -> None:
moveToMake = self.getRandomMove()
self.board.makeMove(moveToMake)


if __name__ == "__main__":
if __name__ == '__main__':
mainBoard = Board()
ai = AI(mainBoard, True, 3)
print(mainBoard)
Expand Down
12 changes: 7 additions & 5 deletions src/Bishop.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@
BLACK = False


class Bishop (Piece):

class Bishop(Piece):
stringRep = 'B'
value = 3

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

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:
for move in self.movesInDirectionFromPos(currentPosition,
direction, self.side):
for move in self.movesInDirectionFromPos(
currentPosition, direction, self.side
):
yield move
Loading

0 comments on commit aa09c50

Please sign in to comment.