Skip to content

Commit

Permalink
Merge pull request #10 from karol-brejna-i/import-cleanups
Browse files Browse the repository at this point in the history
Remove unused import; resolve some python code style violations
  • Loading branch information
vitogit authored Mar 29, 2021
2 parents e737962 + 23f543c commit 233c920
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ Stockfish
slack_key.txt
venv
*pgn
.idea/
.venv
.DS_Store
16 changes: 6 additions & 10 deletions download_games.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@
"""Downloading chess puzzles for lichess.org"""

import argparse
import chess
import chess.pgn
import logging
import os
import sys

import requests
import chess
import re
import time

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--token", metavar="TOKEN",default="",
parser.add_argument("--token", metavar="TOKEN", default="",
help="secret token for the lichess api")
parser.add_argument("username", metavar="USERNAME",
help="Username in lichess")
parser.add_argument("--quiet", dest="loglevel",
default=logging.DEBUG, action="store_const", const=logging.INFO,
help="substantially reduce the number of logged messages")
parser.add_argument("--max", metavar="MAX",default="60",
parser.add_argument("--max", metavar="MAX", default="60",
help="max number of games")
settings = parser.parse_args()
logging.basicConfig(format="%(message)s", level=settings.loglevel, stream=sys.stdout)

logging.debug("Downloading games from: "+settings.username)
logging.debug("Downloading games from: " + settings.username)

response = requests.get('https://lichess.org/api/games/user/'+settings.username+'?max='+settings.max+'&token=' + settings.token+'&perfType=blitz,rapid,classical&opening=true')
response = requests.get(
'https://lichess.org/api/games/user/' + settings.username + '?max=' + settings.max + '&token=' + settings.token + '&perfType=blitz,rapid,classical&opening=true')
pgn = str(response.text)
all_games = open("games.pgn", "w")
all_games.write(pgn)
Expand Down
30 changes: 11 additions & 19 deletions download_tourments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@

"""Downloading chess puzzles for lichess.org"""

import argparse
import chess
import chess.pgn
import logging
import os
import sys
import requests
import chess
import re
import time

import requests

# tourments
tourment_ids = ['25MtoToy',
'E14kHVwX',
'tdntXNhy',
'sj5GoEdS',
'C4zdQLax',
'wobqi6QP',
'T4RW1ux2',
'nzw7OKBq']
'E14kHVwX',
'tdntXNhy',
'sj5GoEdS',
'C4zdQLax',
'wobqi6QP',
'T4RW1ux2',
'nzw7OKBq']

all_games = open("games.pgn", "w")
pgn = ""
for id in tourment_ids:
print ('https://lichess.org/api/tournament/'+id+'/games')
response = requests.get('https://lichess.org/api/tournament/'+id+'/games')
pgn = pgn +'\n'+ str(response.text)
print('https://lichess.org/api/tournament/' + id + '/games')
response = requests.get('https://lichess.org/api/tournament/' + id + '/games')
pgn = pgn + '\n' + str(response.text)

all_games.write(pgn)
all_games.close()
Expand Down
38 changes: 20 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"""Creating chess puzzles for lichess.org"""

import argparse
import chess
import chess.uci
import chess.pgn
import logging
import os
import sys
from modules.fishnet.fishnet import stockfish_command
from modules.puzzle.puzzle import puzzle

import chess.pgn
import chess.uci

from modules.api.api import post_puzzle
from modules.bcolors.bcolors import bcolors
from modules.fishnet.fishnet import stockfish_command
from modules.investigate.investigate import investigate
from modules.api.api import post_puzzle
from modules.puzzle.puzzle import puzzle

parser = argparse.ArgumentParser(description=__doc__)

Expand All @@ -35,6 +35,7 @@
# Optionally fix colors on Windows and in journals if the colorama module
# is available.
import colorama

wrapper = colorama.AnsiToWin32(sys.stdout)
if wrapper.should_wrap():
sys.stdout = wrapper.stream
Expand All @@ -56,38 +57,39 @@
game_id = 0
while True:
game = chess.pgn.read_game(all_games)
if game == None:
if game is None:
break
node = game

game_id = game_id + 1
game_id = game_id + 1
logging.debug(bcolors.WARNING + "Game ID: " + str(game_id) + bcolors.ENDC)
logging.debug(bcolors.WARNING + "Game headers: " + str(game) + bcolors.ENDC)
logging.debug(bcolors.WARNING + "Game headers: " + str(game) + bcolors.ENDC)

