wip
This commit is contained in:
parent
d212b32da9
commit
2c06b5c327
8
blunderboard-py/.idea/.gitignore
vendored
Normal file
8
blunderboard-py/.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
10
blunderboard-py/.idea/blunderboard-py.iml
Normal file
10
blunderboard-py/.idea/blunderboard-py.iml
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,6 @@
|
|||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
4
blunderboard-py/.idea/misc.xml
Normal file
4
blunderboard-py/.idea/misc.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (blunderboard-py)" project-jdk-type="Python SDK" />
|
||||
</project>
|
8
blunderboard-py/.idea/modules.xml
Normal file
8
blunderboard-py/.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/blunderboard-py.iml" filepath="$PROJECT_DIR$/.idea/blunderboard-py.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
blunderboard-py/.idea/vcs.xml
Normal file
6
blunderboard-py/.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -2,6 +2,9 @@ from stockfish import Stockfish
|
|||
from playsound import playsound
|
||||
from pathlib import Path
|
||||
import random
|
||||
from pgn_parser import parser, pgn
|
||||
|
||||
# import RPi.GPIO as GPIO
|
||||
|
||||
settings = {
|
||||
"Debug Log File": "",
|
||||
|
@ -46,39 +49,66 @@ class Game:
|
|||
A class representing the game state
|
||||
"""
|
||||
|
||||
def __init__(self, settings: dict):
|
||||
self.engine = Stockfish("/usr/local/bin/stockfish")
|
||||
self.settings = settings
|
||||
def __init__(self, engine_settings: dict):
|
||||
self.engine = Stockfish("/usr/bin/stockfish")
|
||||
self.settings = engine_settings
|
||||
self.engine.update_engine_parameters(self.settings)
|
||||
self.matrix = BoardReader()
|
||||
self.current_evaluation = self.engine.get_evaluation()
|
||||
self.current_evaluation = self.engine.get_evaluation() # This is not necessary, now that I think about it.
|
||||
self.evaluations = []
|
||||
self.engine.set_position()
|
||||
|
||||
def make_move(move) -> None:
|
||||
"""
|
||||
Makes a move on the board and updates the game state
|
||||
:param move:
|
||||
:return: None
|
||||
"""
|
||||
if self.engine.is_move_correct(move):
|
||||
self.engine.make_moves_from_current_position([move])
|
||||
self.current_evaluation = self.engine.get_evaluation()
|
||||
self.evaluations.append(self.current_evaluation)
|
||||
if move_was_blunder():
|
||||
# If the played move was a blunder play a random sound from the sound path
|
||||
playsound(random.choice(sound_path))
|
||||
|
||||
def move_was_blunder() -> bool:
|
||||
"""
|
||||
Returns true if the last move was a blunder
|
||||
:return: bool
|
||||
"""
|
||||
def move_was_blunder(self) -> bool:
|
||||
"""
|
||||
Returns true if the last move was a blunder
|
||||
:return: bool
|
||||
"""
|
||||
if len(self.evaluations) > 1:
|
||||
previous_evaluation = self.evaluations[-1]
|
||||
return self.current_evaluation["value"] < (previous_evaluation["value"] + 3) # TODO This is not a
|
||||
# particularly good way to identify a blunder
|
||||
return abs(self.current_evaluation["value"] - previous_evaluation["value"]) > 300
|
||||
# TODO This is not a particularly good way to identify a blunder
|
||||
else:
|
||||
return False
|
||||
|
||||
def make_move(self, move) -> None:
|
||||
"""
|
||||
Makes a move on the board and updates the game state
|
||||
:param move:
|
||||
:return: None
|
||||
"""
|
||||
if self.engine.is_move_correct(move):
|
||||
self.engine.make_moves_from_current_position([move])
|
||||
self.current_evaluation = self.engine.get_evaluation()
|
||||
self.evaluations.append(self.current_evaluation)
|
||||
if self.move_was_blunder():
|
||||
# If the played move was a blunder play a random sound from the sound path
|
||||
# playsound(random.choice(sound_path))
|
||||
print("Blunder!")
|
||||
|
||||
def __str__(self):
|
||||
return ""
|
||||
|
||||
|
||||
def get_moves_from_pgn(pgn_file: Path):
|
||||
"""
|
||||
Returns a list of moves from a PGN file
|
||||
:param pgn_file: str
|
||||
:return: list
|
||||
"""
|
||||
with open(pgn_file, "r") as f:
|
||||
pgn_file = f.read()
|
||||
return parser.parse(pgn_file, actions=pgn.Actions())
|
||||
|
||||
|
||||
test_game = Game(settings)
|
||||
|
||||
pgn_path = Path("../spongeboyahoy_vs_tomlx.pgn")
|
||||
moves = get_moves_from_pgn(pgn_path)
|
||||
for i in range(1, 250):
|
||||
print(str(moves.move(i)).split(". ")[1].split(" ")[0])
|
||||
test_game.make_move(str(moves.move(i)).split(". ")[1].split(" ")[0])
|
||||
print(test_game.current_evaluation)
|
||||
test_game.make_move(str(moves.move(i)).split(". ")[1].split(" ")[1])
|
||||
print(test_game.current_evaluation)
|
||||
|
||||
|
||||
|
|
|
@ -1,2 +1,4 @@
|
|||
stockfish
|
||||
playsound
|
||||
playsound
|
||||
pygobject
|
||||
pgn_parser
|
Binary file not shown.
Loading…
Reference in a new issue