prev_score = chess.uci.Score(None, None)
puzzles = []

logging.debug(bcolors.OKGREEN + "Game Length: " + str(game.end().board().fullmove_number))
logging.debug("Analysing Game..." + bcolors.ENDC)

engine.ucinewgame()

while not node.is_end():
next_node = node.variation(0)
engine.position(next_node.board())

engine.go(depth=settings.depth)
cur_score = info_handler.info["score"][1]
logging.debug(bcolors.OKGREEN + node.board().san(next_node.move) + bcolors.ENDC)
logging.debug(bcolors.OKBLUE + " CP: " + str(cur_score.cp))
logging.debug(" Mate: " + str(cur_score.mate) + bcolors.ENDC)
if investigate(prev_score, cur_score, node.board()):
logging.debug(bcolors.WARNING + " Investigate!" + bcolors.ENDC)
puzzles.append(puzzle(node.board(), next_node.move, str(game_id), engine, info_handler, game, settings.strict))

puzzles.append(
puzzle(node.board(), next_node.move, str(game_id), engine, info_handler, game, settings.strict))

prev_score = cur_score
node = next_node

for i in puzzles:
logging.debug(bcolors.WARNING + "Generating new puzzle..." + bcolors.ENDC)
i.generate(settings.depth)
Expand Down
6 changes: 2 additions & 4 deletions modules/api/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import requests
import chess
import logging
import re
import time

from modules.bcolors.bcolors import bcolors


def post_puzzle(puzzle):
logging.debug(bcolors.WARNING + "NEW PUZZLE GENERATED" + bcolors.ENDC)
logging.info(bcolors.OKBLUE + str(puzzle.to_pgn()) + bcolors.ENDC)
Expand Down
2 changes: 1 addition & 1 deletion modules/bcolors/bcolors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class bcolors:
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
UNDERLINE = '\033[4m'
3 changes: 1 addition & 2 deletions modules/fishnet/fishnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def stockfish_filename():
def update_stockfish(filename):
print("Looking up %s ..." % filename)

headers = {}
headers["User-Agent"] = "Python-Puzzle-Generator"
headers = {"User-Agent": "Python-Puzzle-Generator"}

# Only update to newer versions
try:
Expand Down
34 changes: 16 additions & 18 deletions modules/investigate/investigate.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import chess


def sign(a):
if a > 0:
return 1
elif a < 0:
return -1
else:
return 0
return (a > 0) - (a < 0)


def material_value(board):
return sum(v * (len(board.pieces(pt, True)) + len(board.pieces(pt, False))) for v, pt in zip([0,3,3,5.5,9], chess.PIECE_TYPES))
return sum(v * (len(board.pieces(pt, True)) + len(board.pieces(pt, False))) for v, pt in
zip([0, 3, 3, 5.5, 9], chess.PIECE_TYPES))


def material_count(board):
return chess.popcount(board.occupied)


def investigate(a, b, board):
# determine if the difference between position A and B
# is worth investigating for a puzzle.
if a.cp is not None and b.cp is not None:
if (((a.cp > -110 and a.cp < 850 and b.cp > 200 and b.cp < 850)
or (a.cp > -850 and a.cp < 110 and b.cp < -200 and b.cp > -850))
and material_value(board) > 3
and material_count(board) > 6):
if (((-110 < a.cp < 850 and 200 < b.cp < 850)
or (-850 < a.cp < 110 and -200 > b.cp > -850))
and material_value(board) > 3
and material_count(board) > 6):
return True
elif (a.cp is not None
and b.mate is not None
and material_value(board) > 3):
if ((a.cp < 110 and sign(b.mate) == -1) or (a.cp > -110 and sign(b.mate) == 1) ):
elif a.cp is not None and b.mate is not None and material_value(board) > 3:
if (a.cp < 110 and sign(b.mate) == -1) or (a.cp > -110 and sign(b.mate) == 1):
return True
elif (a.mate is not None
and b.mate is not None):
if sign(a.mate) == sign(b.mate): #actually means that they're opposite
and b.mate is not None):
if sign(a.mate) == sign(b.mate): # actually means that they're opposite
return True
return False
return False
2 changes: 1 addition & 1 deletion modules/puzzle/analysed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def sort_val(self):
elif self.evaluation.mate is not None:
return self.sign(self.evaluation.mate) * (abs(100 + self.evaluation.mate)) * 10000
else:
return 0
return 0
63 changes: 33 additions & 30 deletions modules/puzzle/position_list.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import logging
from operator import methodcaller

import chess
import chess.uci
import logging
import os

from modules.bcolors.bcolors import bcolors
from modules.puzzle.analysed import analysed
from operator import methodcaller


class position_list:
def __init__(self, position, engine, info_handler, player_turn=True, best_move=None, evaluation=None, strict = True):
def __init__(self, position, engine, info_handler, player_turn=True, best_move=None, evaluation=None, strict=True):
self.position = position.copy()
self.engine = engine
self.info_handler = info_handler
Expand Down Expand Up @@ -61,10 +63,10 @@ def evaluate_best(self, depth):
if self.best_move.bestmove is not None:
self.evaluation = self.info_handler.info["score"][1]
self.next_position = position_list(self.position.copy(),
self.engine,
self.info_handler,
not self.player_turn,
strict = self.strict)
self.engine,
self.info_handler,
not self.player_turn,
strict=self.strict)
self.next_position.position.push(self.best_move.bestmove)
logging.debug("Best Move: " + self.best_move.bestmove.uci() + bcolors.ENDC)
logging.debug(bcolors.OKBLUE + " CP: " + str(self.evaluation.cp))
Expand All @@ -90,33 +92,34 @@ def evaluate_legals(self, depth):
logging.debug("... and " + str(max(0, len(self.analysed_legals) - 3)) + " more moves" + bcolors.ENDC)

def material_difference(self):
return sum(v * (len(self.position.pieces(pt, True)) - len(self.position.pieces(pt, False))) for v, pt in zip([0,3,3,5.5,9], chess.PIECE_TYPES))
return sum(v * (len(self.position.pieces(pt, True)) - len(self.position.pieces(pt, False))) for v, pt in
zip([0, 3, 3, 5.5, 9], chess.PIECE_TYPES))

def material_count(self):
return chess.popcount(self.position.occupied)

def is_complete(self, category, color, first_node, first_val):
if self.next_position is not None:
if ((category == 'Mate' and not self.ambiguous())
or (category == 'Material' and self.next_position.next_position is not None)):
or (category == 'Material' and self.next_position.next_position is not None)):
return self.next_position.is_complete(category, color, False, first_val)

if category == 'Material':
if color:
if (self.material_difference() > 0.2
and abs(self.material_difference() - first_val) > 0.1
and first_val < 2
and self.evaluation.mate is None
and self.material_count() > 6):
if (self.material_difference() > 0.2
and abs(self.material_difference() - first_val) > 0.1
and first_val < 2
and self.evaluation.mate is None
and self.material_count() > 6):
return True
else:
return False
else:
if (self.material_difference() < -0.2
and abs(self.material_difference() - first_val) > 0.1
and first_val > -2
and self.evaluation.mate is None
and self.material_count() > 6):
if (self.material_difference() < -0.2
and abs(self.material_difference() - first_val) > 0.1
and first_val > -2
and self.evaluation.mate is None
and self.material_count() > 6):
return True
else:
return False
Expand All @@ -128,22 +131,22 @@ def is_complete(self, category, color, first_node, first_val):

def ambiguous(self):
# If strict == False then it will generate more tactics but more ambiguous
move_number = 1 if self.strict == True else 2
move_number = 1 if self.strict else 2
if len(self.analysed_legals) > 1:
if (self.analysed_legals[0].evaluation.cp is not None
and self.analysed_legals[1].evaluation.cp is not None):
and self.analysed_legals[1].evaluation.cp is not None):
if (self.analysed_legals[0].evaluation.cp > -210
or self.analysed_legals[move_number].evaluation.cp < -90):
or self.analysed_legals[move_number].evaluation.cp < -90):
return True
if (self.analysed_legals[0].evaluation.mate is not None
and self.analysed_legals[1].evaluation.mate is not None):
and self.analysed_legals[1].evaluation.mate is not None):
if (self.analysed_legals[0].evaluation.mate < 1
and self.analysed_legals[1].evaluation.mate < 1):
return True
if (self.analysed_legals[0].evaluation.mate is not None
and self.analysed_legals[1].evaluation.cp is not None):
if (self.analysed_legals[1].evaluation.cp < -200):
and self.analysed_legals[1].evaluation.mate < 1):
return True
if self.analysed_legals[0].evaluation.mate is None or self.analysed_legals[1].evaluation.cp is None:
return
if self.analysed_legals[1].evaluation.cp < -200:
return True
return False

def game_over(self):
Expand Down
Loading

0 comments on commit 233c920

Please sign in to comment.