diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index b7889de..d8c1c41 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -2,19 +2,19 @@ name: Python tests on: push: - branches: [ main ] + branches: [main] paths: - - 'PYTHON/lichess_bot/**' - - 'PYTHON/**' - - 'tests/**' - - 'requirements.txt' + - "PYTHON/lichess_bot/**" + - "PYTHON/**" + - "tests/**" + - "requirements.txt" pull_request: - branches: [ main ] + branches: [main] paths: - - 'PYTHON/lichess_bot/**' - - 'PYTHON/**' - - 'tests/**' - - 'requirements.txt' + - "PYTHON/lichess_bot/**" + - "PYTHON/**" + - "tests/**" + - "requirements.txt" jobs: test: @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: '3.11' + python-version: "3.11" - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/.gitignore b/.gitignore index 7ff42b5..dc95ec8 100644 --- a/.gitignore +++ b/.gitignore @@ -227,9 +227,9 @@ cython_debug/ .abstra/ # Visual Studio Code -# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore +# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore -# and can be added to the global gitignore or merged into this file. However, if you prefer, +# and can be added to the global gitignore or merged into this file. However, if you prefer, # you could uncomment the following to ignore the entire vscode folder # .vscode/ @@ -247,4 +247,4 @@ __marimo__/ # Streamlit .streamlit/secrets.toml fps_demo -server_c \ No newline at end of file +server_c diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..8693790 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,268 @@ +# ============================================================================== +# Pre-commit Configuration - AGGRESSIVE Python Linting & Formatting +# ============================================================================== +# Install: pre-commit install +# Run all: pre-commit run --all-files +# Update hooks: pre-commit autoupdate +# ============================================================================== + +# Global settings +default_language_version: + python: python3 + +# Fail fast on first error (set to false to see all errors) +fail_fast: false + +# Configuration +ci: + autofix_commit_msg: "style: auto-fix by pre-commit hooks" + autoupdate_commit_msg: "chore: update pre-commit hooks" + +repos: + # =========================================================================== + # GENERAL HOOKS - File formatting and validation + # =========================================================================== + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + args: [--markdown-linebreak-ext=md] + - id: end-of-file-fixer + - id: check-yaml + args: [--unsafe] + - id: check-json + - id: check-toml + - id: check-xml + - id: check-added-large-files + args: [--maxkb=1000] + - id: check-merge-conflict + - id: check-case-conflict + - id: check-symlinks + - id: check-executables-have-shebangs + - id: check-shebang-scripts-are-executable + - id: detect-private-key + - id: debug-statements + - id: name-tests-test + args: [--pytest-test-first] + - id: check-ast + - id: check-builtin-literals + - id: check-docstring-first + - id: fix-byte-order-marker + - id: mixed-line-ending + args: [--fix=lf] + - id: requirements-txt-fixer + + # =========================================================================== + # RUFF - Fast Python linter and formatter (replaces black, isort, flake8, etc.) + # =========================================================================== + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.8.1 + hooks: + # Linter - run first to catch issues + - id: ruff + args: + - --fix + - --exit-non-zero-on-fix + - --show-fixes + types_or: [python, pyi] + # Formatter - run after linting + - id: ruff-format + types_or: [python, pyi] + + # =========================================================================== + # MYPY - Static type checking (strict mode) + # =========================================================================== + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.13.0 + hooks: + - id: mypy + args: + - --strict + - --ignore-missing-imports + - --show-error-codes + - --no-error-summary + additional_dependencies: + - types-requests + - types-PyYAML + - types-python-dateutil + exclude: ^(Bash/|\.venv/) + + # =========================================================================== + # PYLINT - Comprehensive Python linter + # =========================================================================== + - repo: https://github.com/pylint-dev/pylint + rev: v3.3.2 + hooks: + - id: pylint + args: + - --rcfile=pyproject.toml + - --fail-under=5.0 + - --jobs=0 + additional_dependencies: + - python-chess + - requests + - pygame + exclude: ^(Bash/|\.venv/) + + # =========================================================================== + # BANDIT - Security linter + # =========================================================================== + - repo: https://github.com/PyCQA/bandit + rev: 1.7.10 + hooks: + - id: bandit + args: + - -c + - pyproject.toml + - --severity-level=low + - --confidence-level=low + additional_dependencies: ["bandit[toml]"] + exclude: ^(Bash/|\.venv/|tests/|.*test.*\.py$) + + # =========================================================================== + # VULTURE - Dead code detection + # =========================================================================== + - repo: https://github.com/jendrikseipp/vulture + rev: v2.13 + hooks: + - id: vulture + args: + - --min-confidence=80 + - --exclude=.venv,Bash,__pycache__ + exclude: ^(Bash/|\.venv/) + + # =========================================================================== + # PYUPGRADE - Upgrade Python syntax + # =========================================================================== + - repo: https://github.com/asottile/pyupgrade + rev: v3.19.0 + hooks: + - id: pyupgrade + args: + - --py310-plus + + # =========================================================================== + # CODESPELL - Spell checking in code + # =========================================================================== + - repo: https://github.com/codespell-project/codespell + rev: v2.3.0 + hooks: + - id: codespell + args: + - --skip=*.json,*.lock,*.min.js,*.min.css,.git,__pycache__,.venv + - --ignore-words-list=ans,ect,nd,som,sur + exclude: ^(Bash/ffmpeg-build/) + + # =========================================================================== + # DOCFORMATTER - Format docstrings (using local hook due to compatibility) + # =========================================================================== + - repo: local + hooks: + - id: docformatter + name: docformatter + entry: docformatter + language: system + types: [python] + args: + - --in-place + - --wrap-summaries=88 + - --wrap-descriptions=88 + + # =========================================================================== + # INTERROGATE - Docstring coverage + # =========================================================================== + - repo: https://github.com/econchick/interrogate + rev: 1.7.0 + hooks: + - id: interrogate + args: + - --fail-under=0 + - --verbose + - --ignore-init-method + - --ignore-init-module + - --ignore-magic + - --ignore-private + - --ignore-semiprivate + - --exclude=Bash,.venv,__pycache__ + pass_filenames: false + + # =========================================================================== + # AUTOFLAKE - Remove unused imports/variables + # =========================================================================== + - repo: https://github.com/PyCQA/autoflake + rev: v2.3.1 + hooks: + - id: autoflake + args: + - --in-place + - --remove-all-unused-imports + - --remove-unused-variables + - --remove-duplicate-keys + - --expand-star-imports + + # =========================================================================== + # SAFETY - Check for security vulnerabilities in dependencies + # =========================================================================== + # Note: Safety requires API key for full functionality, disabled by default + # - repo: https://github.com/Lucas-C/pre-commit-hooks-safety + # rev: v1.3.2 + # hooks: + # - id: python-safety-dependencies-check + # files: requirements.*\.txt$ + + # =========================================================================== + # PYRIGHT - Microsoft's type checker (very strict, optional) + # =========================================================================== + # Uncomment to enable - can be slow and very strict + # - repo: https://github.com/RobertCraiworthy/pyright-action + # rev: v1.1.350 + # hooks: + # - id: pyright + + # =========================================================================== + # FLAKE8 - Traditional linter (supplementary to ruff) + # =========================================================================== + - repo: https://github.com/PyCQA/flake8 + rev: 7.1.1 + hooks: + - id: flake8 + args: + - --max-line-length=88 + - --extend-ignore=E203,W503 + - --max-complexity=10 + - --statistics + additional_dependencies: + - flake8-bugbear + - flake8-comprehensions + - flake8-simplify + - flake8-print + exclude: ^(Bash/|\.venv/) + + # =========================================================================== + # CHECK JSON/YAML/TOML formatting + # =========================================================================== + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v4.0.0-alpha.8 + hooks: + - id: prettier + types_or: [yaml, json, markdown] + exclude: ^(Bash/|\.venv/|.*\.lock$) + + # =========================================================================== + # SHELLCHECK - Shell script linting + # =========================================================================== + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.10.0.1 + hooks: + - id: shellcheck + args: [--severity=warning] + + # =========================================================================== + # COMMITIZEN - Conventional commits (optional) + # =========================================================================== + # - repo: https://github.com/commitizen-tools/commitizen + # rev: v3.13.0 + # hooks: + # - id: commitizen + # - id: commitizen-branch + # stages: [push] diff --git a/.vscode/settings.json b/.vscode/settings.json index 4f0087b..bdd0151 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { - "files.associations": { - "*.py": "python", - "stdio.h": "c" - } -} \ No newline at end of file + "files.associations": { + "*.py": "python", + "stdio.h": "c" + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 31bb0f7..4af929b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,64 +1,62 @@ { - "version": "2.0.0", - "tasks": [ - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "problemMatcher": [ - "$pytest" - ], - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "isBackground": false, - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "isBackground": false, - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - }, - { - "label": "pytest quick", - "type": "shell", - "command": "python -m pip install -r requirements.txt && pytest -q", - "group": "build" - } - ] -} \ No newline at end of file + "version": "2.0.0", + "tasks": [ + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "problemMatcher": ["$pytest"], + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "isBackground": false, + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "isBackground": false, + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + } + ] +} diff --git a/Bash/ffmpeg-build/FFmpeg b/Bash/ffmpeg-build/FFmpeg new file mode 160000 index 0000000..0bc54cd --- /dev/null +++ b/Bash/ffmpeg-build/FFmpeg @@ -0,0 +1 @@ +Subproject commit 0bc54cddb1050c3c55bc65adbd3c8aa90d7eb457 diff --git a/C/.clang-tidy b/C/.clang-tidy index 2b24a7a..0aa3fd4 100644 --- a/C/.clang-tidy +++ b/C/.clang-tidy @@ -14,5 +14,5 @@ WarningsAsErrors: > cert-err33-c, cert-err34-c, cert-fio38-c -HeaderFilterRegex: '.*' +HeaderFilterRegex: ".*" FormatStyle: none diff --git a/C/.gitignore b/C/.gitignore index 4a2bd1a..ba0a89c 100644 --- a/C/.gitignore +++ b/C/.gitignore @@ -1 +1 @@ -random_engine \ No newline at end of file +random_engine diff --git a/C/1dvelocitysimulator/main.c b/C/1dvelocitysimulator/main.c index 0a9ee10..5b0c967 100644 --- a/C/1dvelocitysimulator/main.c +++ b/C/1dvelocitysimulator/main.c @@ -1,168 +1,168 @@ -#include -#include -#include -#include -#define LINE_LENGTH 100 - -void C() -{ - printf("\nCheck\n"); - return; -} - -void printAcceleration(int acceleration) -{ - printf("The value of acceleration is: %d\n", acceleration); - system("PAUSE"); - return; -} - -void pauseSystem() { system("PAUSE"); } - -void clearScreen() -{ - system("CLS"); - return; -} - -void pauseForASecond() -{ - Sleep(1000); - return; -} - -void pauseForGivenTime(float given_time) -{ - Sleep(fabs(given_time * 1000)); - return; -} - -float calculateVelocity(float starting_velocity, unsigned int physics_time, int *acceleration) -{ - return (*acceleration) * physics_time + starting_velocity; -} - -int calculateDisplacement(float starting_velocity, int *acceleration, unsigned int physics_time) -{ - return starting_velocity * physics_time + ((1 / 2) * (*acceleration) * (physics_time ^ 2)); -} - -void printXPosition(int position) -{ - printf("\nx position is: %d\n", position); - return; -} - -void printClock(unsigned int *time) -{ - printf("%d seconds passed\n", *time); - return; -} - -float calculateStopTime(float velocity) { return 1 / velocity; } - -void printLine(int position) -{ - clearScreen(); - for (int i = -(LINE_LENGTH / 2); i < LINE_LENGTH / 2; i++) - { - if (i == position) - printf("x"); - else - printf("-"); - } - return; -} - -void printVelocity(float velocity) -{ - printf("Velocity is: %f\n", velocity); - return; -} - -int calculateTimePassed(float velocity) -{ - if (velocity >= 1 || velocity <= -1) - return 1; - else - { - printf("Time passed is: %f\n", fabs(1 / velocity)); - return fabs(1 / velocity); - } -} - -void printAllInfo(int position, unsigned int *time, float *velocity) -{ - pauseForGivenTime(calculateStopTime(*velocity)); - printLine(position); - printXPosition(position); - *time += calculateTimePassed(*velocity); - printClock(time); - printVelocity(*velocity); - // pauseForASecond(); - return; -} - -float chooseVelocity() -{ - float velocity; - printf("Write velocity of the object in m / s: "); - scanf("%f", &velocity); - return velocity; -} - -int chooseAcceleration() -{ - int acceleration; - printf("Choose acceleration of the object in m / (s ^ 2):"); - scanf("%d", &acceleration); - return acceleration; -} - -int outOfLine(int position) -{ - if ((position < LINE_LENGTH / 2) && (position > -1 * (LINE_LENGTH / 2))) - { - return 0; - } - else - return 1; -} - -void moveUntillOutOfLine(int position, unsigned int *time) -{ - while (!outOfLine(position)) - { - float velocity = chooseVelocity(); - float *Pvelocity = &velocity; - position += calculateDisplacement(velocity, 0, 1); - printAllInfo(position, time, Pvelocity); - } - return; -} - -void moveUntillOutOfVelocity(int position, int *acceleration, unsigned int *time) -{ - float velocity = 0; - float *Pvelocity = &velocity; - while (!outOfLine(position)) - { - position += calculateDisplacement(velocity, acceleration, 1); - printXPosition(position); - pauseSystem(); - velocity = calculateVelocity(velocity, 1, acceleration); - printAllInfo(position, time, Pvelocity); - } - return; -} - -int main() -{ - int position = 0, acceleration = -1; - int *Pacceleration = &acceleration; - unsigned int time = 0; - unsigned int *Ptime = &time; - moveUntillOutOfLine(position, Ptime); - // moveUntillOutOfVelocity(position, Pacceleration, Ptime); - return 0; -} +#include +#include +#include +#include +#define LINE_LENGTH 100 + +void C() +{ + printf("\nCheck\n"); + return; +} + +void printAcceleration(int acceleration) +{ + printf("The value of acceleration is: %d\n", acceleration); + system("PAUSE"); + return; +} + +void pauseSystem() { system("PAUSE"); } + +void clearScreen() +{ + system("CLS"); + return; +} + +void pauseForASecond() +{ + Sleep(1000); + return; +} + +void pauseForGivenTime(float given_time) +{ + Sleep(fabs(given_time * 1000)); + return; +} + +float calculateVelocity(float starting_velocity, unsigned int physics_time, int *acceleration) +{ + return (*acceleration) * physics_time + starting_velocity; +} + +int calculateDisplacement(float starting_velocity, int *acceleration, unsigned int physics_time) +{ + return starting_velocity * physics_time + ((1 / 2) * (*acceleration) * (physics_time ^ 2)); +} + +void printXPosition(int position) +{ + printf("\nx position is: %d\n", position); + return; +} + +void printClock(unsigned int *time) +{ + printf("%d seconds passed\n", *time); + return; +} + +float calculateStopTime(float velocity) { return 1 / velocity; } + +void printLine(int position) +{ + clearScreen(); + for (int i = -(LINE_LENGTH / 2); i < LINE_LENGTH / 2; i++) + { + if (i == position) + printf("x"); + else + printf("-"); + } + return; +} + +void printVelocity(float velocity) +{ + printf("Velocity is: %f\n", velocity); + return; +} + +int calculateTimePassed(float velocity) +{ + if (velocity >= 1 || velocity <= -1) + return 1; + else + { + printf("Time passed is: %f\n", fabs(1 / velocity)); + return fabs(1 / velocity); + } +} + +void printAllInfo(int position, unsigned int *time, float *velocity) +{ + pauseForGivenTime(calculateStopTime(*velocity)); + printLine(position); + printXPosition(position); + *time += calculateTimePassed(*velocity); + printClock(time); + printVelocity(*velocity); + // pauseForASecond(); + return; +} + +float chooseVelocity() +{ + float velocity; + printf("Write velocity of the object in m / s: "); + scanf("%f", &velocity); + return velocity; +} + +int chooseAcceleration() +{ + int acceleration; + printf("Choose acceleration of the object in m / (s ^ 2):"); + scanf("%d", &acceleration); + return acceleration; +} + +int outOfLine(int position) +{ + if ((position < LINE_LENGTH / 2) && (position > -1 * (LINE_LENGTH / 2))) + { + return 0; + } + else + return 1; +} + +void moveUntillOutOfLine(int position, unsigned int *time) +{ + while (!outOfLine(position)) + { + float velocity = chooseVelocity(); + float *Pvelocity = &velocity; + position += calculateDisplacement(velocity, 0, 1); + printAllInfo(position, time, Pvelocity); + } + return; +} + +void moveUntillOutOfVelocity(int position, int *acceleration, unsigned int *time) +{ + float velocity = 0; + float *Pvelocity = &velocity; + while (!outOfLine(position)) + { + position += calculateDisplacement(velocity, acceleration, 1); + printXPosition(position); + pauseSystem(); + velocity = calculateVelocity(velocity, 1, acceleration); + printAllInfo(position, time, Pvelocity); + } + return; +} + +int main() +{ + int position = 0, acceleration = -1; + int *Pacceleration = &acceleration; + unsigned int time = 0; + unsigned int *Ptime = &time; + moveUntillOutOfLine(position, Ptime); + // moveUntillOutOfVelocity(position, Pacceleration, Ptime); + return 0; +} diff --git a/C/fps/README.md b/C/fps/README.md index d89404a..d11a890 100644 --- a/C/fps/README.md +++ b/C/fps/README.md @@ -1,6 +1,7 @@ # Simple OpenGL FPS (C + FreeGLUT) A tiny first-person demo using legacy OpenGL (compat) and FreeGLUT: + - Move with WASD, hold Tab or Q to sprint - Aim with mouse (captured by default). Press M to toggle capture - Shoot with Left Mouse or Space. Hit the red cube to score; it respawns @@ -24,9 +25,11 @@ make -C C/fps run ``` If your distro uses different package names, install the equivalents of: + - libgl1, libglu1, freeglut (dev headers) ## Notes + - This uses old-school fixed-function OpenGL for simplicity and broad compatibility. - Mouse is confined via glutWarpPointer; press M if you need to release it. - SDL2 is used only for simple procedurally generated sound effects (shoot, hit, game over). diff --git a/C/imageViewer/.vscode/settings.json b/C/imageViewer/.vscode/settings.json index 80ef480..631ffc4 100644 --- a/C/imageViewer/.vscode/settings.json +++ b/C/imageViewer/.vscode/settings.json @@ -11,21 +11,21 @@ "C_Cpp.default.compilerPath": "/usr/bin/gcc", "C_Cpp.clang_format_style": "file", "C_Cpp.clang_format_fallbackStyle": "LLVM", - + "files.associations": { "*.h": "c", "*.c": "c" }, - + "editor.formatOnSave": true, "editor.tabSize": 4, "editor.insertSpaces": true, "editor.rulers": [100], - + "clang-tidy.executable": "clang-tidy", "clang-tidy.checks": [ "clang-diagnostic-*", - "clang-analyzer-*", + "clang-analyzer-*", "bugprone-*", "cert-*", "misc-*", @@ -33,7 +33,7 @@ "portability-*", "readability-*" ], - + "cppcheck.enable": true, "cppcheck.standard": ["c99"], "cppcheck.suppress": [ diff --git a/C/imageViewer/SECURITY.md b/C/imageViewer/SECURITY.md index 0c65d3d..2de4a61 100644 --- a/C/imageViewer/SECURITY.md +++ b/C/imageViewer/SECURITY.md @@ -7,7 +7,7 @@ The imageviewer project uses secure coding practices with proper bounds checking ### Why These Warnings Appear The static analyzer flags standard C library functions like: -- `memcpy()` - suggests using `memcpy_s()` +- `memcpy()` - suggests using `memcpy_s()` - `snprintf()` - suggests using `snprintf_s()` - `strncpy()` - suggests using `strncpy_s()` diff --git a/C/imageViewer/install_arch.sh b/C/imageViewer/install_arch.sh index f983792..5ef01be 100755 --- a/C/imageViewer/install_arch.sh +++ b/C/imageViewer/install_arch.sh @@ -112,16 +112,16 @@ install_dependencies() { build_imageviewer() { print_step "Building imageviewer..." - + # Check if we're in the right directory if [[ ! -f "main.c" ]] || [[ ! -f "Makefile" ]]; then print_error "main.c or Makefile not found. Please run this script from the imageViewer directory." exit 1 fi - + # Clean any previous builds make clean 2>/dev/null || true - + # Build the project if make; then print_success "Build completed successfully" @@ -129,35 +129,35 @@ build_imageviewer() { print_error "Build failed" exit 1 fi - + # Verify the binary was created if [[ ! -f "imageviewer" ]]; then print_error "imageviewer binary not found after build" exit 1 fi - + print_success "imageviewer binary created" } install_binary() { print_step "Installing imageviewer to ${INSTALL_DIR}..." - + # Create install directory if it doesn't exist sudo mkdir -p "${INSTALL_DIR}" - + # Copy the binary sudo cp imageviewer "${INSTALL_DIR}/" sudo chmod +x "${INSTALL_DIR}/imageviewer" - + print_success "imageviewer installed to ${INSTALL_DIR}/imageviewer" } create_desktop_entry() { print_step "Creating desktop entry..." - + # Create applications directory if it doesn't exist sudo mkdir -p "${DESKTOP_FILE_DIR}" - + # Create desktop file sudo tee "${DESKTOP_FILE_DIR}/imageviewer.desktop" > /dev/null << EOF [Desktop Entry] @@ -179,10 +179,10 @@ EOF create_simple_icon() { print_step "Creating application icon..." - + # Create icon directory if it doesn't exist sudo mkdir -p "${ICON_DIR}" - + # Create a simple text-based icon (SVG) sudo tee "${ICON_DIR}/imageviewer.svg" > /dev/null << 'EOF' @@ -201,7 +201,7 @@ EOF update_desktop_database() { print_step "Updating desktop database..." - + if command -v update-desktop-database &> /dev/null; then sudo update-desktop-database "${DESKTOP_FILE_DIR}" 2>/dev/null || true print_success "Desktop database updated" @@ -212,11 +212,11 @@ update_desktop_database() { set_default_image_viewer() { print_step "Setting imageviewer as default image viewer..." - + # List of MIME types for images local mime_types=( "image/jpeg" - "image/jpg" + "image/jpg" "image/png" "image/bmp" "image/gif" @@ -224,34 +224,34 @@ set_default_image_viewer() { "image/tif" "image/webp" ) - + # Set default application for each MIME type for mime_type in "${mime_types[@]}"; do if command -v xdg-mime &> /dev/null; then xdg-mime default imageviewer.desktop "$mime_type" 2>/dev/null || true fi done - + # Also update MIME database if available if command -v update-mime-database &> /dev/null; then sudo update-mime-database /usr/share/mime 2>/dev/null || true fi - + print_success "imageviewer set as default image viewer" } test_installation() { print_step "Testing installation..." - + # Check if binary is in PATH if command -v imageviewer &> /dev/null; then print_success "imageviewer is available in PATH" - + # Show version/help echo -e "${BLUE}Running imageviewer --help equivalent:${NC}" echo "Usage: imageviewer " echo "Supported formats: JPG, JPEG, PNG, BMP, GIF, TIF" - + # Test default application association if command -v xdg-mime &> /dev/null; then local default_app=$(xdg-mime query default image/jpeg 2>/dev/null) @@ -261,7 +261,7 @@ test_installation() { print_warning "Default image viewer association may not have been set correctly" fi fi - + else print_error "imageviewer not found in PATH. Installation may have failed." exit 1 @@ -311,10 +311,10 @@ main() { echo -e "${BLUE}ImageViewer Installation Script for Arch Linux${NC}" echo "==============================================" echo - + check_arch check_permissions - + # Show what the script will do echo -e "${YELLOW}This script will:${NC}" echo " 1. Install SDL2 dependencies via pacman" @@ -323,7 +323,7 @@ main() { echo " 4. Create a desktop entry" echo " 5. Set imageviewer as default image viewer" echo - + install_dependencies build_imageviewer install_binary diff --git a/C/imageViewer/lint.sh b/C/imageViewer/lint.sh index 8a095d1..6bd2613 100755 --- a/C/imageViewer/lint.sh +++ b/C/imageViewer/lint.sh @@ -1,5 +1,5 @@ -# Lint script for imageViewer project #!/bin/bash +# Lint script for imageViewer project set -e @@ -29,25 +29,25 @@ print_error() { # Check if required tools are installed check_tools() { print_step "Checking required tools..." - + local missing_tools=() - + if ! command -v clang-tidy &> /dev/null; then missing_tools+=("clang-tidy") fi - + if ! command -v cppcheck &> /dev/null; then missing_tools+=("cppcheck") fi - + if ! command -v clang-format &> /dev/null; then missing_tools+=("clang-format") fi - + if [ ${#missing_tools[@]} -ne 0 ]; then print_error "Missing required tools: ${missing_tools[*]}" print_step "Installing missing tools..." - + # Check if we're on Arch Linux if command -v pacman &> /dev/null; then sudo pacman -S --needed clang cppcheck @@ -60,14 +60,14 @@ check_tools() { exit 1 fi fi - + print_success "All required tools are available" } # Run clang-tidy run_clang_tidy() { print_step "Running clang-tidy analysis..." - + if [ -f ".clang-tidy" ]; then clang-tidy main.c -- -I/usr/include/SDL2 -D_REENTRANT 2>/dev/null || { print_warning "clang-tidy found issues (see output above)" @@ -78,26 +78,26 @@ run_clang_tidy() { print_warning "clang-tidy found issues (see output above)" } fi - + print_success "clang-tidy analysis completed" } # Run cppcheck run_cppcheck() { print_step "Running cppcheck analysis..." - + cppcheck --enable=all --check-level=exhaustive --suppress=missingIncludeSystem \ --quiet --std=c23 main.c || { print_warning "cppcheck found issues (see output above)" } - + print_success "cppcheck analysis completed" } # Check code formatting check_formatting() { print_step "Checking code formatting..." - + if [ -f ".clang-format" ]; then if clang-format --dry-run --Werror main.c 2>/dev/null; then print_success "Code formatting is correct" @@ -113,7 +113,7 @@ check_formatting() { # Run basic compile check compile_check() { print_step "Running compile check..." - + # Try to compile with extra warnings if gcc -Wall -Wextra -Wpedantic -std=c99 -O2 \ $(pkg-config --cflags sdl2 2>/dev/null || echo "-I/usr/include/SDL2") \ @@ -132,27 +132,27 @@ compile_check() { # Check for common C issues check_common_issues() { print_step "Checking for common C issues..." - + local issues=0 - + # Check for TODO/FIXME comments if grep -n "TODO\|FIXME\|XXX\|HACK" main.c 2>/dev/null; then print_warning "Found TODO/FIXME comments" issues=$((issues + 1)) fi - + # Check for potential buffer overflows if grep -n "strcpy\|strcat\|sprintf\|gets" main.c 2>/dev/null; then print_warning "Found potentially unsafe string functions" issues=$((issues + 1)) fi - + # Check for magic numbers (basic check) if grep -E "\b[0-9]{3,}\b" main.c | grep -v "printf\|#define" 2>/dev/null; then print_warning "Found potential magic numbers" issues=$((issues + 1)) fi - + if [ $issues -eq 0 ]; then print_success "No common issues found" fi @@ -163,31 +163,31 @@ main() { echo -e "${BLUE}C Language Linter for imageViewer Project${NC}" echo "==========================================" echo - + # Check if we're in the right directory if [ ! -f "main.c" ]; then print_error "main.c not found. Please run this script from the imageViewer directory." exit 1 fi - + check_tools echo - + compile_check echo - + run_clang_tidy echo - + run_cppcheck echo - + check_formatting echo - + check_common_issues echo - + print_success "Linting completed!" echo echo -e "${BLUE}Available commands:${NC}" diff --git a/C/imageViewer/main.c b/C/imageViewer/main.c index 35f32fd..4ef169b 100644 --- a/C/imageViewer/main.c +++ b/C/imageViewer/main.c @@ -1415,4 +1415,4 @@ static int save_processed_image(const ImageViewer *viewer) { printf("Saved %s image: %s\n", any_trim ? "trimmed" : "rotated", out_path); } return 1; -} \ No newline at end of file +} diff --git a/C/imageViewer/uninstall_arch.sh b/C/imageViewer/uninstall_arch.sh index e993cfc..8780e31 100755 --- a/C/imageViewer/uninstall_arch.sh +++ b/C/imageViewer/uninstall_arch.sh @@ -34,7 +34,7 @@ print_error() { remove_files() { print_step "Removing imageviewer files..." - + # Remove binary if [[ -f "${INSTALL_DIR}/imageviewer" ]]; then sudo rm "${INSTALL_DIR}/imageviewer" @@ -42,7 +42,7 @@ remove_files() { else print_warning "Binary not found at ${INSTALL_DIR}/imageviewer" fi - + # Remove desktop entry if [[ -f "${DESKTOP_FILE_DIR}/imageviewer.desktop" ]]; then sudo rm "${DESKTOP_FILE_DIR}/imageviewer.desktop" @@ -50,7 +50,7 @@ remove_files() { else print_warning "Desktop entry not found" fi - + # Remove icon if [[ -f "${ICON_DIR}/imageviewer.svg" ]]; then sudo rm "${ICON_DIR}/imageviewer.svg" @@ -62,11 +62,11 @@ remove_files() { reset_default_associations() { print_step "Resetting default image viewer associations..." - + # List of MIME types for images local mime_types=( "image/jpeg" - "image/jpg" + "image/jpg" "image/png" "image/bmp" "image/gif" @@ -74,7 +74,7 @@ reset_default_associations() { "image/tif" "image/webp" ) - + # Reset default application for each MIME type for mime_type in "${mime_types[@]}"; do if command -v xdg-mime &> /dev/null; then @@ -89,13 +89,13 @@ reset_default_associations() { fi fi done - + print_success "Default image viewer associations reset" } update_desktop_database() { print_step "Updating desktop database..." - + if command -v update-desktop-database &> /dev/null; then sudo update-desktop-database "${DESKTOP_FILE_DIR}" 2>/dev/null || true print_success "Desktop database updated" @@ -108,7 +108,7 @@ main() { echo -e "${BLUE}ImageViewer Uninstallation Script${NC}" echo "=================================" echo - + # Show what will be removed echo -e "${YELLOW}This script will remove:${NC}" echo " - ${INSTALL_DIR}/imageviewer" @@ -117,11 +117,11 @@ main() { echo echo -e "${YELLOW}Note: Dependencies (SDL2 libraries) will NOT be removed.${NC}" echo - + remove_files reset_default_associations update_desktop_database - + echo echo -e "${GREEN}ImageViewer has been successfully uninstalled!${NC}" echo diff --git a/C/lichess_random_engine/micro_max.c b/C/lichess_random_engine/micro_max.c index fa410f0..7806d1f 100644 --- a/C/lichess_random_engine/micro_max.c +++ b/C/lichess_random_engine/micro_max.c @@ -747,4 +747,4 @@ int main(int argc, char **argv) printf("}\n"); free(arr); return 0; -} \ No newline at end of file +} diff --git a/C/lichess_random_engine/movegen.h b/C/lichess_random_engine/movegen.h index c60814b..d91973a 100644 --- a/C/lichess_random_engine/movegen.h +++ b/C/lichess_random_engine/movegen.h @@ -69,4 +69,4 @@ int in_check(const Position *pos, Color side); int gen_moves(const Position *pos, Move *moves, int max_moves, int captures_only); int gen_moves_pseudo(const Position *pos, Move *moves, int max_moves, int captures_only); -#endif // MOVEGEN_H \ No newline at end of file +#endif // MOVEGEN_H diff --git a/C/lichess_random_engine/search.h b/C/lichess_random_engine/search.h index b707b4d..b943946 100644 --- a/C/lichess_random_engine/search.h +++ b/C/lichess_random_engine/search.h @@ -21,4 +21,4 @@ int evaluate(const Position *pos); // Negamax alpha-beta returning score in centipawns from side-to-move perspective. int alphabeta(Position pos, int depth, int alpha, int beta, PrincipalVariation *pv); -#endif // SEARCH_H \ No newline at end of file +#endif // SEARCH_H diff --git a/C/misc/generatingWordsEndingWIthalka.c b/C/misc/generatingWordsEndingWIthalka.c index 1a417bb..df443e6 100644 --- a/C/misc/generatingWordsEndingWIthalka.c +++ b/C/misc/generatingWordsEndingWIthalka.c @@ -1,12 +1,12 @@ -#include - -const int NUMBER_FOR_POLISH_SMALL_L = 136; - -int main(void) -{ - for (char i = 'a'; i < 'z' + 1; ++i) - { - printf("%ca%cka\n", i, NUMBER_FOR_POLISH_SMALL_L); - } - return 0; -} +#include + +const int NUMBER_FOR_POLISH_SMALL_L = 136; + +int main(void) +{ + for (char i = 'a'; i < 'z' + 1; ++i) + { + printf("%ca%cka\n", i, NUMBER_FOR_POLISH_SMALL_L); + } + return 0; +} diff --git a/C/misc/randomJPG/.gitignore b/C/misc/randomJPG/.gitignore new file mode 100644 index 0000000..c35775a --- /dev/null +++ b/C/misc/randomJPG/.gitignore @@ -0,0 +1,4 @@ +*.jpeg +*.jpg +generated_images*/* +generate_images diff --git a/C/misc/randomJPG/Readme.md b/C/misc/randomJPG/Readme.md new file mode 100644 index 0000000..508191d --- /dev/null +++ b/C/misc/randomJPG/Readme.md @@ -0,0 +1,6 @@ +Did you ever need to generate random jpg images with huge file size? Now you can! \\ +Compilation: Install libjpeg-dev \\ +sudo apt-get install libjpeg-dev \\ +Run make \\ +make \\ +Run ./generate_images diff --git a/C/misc/split/.gitignore b/C/misc/split/.gitignore new file mode 100644 index 0000000..075ea8d --- /dev/null +++ b/C/misc/split/.gitignore @@ -0,0 +1 @@ +split diff --git a/C/opening_learner/README.md b/C/opening_learner/README.md index 31176c2..33ce054 100644 --- a/C/opening_learner/README.md +++ b/C/opening_learner/README.md @@ -18,9 +18,11 @@ Run: ``` Tips: + - ESC clears selection. - Press `m` to cycle to a stored mistake position and practice the best move there. - If you play Black, the board flips so Black is at the bottom. Notes: + - Rendering avoids TTF dependency; pieces are clear, high-contrast geometric glyphs. diff --git a/C/scrapeWebsite/.gitignore b/C/scrapeWebsite/.gitignore new file mode 100644 index 0000000..76fcb53 --- /dev/null +++ b/C/scrapeWebsite/.gitignore @@ -0,0 +1,65 @@ +# JPEG +*.jpg +*.jpeg +*.jpe +*.jif +*.jfif +*.jfi + +# JPEG 2000 +*.jp2 +*.j2k +*.jpf +*.jpx +*.jpm +*.mj2 + +# JPEG XR +*.jxr +*.hdp +*.wdp + +# Graphics Interchange Format +*.gif + +# RAW +*.raw + +# Web P +*.webp + +# Portable Network Graphics +*.png + +# Animated Portable Network Graphics +*.apng + +# Multiple-image Network Graphics +*.mng + +# Tagged Image File Format +*.tiff +*.tif + +# Scalable Vector Graphics +*.svg +*.svgz + +# Portable Document Format +*.pdf + +# X BitMap +*.xbm + +# BMP +*.bmp +*.dib + +# ICO +*.ico + +# 3D Images +*.3dm +*.max + +scrape diff --git a/C/tests/generatingPolishLettersOnWindowsTerminal.c b/C/tests/generatingPolishLettersOnWindowsTerminal.c index a85765c..d69be9a 100644 --- a/C/tests/generatingPolishLettersOnWindowsTerminal.c +++ b/C/tests/generatingPolishLettersOnWindowsTerminal.c @@ -1,10 +1,10 @@ -#include -#include -#include - -int main() -{ - printf("Henlo\n"); - sleep(20); - return 0; -} +#include +#include +#include + +int main() +{ + printf("Henlo\n"); + sleep(20); + return 0; +} diff --git a/CPP/SFMLEngine/readme.md b/CPP/SFMLEngine/readme.md new file mode 100644 index 0000000..7d43375 --- /dev/null +++ b/CPP/SFMLEngine/readme.md @@ -0,0 +1,5 @@ +# Textures and Audio used: + +## Space Game Starter Set by [hc](https://opengameart.org/users/hc) - https://opengameart.org/content/space-game-starter-set + +## 100 seamless textures by [Mitch Featherston](http://pdtextures.blogspot.com/) (Submitted by [Clint Bellanger](https://opengameart.org/users/clint-bellanger) - https://opengameart.org/content/100-seamless-textures diff --git a/CPP/miscelanious/Pi/main.cpp b/CPP/miscelanious/Pi/main.cpp new file mode 100644 index 0000000..9c91a24 --- /dev/null +++ b/CPP/miscelanious/Pi/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +const unsigned long long int ITERATIONS = 10000; + +long double getPi() +{ + long double pi = 4; + bool negative = 1; + for(unsigned int i = 3; i < ITERATIONS; i += 2) + { + if(negative) pi -= 4.0 / i; + else pi += 4.0 / i; + negative = !negative; + } + std::cout << std::setprecision(2000) << pi << std::endl; + return pi; +} + +int main() +{ + getPi(); + return 0; +} diff --git a/CPP/miscelanious/brydz/brydz.cpp b/CPP/miscelanious/brydz/brydz.cpp new file mode 100644 index 0000000..b58b483 --- /dev/null +++ b/CPP/miscelanious/brydz/brydz.cpp @@ -0,0 +1,410 @@ +#include +#include + +const std::vector ATUTY = {"BA", "Trefl", "Karo", "Kier", "Pik"}; +const bool A_ID = 0; +const bool B_ID = 1; +const std::vector GRACZE = {}; +const std::vector PO_PARTII {"Nikt", GRACZE[A_ID], GRACZE[B_ID], "Obaj Gracze"}; +const int DOMYSLNE_LEWY = 6; +const int BEZ_ATUTU_ID = 1; +const int TREFL_ID = 2; +const int KARO_ID = 3; +const int KIER_ID = 4; +const int PIK_ID = 5; +const int SZLEMIK = 6; +const int SZLEM = 7; +const int CYKL_PO_PARTII = 4; +const int MAKSYMALNY_LEW = 7; +const int MINIMALNY_LEW = 1; +const int ILOSC_LEW = 13; + +void print(const std::string s) +{ + std::cout << s << std::endl; +} + +void tabela(std::vector punktyA, std::vector punktyB) +{ + + std::cout << "Numer Gry" << " Po Partii" << " " << GRACZE[A_ID] << " " << GRACZE[B_ID] << std::endl; + for(int i = 0; i < punktyA.size(); i++) + { + + std::cout << i + 1 << " " << PO_PARTII[i % CYKL_PO_PARTII] << " " << punktyA[i] << " " << punktyB[i] << std::endl; + } +} + +void lwyAtut(int lwy, int atut) +{ + if(lwy == SZLEMIK) + { + print("Wybrano szlemik!"); + return; + } + if(lwy == SZLEM) + { + print("Wybrano szlema!"); + return; + } + std::cout << "Wybrano kontrakt: " << lwy << " " << ATUTY[atut - 1] << std::endl; +} + +int zagraneLwy() +{ + int lwy; + bool flagaLwy; + do + { + flagaLwy = 0; + print("Ile lew?"); + char lwyC; + std::cin >> lwyC; + lwy = lwyC - '0'; + if(lwy < MINIMALNY_LEW) + { + print("Podales za malo lew!"); + flagaLwy = 1; + } + + if(lwy > MAKSYMALNY_LEW) + { + print("Podales za duzo lew!"); + flagaLwy = 1; + } + }while(flagaLwy); + return lwy; +} + +int zagranyAtut(int lwy) +{ + int atut; + bool flagaAtut; + if(lwy > 6) return 1; + do + { + flagaAtut = 0; + print("Jaki atut?"); + print("1 - BA"); + print("2 - Trefl"); + print("3 - Karo"); + print("4 - Kier"); + print("5 - Pik"); + char atutC; + std::cin >> atutC; + atut = atutC - '0'; + if(atut < 1 || atut > 5) + { + print("Wybrales zla liczbe!"); + flagaAtut = 1; + } + }while(flagaAtut); + return atut; +} + +bool zagranaKontra() +{ + char kontraC = '0'; + print("Czy zostala zagrana kontra?"); + print("1 - TAK"); + print("0 - NIE"); + std::cin >> kontraC; + bool kontraBool = kontraC - '0'; + return kontraBool; +} + +bool zagranaRekontra() +{ + char rekontraC = '0'; + print("Czy zostala zagrana rekontra?"); + print("1 - TAK"); + print("0 - NIE"); + std::cin >> rekontraC; + bool rekontraBool = rekontraC - '0'; + return rekontraBool; +} + +void stanGry(int lwy, int atut, bool kontraBool, bool rekontraBool, int ktoraGra, int ktoKontrakt) +{ + std::cout << "Kontrakt Wygrali: " << GRACZE[ktoKontrakt] << std::endl; + lwyAtut(lwy, atut); + if(kontraBool) + { + if(rekontraBool) print("Zostala zagrana REkontra!"); + else print("Zostala zagrana Kontra!"); + } + std::cout << "Po partii sa: " << PO_PARTII[ktoraGra % 4] << std::endl; +} + +int ktoKontrakt() +{ + char ktoKontraktC; + print("Kto wygral Kontrakt?"); + std::cout << "1. " << GRACZE[A_ID] << std::endl; + std::cout << "2. " << GRACZE[B_ID] << std::endl; + std::cin >> ktoKontraktC; + int ktoKontraktI = ktoKontraktC - '1'; + std::cout << "ktoKontraktI " << ktoKontraktI; + return ktoKontraktI; +} + +int ileWpadek() +{ + std::string ileWpadekS; + print("ile lew wygrali obroncy?"); + std::cin >> ileWpadekS; + int ileWpadek = stoi(ileWpadekS); + return ileWpadek; +} + +void punkty(std::vector &punktyA, std::vector &punktyB, int lwy, int atut, bool kontraBool, bool rekontraBool, +int ktoraGra, int ktoKontraktI, bool rozgrywajacyWygral, int wpadki) +{ + int sumaPunktow = 0; + if(rozgrywajacyWygral) + { + int zdobyteLewy = ILOSC_LEW - wpadki - DOMYSLNE_LEWY; + int nadrobki = zdobyteLewy - lwy; + int punktyZaLew; + std::cout << "wartosc kontraBool: " << kontraBool << "; wartosc rekontraBool: " << rekontraBool << std::endl; + + // Lewy Deklarowane + if(atut == TREFL_ID || atut == KARO_ID) + { + print("kontrakt TREFL lub KARO kazda karta kontraktowa za 20"); + punktyZaLew = 20; + if(kontraBool) + { + print("kontra TREFL lub KARO, kazda karta kontraktowa za 40"); + punktyZaLew = 40; + } + if(rekontraBool) + { + print("rekontra TREFL lub KARO, kazda karta kontraktowa za 80"); + punktyZaLew = 80; + } + + std::cout << "Ilosc lew w kontrakcie: " << lwy << " do punktow dodaje sie " << lwy * punktyZaLew << std::endl; + sumaPunktow += (lwy * punktyZaLew); + } + + if(atut == KIER_ID || atut == PIK_ID) + { + print("kontrakt KIER lub PIK, kazda kontraktowa 30"); + punktyZaLew = 30; + if(kontraBool) + { + print("kontra KIER lub PIK, kazda kontraktowa za 60"); + punktyZaLew = 60; + } + if(rekontraBool) + { + print("rekontra KIER lub PIK, kazda kontraktowa za 120"); + punktyZaLew = 120; + } + + std::cout << "Ilosc lew w kontrakcie: " << lwy << " do punktow dodaje sie " << lwy * punktyZaLew << std::endl; + sumaPunktow += (lwy * punktyZaLew); + } + + if(atut == BEZ_ATUTU_ID) + { + punktyZaLew = 30; + print("kontrakt BEZ_ATUTU, pierwsza lewa za 40, kazda nastepna za 30"); + sumaPunktow = 40; + if(kontraBool) + { + print("kontrakt BEZ_ATUTU, pierwsza lewa za 80, kazda nastepna za 60"); + sumaPunktow = 80; + punktyZaLew = 60; + } + if(rekontraBool) + { + print("kontrakt BEZ_ATUTU, pierwsza lewa za 160, kazda nastepna za 120"); + sumaPunktow = 160; + punktyZaLew = 120; + } + sumaPunktow += ( (lwy - 1) * punktyZaLew); + } + + bool czyRozgrywajacyPoPartii = ( ( (ktoraGra % CYKL_PO_PARTII) - 1) == ktoKontraktI || ktoraGra % CYKL_PO_PARTII == 3); + + if(lwy == SZLEMIK) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 750; + else sumaPunktow += 500; + } + + if(lwy == SZLEM) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 1500; + else sumaPunktow += 1000; + } + + + + bool dograna = (sumaPunktow >= 100); + if(dograna) + { + if(czyRozgrywajacyPoPartii) sumaPunktow += 500; + else sumaPunktow += 300; + }else sumaPunktow += 50; + + // Nadrobki + + if(!kontraBool && !rekontraBool) + { + int punktyZaNadrobki = punktyZaLew; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + if(kontraBool && !rekontraBool) + { + int punktyZaNadrobki = 100; + if(czyRozgrywajacyPoPartii) punktyZaNadrobki = 200; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + + if(kontraBool && rekontraBool) + { + + int punktyZaNadrobki = 200; + if(czyRozgrywajacyPoPartii) punktyZaNadrobki = 400; + sumaPunktow += nadrobki * punktyZaNadrobki; + } + + if(kontraBool && !rekontraBool) sumaPunktow += 50; + + if(kontraBool && rekontraBool) sumaPunktow += 100; + std::cout << "Rozgrywajacy zdobyl: " << sumaPunktow << std::endl; + if(ktoKontraktI == A_ID) + { + punktyA.push_back(sumaPunktow); + punktyB.push_back(0); + } + else + { + punktyB.push_back(sumaPunktow); + punktyA.push_back(0); + } + return; + }else + { + int zebraneLewy = ILOSC_LEW - wpadki; + int lewyWpadkowe = (lwy + DOMYSLNE_LEWY) - zebraneLewy; + int sumaPunktow = 0; + bool broniacyPoPartii = ( ((ktoraGra % CYKL_PO_PARTII) - 1) == !ktoKontraktI || ktoraGra % CYKL_PO_PARTII == 3); + if(broniacyPoPartii) + { + + if(!kontraBool && !rekontraBool) + { + sumaPunktow = 100; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 100; + else sumaPunktow += 0; + } + } + + if(kontraBool && !rekontraBool) + { + sumaPunktow = 200; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 300; + else sumaPunktow += 0; + } + } + + if(kontraBool && rekontraBool) + { + sumaPunktow = 400; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 600; + else sumaPunktow += 0; + } + } + }else + { + if(!kontraBool && !rekontraBool) + { + sumaPunktow = 50; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 50; + else sumaPunktow += 0; + } + } + + if(kontraBool && !rekontraBool) + { + sumaPunktow = 100; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 200; + else sumaPunktow += 100; + } + } + + if(kontraBool && rekontraBool) + { + sumaPunktow = 200; + for(int i = 1; i < lewyWpadkowe; i++) + { + if(i < 4) sumaPunktow += 400; + else sumaPunktow += 200; + } + } + } + std::cout << "Broniacy zdobyli: " << sumaPunktow << std::endl; + if(ktoKontraktI == A_ID) + { + punktyB.push_back(sumaPunktow); + punktyA.push_back(0); + } + else + { + punktyA.push_back(sumaPunktow); + punktyB.push_back(0); + } + return; + + } +} + + +bool gra() +{ + bool koniecGry = 0; + std::vector punktyA; + std::vector punktyB; + do{ + int ktoraGra = 0; + tabela(punktyA, punktyB); + int ktoKontraktI = ktoKontrakt(); + int lwy = zagraneLwy(); + int atut = zagranyAtut(lwy); + bool kontraBool = zagranaKontra(); + bool rekontraBool = 0; + if(kontraBool) rekontraBool = zagranaRekontra(); + stanGry(lwy, atut, kontraBool, rekontraBool, ktoraGra, ktoKontraktI); + int wpadki = ileWpadek(); + int zebraneLewy = ILOSC_LEW - wpadki; + + bool rozgrywajacyWygral = 1; + if( zebraneLewy >= lwy + DOMYSLNE_LEWY) rozgrywajacyWygral = 1; + else rozgrywajacyWygral = 0; + punkty(punktyA, punktyB, lwy, atut, kontraBool, rekontraBool, ktoraGra, ktoKontraktI, rozgrywajacyWygral, wpadki); + print("Czy koniec gry? 1 - TAK, 0 - NIE"); + std::cin >> koniecGry; + }while(!koniecGry); + tabela(punktyA, punktyB); + return 0; +} + +int main() +{ + while(gra()); + return 0; +} diff --git a/CPP/miscelanious/calculateShotsDarts/basic.cpp b/CPP/miscelanious/calculateShotsDarts/basic.cpp new file mode 100644 index 0000000..e41c8b0 --- /dev/null +++ b/CPP/miscelanious/calculateShotsDarts/basic.cpp @@ -0,0 +1,91 @@ +#ifndef BASIC_CPP +#define BASIC_CPP + +#include +#include +#include +#include + +void print(const std::string s) { std::cout << s << std::endl; } +void printErrorStringContainsNotNumber(const std::string s) +{ + std::cout << "string: \"" << s + << "\" contains character different than number " << std::endl; +} + +void printNumberTooLow(const int number, const int min) +{ + std::cout << "number: " << number + << " is too low. Minimal number is: " << min << std::endl; +} + +void printNumberTooHigh(const int number, const int max) +{ + std::cout << "number: " << number + << " is too high. Maximal number is: " << max << std::endl; +} + +void printNotValidStringLength(const std::string s, const int desiredLength) +{ + std::cout << "String: \"" << s << "\" is too short/too long, it is: " + << s.length() << " characters long but should be: " << desiredLength + << " characters long " << std::endl; +} + +void printInvalidCharacter(const char c, const char desiredCharacter) +{ + std::cout << "[ " << c << " ] Is invalid character, expected: [ " + << desiredCharacter << " ]" << std::endl; +} + +void printContainsIllegalCharacter( const std::string s, + const char illegalCharacter ) +{ + std::cout << "String: " << s << " consists of illegal sign: [" + << illegalCharacter << "]!" << std::endl; +} + + +bool numberTooLow(const int number, const int min) +{ + if(number < min) + { + printNumberTooLow(number, min); + return 1; + } + return 0; +} + +bool numberTooHigh(const int number, const int max) +{ + if(number > max) + { + printNumberTooHigh(number, max); + return 1; + } + return 0; +} + +bool containsIllegalCharacter(const std::string s, const char illegalCharacter) +{ + if( s.find(illegalCharacter) != std::string::npos) + { + printContainsIllegalCharacter(s, illegalCharacter); + return 1; + } + return 0; +} + + +void e() +{ + print("Poor man breakboint"); +} + + +bool charIsNumber(const char c) +{ + return c >= '0' && c <= '9'; +} + +#endif diff --git a/CPP/miscelanious/calculateShotsDarts/main.cpp b/CPP/miscelanious/calculateShotsDarts/main.cpp new file mode 100644 index 0000000..2834c11 --- /dev/null +++ b/CPP/miscelanious/calculateShotsDarts/main.cpp @@ -0,0 +1,89 @@ +#ifndef MAIN_CPP +#define MAIN_CPP + +#include +#include +#include +#include "basic.cpp" + +std::vector fillVector(const int min, const int max) +{ + std::vector newVector; + for(int i = min; i <= max; i++) + { + newVector.push_back(i); + } + return newVector; +} + +const int MAX_SPOT = 20; +const int MIN_SPOT = 1; +const std::vector NORMAL_POINTS = fillVector(MIN_SPOT, MAX_SPOT); + +std::vector multiplyVector(const std::vector v, int multiplyBy) +{ + std::vector newVector; + for(unsigned int i = 0; i < v.size(); i++) + { + newVector.push_back(v.at(i)*multiplyBy); + } + return newVector; +} + +const std::vector DOUBLE_POINTS = multiplyVector(NORMAL_POINTS, 2); +const std::vector TRIPLE_POINTS = multiplyVector(NORMAL_POINTS, 3); +const int MAX_ONE_HIT = TRIPLE_POINTS.at(TRIPLE_POINTS.size() - 1); +const int THROWS_IN_ONE_HIT = 3; +const int MAX_POINTS_TURN = THROWS_IN_ONE_HIT * MAX_ONE_HIT; +const int STARTING_POINTS = 501; +const int FINAL_POINTS = 0; + + +bool validString(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(!charIsNumber(s.at(i))) + { + printErrorStringContainsNotNumber(s); + return 0; + } + } + return 1; +} + +bool validNumberInput(const std::string input, const int min, const int max) +{ + if(!validString(input)) return 0; + int inputInt = std::stoi(input); + if(numberTooLow(inputInt, min)) return 0; + if(numberTooHigh(inputInt, max)) return 0; + return 1; +} + +bool validInput(const std::string s) +{ + if(s.length() > 3) return 0; + if(!validNumberInput(s, FINAL_POINTS, STARTING_POINTS)) return 0; + return 1; +} + +std::vector requiredShoots(const int pointsLeft) +{ + +} + + +int main() +{ + print("Enter points left: "); + std::string pointsLeft; + do{ + getline(std::cin, pointsLeft); + }while(!validInput(pointsLeft)); + int pointsLeftInt = std::stoi(pointsLeft); + requiredShoots(pointsLeftInt); + return 0; +} + +#endif diff --git a/CPP/miscelanious/findIntegerPercentageValue/main.cpp b/CPP/miscelanious/findIntegerPercentageValue/main.cpp new file mode 100644 index 0000000..0ff9355 --- /dev/null +++ b/CPP/miscelanious/findIntegerPercentageValue/main.cpp @@ -0,0 +1,60 @@ +#include +#include + +const int PERCENTAGE = 44; +const float PERCENTAGE_DENOMINATOR = 100; +const int NUMBERS_TO_CHECK = 200; +const bool DEBUG = 0; + +bool isInteger(const float number) +{ + return number == std::floor(number); +} + +void printIsInteger(const int i, const int inputPercentage, + const float calculatedPercentage) +{ + std::cout << i << "*" << inputPercentage << "% is an integer number: " + << i*calculatedPercentage << std::endl; +} + +void printDebug(const int i, const float calculatedPercentage) +{ + std::cout << "i = " << i << std::endl; + std::cout << i*calculatedPercentage << std::endl; +} + +void isIntegerLoop( const bool debugOn = DEBUG, + const int maxNumber = NUMBERS_TO_CHECK, + const int inputPercentage = PERCENTAGE ) +{ + float actualPercentage = inputPercentage / PERCENTAGE_DENOMINATOR; + for(int i = 1; i <= maxNumber; i++) + { + if(isInteger(i*actualPercentage)) + { + printIsInteger(i, inputPercentage, actualPercentage); + } + else if(debugOn) printDebug(i, actualPercentage); + } +} + +void printStartingMessage(const int maxNumber = NUMBERS_TO_CHECK, + const int inputPercentage = PERCENTAGE) +{ + std::cout << "For max number = " << maxNumber + << "; and a percentage: " << inputPercentage + << "; Found following integer numbers: " << std::endl; +} + +void mainFunctions() +{ + printStartingMessage(); + isIntegerLoop(); +} + +int main() +{ + mainFunctions(); + return 0; +} diff --git a/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp b/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp new file mode 100644 index 0000000..2e0d8c6 --- /dev/null +++ b/CPP/miscelanious/howManyValidISBNNumbersAreThere/11.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include + +#ifndef CHECK_ISBN_CPP +#define CHECK_ISBN_CPP + + +const bool DEBUG = 0; +const int ISBN_LENGTH = 10; +const int CHECK_NUMBER = 11; +const unsigned long long int HIGHEST_ISBN = 9999999999; + + +void printVector(std::vector v) +{ + for(unsigned int i = 0; i < v.size(); i++) + { + std::cout << v[i] << "; "; + } +} + +void print(const std::string printMe) +{ + std::cout << printMe << std::endl; +} + +void e() +{ + print("PRINT"); +} + +bool checkInput(const std::string input) +{ + if(input.length() != ISBN_LENGTH) + { + print("Your number is too short/too long"); + return 0; + } + for(int i = 0; i <= ISBN_LENGTH - 1; i++) + { + if(input.at(i) < '0' || input.at(i) > '9') + { + print("Your number consists of illegal characters"); + return 0; + } + } + return 1; +} + +std::vector stringToIntVector(const std::string input) +{ + std::vector vector; + for(int i = input.length() - 1; i >= 0; i--) + { + vector.push_back(input.at(i) - '0'); + } + return vector; +} + +std::vector userISBN() +{ + std::string input; + do{ + std::cout << "Enter the ISBN number (10 digits): "; + getline(std::cin, input); + }while(!checkInput(input)); + return stringToIntVector(input); +} + + + + +bool checkISBN(const std::vector isbn) +{ + int sum = 0, t = 0; + for(int i = 0; i < ISBN_LENGTH; i++) + { + t += isbn[i]; + sum += t; + } + + /*if(DEBUG) + { + if(!(sum % CHECK_NUMBER)) print("^^^ VALID NUMBER ^^^"); + } */ + + return !(sum % CHECK_NUMBER); +} + +std::vector intToVector(unsigned long long int number) +{ + std::vector numbers; + while(number > 0) + { + numbers.push_back(number % 10); + number /= 10; + } + std::reverse(numbers.begin(), numbers.end()); + + return numbers; +} + + + +int checkAll() +{ + int sum = 0; + std::ofstream file; + file.open("ISBN.txt"); + for(unsigned long long int i = HIGHEST_ISBN; i >= 1; i--) + { + //if(DEBUG) std::cout << i << std::endl; + if( checkISBN(intToVector(i)) ) + { + ++sum; + file << std::to_string(i) << "\n"; + } + } + file << "There are " << sum << " valid ISBN numbers\n"; + file.close(); + return sum; +} + +int main() +{ + checkAll(); + return 0; +} + +#endif diff --git a/CPP/miscelanious/howOftenDoesCharOccur.cpp b/CPP/miscelanious/howOftenDoesCharOccur.cpp new file mode 100644 index 0000000..38877cc --- /dev/null +++ b/CPP/miscelanious/howOftenDoesCharOccur.cpp @@ -0,0 +1,48 @@ +#include +#include + + +struct charOccurence +{ + char c; + int occurrence; +}; + +void printCharOccurenceVector(const std::vector v) +{ + std::cout << "["; + for(unsigned int i = 0; i < v.size(); i++) + { + std::cout << "(\"" << v.at(i).c << "\", " << v.at(i).occurrence << ")" << (i + 1 == v.size() ? "" : ", "); + } + std::cout << "]" << std::endl; + +} + + +int main() +{ + std::vector list; + std::string userInput = "aaaabbbcca"; + charOccurence newCharOccurence; + newCharOccurence.c = userInput.at(0); + newCharOccurence.occurrence = 1; + for(unsigned int i = 1, j = 1; i < userInput.length(); i++) + { + char newCharacter = userInput.at(i); + if(newCharacter != newCharOccurence.c) + { + list.push_back(newCharOccurence); + j = 1; + newCharOccurence.c = newCharacter; + newCharOccurence.occurrence = j; + + }else + { + newCharOccurence.occurrence++; + } + } + list.push_back(newCharOccurence); + printCharOccurenceVector(list); + return 0; +} diff --git a/CPP/miscelanious/markovChainGenerator/basic.cpp b/CPP/miscelanious/markovChainGenerator/basic.cpp new file mode 100644 index 0000000..55072f0 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/basic.cpp @@ -0,0 +1,177 @@ +#ifndef BASIC_CPP +#define BASIC_CPP + +#include +#include +#include +#include + +void print(const std::string s) { std::cout << s << std::endl; } + +int charToInt(const char c) { return c - '0'; } + +void e() { print("Poor man breakboint"); } + +bool charIsNumber(const char c) { return c >= '0' && c <= '9'; } + +void printStringNewLine(const std::string s) +{ + std::cout << "string: " << std::endl; + std::cout << "\"" << s << "\"" << std::endl; +} + +void printStringContainsNotNumbers(const std::string s, const int position) +{ + printStringNewLine(s); + std::cout << "contains character different than number at position: " << position + << "; this character is: " << s.at(position) << std::endl; +} + +void printStringContainsNumbers(const std::string s, const int position) +{ + printStringNewLine(s); + std::cout << "contains number at postion: " << position + << "; this number is: " << s.at(position) << std::endl; +} + +void printNumberTooLow(const int number, const int min) +{ + std::cout << "number: " << number + << " is too low. Minimal number is: " << min << std::endl; +} + +void printNumberTooHigh(const int number, const int max) +{ + std::cout << "number: " << number + << " is too high. Maximal number is: " << max << std::endl; +} + +void printNotValidStringLength(const std::string s, const int desiredLength) +{ + printStringNewLine(s); + std::cout << "is too short/too long, it is: " + << s.length() << " characters long but should be: " << desiredLength + << " characters long " << std::endl; +} + +void printInvalidCharacter(const char c, const char desiredCharacter) +{ + std::cout << "[ " << c << " ] Is invalid character, expected: [ " + << desiredCharacter << " ]" << std::endl; +} + +void printContainsIllegalCharacter( const std::string s, + const char illegalCharacter ) +{ + printStringNewLine(s); + std::cout << " consists of illegal sign: [" + << illegalCharacter << "]!" << std::endl; +} + + +bool numberTooLow(const int number, const int min) +{ + if(number < min) + { + printNumberTooLow(number, min); + return 1; + } + return 0; +} + +bool numberTooHigh(const int number, const int max) +{ + if(number > max) + { + printNumberTooHigh(number, max); + return 1; + } + return 0; +} + +bool containsIllegalCharacter(const std::string s, const char illegalCharacter) +{ + if( s.find(illegalCharacter) != std::string::npos) + { + printContainsIllegalCharacter(s, illegalCharacter); + return 1; + } + return 0; +} + +void printStringVector(const std::vector vector) +{ + for(unsigned int i = 0; i < vector.size(); i++) print(vector.at(i)); +} + +bool stringContainsNotNumbers(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(!charIsNumber(s.at(i))) + { + printStringContainsNotNumbers(s, i); + return 1; + } + } + return 0; +} + +bool stringContainsNumbers(const std::string s) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + if(charIsNumber(s.at(i))) + { + printStringContainsNumbers(s, i); + return 1; + } + } + return 0; +} + +bool validStringLength(const std::string s, const int desiredLength) +{ + int stringLength = s.length(); + if(stringLength != desiredLength) + { + printNotValidStringLength(s, desiredLength); + return 0; + } + return 1; +} + +bool validCharacter(const char inputC, const char desiredC) +{ + if(inputC != desiredC) + { + printInvalidCharacter(inputC, desiredC); + return 0; + } + return 1; +} + +void vectorToFile(const std::vector strings, std::ofstream &file) +{ + for(unsigned int i = 0; i < strings.size(); i++) + { + file << strings.at(i) << std::endl; + } +} + +std::vector fileToVector(std::ifstream &file, + std::vector strings) +{ + std::string line; + if(file.is_open()) + { + while(getline(file, line)) + { + strings.push_back(line); + } + file.close(); + } + return strings; +} + +#endif diff --git a/CPP/miscelanious/markovChainGenerator/loremIpsum.txt b/CPP/miscelanious/markovChainGenerator/loremIpsum.txt new file mode 100644 index 0000000..f496d37 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/loremIpsum.txt @@ -0,0 +1,11 @@ + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris fermentum ac mi quis porta. Aenean vehicula dolor sed leo tristique, ut semper sem sagittis. Nam et faucibus urna. Nam ut neque vitae nisl blandit euismod. Morbi et odio eget ante egestas tempor. Aenean vehicula quis lectus et convallis. Quisque dictum, augue ut ultricies elementum, felis quam rhoncus purus, accumsan tincidunt nunc orci vel nulla. Quisque eros est, tempus nec erat pellentesque, accumsan maximus massa. Aliquam non ante in ex fringilla vehicula in eu magna. Sed aliquet egestas tincidunt. Mauris at libero et nulla mollis bibendum accumsan id metus. Vestibulum ornare nibh ac cursus posuere. Proin efficitur fermentum sapien sit amet porta. + +Maecenas luctus neque sed aliquam iaculis. Maecenas turpis metus, fermentum et vehicula eu, consequat ac nisi. Curabitur porta mauris vel nisi vehicula scelerisque. Mauris dolor ex, mattis sit amet porta quis, consectetur volutpat velit. Maecenas sit amet vehicula metus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum ligula mauris, iaculis in porttitor in, viverra quis est. Sed mattis, turpis non facilisis interdum, sem risus volutpat lacus, sed molestie metus tellus vel diam. In nec eleifend ipsum. Donec auctor, dolor fringilla laoreet hendrerit, est tortor luctus nunc, et malesuada lorem diam quis sem. In hac habitasse platea dictumst. Interdum et malesuada fames ac ante ipsum primis in faucibus. + +Mauris mollis massa ac massa laoreet posuere. In ullamcorper nunc eu lobortis facilisis. Mauris et risus non ipsum tempus mattis eget a enim. In congue faucibus ante quis iaculis. Vestibulum dictum ultricies augue sed auctor. Maecenas ut mattis leo. Suspendisse quis nisl et elit congue consequat. Fusce tincidunt lacus a tellus lobortis, et tristique diam lacinia. + +Aliquam congue enim justo, ac scelerisque risus pharetra quis. Curabitur nec tincidunt nunc, in molestie magna. Maecenas quis tincidunt orci. Ut rutrum ut ex rhoncus accumsan. Nunc sed ligula hendrerit, venenatis urna sit amet, commodo ante. Mauris ac urna viverra, fringilla turpis eu, ornare turpis. Curabitur sodales, sapien sed tristique tristique, dolor leo mollis est, at blandit neque est id mi. + +Donec euismod venenatis mauris non bibendum. Duis tellus eros, maximus vel tristique at, congue nec lorem. Vivamus cursus, magna quis lacinia blandit, leo magna varius libero, at pharetra velit metus id massa. Quisque ullamcorper erat eget lacus rhoncus, id imperdiet orci tempus. Sed placerat rutrum vehicula. Suspendisse at sodales dui. Praesent tortor est, ornare vitae cursus sed, hendrerit nec justo. Nam at rhoncus lacus, vitae lobortis nunc. Nunc ac ligula et mauris consequat laoreet. Proin id nulla porttitor, rhoncus massa vel, pretium odio. Aenean in purus velit. Phasellus molestie luctus blandit. Integer nec auctor risus, eu molestie odio. Aliquam ipsum urna, eleifend non ultricies sit amet, blandit sed risus. Pellentesque vitae gravida lacus, vel pellentesque nisi. diff --git a/CPP/miscelanious/markovChainGenerator/main.cpp b/CPP/miscelanious/markovChainGenerator/main.cpp new file mode 100644 index 0000000..15f2036 --- /dev/null +++ b/CPP/miscelanious/markovChainGenerator/main.cpp @@ -0,0 +1,138 @@ +#ifndef MAIN_CPP +#define MAIN_CPP + +#include +#include +#include +#include "basic.cpp" + +struct wordOccurences +{ + std::string word; + int occurences; +} + +struct previousWords +{ + std::string word; + std::vector previousWords; +}; + +struct wordProbabiliy +{ + std::string previousWord; + std::string nextWord; + float probability; +} + +bool validInput(const std::string userInput) +{ + if(stringContainsNumbers(userInput)) return 0; + return 1; +} + +std::vector divideIntoWords(const std::string userInput) +{ + std::vector words; + int inputLength = userInput.length(); + int wordLength = 0; + for(int i = 0; i < inputLength; i++) + { + if(userInput.at(i) == ' ') + { + words.push_back(userInput.substr(i - wordLength, wordLength)); + wordLength = 0; + }else wordLength++; + + if(i + 1 == inputLength) + { + words.push_back(userInput.substr(i - wordLength + 1, wordLength + 1)); + wordLength = 0; + } + } + return words; +} + +int wordRepeats(const std::vector wordsList, const std::string word) +{ + int wordsSize = wordsList.size(); + for(int i = 0; i < wordsSize; i++) + { + if(wordsList.at(i).word == word) return i; + } + return -1; +} + +bool alreadyExists(const std::vector wordsList, const std::string s) +{ + for(unsigned int i = 0; i < wordsList.size(); i++) + { + if(s == wordsList.previousWOrds + } +} + +std::vector getWordsAndTheirPrevious(const std::vector words) +{ + std::vector wordsList; + int wordsSize = words.size(); + for(int i = 1; i < wordsSize; i++) + { + previousWords temp; + temp.word = words.at(i); + wordOccurences tempTwo; + tempTwo.word = words.at(i - 1)); + tempTwo.occurences = 1; + temp.previousWords.push_back(tempTwo); + int position = wordRepeats(wordsList, temp.word); + if(position == -1) + { + wordsList.push_back(temp); + }else + { + + wordsList.at(position).previousWords.push_back(temp.previousWords.at(0)); + } + } + return wordsList; +} + +void printPreviousWord(const previousWords word) +{ + std::cout << "The word is \"" << word.word << "\" Words before it are: " << std::endl; + for(unsigned int i = 0; i < word.previousWords.size(); i++) + { + print(word.previousWords.at(i)); + } +} + + +void printPreviousWordsVector(const std::vector v) +{ + for(unsigned int i = 0; i < v.size(); i++) + { + printPreviousWord(v.at(i)); + } +} + +std::vector getWordProbability(const std::vector wordsList) +{ + std::vector probalityVector; + for(unsigned int i = 0; i - 1 < wordsList.size(); i++) + { + pro + } +} + +int main() +{ + std::string userInput; + do{ + getline(std::cin, userInput); + }while(!validInput(userInput)); + std::vector words = divideIntoWords(userInput); + std::vector prev = getWordsAndTheirPrevious(words); + printPreviousWordsVector(prev); + return 0; +} + +#endif diff --git a/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp b/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp new file mode 100644 index 0000000..8c0bc0b --- /dev/null +++ b/CPP/miscelanious/mutiplicationWithoutStar/multiplication.cpp @@ -0,0 +1,27 @@ +#include + + +int multiplication(int a, int b) +{ + int answer = 0; + for(int i = 0; i < a; i++) + { + answer += b; + } + if(answer != a*b) + { + std::cout << "There is a mistake in your code!" << std::endl; + return -1; + } else return answer; +} + +int main() +{ + int a,b; + std::cout << "Enter number a" << std::endl; + std::cin >> a; + std::cout << "Enter number b" << std::endl; + std::cin >> b; + std::cout << multiplication(a, b) << std::endl; + return 0; +} diff --git a/CPP/miscelanious/quickchallenges.cpp b/CPP/miscelanious/quickchallenges.cpp new file mode 100644 index 0000000..f0ab290 --- /dev/null +++ b/CPP/miscelanious/quickchallenges.cpp @@ -0,0 +1,96 @@ +#include +#include + +int sumStartEnd(int start, int end) +{ + int sum = 0; + for(int i = start; i <= end; i++) + { + sum += i; + } + return sum; +} + +int main() +{ + std::cout << "Krzysztof" << std::endl; + for(int i = 700; i >= 200; i -= 13) std::cout << i << std::endl; + std::vector array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + // if SECOND means 0, 1, TWO + std::cout << array[2] << std::endl; + // if SECOND means 1, TWO + std::cout << array[1] << std::endl; + std::cout << sumStartEnd(0, 1000); + + std::string userName; + std::cout << std::endl; + getline(std::cin, userName); + if(userName == "Jack") std::cout << "Hi Jack!" << std::endl; + else std::cout << "Hello, " << userName << std::endl; + + for(int i = 0; i <= 100; i++) + { + if(i % 2 == 0) std::cout << i << " is an even number" << std::endl; + else std::cout << i << " is an odd number" << std::endl; + } + + bool flag = 1; + for(int i = 0; i <= 100; i++) + { + if(flag) std::cout << i << " is an even number" << std::endl; + else std::cout << i << " is an odd number" << std::endl; + flag = -flag; + } + + for(int i = 0; i <= 100; i += 2) std::cout << i << " is an even number " << std::endl; + for(int i = 1; i <= 99; i += 2) std::cout << i << " is an odd number " << std::endl; + + for(int i = 1, j = 1; j <= 12; i++) + { + std::cout << i * j << " "; + + if(i == 12) + { + i = 1; + j++; + std::cout << std::endl; + } + } + + std::cout << std::endl; + std::string sentence; + getline(std::cin, sentence); + std::vector words; + std::string temp; + for(unsigned int i = 0; i < sentence.length(); i++) + { + if(sentence.at(i) == ' ' || i + 1 == sentence.length()) + { + if(i + 1 == sentence.length()) temp.push_back(sentence.at(i)); + words.push_back(temp); + temp = ""; + }else temp.push_back(sentence.at(i)); + + } + + for(unsigned int i = 0; i < words.size(); i++) + { + std::cout << words[i] << std::endl; + } + + int score; + char project; + std::vector GRADES = {'F', 'C', 'B', 'A'}; + std::cin >> score; + std::cout << std::endl; + std::cin >> project; + std::cout << std::endl; + bool doneProject = 0; + if(project == 'Y') doneProject = 1; + if(score < 50) std::cout << GRADES[0 + doneProject]; + else if(score < 70) std::cout << GRADES[1 + doneProject]; + else if(score < 90) std::cout << GRADES[2 + doneProject]; + else std::cout << GRADES[3]; + + +} diff --git a/CPP/miscelanious/randomDevice/main.cpp b/CPP/miscelanious/randomDevice/main.cpp new file mode 100644 index 0000000..6213e5c --- /dev/null +++ b/CPP/miscelanious/randomDevice/main.cpp @@ -0,0 +1,10 @@ +#include +#include + +int main() { + std::random_device rd; + std::uniform_real_distribution dist(1.0, 10.0); + + for (int i=0; i<16; ++i) + std::cout << dist(rd) << "\n"; +} diff --git a/CPP/miscelanious/reverseString.cpp b/CPP/miscelanious/reverseString.cpp new file mode 100644 index 0000000..34e4810 --- /dev/null +++ b/CPP/miscelanious/reverseString.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +int main() +{ + std::string userString; + getline(std::cin, userString); + int sLength = userString.length(); + std::string tempString = userString; + for(int i = 0; i < sLength/2; i++) + { + char temp = tempString[sLength - 1 - i]; + tempString[sLength - 1 - i] = tempString[i]; + tempString[i] = temp; + } + reverse(userString.begin(), userString.end()); + bool correct = tempString == userString; + std::cout << correct << std::endl; + std::cout << tempString << std::endl; + return 0; +} diff --git a/CPP/miscelanious/solveQuadraticEquation.cpp b/CPP/miscelanious/solveQuadraticEquation.cpp new file mode 100644 index 0000000..56195ba --- /dev/null +++ b/CPP/miscelanious/solveQuadraticEquation.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +const std::string ENTER = "Enter quadratic equation constants: "; +const std::string WHAT_TO_INPUT = "a, b, c as in: ax^2 + bx + c = 0"; +const std::string START = ENTER + WHAT_TO_INPUT; + +void print(const std::string s) { std::cout << s << std::endl; } + +float getDelta(float a, float b, float c) +{ + return b*b - 4*a*c; +} + +float calculateFirstTerm(float a, float b, float delta) +{ + return (-b - sqrt(delta))/(2*a); +} + +float calculateSecondTerm(float a, float b, float delta) +{ + return (-b + sqrt(delta))/(2*a); +} + +int main() +{ + print(START); + float a, b, c; + std::cin >> a; + std::cin >> b; + std::cin >> c; + float delta = getDelta(a, b, c); + if(delta < 0) + { + print("delta smaller than 0"); + return -1; + } + + float x_1 = calculateFirstTerm(a, b, delta); + float x_2 = calculateSecondTerm(a, b, delta); + print("Solutions:"); + std::cout << "x_1 = " << x_1 << std::endl; + std::cout << "x_2 = " << x_2 << std::endl; + return 0; + + +} diff --git a/CPP/miscelanious/tictactoe/tictactoe.cpp b/CPP/miscelanious/tictactoe/tictactoe.cpp new file mode 100644 index 0000000..4583dc3 --- /dev/null +++ b/CPP/miscelanious/tictactoe/tictactoe.cpp @@ -0,0 +1,111 @@ +#include +#include + +void printField(std::vector &field) +{ + std::cout << std::endl; + for(int i = 0; i < 9; i++) + { + if(i % 3 == 0) std::cout << std::endl; + if(field[i] == 0) std::cout << "-"; + else if(field[i] == 1) std::cout << "X"; + else if(field[i] == 2) std::cout << "O"; + } + std::cout << std::endl; +} + + +unsigned int chooseField(unsigned int playerNumber, std::vector &field) +{ + unsigned int chosenField; + do + { + std::cout << "player " << playerNumber << " choose a field:" << std::endl; + std::cin >> chosenField; + }while(field[chosenField] != 0); + return chosenField; +} + +bool vertical(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[1] == playerNumber && field[2] == playerNumber) + || (field[3] == playerNumber && field[4] == playerNumber && field[5] == playerNumber) + || (field[6] == playerNumber && field[7] == playerNumber && field[8] == playerNumber)) + { + return 1; + }else return 0; +} + +bool horizontal(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[3] == playerNumber && field[6] == playerNumber) + || (field[1] == playerNumber && field[4] == playerNumber && field[7] == playerNumber) + || (field[2] == playerNumber && field[5] == playerNumber && field[8] == playerNumber)) + { + return 1; + }else return 0; +} + +bool across(unsigned int playerNumber, std::vector &field) +{ + if((field[0] == playerNumber && field[4] == playerNumber && field[8] == playerNumber) + || (field[2] == playerNumber && field[4] == playerNumber && field[6] == playerNumber)) + { + return 1; + }else return 0; +} + +bool checkPlayerWin(unsigned int playerNumber, std::vector &field) +{ + if(vertical(playerNumber, field)) return 1; + if(horizontal(playerNumber, field)) return 1; + if(across(playerNumber, field)) return 1; + else return 0; +} + +unsigned int checkIfWin(std::vector &field) +{ + if(checkPlayerWin(1, field)) return 1; + else if(checkPlayerWin(2, field)) return 2; + else return 0; +} + +bool checkIfFilled(std::vector &field) +{ + bool filled = 1; + for(int i = 0; i < 9; i++) + { + if(field[i] == 0) + { + filled = 0; + return filled; + } + } + return filled; +} + +bool turn(unsigned int playerNumber, std::vector &field, bool *filled, unsigned int *whoWon) +{ + field[chooseField(playerNumber, field)] = playerNumber; + printField(field); + *whoWon = checkIfWin(field); + *filled = checkIfFilled(field); + if(*whoWon != 0 || *filled != 0) return 1; + else return 0; +} + +int main() +{ + std::vector field = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + unsigned int whoWon = 0; + bool filled = 0; + + while(whoWon == 0 || filled == 0) + { + if(turn(1, field, &filled, &whoWon)) break; + if(turn(2, field, &filled, &whoWon)) break; + } + if(!filled) std::cout << "Player " << whoWon << " Won!" << std::endl; + else std::cout << "DRAW!" << std::endl; + return 0; +} diff --git a/CPP/miscelanious/tierListConverter/tierListConverter.cpp b/CPP/miscelanious/tierListConverter/tierListConverter.cpp new file mode 100644 index 0000000..03eb7ce --- /dev/null +++ b/CPP/miscelanious/tierListConverter/tierListConverter.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +const std::vector TIERS = {"Abhorrent", "Bad", "Mid", "Good", "Top", "God Tier"}; +const float TIER_BASE = TIERS.size(); + +void print(std::string const s) +{ + std::cout << s << std::endl; +} + +bool errorUserInput(std::string userInput) +{ + if(userInput.find("/") == std::string::npos) + { + print("No '/' was found!"); + return 1; + } + + size_t positionOfSlash = userInput.find("/"); + std::string nominatorS = userInput.substr(0, positionOfSlash); + + try + { + float nominator = stof(nominatorS); + }catch ( std::invalid_argument ) + { + print("No number was found before the slash!"); + return 1; + } + + std::string denominatorS = userInput.substr(positionOfSlash + 1, userInput.length() - 1); + + try + { + float denominator = stof(denominatorS); + if(denominator == 0) + { + print("You cannot divide by 0!"); + return 1; + } + }catch ( std::invalid_argument ) + { + print("No number was found after the slash!"); + return 1; + } + + return 0; +} + +std::string convertToTier(float nominator, float denominator) +{ + float fraction = nominator / denominator; + int tierIndex; + for(int i = TIER_BASE; i > 0; i--) + { + if(fraction >= ( i / TIER_BASE)) + { + tierIndex = i - 1; + break; + } + } + if(tierIndex == 0 & fraction > (1.1/10.0)) return TIERS[1]; + return TIERS[tierIndex]; +} + + +int main() +{ + std::string userScore; + do + { + print("Enter your score in a format: numberOne/numberTwo"); + getline(std::cin, userScore); + }while(errorUserInput(userScore)); + + size_t positionOfSlash = userScore.find("/"); + std::string nominatorS = userScore.substr(0, positionOfSlash); + + + float nominator = stof(nominatorS); + std::string denominatorS = userScore.substr(positionOfSlash + 1, userScore.length() - 1); + + + float denominator = stof(denominatorS); + print(convertToTier(nominator, denominator)); + return 0; +} diff --git a/CPP/miscelanious/xGoesTo0/xgoes.cpp b/CPP/miscelanious/xGoesTo0/xgoes.cpp new file mode 100644 index 0000000..4253c21 --- /dev/null +++ b/CPP/miscelanious/xGoesTo0/xgoes.cpp @@ -0,0 +1,11 @@ +#include + +int main() +{ + int x = 10; + while (x-- > 0) + { + printf("%d;", x); + } + return 0; +} diff --git a/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp b/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp new file mode 100644 index 0000000..674cd2c --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/Bernouli/bernouli.cpp @@ -0,0 +1,22 @@ +// bernoulli_distribution +#include +#include + +int main() +{ + const int nrolls=10000; + + std::random_device rd; + std::mt19937 gen(rd()); + std::bernoulli_distribution distribution(0.5); + + int count=0; // count number of trues + + for (int i=0; i +#include + +int main() { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0, 1); + + for(int i = 0; i < 10; i++) { + + std::cout << dis(gen) << std::endl; + }return 0; +} diff --git a/CPP/miscelanious/yousuckatcards/makefile b/CPP/miscelanious/yousuckatcards/makefile new file mode 100644 index 0000000..3c53a78 --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/makefile @@ -0,0 +1,2 @@ +yousuckatcards: + g++ -Wall -Wextra -pedantic yousuckatcards.cpp diff --git a/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp b/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp new file mode 100644 index 0000000..e8ee143 --- /dev/null +++ b/CPP/miscelanious/yousuckatcards/yousuckatcards.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +const int SEQUENCE_LENGTH = 3; + +const bool BOT_WON = 0; +const bool PLAYER_WON = 1; +const int NOBODY_WON = 2; + +void print(std::string const s) +{ + std::cout << s << std::endl; +} + +bool validSequence(std::string const s) +{ + if(s.size() != SEQUENCE_LENGTH) + { + print("Sequence too long"); + return false; + } + if( (s[0] != 'B' && s[0] != 'R') || + (s[1] != 'B' && s[1] != 'R') || + (s[2] != 'B' && s[2] != 'R')) + { + print("Sequence consists of illegal signs!"); + return false; + } + return true; +} + +std::string playerChoice() +{ + std::string playerSequence; + do + { + std::cin >> playerSequence; + } + while(!validSequence(playerSequence)); + return playerSequence; +} + +std::string botChoice(std::string const playerSequence) +{ + std::string botSequence; + if(playerSequence[1] == 'B') botSequence.push_back('R'); + else botSequence.push_back('B'); + botSequence.push_back(playerSequence[0]); + botSequence.push_back(playerSequence[2]); + return botSequence; +} + +int compareGeneratedAndPlayers(std::string playerSequence, std::string botSequence, std::string generatedSequence) +{ + int generatedSequenceLength = generatedSequence.length(); + std::string sequenceToCompare = generatedSequence.substr(generatedSequenceLength - SEQUENCE_LENGTH, generatedSequenceLength); + if(sequenceToCompare.compare(playerSequence) == 0) return PLAYER_WON; + if(sequenceToCompare.compare(botSequence) == 0) return BOT_WON; + else return NOBODY_WON; +} + +bool game(std::string playerSequence, std::string botSequence) +{ + std::string generatedSequence; + std::random_device rd; + std::mt19937 gen(rd()); + std::bernoulli_distribution distribution(0.5); + for(int i = 0; i < SEQUENCE_LENGTH; i++) + { + if(distribution(gen)) generatedSequence.push_back('R'); + else generatedSequence.push_back('B'); + } + + while(compareGeneratedAndPlayers(playerSequence, botSequence, generatedSequence) == NOBODY_WON) + { + if(distribution(gen)) generatedSequence.push_back('R'); + else generatedSequence.push_back('B'); + } + + print(generatedSequence); + if(compareGeneratedAndPlayers(playerSequence, botSequence, generatedSequence) == PLAYER_WON) return PLAYER_WON; + else return BOT_WON; +} + +void score(int playerWins, int botWins) +{ + std::cout << "Player won: " << playerWins << " times!" << std::endl; + std::cout << "Bot won: " << botWins << " times!" << std::endl; +} + + + +int main() +{ + int playerWins = 0; + int botWins = 0; + do + { + print("Do you want to play the game? 1 - yes, 0 - no"); + bool continue_ = 1; + std::string playerInput; + std::cin >> playerInput; + if(playerInput[0] == '1') continue_ = 1; + else continue_ = 0; + if(!continue_) break; + std::string playerSequence; + print("Write three colors sequence created from 52 cards from the deck (26 Black, 26 Red), write B for Black and R for Red"); + playerSequence = playerChoice(); + std::string botSequence = botChoice(playerSequence); + print("Bot has chosen this sequence:"); + print(botSequence); + if(game(playerSequence, botSequence)) + { + print("You won!"); + playerWins++; + score(playerWins, botWins); + } + else + { + print("Bot won!"); + botWins++; + score(playerWins, botWins); + } + }while(1); + return 1; +} diff --git a/CPP/tests/howCppHandlesDivision.cpp b/CPP/tests/howCppHandlesDivision.cpp new file mode 100644 index 0000000..1e3f2d5 --- /dev/null +++ b/CPP/tests/howCppHandlesDivision.cpp @@ -0,0 +1,8 @@ +#include + +int main() +{ + float X = 1/2; + std::cout << X << std::endl; + return 0; +} diff --git a/LaTeX/.gitignore b/LaTeX/.gitignore index 09e0b13..5a81ed1 100644 --- a/LaTeX/.gitignore +++ b/LaTeX/.gitignore @@ -305,4 +305,4 @@ TSWLatexianTemp* # option is specified. Footnotes are the stored in a file with suffix Notes.bib. # Uncomment the next line to have this generated file ignored. #*Notes.bib -*.pdf \ No newline at end of file +*.pdf diff --git a/LaTeX/main.tex b/LaTeX/main.tex index 8363d00..218593d 100644 --- a/LaTeX/main.tex +++ b/LaTeX/main.tex @@ -41,4 +41,4 @@ Na czym polega odmaskowanie dwuuszne (BMLD)? \\ Podać typowe wartości odmaskowania dwuusznego. \\ Typowe wartości odmaskowania dwuusznego (MLD) to różnice w progu słyszenia sygnału w obecności szumu przy korzystaniu ze słuchu obuusznego. MLD jest największe dla niskich częstotliwości, osiągając około **15 dB** dla 500 Hz. Dla średnich częstotliwości, takich jak 1000 Hz, MLD wynosi około **10 dB**. W przypadku wyższych częstotliwości, powyżej 1500 Hz, MLD maleje do wartości często poniżej **5 dB**. Te wartości pokazują, że słuch obuuszny znacząco poprawia wykrywanie sygnałów w szumie na niskich i średnich częstotliwościach. -\end{document} \ No newline at end of file +\end{document} diff --git a/LaTeX/mauin2.tex b/LaTeX/mauin2.tex index d3a5df9..af42f02 100644 --- a/LaTeX/mauin2.tex +++ b/LaTeX/mauin2.tex @@ -12,10 +12,10 @@ \SetBgContents{\tikz{\draw[step=\mylen] (-.5\paperwidth,-.5\paperheight) grid (.5\paperwidth,.5\paperheight);}} \begin{document} -\newpage +\newpage \ % The empty page \newpage -\end{document} \ No newline at end of file +\end{document} diff --git a/PYTHON/.gitignore b/PYTHON/.gitignore index 592fd7a..97b44b0 100644 --- a/PYTHON/.gitignore +++ b/PYTHON/.gitignore @@ -215,4 +215,4 @@ __marimo__/ # Streamlit .streamlit/secrets.toml -*lichess_db_puzzle.csv* \ No newline at end of file +*lichess_db_puzzle.csv* diff --git a/PYTHON/downloadCats/generate_cats.py b/PYTHON/downloadCats/generate_cats.py new file mode 100644 index 0000000..cd11851 --- /dev/null +++ b/PYTHON/downloadCats/generate_cats.py @@ -0,0 +1,34 @@ +import json +import os +from pathlib import Path + +import requests + +requests_send = 0 +while requests_send < 90: + res = requests.get("https://api.thecatapi.com/v1/images/search?limit=100&api_key=") + requests_send += 1 + response = json.loads(res.text) + urls = [] + for cat in response: + urls.append(cat.get("url")) + + Path("./CATS2").mkdir(parents=True, exist_ok=True) + for url in urls: + try: + # Get the image content + response = requests.get(url) + response.raise_for_status() # Raise an exception for HTTP errors + + # Extract the image name from the URL + image_name = os.path.basename(url) + image_path = os.path.join("./CATS2/", image_name) + + # Save the image to the directory + with open(image_path, "wb") as file: + file.write(response.content) + + print(f"Saved {url} as {image_path}") + + except requests.exceptions.RequestException as e: + print(f"Failed to download {url}: {e}") diff --git a/PYTHON/downloadCats/requirements.txt b/PYTHON/downloadCats/requirements.txt new file mode 100644 index 0000000..d90a3af --- /dev/null +++ b/PYTHON/downloadCats/requirements.txt @@ -0,0 +1,4 @@ +json +os +pathlib +requests diff --git a/PYTHON/extractLinks/main.py b/PYTHON/extractLinks/main.py old mode 100644 new mode 100755 index 0ad1ae7..991a092 --- a/PYTHON/extractLinks/main.py +++ b/PYTHON/extractLinks/main.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -""" -Extract hosts from href attributes in an HTML file and write them as *host* per line. +"""Extract hosts from href attributes in an HTML file and write them as *host* per line. Usage: python main.py INPUT_HTML [OUTPUT_TXT] @@ -12,79 +11,79 @@ alongside the input file. from __future__ import annotations import argparse -import os from html.parser import HTMLParser -from typing import List, Set +import os from urllib.parse import urlparse class _HrefParser(HTMLParser): - def __init__(self) -> None: - super().__init__() - self.hrefs: List[str] = [] + def __init__(self) -> None: + super().__init__() + self.hrefs: list[str] = [] - def handle_starttag(self, tag: str, attrs): # type: ignore[override] - # Collect any href attribute on any tag - for (k, v) in attrs: - if k.lower() == "href" and v is not None: - self.hrefs.append(v) + def handle_starttag(self, tag: str, attrs): # type: ignore[override] + # Collect any href attribute on any tag + for k, v in attrs: + if k.lower() == "href" and v is not None: + self.hrefs.append(v) -def extract_hosts_from_html(html_text: str) -> List[str]: - """Parse HTML text, extract href values, and return a list of hostnames. +def extract_hosts_from_html(html_text: str) -> list[str]: + """Parse HTML text, extract href values, and return a list of hostnames. - Rules: - - Only http/https URLs are considered. - - Output is the network location (host[:port]) without scheme or path. - - Duplicates are removed, preserving first-seen order. - """ - parser = _HrefParser() - parser.feed(html_text) + Rules: + - Only http/https URLs are considered. + - Output is the network location (host[:port]) without scheme or path. + - Duplicates are removed, preserving first-seen order. + """ + parser = _HrefParser() + parser.feed(html_text) - seen: Set[str] = set() - hosts: List[str] = [] - for href in parser.hrefs: - parsed = urlparse(href) - if parsed.scheme in {"http", "https"} and parsed.netloc: - host = parsed.netloc - if host not in seen: - seen.add(host) - hosts.append(host) - return hosts + seen: set[str] = set() + hosts: list[str] = [] + for href in parser.hrefs: + parsed = urlparse(href) + if parsed.scheme in {"http", "https"} and parsed.netloc: + host = parsed.netloc + if host not in seen: + seen.add(host) + hosts.append(host) + return hosts def main() -> int: - ap = argparse.ArgumentParser(description="Extract hosts from hrefs in an HTML file.") - ap.add_argument("input_html", help="Path to input HTML file") - ap.add_argument( - "output_txt", - nargs="?", - help="Path to output text file (defaults to _links.txt in the same directory)", - ) - args = ap.parse_args() + ap = argparse.ArgumentParser( + description="Extract hosts from hrefs in an HTML file." + ) + ap.add_argument("input_html", help="Path to input HTML file") + ap.add_argument( + "output_txt", + nargs="?", + help="Path to output text file (defaults to _links.txt in the same directory)", + ) + args = ap.parse_args() - input_path = args.input_html - if not os.path.isfile(input_path): - raise SystemExit(f"Input file not found: {input_path}") + input_path = args.input_html + if not os.path.isfile(input_path): + raise SystemExit(f"Input file not found: {input_path}") - out_path = args.output_txt - if not out_path: - base = os.path.splitext(os.path.basename(input_path))[0] - out_path = os.path.join(os.path.dirname(input_path), f"{base}_links.txt") + out_path = args.output_txt + if not out_path: + base = os.path.splitext(os.path.basename(input_path))[0] + out_path = os.path.join(os.path.dirname(input_path), f"{base}_links.txt") - with open(input_path, "r", encoding="utf-8", errors="ignore") as f: - html_text = f.read() + with open(input_path, encoding="utf-8", errors="ignore") as f: + html_text = f.read() - hosts = extract_hosts_from_html(html_text) + hosts = extract_hosts_from_html(html_text) - with open(out_path, "w", encoding="utf-8") as f: - for host in hosts: - f.write(f"*{host}*\n") + with open(out_path, "w", encoding="utf-8") as f: + for host in hosts: + f.write(f"*{host}*\n") - print(f"Wrote {len(hosts)} host(s) to {out_path}") - return 0 + print(f"Wrote {len(hosts)} host(s) to {out_path}") + return 0 if __name__ == "__main__": - raise SystemExit(main()) - + raise SystemExit(main()) diff --git a/PYTHON/extractLinks/tests/test_main.py b/PYTHON/extractLinks/tests/test_main.py index 2eee282..6f97782 100644 --- a/PYTHON/extractLinks/tests/test_main.py +++ b/PYTHON/extractLinks/tests/test_main.py @@ -1,9 +1,6 @@ -import os +from pathlib import Path import subprocess import sys -from pathlib import Path - -import pytest # Allow importing from project root when running pytest from this folder ROOT = Path(__file__).resolve().parents[1] diff --git a/PYTHON/keyboardCoop/README.md b/PYTHON/keyboardCoop/README.md index 61d1be0..26d5f2e 100644 --- a/PYTHON/keyboardCoop/README.md +++ b/PYTHON/keyboardCoop/README.md @@ -10,7 +10,7 @@ A fun 2-player cooperative word game where players take turns selecting adjacent 4. **Word Formation**: Continue taking turns until you want to submit a word 5. **Scoring**: Press ENTER to submit the word. Valid words score points exponentially based on length: - 3 letters: 2 points - - 4 letters: 4 points + - 4 letters: 4 points - 5 letters: 8 points - 6 letters: 16 points - And so on... @@ -25,6 +25,7 @@ A fun 2-player cooperative word game where players take turns selecting adjacent ## Keyboard Adjacency Each key is adjacent to its neighbors (including diagonals). For example: + - 'S' is adjacent to: Q, W, E, A, D, Z, X, C - 'F' is adjacent to: E, R, T, D, G, C, V, B diff --git a/PYTHON/keyboardCoop/main.py b/PYTHON/keyboardCoop/main.py index f6f39be..667adf9 100644 --- a/PYTHON/keyboardCoop/main.py +++ b/PYTHON/keyboardCoop/main.py @@ -1,8 +1,9 @@ -import pygame -import sys import json import os import random +import sys + +import pygame # Initialize Pygame pygame.init() @@ -21,41 +22,42 @@ PLAYER_COLORS = [(255, 100, 100), (100, 100, 255)] # Keyboard layout KEYBOARD_LAYOUT = [ - ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'], - ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'], - ['z', 'x', 'c', 'v', 'b', 'n', 'm'] + ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"], + ["a", "s", "d", "f", "g", "h", "j", "k", "l"], + ["z", "x", "c", "v", "b", "n", "m"], ] # Key adjacency mapping KEY_ADJACENCY = { - 'q': ['w', 'a', 's'], - 'w': ['q', 'e', 'a', 's', 'd'], - 'e': ['w', 'r', 's', 'd', 'f'], - 'r': ['e', 't', 'd', 'f', 'g'], - 't': ['r', 'y', 'f', 'g', 'h'], - 'y': ['t', 'u', 'g', 'h', 'j'], - 'u': ['y', 'i', 'h', 'j', 'k'], - 'i': ['u', 'o', 'j', 'k', 'l'], - 'o': ['i', 'p', 'k', 'l'], - 'p': ['o', 'l'], - 'a': ['q', 'w', 's', 'z', 'x'], - 's': ['q', 'w', 'e', 'a', 'd', 'z', 'x', 'c'], - 'd': ['w', 'e', 'r', 's', 'f', 'x', 'c', 'v'], - 'f': ['e', 'r', 't', 'd', 'g', 'c', 'v', 'b'], - 'g': ['r', 't', 'y', 'f', 'h', 'v', 'b', 'n'], - 'h': ['t', 'y', 'u', 'g', 'j', 'b', 'n', 'm'], - 'j': ['y', 'u', 'i', 'h', 'k', 'n', 'm'], - 'k': ['u', 'i', 'o', 'j', 'l', 'm'], - 'l': ['i', 'o', 'p', 'k'], - 'z': ['a', 's', 'x'], - 'x': ['a', 's', 'd', 'z', 'c'], - 'c': ['s', 'd', 'f', 'x', 'v'], - 'v': ['d', 'f', 'g', 'c', 'b'], - 'b': ['f', 'g', 'h', 'v', 'n'], - 'n': ['g', 'h', 'j', 'b', 'm'], - 'm': ['h', 'j', 'k', 'n'] + "q": ["w", "a", "s"], + "w": ["q", "e", "a", "s", "d"], + "e": ["w", "r", "s", "d", "f"], + "r": ["e", "t", "d", "f", "g"], + "t": ["r", "y", "f", "g", "h"], + "y": ["t", "u", "g", "h", "j"], + "u": ["y", "i", "h", "j", "k"], + "i": ["u", "o", "j", "k", "l"], + "o": ["i", "p", "k", "l"], + "p": ["o", "l"], + "a": ["q", "w", "s", "z", "x"], + "s": ["q", "w", "e", "a", "d", "z", "x", "c"], + "d": ["w", "e", "r", "s", "f", "x", "c", "v"], + "f": ["e", "r", "t", "d", "g", "c", "v", "b"], + "g": ["r", "t", "y", "f", "h", "v", "b", "n"], + "h": ["t", "y", "u", "g", "j", "b", "n", "m"], + "j": ["y", "u", "i", "h", "k", "n", "m"], + "k": ["u", "i", "o", "j", "l", "m"], + "l": ["i", "o", "p", "k"], + "z": ["a", "s", "x"], + "x": ["a", "s", "d", "z", "c"], + "c": ["s", "d", "f", "x", "v"], + "v": ["d", "f", "g", "c", "b"], + "b": ["f", "g", "h", "v", "n"], + "n": ["g", "h", "j", "b", "m"], + "m": ["h", "j", "k", "n"], } + class KeyboardCoopGame: def __init__(self): self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) @@ -64,10 +66,10 @@ class KeyboardCoopGame: self.font = pygame.font.Font(None, 24) self.large_font = pygame.font.Font(None, 32) self.small_font = pygame.font.Font(None, 20) - + # Load dictionary self.dictionary = self.load_dictionary() - + # Initialize game state self.current_player = 0 self.current_word = "" @@ -75,18 +77,20 @@ class KeyboardCoopGame: self.score = 0 self.game_over = False self.message = "Player 1: Choose any letter to start!" - + # Generate random keyboard layout and adjacency self.generate_random_keyboard() - + # Key positions self.key_positions = self.calculate_key_positions() - + def load_dictionary(self): """Load dictionary from words_dictionary.json file""" try: - dictionary_path = os.path.join(os.path.dirname(__file__), 'words_dictionary.json') - with open(dictionary_path, 'r', encoding='utf-8') as f: + dictionary_path = os.path.join( + os.path.dirname(__file__), "words_dictionary.json" + ) + with open(dictionary_path, encoding="utf-8") as f: dictionary_data = json.load(f) # Convert to set for faster lookup (we only need the keys) return set(dictionary_data.keys()) @@ -94,65 +98,142 @@ class KeyboardCoopGame: print("Warning: words_dictionary.json not found, using fallback dictionary") # Fallback to a smaller dictionary if file not found return { - 'cat', 'dog', 'car', 'bat', 'rat', 'hat', 'mat', 'sat', 'fat', 'pat', - 'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'had', - 'her', 'was', 'one', 'our', 'out', 'day', 'get', 'has', 'him', 'his', - 'how', 'man', 'new', 'now', 'old', 'see', 'two', 'way', 'who', 'boy', - 'work', 'know', 'place', 'year', 'live', 'me', 'back', 'give', 'good' + "cat", + "dog", + "car", + "bat", + "rat", + "hat", + "mat", + "sat", + "fat", + "pat", + "the", + "and", + "for", + "are", + "but", + "not", + "you", + "all", + "can", + "had", + "her", + "was", + "one", + "our", + "out", + "day", + "get", + "has", + "him", + "his", + "how", + "man", + "new", + "now", + "old", + "see", + "two", + "way", + "who", + "boy", + "work", + "know", + "place", + "year", + "live", + "me", + "back", + "give", + "good", } except json.JSONDecodeError: - print("Warning: Error reading words_dictionary.json, using fallback dictionary") + print( + "Warning: Error reading words_dictionary.json, using fallback dictionary" + ) return { - 'cat', 'dog', 'car', 'bat', 'rat', 'hat', 'mat', 'sat', 'fat', 'pat', - 'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'can', 'had', - 'work', 'know', 'place', 'year', 'live', 'me', 'back', 'give', 'good' + "cat", + "dog", + "car", + "bat", + "rat", + "hat", + "mat", + "sat", + "fat", + "pat", + "the", + "and", + "for", + "are", + "but", + "not", + "you", + "all", + "can", + "had", + "work", + "know", + "place", + "year", + "live", + "me", + "back", + "give", + "good", } - + def generate_random_keyboard(self): """Generate a random keyboard layout and calculate adjacencies""" # All 26 letters - all_letters = list('abcdefghijklmnopqrstuvwxyz') + all_letters = list("abcdefghijklmnopqrstuvwxyz") random.shuffle(all_letters) - + # Create random layout with same structure as QWERTY (10-9-7) self.keyboard_layout = [ - all_letters[0:10], # Top row: 10 keys - all_letters[10:19], # Middle row: 9 keys - all_letters[19:26] # Bottom row: 7 keys + all_letters[0:10], # Top row: 10 keys + all_letters[10:19], # Middle row: 9 keys + all_letters[19:26], # Bottom row: 7 keys ] - + # Update available letters self.available_letters = set(all_letters) - + # Calculate adjacencies based on new layout self.calculate_adjacencies() - + def calculate_adjacencies(self): """Calculate adjacencies based on current keyboard layout""" self.key_adjacency = {} - + for row_idx, row in enumerate(self.keyboard_layout): for col_idx, letter in enumerate(row): adjacents = [] - + # Check all 8 directions (including diagonals) directions = [ - (-1, -1), (-1, 0), (-1, 1), # Above - (0, -1), (0, 1), # Same row - (1, -1), (1, 0), (1, 1) # Below + (-1, -1), + (-1, 0), + (-1, 1), # Above + (0, -1), + (0, 1), # Same row + (1, -1), + (1, 0), + (1, 1), # Below ] - + for dr, dc in directions: new_row = row_idx + dr new_col = col_idx + dc - + # Check bounds if 0 <= new_row < len(self.keyboard_layout): if 0 <= new_col < len(self.keyboard_layout[new_row]): adjacents.append(self.keyboard_layout[new_row][new_col]) - + self.key_adjacency[letter] = adjacents - + def calculate_key_positions(self): """Calculate the position of each key on screen""" positions = {} @@ -161,81 +242,87 @@ class KeyboardCoopGame: key_spacing = 8 start_x = 50 start_y = 320 - + for row_idx, row in enumerate(self.keyboard_layout): row_offset = row_idx * 30 # Offset for layout for col_idx, key in enumerate(row): x = start_x + col_idx * (key_width + key_spacing) + row_offset y = start_y + row_idx * (key_height + key_spacing) positions[key] = pygame.Rect(x, y, key_width, key_height) - + return positions - + def get_key_at_position(self, pos): """Get the key at the given mouse position""" for key, rect in self.key_positions.items(): if rect.collidepoint(pos): return key return None - + def is_valid_move(self, letter): """Check if the letter is a valid move""" if not self.selected_letters: return True # First move can be any letter - + last_letter = self.selected_letters[-1] return letter in self.key_adjacency[last_letter] - + def is_valid_word(self, word): """Check if the word is in the dictionary""" return word.lower() in self.dictionary - + def calculate_score(self, word_length): """Calculate score exponentially based on word length""" if word_length < 3: return 0 return 2 ** (word_length - 2) - + def handle_letter_click(self, letter): """Handle clicking on a letter""" if letter in self.available_letters and self.is_valid_move(letter): self.selected_letters.append(letter) self.current_word += letter - + # Update available letters to include adjacent letters AND the same letter - adjacent_letters = set(self.key_adjacency[letter]) if letter in self.key_adjacency else set() + adjacent_letters = ( + set(self.key_adjacency[letter]) + if letter in self.key_adjacency + else set() + ) adjacent_letters.add(letter) # Allow selecting the same letter again self.available_letters = adjacent_letters - + # Switch player self.current_player = 1 - self.current_player - self.message = f"Player {self.current_player + 1}: Choose an adjacent letter!" - + self.message = ( + f"Player {self.current_player + 1}: Choose an adjacent letter!" + ) + # If no valid moves available, force word submission if not self.available_letters: self.submit_word() - + def submit_word(self): """Submit the current word and check if it's valid""" if len(self.current_word) >= 3 and self.is_valid_word(self.current_word): points = self.calculate_score(len(self.current_word)) self.score += points self.message = f"'{self.current_word}' is valid! +{points} points (Total: {self.score}) - New keyboard!" - + # Randomize keyboard layout after scoring self.generate_random_keyboard() self.key_positions = self.calculate_key_positions() - + elif len(self.current_word) < 3: self.message = f"'{self.current_word}' is too short! (minimum 3 letters)" else: self.message = f"'{self.current_word}' is not a valid word!" - + # Reset for next word self.current_word = "" self.selected_letters = [] self.current_player = 0 - + def reset_game(self): """Reset the game to initial state""" self.current_player = 0 @@ -244,15 +331,15 @@ class KeyboardCoopGame: self.score = 0 self.game_over = False self.message = "Player 1: Choose any letter to start!" - + # Generate new random keyboard layout self.generate_random_keyboard() self.key_positions = self.calculate_key_positions() - + def draw_keyboard(self): """Draw the virtual keyboard""" mouse_pos = pygame.mouse.get_pos() - + for letter, rect in self.key_positions.items(): # Determine key color if letter in self.selected_letters: @@ -263,39 +350,43 @@ class KeyboardCoopGame: color = KEY_HOVER_COLOR else: color = KEY_COLOR - + # Draw key pygame.draw.rect(self.screen, color, rect) pygame.draw.rect(self.screen, TEXT_COLOR, rect, 2) - + # Draw letter text = self.small_font.render(letter.upper(), True, TEXT_COLOR) text_rect = text.get_rect(center=rect.center) self.screen.blit(text, text_rect) - + def draw_ui(self): """Draw the user interface""" # Title title = self.large_font.render("Keyboard Coop Game", True, TEXT_COLOR) self.screen.blit(title, (30, 20)) - + # Current word - word_text = self.font.render(f"Current Word: {self.current_word.upper()}", True, TEXT_COLOR) + word_text = self.font.render( + f"Current Word: {self.current_word.upper()}", True, TEXT_COLOR + ) self.screen.blit(word_text, (30, 50)) - + # Score score_text = self.font.render(f"Score: {self.score}", True, TEXT_COLOR) self.screen.blit(score_text, (30, 75)) - + # Current player player_color = PLAYER_COLORS[self.current_player] - player_text = self.font.render(f"Current Player: {self.current_player + 1}", True, player_color) + player_text = self.font.render( + f"Current Player: {self.current_player + 1}", True, player_color + ) self.screen.blit(player_text, (30, 100)) - + # Message message_text = self.font.render(self.message, True, TEXT_COLOR) self.screen.blit(message_text, (30, 125)) - + # Instructions - Move to right side and make more compact instructions = [ "Instructions:", @@ -305,14 +396,14 @@ class KeyboardCoopGame: "• Press ENTER to submit word (min 3 letters)", "• Press R to reset game", "• Score increases exponentially", - "• Keyboard randomizes after each valid word!" + "• Keyboard randomizes after each valid word!", ] - + start_x = 750 for i, instruction in enumerate(instructions): inst_text = self.small_font.render(instruction, True, TEXT_COLOR) self.screen.blit(inst_text, (start_x, 20 + i * 22)) - + # Enter button - smaller and repositioned enter_rect = pygame.Rect(750, 190, 120, 40) pygame.draw.rect(self.screen, KEY_COLOR, enter_rect) @@ -320,7 +411,7 @@ class KeyboardCoopGame: enter_text = self.small_font.render("ENTER", True, TEXT_COLOR) enter_text_rect = enter_text.get_rect(center=enter_rect.center) self.screen.blit(enter_text, enter_text_rect) - + # Reset button - smaller and repositioned reset_rect = pygame.Rect(880, 190, 120, 40) pygame.draw.rect(self.screen, KEY_COLOR, reset_rect) @@ -328,29 +419,29 @@ class KeyboardCoopGame: reset_text = self.small_font.render("RESET", True, TEXT_COLOR) reset_text_rect = reset_text.get_rect(center=reset_rect.center) self.screen.blit(reset_text, reset_text_rect) - + return enter_rect, reset_rect - + def handle_click(self, pos): """Handle mouse clicks""" # Check if clicked on a key key = self.get_key_at_position(pos) if key: self.handle_letter_click(key) - + # Check UI buttons enter_rect = pygame.Rect(750, 190, 120, 40) reset_rect = pygame.Rect(880, 190, 120, 40) - + if enter_rect.collidepoint(pos): self.submit_word() elif reset_rect.collidepoint(pos): self.reset_game() - + def run(self): """Main game loop""" running = True - + while running: for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -368,21 +459,22 @@ class KeyboardCoopGame: key_name = pygame.key.name(event.key) if len(key_name) == 1 and key_name.isalpha(): self.handle_letter_click(key_name.lower()) - + # Clear screen self.screen.fill(BACKGROUND_COLOR) - + # Draw everything self.draw_keyboard() self.draw_ui() - + # Update display pygame.display.flip() self.clock.tick(60) - + pygame.quit() sys.exit() + if __name__ == "__main__": game = KeyboardCoopGame() game.run() diff --git a/PYTHON/keyboardCoop/words_dictionary.json b/PYTHON/keyboardCoop/words_dictionary.json index 4dc7dfc..44b11e5 100644 --- a/PYTHON/keyboardCoop/words_dictionary.json +++ b/PYTHON/keyboardCoop/words_dictionary.json @@ -370099,4 +370099,4 @@ "zwitter": 1, "zwitterion": 1, "zwitterionic": 1 -} \ No newline at end of file +} diff --git a/PYTHON/lichess_bot/.bot_version b/PYTHON/lichess_bot/.bot_version index ac4213d..920a139 100644 --- a/PYTHON/lichess_bot/.bot_version +++ b/PYTHON/lichess_bot/.bot_version @@ -1 +1 @@ -43 \ No newline at end of file +43 diff --git a/PYTHON/lichess_bot/README.md b/PYTHON/lichess_bot/README.md index 56ec9e7..25406e7 100644 --- a/PYTHON/lichess_bot/README.md +++ b/PYTHON/lichess_bot/README.md @@ -27,10 +27,10 @@ pip install -r PYTHON/lichess_bot/requirements.txt ## Activate BOT and get a token -1) Create or use an existing Lichess account for your bot. -2) Activate it as a BOT (one-time): https://lichess.org/api#tag/Bot +1. Create or use an existing Lichess account for your bot. +2. Activate it as a BOT (one-time): https://lichess.org/api#tag/Bot - If not already BOT, you need to convert the account; follow Lichess docs. -3) Create a personal API token: https://lichess.org/account/oauth/token/create +3. Create a personal API token: https://lichess.org/account/oauth/token/create - Grant scopes: bot:play, challenge:read, challenge:write Export the token in your shell (recommended): @@ -71,4 +71,4 @@ bash PYTHON/lichess_bot/run.sh python -m pytest PYTHON/lichess_bot/tests -q ``` -If you add tests requiring third-party packages, install them in your environment first. \ No newline at end of file +If you add tests requiring third-party packages, install them in your environment first. diff --git a/PYTHON/lichess_bot/__init__.py b/PYTHON/lichess_bot/__init__.py index d011426..d6e4684 100644 --- a/PYTHON/lichess_bot/__init__.py +++ b/PYTHON/lichess_bot/__init__.py @@ -1,2 +1,3 @@ """Package marker for lichess_bot.""" + __all__ = [] diff --git a/PYTHON/lichess_bot/engine.py b/PYTHON/lichess_bot/engine.py index c3d1e6e..f3f1ad2 100644 --- a/PYTHON/lichess_bot/engine.py +++ b/PYTHON/lichess_bot/engine.py @@ -1,15 +1,11 @@ import os -import shutil import subprocess -import logging -from typing import Optional, Tuple import chess class RandomEngine: - """ - Thin wrapper around the C engine in C/lichess_random_engine/random_engine. + """Thin wrapper around the C engine in C/lichess_random_engine/random_engine. Contract: - Given a chess.Board, call the C binary with all legal moves encoded as @@ -20,7 +16,13 @@ class RandomEngine: - If the binary is missing or returns an invalid/illegal move, raise. """ - def __init__(self, *, engine_path: Optional[str] = None, max_time_sec: float = 2.0, depth: Optional[int] = None): + def __init__( + self, + *, + engine_path: str | None = None, + max_time_sec: float = 2.0, + depth: int | None = None, + ): self.max_time_sec = max_time_sec # depth is accepted for compatibility with existing callers but is unused; # the C engine handles its own scoring/selection. @@ -29,12 +31,17 @@ class RandomEngine: default_path = os.path.abspath( os.path.join( os.path.dirname(__file__), - "..", "..", - "C", "lichess_random_engine", "random_engine", + "..", + "..", + "C", + "lichess_random_engine", + "random_engine", ) ) self.engine_path = engine_path or default_path - if not os.path.isfile(self.engine_path) or not os.access(self.engine_path, os.X_OK): + if not os.path.isfile(self.engine_path) or not os.access( + self.engine_path, os.X_OK + ): raise FileNotFoundError( f"C engine not found or not executable at '{self.engine_path}'. " "Build it first (make -C C/lichess_random_engine)." @@ -58,10 +65,14 @@ class RandomEngine: return out def choose_move(self, board: chess.Board) -> chess.Move: - mv, _ = self.choose_move_with_explanation(board, time_budget_sec=self.max_time_sec) + mv, _ = self.choose_move_with_explanation( + board, time_budget_sec=self.max_time_sec + ) return mv - def choose_move_with_explanation(self, board: chess.Board, *, time_budget_sec: float) -> Tuple[Optional[chess.Move], str]: + def choose_move_with_explanation( + self, board: chess.Board, *, time_budget_sec: float + ) -> tuple[chess.Move | None, str]: # Collect legal moves and send to engine as plain UCI tokens. legal = list(board.legal_moves) if not legal: @@ -78,10 +89,14 @@ class RandomEngine: try: move = chess.Move.from_uci(chosen_uci) except Exception: - raise RuntimeError(f"Engine returned invalid move: '{chosen_uci}' (output: {output!r})") + raise RuntimeError( + f"Engine returned invalid move: '{chosen_uci}' (output: {output!r})" + ) if move not in board.legal_moves: - raise RuntimeError(f"Engine returned illegal move for position: {chosen_uci}") + raise RuntimeError( + f"Engine returned illegal move for position: {chosen_uci}" + ) return move, "from_c_engine" @@ -91,9 +106,8 @@ class RandomEngine: proposed_move_uci: str, *, time_budget_sec: float, - ) -> Tuple[float, str, Optional[chess.Move], str]: - """ - Ask the C engine to explain the current move list and analyze a specific candidate. + ) -> tuple[float, str, chess.Move | None, str]: + """Ask the C engine to explain the current move list and analyze a specific candidate. Returns (candidate_score, candidate_expl, best_move, best_expl) where explanations are concise JSON snippets from the engine. All logic is @@ -103,13 +117,16 @@ class RandomEngine: if not legal: return 0.0, "no_legal_moves", None, "no_best_move" - args = ["--fen", board.fen(), "--explain", "--analyze", proposed_move_uci] + [m.uci() for m in legal] + args = ["--fen", board.fen(), "--explain", "--analyze", proposed_move_uci] + [ + m.uci() for m in legal + ] out = self._call_engine(args, timeout=max(0.1, time_budget_sec)) # Try to parse the engine's JSON explanation import json as _json + cand_score = 0.0 - best_move: Optional[chess.Move] = None + best_move: chess.Move | None = None cand_expl = out best_expl = out try: @@ -130,10 +147,13 @@ class RandomEngine: best_move = None # Store compact explanations for debugging cand_expl = _json.dumps(analyze, ensure_ascii=False) - best_expl = _json.dumps({ - "chosen_index": data.get("chosen_index"), - "chosen_move": data.get("chosen_move"), - }, ensure_ascii=False) + best_expl = _json.dumps( + { + "chosen_index": data.get("chosen_index"), + "chosen_move": data.get("chosen_move"), + }, + ensure_ascii=False, + ) except Exception: # Leave defaults with raw output text pass diff --git a/PYTHON/lichess_bot/lichess_api.py b/PYTHON/lichess_bot/lichess_api.py index ad7e504..9c1d480 100644 --- a/PYTHON/lichess_bot/lichess_api.py +++ b/PYTHON/lichess_bot/lichess_api.py @@ -1,26 +1,29 @@ +from collections.abc import Generator import json import logging import time -from typing import Dict, Generator, Optional, Tuple -import requests import chess - +import requests LICHESS_API = "https://lichess.org" class LichessAPI: - def __init__(self, token: str, session: Optional[requests.Session] = None): + def __init__(self, token: str, session: requests.Session | None = None): self.token = token self.session = session or requests.Session() - self.session.headers.update({ - "Authorization": f"Bearer {self.token}", - "Accept": "application/json", - "User-Agent": "minimal-lichess-bot/0.1 (+https://lichess.org)" - }) + self.session.headers.update( + { + "Authorization": f"Bearer {self.token}", + "Accept": "application/json", + "User-Agent": "minimal-lichess-bot/0.1 (+https://lichess.org)", + } + ) - def _request(self, method: str, url: str, *, raise_for_status: bool = False, **kwargs) -> requests.Response: + def _request( + self, method: str, url: str, *, raise_for_status: bool = False, **kwargs + ) -> requests.Response: """Wrapper around session.request that logs every request/response. - Logs start (method+URL) and end (status, elapsed). @@ -32,7 +35,7 @@ class LichessAPI: try: r = self.session.request(method, url, **kwargs) except Exception as e: - logging.error(f"HTTP {method} {url} -> exception: {e}") + logging.exception(f"HTTP {method} {url} -> exception: {e}") raise elapsed = time.monotonic() - t0 status = r.status_code @@ -45,7 +48,9 @@ class LichessAPI: except Exception: snippet = None if snippet: - logging.warning(f"HTTP {method} {url} -> {status} in {elapsed:.2f}s body='{snippet}'") + logging.warning( + f"HTTP {method} {url} -> {status} in {elapsed:.2f}s body='{snippet}'" + ) else: logging.warning(f"HTTP {method} {url} -> {status} in {elapsed:.2f}s") else: @@ -54,14 +59,16 @@ class LichessAPI: r.raise_for_status() return r - def stream_events(self) -> Generator[Dict, None, None]: + def stream_events(self) -> Generator[dict, None, None]: url = f"{LICHESS_API}/api/stream/event" backoff = 0.5 while True: try: # Use NDJSON Accept and no timeout for long-lived stream headers = {"Accept": "application/x-ndjson"} - with self._request("GET", url, headers=headers, stream=True, timeout=None) as r: + with self._request( + "GET", url, headers=headers, stream=True, timeout=None + ) as r: r.raise_for_status() backoff = 0.5 # reset on success for line in r.iter_lines(decode_unicode=True): @@ -89,7 +96,9 @@ class LichessAPI: data = {"reason": reason} self._request("POST", url, data=data, timeout=30, raise_for_status=True) - def join_game_stream(self, game_id: str, my_color: Optional[str]) -> Tuple[chess.Board, str]: + def join_game_stream( + self, game_id: str, my_color: str | None + ) -> tuple[chess.Board, str]: """Deprecated: use stream_game_events and parse initial state there.""" # Fallback to initial behavior for compatibility url = f"{LICHESS_API}/api/board/game/stream/{game_id}" @@ -125,7 +134,7 @@ class LichessAPI: break return board, color - def stream_game_events(self, game_id: str) -> Generator[Dict, None, None]: + def stream_game_events(self, game_id: str) -> Generator[dict, None, None]: url = f"{LICHESS_API}/api/board/game/stream/{game_id}" headers = {"Accept": "application/x-ndjson"} with self._request("GET", url, headers=headers, stream=True, timeout=None) as r: @@ -151,11 +160,11 @@ class LichessAPI: r = self._request("POST", url, timeout=30) r.raise_for_status() - def get_game_state(self, game_id: str) -> Optional[Dict]: + def get_game_state(self, game_id: str) -> dict | None: """Deprecated: use stream_game_events in a persistent loop.""" return None - def get_my_user_id(self) -> Optional[str]: + def get_my_user_id(self) -> str | None: url = f"{LICHESS_API}/api/account" r = self._request("GET", url, timeout=30) if r.status_code == 200: diff --git a/PYTHON/lichess_bot/main.py b/PYTHON/lichess_bot/main.py index c752eab..6ed9eb3 100644 --- a/PYTHON/lichess_bot/main.py +++ b/PYTHON/lichess_bot/main.py @@ -2,14 +2,12 @@ import argparse import json import logging import os +import subprocess +import sys import threading -import time -from typing import Optional import chess import chess.pgn -import subprocess -import sys from .engine import RandomEngine from .lichess_api import LichessAPI @@ -35,10 +33,10 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No game_threads = {} - def handle_game(game_id: str, my_color: Optional[str] = None): + def handle_game(game_id: str, my_color: str | None = None): logging.info(f"Starting game thread for {game_id} [bot v{bot_version}]") board = chess.Board() - color: Optional[str] = my_color + color: str | None = my_color # Track how many moves we have already processed; start at -1 so we act on the first state (0 moves) last_handled_len = -1 # Prepare a per-game log file @@ -54,14 +52,13 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No opp_ms = None inc_ms = 0 # Meta info for logging/PGN - game_date_iso: Optional[str] = None - white_name: Optional[str] = None - black_name: Optional[str] = None - site_url: Optional[str] = None + game_date_iso: str | None = None + white_name: str | None = None + black_name: str | None = None + site_url: str | None = None try: # Only send moves on authoritative gameState events to avoid race # conditions right after gameFull arrives. - seen_game_full = False for event in api.stream_game_events(game_id): et = event.get("type") if et in ("gameFull", "gameState"): @@ -71,8 +68,16 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No moves = state.get("moves", "") status = state.get("status") # clocks are in milliseconds if present - my_ms = state.get("wtime") if color == "white" else state.get("btime") - opp_ms = state.get("btime") if color == "white" else state.get("wtime") + my_ms = ( + state.get("wtime") + if color == "white" + else state.get("btime") + ) + opp_ms = ( + state.get("btime") + if color == "white" + else state.get("wtime") + ) inc_ms = state.get("winc") or state.get("binc") or 0 # Discover my color from gameFull white_id = event["white"].get("id") @@ -82,10 +87,15 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No # Set site and date if available try: # Lichess event may include 'createdAt' ms epoch - created_ms = event.get("createdAt") or event.get("createdAtDate") + created_ms = event.get("createdAt") or event.get( + "createdAtDate" + ) if created_ms: import datetime - game_date_iso = datetime.datetime.utcfromtimestamp(int(created_ms)/1000).strftime("%Y.%m.%d") + + game_date_iso = datetime.datetime.utcfromtimestamp( + int(created_ms) / 1000 + ).strftime("%Y.%m.%d") except Exception: pass site_url = f"https://lichess.org/{game_id}" @@ -95,7 +105,6 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No elif me == black_id: color = "black" logging.info(f"Game {game_id}: joined as {color} (gameFull)") - seen_game_full = True else: moves = event.get("moves", "") status = event.get("status") @@ -115,7 +124,9 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No f"Game {game_id}: event={et}, moves={new_len}, color={color}" ) if new_len == last_handled_len: - logging.debug(f"Game {game_id}: position unchanged (len={new_len}), skipping") + logging.debug( + f"Game {game_id}: position unchanged (len={new_len}), skipping" + ) continue # Rebuild board from moves @@ -127,14 +138,18 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No logging.debug(f"Game {game_id}: could not apply move {m}") if color is None: - logging.info(f"Game {game_id}: color unknown yet; waiting for gameFull") + logging.info( + f"Game {game_id}: color unknown yet; waiting for gameFull" + ) # Do not mark this position handled on gameFull; wait for authoritative gameState if et == "gameState": last_handled_len = new_len continue is_white_turn = board.turn - my_turn = (is_white_turn and color == "white") or ((not is_white_turn) and color == "black") + my_turn = (is_white_turn and color == "white") or ( + (not is_white_turn) and color == "black" + ) logging.info( f"Game {game_id}: turn={'white' if is_white_turn else 'black'}, my_turn={my_turn}" ) @@ -142,35 +157,54 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No # - Always move on 'gameState' (authoritative) # - Also allow moving on the initial 'gameFull' when there are zero moves and it's our turn. # This avoids stalling at game start when Lichess doesn't immediately send a 'gameState' for 0 moves. - allow_move = (et == "gameState") or (et == "gameFull" and new_len == 0) + allow_move = (et == "gameState") or ( + et == "gameFull" and new_len == 0 + ) if my_turn and allow_move: # Compute a per-move time budget (seconds) based on remaining time # Heuristic: use min( max_time_sec, max(0.05, 0.6 * my_time_left/remaining_moves + inc) ) # Estimate remaining moves as 30 - ply/2 bounded to [10, 60] - est_moves_left = max(10, min(60, 30 - board.fullmove_number // 2)) + est_moves_left = max( + 10, min(60, 30 - board.fullmove_number // 2) + ) time_left_sec = (my_ms or 0) / 1000.0 inc_sec = (inc_ms or 0) / 1000.0 - budget = 0.6 * (time_left_sec / max(1, est_moves_left)) + 0.5 * inc_sec + budget = ( + 0.6 * (time_left_sec / max(1, est_moves_left)) + + 0.5 * inc_sec + ) # Spend more time per move (requested): double the budget budget *= 2.0 # Keep within reasonable bounds budget = max(0.05, min(engine.max_time_sec, budget)) - move, reason = engine.choose_move_with_explanation(board, time_budget_sec=budget) + move, reason = engine.choose_move_with_explanation( + board, time_budget_sec=budget + ) if move is None: - logging.info(f"Game {game_id}: no legal moves (game likely over)") + logging.info( + f"Game {game_id}: no legal moves (game likely over)" + ) break try: # Double-check legality just before sending to avoid 400s when state changed. if move not in board.legal_moves: - logging.info(f"Game {game_id}: selected move no longer legal; skipping send") + logging.info( + f"Game {game_id}: selected move no longer legal; skipping send" + ) else: - logging.info(f"Game {game_id}: playing {move.uci()} (budget={budget:.2f}s, my_time_left={time_left_sec:.1f}s, inc={inc_sec:.2f}s)") + logging.info( + f"Game {game_id}: playing {move.uci()} (budget={budget:.2f}s, my_time_left={time_left_sec:.1f}s, inc={inc_sec:.2f}s)" + ) if game_log_path: with open(game_log_path, "a") as lf: - lf.write(f"ply {last_handled_len+1}: {move.uci()}\n{reason}\n\n") + lf.write( + f"ply {last_handled_len+1}: {move.uci()}\n{reason}\n\n" + ) api.make_move(game_id, move) except Exception as e: - logging.warning(f"Game {game_id}: move {move.uci()} failed: {e}") + logging.warning( + f"Game {game_id}: move {move.uci()} failed: {e}" + ) # Mark this position as handled on authoritative gameState, or after we've # actually attempted a move (including the first move on gameFull len=0). if et == "gameState" or (my_turn and allow_move): @@ -178,9 +212,7 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No if status in {"mate", "resign", "stalemate", "timeout", "draw"}: logging.info(f"Game {game_id} finished: {status}") break - elif et == "chatLine": - continue - elif et == "opponentGone": + elif et == "chatLine" or et == "opponentGone": continue except Exception as e: logging.exception(f"Game {game_id} thread error: {e}") @@ -204,12 +236,14 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No pass with open(game_log_path, "a") as lf: lf.write("\nPGN:\n") - exporter = chess.pgn.StringExporter(headers=True, variations=False, comments=False) + exporter = chess.pgn.StringExporter( + headers=True, variations=False, comments=False + ) lf.write(game.accept(exporter)) lf.write("\n") # After PGN is written, run analysis and save it to the same file (inserted before PGN) if game_log_path: - analysis_text: Optional[str] = None + analysis_text: str | None = None try: analyze_script = os.path.join( os.path.dirname(os.path.dirname(__file__)), @@ -245,7 +279,11 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No if m: # Count as one analyzed ply analyzed += 1 - left = max(0, (total_plies or 0) - analyzed) if total_plies else "?" + left = ( + max(0, (total_plies or 0) - analyzed) + if total_plies + else "?" + ) if total_plies: pct = analyzed / total_plies * 100.0 logging.info( @@ -266,7 +304,7 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No f"Game {game_id}: analysis script exited with code {ret}" ) if stderr_text: - analysis_text += ("\n[stderr]\n" + stderr_text) + analysis_text += "\n[stderr]\n" + stderr_text logging.info(f"Game {game_id}: analysis complete") else: logging.info( @@ -278,14 +316,18 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No # Insert analysis before the PGN section so future runs can still parse PGN cleanly if analysis_text: try: - with open(game_log_path, "r", encoding="utf-8", errors="replace") as f: + with open( + game_log_path, encoding="utf-8", errors="replace" + ) as f: content = f.read() # Find the start of the 'PGN:' line insert_idx = 0 p = content.find("\nPGN:\n") if p != -1: - insert_idx = p + 1 # start of the line after the preceding newline + insert_idx = ( + p + 1 + ) # start of the line after the preceding newline elif content.startswith("PGN:\n"): insert_idx = 0 else: @@ -297,21 +339,31 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No if game_date_iso: meta_lines.append(f"Date: {game_date_iso}") if white_name or black_name: - meta_lines.append(f"Players: {white_name or '?'} vs {black_name or '?'}") + meta_lines.append( + f"Players: {white_name or '?'} vs {black_name or '?'}" + ) if meta_lines: meta_block = "\n".join(meta_lines) + "\n" else: meta_block = "" analysis_block = ( - (meta_block if meta_block else "") + - "ANALYSIS:\n" + analysis_text.rstrip() + "\n\n" + (meta_block if meta_block else "") + + "ANALYSIS:\n" + + analysis_text.rstrip() + + "\n\n" + ) + new_content = ( + content[:insert_idx] + + analysis_block + + content[insert_idx:] ) - new_content = content[:insert_idx] + analysis_block + content[insert_idx:] with open(game_log_path, "w", encoding="utf-8") as f: f.write(new_content) except Exception as e: - logging.debug(f"Game {game_id}: could not write analysis to log: {e}") + logging.debug( + f"Game {game_id}: could not write analysis to log: {e}" + ) except Exception as e: logging.debug(f"Game {game_id}: could not write PGN: {e}") logging.info(f"Ending game thread for {game_id}") @@ -328,19 +380,29 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No variant = challenge.get("variant", {}).get("key", "standard") speed = challenge.get("speed") perf_ok = speed in {"bullet", "blitz", "rapid", "classical"} - not_corr = challenge.get("speed") != "correspondence" or not decline_correspondence + not_corr = ( + challenge.get("speed") != "correspondence" + or not decline_correspondence + ) if variant == "standard" and perf_ok and not_corr: logging.info(f"Accepting challenge {ch_id} ({speed})") api.accept_challenge(ch_id) else: - logging.info(f"Declining challenge {ch_id} (variant={variant}, speed={speed})") + logging.info( + f"Declining challenge {ch_id} (variant={variant}, speed={speed})" + ) api.decline_challenge(ch_id) elif event.get("type") == "gameStart": game_id = event["game"]["id"] # Spin up a game thread - if game_id not in game_threads or not game_threads[game_id].is_alive(): - t = threading.Thread(target=handle_game, args=(game_id,), name=f"game-{game_id}") + if ( + game_id not in game_threads + or not game_threads[game_id].is_alive() + ): + t = threading.Thread( + target=handle_game, args=(game_id,), name=f"game-{game_id}" + ) t.daemon = True game_threads[game_id] = t t.start() @@ -359,8 +421,14 @@ def run_bot(log_level: str = "INFO", decline_correspondence: bool = False) -> No def main(): parser = argparse.ArgumentParser(description="Run a minimal Lichess bot") - parser.add_argument("--log-level", default="INFO", help="Logging level (default: INFO)") - parser.add_argument("--decline-correspondence", action="store_true", help="Decline correspondence challenges") + parser.add_argument( + "--log-level", default="INFO", help="Logging level (default: INFO)" + ) + parser.add_argument( + "--decline-correspondence", + action="store_true", + help="Decline correspondence challenges", + ) args = parser.parse_args() run_bot(args.log_level, args.decline_correspondence) diff --git a/PYTHON/lichess_bot/requirements.txt b/PYTHON/lichess_bot/requirements.txt index 87fe6a8..a4c71ca 100644 --- a/PYTHON/lichess_bot/requirements.txt +++ b/PYTHON/lichess_bot/requirements.txt @@ -1,6 +1,6 @@ -python-chess==1.999 -requests==2.32.3 -urllib3==2.2.3 certifi>=2024.2.2 chardet>=5.2.0 idna>=3.7 +python-chess==1.999 +requests==2.32.3 +urllib3==2.2.3 diff --git a/PYTHON/lichess_bot/tests/conftest.py b/PYTHON/lichess_bot/tests/conftest.py index ebefd44..a482f71 100644 --- a/PYTHON/lichess_bot/tests/conftest.py +++ b/PYTHON/lichess_bot/tests/conftest.py @@ -1,6 +1,6 @@ import os -import sys from pathlib import Path +import sys # Add repository root to sys.path so 'import PYTHON.*' works when running # pytest with a subdirectory as rootdir. diff --git a/PYTHON/lichess_bot/tests/test_puzzles.py b/PYTHON/lichess_bot/tests/test_puzzles.py index 945c318..d3a6fe3 100644 --- a/PYTHON/lichess_bot/tests/test_puzzles.py +++ b/PYTHON/lichess_bot/tests/test_puzzles.py @@ -1,6 +1,5 @@ import csv import os -from typing import List, Tuple import chess import pytest @@ -8,12 +7,11 @@ import pytest from PYTHON.lichess_bot.engine import RandomEngine -def _load_top_puzzles(csv_path: str, limit: int = 8) -> List[Tuple[str, str]]: - """ - Return a list of (FEN, solution_moves_str) for the first `limit` rows in the CSV. +def _load_top_puzzles(csv_path: str, limit: int = 8) -> list[tuple[str, str]]: + """Return a list of (FEN, solution_moves_str) for the first `limit` rows in the CSV. CSV columns: PuzzleId,FEN,Moves,... """ - puzzles: List[Tuple[str, str]] = [] + puzzles: list[tuple[str, str]] = [] with open(csv_path, newline="", encoding="utf-8") as f: reader = csv.DictReader(f) for row in reader: @@ -28,7 +26,9 @@ def _load_top_puzzles(csv_path: str, limit: int = 8) -> List[Tuple[str, str]]: @pytest.mark.parametrize( "fen,moves_str", - _load_top_puzzles(os.path.join(os.path.dirname(__file__), "lichess_db_puzzle.csv"), limit=8), + _load_top_puzzles( + os.path.join(os.path.dirname(__file__), "lichess_db_puzzle.csv"), limit=8 + ), ) def test_puzzle_engine_follow_solution(fen: str, moves_str: str): board = chess.Board(fen) @@ -46,7 +46,11 @@ def test_puzzle_engine_follow_solution(fen: str, moves_str: str): # If engine move differs from solution, fail immediately but provide analysis of the correct move if mv.uci() != uci: # Ask the engine to analyze the correct move for debug - score_cp, proposed_expl, best_mv, best_expl = eng.evaluate_proposed_move_with_suggestion(board, uci, time_budget_sec=0.5) + score_cp, proposed_expl, best_mv, best_expl = ( + eng.evaluate_proposed_move_with_suggestion( + board, uci, time_budget_sec=0.5 + ) + ) details = [ f"Puzzle failed at step {step}.", f"FEN: {fen}", diff --git a/PYTHON/lichess_bot/tests/test_utils.py b/PYTHON/lichess_bot/tests/test_utils.py index ed184a6..a2489ca 100644 --- a/PYTHON/lichess_bot/tests/test_utils.py +++ b/PYTHON/lichess_bot/tests/test_utils.py @@ -1,5 +1,3 @@ -import builtins - from PYTHON.lichess_bot.utils import backoff_sleep diff --git a/PYTHON/lichess_bot/tests/test_versioning.py b/PYTHON/lichess_bot/tests/test_versioning.py index bf5026c..a41f30c 100644 --- a/PYTHON/lichess_bot/tests/test_versioning.py +++ b/PYTHON/lichess_bot/tests/test_versioning.py @@ -1,6 +1,3 @@ -import os -import tempfile - from PYTHON.lichess_bot.utils import get_and_increment_version @@ -15,5 +12,5 @@ def test_version_file_increments_and_persists(tmp_path, monkeypatch): assert v2 == 2 # Ensure it persisted - with open(version_file, "r") as f: + with open(version_file) as f: assert f.read().strip() == "2" diff --git a/PYTHON/lichess_bot/tools/generate_blunder_tests.py b/PYTHON/lichess_bot/tools/generate_blunder_tests.py old mode 100644 new mode 100755 index 6e4a389..ef757f9 --- a/PYTHON/lichess_bot/tools/generate_blunder_tests.py +++ b/PYTHON/lichess_bot/tools/generate_blunder_tests.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -""" -Generate pytest cases from one or more lichess analysis logs. +"""Generate pytest cases from one or more lichess analysis logs. Input: log files that contain a "Columns:" section and a "PGN:" section. We'll extract each row where class==Blunder, reconstruct the FEN of the @@ -33,12 +32,11 @@ Dependencies: python-chess, pytest (already in requirements.txt) from __future__ import annotations +from dataclasses import dataclass import io import os import re import sys -from dataclasses import dataclass -from typing import List, Tuple import chess import chess.pgn @@ -48,11 +46,11 @@ import chess.pgn class Blunder: ply: int side: str # 'W' or 'B' - san: str # SAN of the played blunder + san: str # SAN of the played blunder best_suggestion_san: str # SAN of the best suggestion from log (mandatory) -def parse_columns_for_blunders(text: str) -> List[Blunder]: +def parse_columns_for_blunders(text: str) -> list[Blunder]: lines = text.splitlines() # Find start of "Columns:" block try: @@ -60,9 +58,9 @@ def parse_columns_for_blunders(text: str) -> List[Blunder]: except StopIteration: return [] - blunders: List[Blunder] = [] + blunders: list[Blunder] = [] # Lines after header until a blank line or "PGN:" marker - for ln in lines[idx + 1:]: + for ln in lines[idx + 1 :]: if not ln.strip(): break if ln.strip().startswith("PGN:"): @@ -90,13 +88,20 @@ def parse_columns_for_blunders(text: str) -> List[Blunder]: f"Missing best_suggestion in Columns for blunder row: ply={ply} side={side} move={move_san}.\n" f"Raw line: '{ln.strip()}'" ) - blunders.append(Blunder(ply=ply, side=side, san=move_san, best_suggestion_san=best_suggestion_san)) + blunders.append( + Blunder( + ply=ply, + side=side, + san=move_san, + best_suggestion_san=best_suggestion_san, + ) + ) return blunders def extract_pgn(text: str) -> str | None: # Extract the PGN block after a line that is exactly 'PGN:' or starts with it - m = re.search(r"^PGN:\s*$", text, flags=re.M) + m = re.search(r"^PGN:\s*$", text, flags=re.MULTILINE) if not m: return None start = m.end() @@ -104,8 +109,8 @@ def extract_pgn(text: str) -> str | None: return pgn if pgn else None -def san_list_from_game(game: chess.pgn.Game) -> List[str]: - san_moves: List[str] = [] +def san_list_from_game(game: chess.pgn.Game) -> list[str]: + san_moves: list[str] = [] node = game while node.variations: node = node.variation(0) @@ -113,13 +118,15 @@ def san_list_from_game(game: chess.pgn.Game) -> List[str]: return san_moves -def fen_and_uci_for_blunders(pgn_text: str, blunders: List[Blunder]) -> List[Tuple[str, str, str, Blunder]]: +def fen_and_uci_for_blunders( + pgn_text: str, blunders: list[Blunder] +) -> list[tuple[str, str, str, Blunder]]: game = chess.pgn.read_game(io.StringIO(pgn_text)) if game is None: raise RuntimeError("Failed to parse PGN from log") main_sans = san_list_from_game(game) - results: List[Tuple[str, str, str, Blunder]] = [] + results: list[tuple[str, str, str, Blunder]] = [] for bl in blunders: # Reconstruct the board before this ply board = game.board() @@ -198,17 +205,25 @@ def test_engine_avoids_logged_blunder(fen, blunder_uci, label): ) -def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, str, Blunder]]) -> int: +def append_cases_to_unified_test( + unified_path: str, cases: list[tuple[str, str, str, Blunder]] +) -> int: """Append new cases to BLUNDER_CASES in the unified test file, skipping duplicates. Returns the number of cases actually appended. """ ensure_unified_test_file(unified_path) - with open(unified_path, "r", encoding="utf-8") as f: + with open(unified_path, encoding="utf-8") as f: content = f.read() # Extract current cases as a set of (fen, uci) to de-duplicate - existing = set(re.findall(r"\(\"(.*?)\",\s*\"(.*?)\",\s*\"ply\d+_[WB]_[^\"]+\"\)\,?", content, flags=re.S)) + existing = set( + re.findall( + r"\(\"(.*?)\",\s*\"(.*?)\",\s*\"ply\d+_[WB]_[^\"]+\"\)\,?", + content, + flags=re.DOTALL, + ) + ) lines = [] updated_existing = 0 @@ -217,7 +232,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, if key in existing: # If a best move UCI is available, try to backfill or update it into the label if best_uci: - side = 'W' if bl.side == 'W' else 'B' + side = "W" if bl.side == "W" else "B" fen_re = re.escape(fen) uci_re = re.escape(uci) base_label = f"ply{bl.ply}_{side}_{uci}" @@ -228,7 +243,9 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, if re.search(pattern_no_best, content): content = re.sub( pattern_no_best, - lambda m: m.group(0).replace(m.group(1), f"{base_label}_best_{best_uci}"), + lambda m: m.group(0).replace( + m.group(1), f"{base_label}_best_{best_uci}" + ), content, count=1, ) @@ -236,7 +253,9 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, elif re.search(pattern_with_best, content): content = re.sub( pattern_with_best, - lambda m: m.group(0).replace(m.group(1), f"{base_label}_best_{best_uci}"), + lambda m: m.group(0).replace( + m.group(1), f"{base_label}_best_{best_uci}" + ), content, count=1, ) @@ -245,7 +264,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, label = f"ply{bl.ply}_{'W' if bl.side=='W' else 'B'}_{uci}" # Encode the best move UCI in the label so tests can extract it without changing tuple shape label += f"_best_{best_uci}" - lines.append(f" (\"{fen}\", \"{uci}\", \"{label}\"),\n") + lines.append(f' ("{fen}", "{uci}", "{label}"),\n') if not lines: return 0 @@ -267,7 +286,7 @@ def append_cases_to_unified_test(unified_path: str, cases: List[Tuple[str, str, def _process_single_log(log_path: str) -> int: """Process a single log file. Returns 0 on success, non-zero otherwise.""" try: - with open(log_path, "r", encoding="utf-8") as fh: + with open(log_path, encoding="utf-8") as fh: text = fh.read() except FileNotFoundError: print(f"Log file not found: {log_path}") @@ -293,7 +312,9 @@ def _process_single_log(log_path: str) -> int: print(f"Error converting SAN to UCI in {os.path.basename(log_path)}: {e}") return 2 if not cases: - print(f"Failed to reconstruct any blunder positions from PGN: {os.path.basename(log_path)}") + print( + f"Failed to reconstruct any blunder positions from PGN: {os.path.basename(log_path)}" + ) return 1 base = os.path.basename(log_path) @@ -301,14 +322,18 @@ def _process_single_log(log_path: str) -> int: game_id = m.group(1) if m else os.path.splitext(base)[0] # Always append to the unified test file - unified = os.path.join(os.path.dirname(__file__), "..", "tests", "test_blunders_all.py") + unified = os.path.join( + os.path.dirname(__file__), "..", "tests", "test_blunders_all.py" + ) unified = os.path.abspath(unified) added = append_cases_to_unified_test(unified, cases) - print(f"Appended {added} new blunder checks to {os.path.relpath(unified)} (game {game_id}).") + print( + f"Appended {added} new blunder checks to {os.path.relpath(unified)} (game {game_id})." + ) return 0 -def main(argv: List[str]) -> int: +def main(argv: list[str]) -> int: script_dir = os.path.dirname(__file__) past_dir = os.path.abspath(os.path.join(script_dir, "past_games")) @@ -332,7 +357,9 @@ def main(argv: List[str]) -> int: rc = _process_single_log(lp) if rc == 0: ok += 1 - print(f"Processed {len(logs)} logs from {past_dir}, succeeded: {ok}, failed: {len(logs)-ok}") + print( + f"Processed {len(logs)} logs from {past_dir}, succeeded: {ok}, failed: {len(logs)-ok}" + ) return 0 if ok > 0 else 1 # One argument: game id or file path @@ -340,15 +367,14 @@ def main(argv: List[str]) -> int: candidate_path = None if os.path.isfile(arg): candidate_path = arg + # Treat as game id, resolve within past_games + elif re.fullmatch(r"[A-Za-z0-9]+", arg): + candidate_path = os.path.join(past_dir, f"lichess_bot_game_{arg}.log") else: - # Treat as game id, resolve within past_games - if re.fullmatch(r"[A-Za-z0-9]+", arg): - candidate_path = os.path.join(past_dir, f"lichess_bot_game_{arg}.log") - else: - # Fallback: if it's a bare filename, try inside past_games - maybe = os.path.join(past_dir, arg) - if os.path.isfile(maybe): - candidate_path = maybe + # Fallback: if it's a bare filename, try inside past_games + maybe = os.path.join(past_dir, arg) + if os.path.isfile(maybe): + candidate_path = maybe if not candidate_path: print("Usage: generate_blunder_tests.py [|]") diff --git a/PYTHON/lichess_bot/utils.py b/PYTHON/lichess_bot/utils.py index c262c37..6567b82 100644 --- a/PYTHON/lichess_bot/utils.py +++ b/PYTHON/lichess_bot/utils.py @@ -22,7 +22,7 @@ def get_and_increment_version() -> int: path = _version_file_path() current = 0 try: - with open(path, "r") as f: + with open(path) as f: raw = f.read().strip() if raw: current = int(raw) @@ -54,7 +54,7 @@ def backoff_sleep(current_backoff: int, base: float = 0.5, cap: float = 8.0) -> - base: base delay in seconds - cap: maximum delay in seconds """ - delay = min(cap, base * (2 ** current_backoff)) + delay = min(cap, base * (2**current_backoff)) logging.info(f"Backing off for {delay:.1f}s") time.sleep(delay) return min(current_backoff + 1, 10) diff --git a/PYTHON/mock_server/README.md b/PYTHON/mock_server/README.md new file mode 100644 index 0000000..e443e4d --- /dev/null +++ b/PYTHON/mock_server/README.md @@ -0,0 +1,54 @@ +# Simulate Connection Failure + +(it takes ≈ 1hr to get it working) + +Install: https://mitmproxy.org/ (install it the way they recommend for your OS, for Ubuntu specifically apt version is 4 main versions behind newest version) + +Run it using `mitmproxy` + +Run your webbrowser using `127.0.0.1:8080` as proxy +on chromium-based browser it should be enough to do: +`google-chrome --proxy-server=127.0.0.1:8080` + +Run `mitmweb` + +open 127.0.0.1:808**1** + +Click `File` in upper left corner and then `Install Certificates` + +You should get a list of Windows/Linux/macOS/Firefox with certificates and how to install them + +Install certificates using those instructions + +**important!** Go to your browser certificate settings and ensure that: + +1. mitmproxy certificate is imported +2. **it is set to trusted** + +Now all of your network communication should go through mitmproxy, you can verify it by going to 127.0.0.1:808**1** and seeing constant flow of requests + +## What can we do with it? + +1. Install mitmproxy python library using pip + +`pip install mitmproxy` + +2. Copy and paste this “hello world”: + +``` +from mitmproxy import http + +def request(flow: http.HTTPFlow) -> None: + # Only intercept traffic to example.com + if "example.com" in flow.request.host: + flow.response = http.Response.make( + 502, # Bad Gateway status code + b"Simulated connection failure", + {"Content-Type": "text/plain"} + ) + +``` + +3. Run it: `mitmdump -s mitm_world.py` +4. Go to [example.com](http://example.com) +5. You should see “simulated connection failure” in plain text diff --git a/PYTHON/mock_server/mock_server.py b/PYTHON/mock_server/mock_server.py new file mode 100644 index 0000000..3f0e8bc --- /dev/null +++ b/PYTHON/mock_server/mock_server.py @@ -0,0 +1,11 @@ +from mitmproxy import http + + +def request(flow: http.HTTPFlow) -> None: + # Only intercept traffic to example.com + if "example.com" in flow.request.host: + flow.response = http.Response.make( + 502, # Bad Gateway status code + b"Simulated connection failure", + {"Content-Type": "text/plain"}, + ) diff --git a/PYTHON/pdfCentered/.gitignore b/PYTHON/pdfCentered/.gitignore index 48d5b32..e2b048f 100644 --- a/PYTHON/pdfCentered/.gitignore +++ b/PYTHON/pdfCentered/.gitignore @@ -160,4 +160,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -*.pdf \ No newline at end of file +*.pdf diff --git a/PYTHON/pdfCentered/pytest.ini b/PYTHON/pdfCentered/pytest.ini index ddc61ae..f745106 100644 --- a/PYTHON/pdfCentered/pytest.ini +++ b/PYTHON/pdfCentered/pytest.ini @@ -1,4 +1,4 @@ [pytest] filterwarnings = - ignore::DeprecationWarning \ No newline at end of file + ignore::DeprecationWarning diff --git a/PYTHON/randomJPG/Readme.md b/PYTHON/randomJPG/Readme.md new file mode 100644 index 0000000..cf4e040 --- /dev/null +++ b/PYTHON/randomJPG/Readme.md @@ -0,0 +1 @@ +Did you ever need to generate random jpg images with huge file size? Now you can! diff --git a/PYTHON/randomJPG/generateJpeg.py b/PYTHON/randomJPG/generateJpeg.py new file mode 100644 index 0000000..a422c37 --- /dev/null +++ b/PYTHON/randomJPG/generateJpeg.py @@ -0,0 +1,133 @@ +import argparse +from datetime import datetime +import os +import random + +from PIL import Image + + +def generate_bloated_jpeg( + size, color_list, block_size, output_path, quality, image_index, folder +): + """Generates a random JPEG image with given size, list of colors, and block size. + + Args: + size (int): Size of the image (both width and height, must be divisible by block_size). + color_list (list of str): List of colors in hex format. + block_size (int): Size of the pixel blocks. + output_path (str): Output path for the JPEG image. + quality (int): Quality setting for the JPEG image (0-100). + image_index (int): Index of the image for unique naming. + folder (str): Folder to save the image. + """ + # Ensure size is divisible by block_size and does not exceed 1000 pixels + if size > 1000 or size % block_size != 0: + raise ValueError("Size must be 1000 pixels or less and divisible by block_size") + + # Create a new image + image = Image.new("RGB", (size, size)) + pixels = image.load() + + # Convert hex colors to RGB + rgb_colors = [ + tuple(int(color[i : i + 2], 16) for i in (1, 3, 5)) for color in color_list + ] + + # Fill the image with block_size x block_size pixel squares of random colors from the list + for y in range(0, size, block_size): + for x in range(0, size, block_size): + color = random.choice(rgb_colors) + for i in range(block_size): + for j in range(block_size): + pixels[x + i, y + j] = color + + # Create the folder if it does not exist + if not os.path.exists(folder): + os.makedirs(folder) + + # Generate unique output path + unique_output_path = os.path.join( + folder, + f"{os.path.splitext(output_path)[0]}_{image_index}{os.path.splitext(output_path)[1]}", + ) + + # Save the image with specified quality to maximize file size + image.save(unique_output_path, "JPEG", quality=quality, optimize=False) + + return unique_output_path + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Generate bloated JPEG images with random colors." + ) + parser.add_argument( + "-n", + "--num_images", + type=int, + default=1, + help="Number of images to generate. Default is 1.", + ) + parser.add_argument( + "-s", + "--size", + type=int, + default=1000, + help="Size of the images (must be 1000 or less and divisible by block size). Default is 1000.", + ) + parser.add_argument( + "-c", + "--colors", + nargs="+", + default=["#FF5733", "#33FF57", "#3357FF", "#F3FF33", "#FF33F6", "#33FFF6"], + help="List of colors in hex format. Default is ['#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33F6', '#33FFF6'].", + ) + parser.add_argument( + "-b", + "--block_size", + type=int, + default=4, + help="Size of the pixel blocks (must divide the image size evenly). Default is 4.", + ) + parser.add_argument( + "-o", + "--output_path", + type=str, + default="bloated_image.jpeg", + help="Base output path for the JPEG images. Default is 'bloated_image.jpeg'.", + ) + parser.add_argument( + "-q", + "--quality", + type=int, + default=100, + help="Quality setting for the JPEG images (0-100). Default is 100.", + ) + + args = parser.parse_args() + + # Create folder named after the current timestamp + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + folder = f"generated_images_{timestamp}" + + # Display used parameters + print(f"Generating {args.num_images} image(s) with the following parameters:") + print(f" Size: {args.size}") + print(f" Colors: {args.colors}") + print(f" Block size: {args.block_size}") + print(f" Base output path: {args.output_path}") + print(f" Quality: {args.quality}") + print(f" Output folder: {folder}") + + # Generate the specified number of images + for i in range(1, args.num_images + 1): + output_path = generate_bloated_jpeg( + args.size, + args.colors, + args.block_size, + args.output_path, + args.quality, + i, + folder, + ) + print(f"Image {i} saved to {os.path.abspath(output_path)}") diff --git a/PYTHON/randomJPG/requirements.txt b/PYTHON/randomJPG/requirements.txt new file mode 100644 index 0000000..3868fb1 --- /dev/null +++ b/PYTHON/randomJPG/requirements.txt @@ -0,0 +1 @@ +pillow diff --git a/PYTHON/randomize_numbers/random_digits.py b/PYTHON/randomize_numbers/random_digits.py index 39147b0..b581d12 100644 --- a/PYTHON/randomize_numbers/random_digits.py +++ b/PYTHON/randomize_numbers/random_digits.py @@ -1,6 +1,7 @@ import random -import sys import re +import sys + def randomize_numbers(numbers, min_percentage=1, max_percentage=20): randomized_numbers = [] @@ -13,9 +14,10 @@ def randomize_numbers(numbers, min_percentage=1, max_percentage=20): randomized_numbers.append(new_number) return randomized_numbers + def parse_input(input_string): # Replace commas with dots and remove non-numeric characters except dots, commas, and digits - cleaned_input = re.sub(r'[^\d.,\s]', '', input_string).replace(',', '.') + cleaned_input = re.sub(r"[^\d.,\s]", "", input_string).replace(",", ".") # Split the cleaned input into individual numbers number_strings = cleaned_input.split() # Convert the number strings to floats @@ -24,8 +26,8 @@ def parse_input(input_string): for num in number_strings: try: float_num = float(num) - if '.' in num: - digits_count = len(num.split('.')[-1]) + if "." in num: + digits_count = len(num.split(".")[-1]) else: digits_count = 0 numbers.append(float_num) @@ -34,13 +36,16 @@ def parse_input(input_string): continue return numbers, decimal_counts + if __name__ == "__main__": if len(sys.argv) < 2: - print("Usage: python random_digits.py ... [min_percentage max_percentage]") + print( + "Usage: python random_digits.py ... [min_percentage max_percentage]" + ) sys.exit(1) try: - input_string = ' '.join(sys.argv[1:]) + input_string = " ".join(sys.argv[1:]) numbers, decimal_counts = parse_input(input_string) min_percentage = 1 max_percentage = 20 @@ -62,7 +67,7 @@ if __name__ == "__main__": randomized_numbers = randomize_numbers(numbers, min_percentage, max_percentage) formatted_numbers = [] for i, num in enumerate(randomized_numbers): - format_str = f'.{decimal_counts[i]}f' + format_str = f".{decimal_counts[i]}f" formatted_numbers.append(float(format(num, format_str))) print("Original numbers:", numbers) @@ -71,4 +76,3 @@ if __name__ == "__main__": print(f"Error: {e}") print("Please provide valid numbers and percentages.") sys.exit(1) - diff --git a/PYTHON/scapeWebsite/.gitignore b/PYTHON/scapeWebsite/.gitignore new file mode 100644 index 0000000..97dcdbe --- /dev/null +++ b/PYTHON/scapeWebsite/.gitignore @@ -0,0 +1,63 @@ +# JPEG +*.jpg +*.jpeg +*.jpe +*.jif +*.jfif +*.jfi + +# JPEG 2000 +*.jp2 +*.j2k +*.jpf +*.jpx +*.jpm +*.mj2 + +# JPEG XR +*.jxr +*.hdp +*.wdp + +# Graphics Interchange Format +*.gif + +# RAW +*.raw + +# Web P +*.webp + +# Portable Network Graphics +*.png + +# Animated Portable Network Graphics +*.apng + +# Multiple-image Network Graphics +*.mng + +# Tagged Image File Format +*.tiff +*.tif + +# Scalable Vector Graphics +*.svg +*.svgz + +# Portable Document Format +*.pdf + +# X BitMap +*.xbm + +# BMP +*.bmp +*.dib + +# ICO +*.ico + +# 3D Images +*.3dm +*.max diff --git a/PYTHON/scapeWebsite/requirements.txt b/PYTHON/scapeWebsite/requirements.txt new file mode 100644 index 0000000..a2ca48c --- /dev/null +++ b/PYTHON/scapeWebsite/requirements.txt @@ -0,0 +1,2 @@ +requests +selenium diff --git a/PYTHON/scapeWebsite/scrape_comics.py b/PYTHON/scapeWebsite/scrape_comics.py new file mode 100644 index 0000000..3180fb1 --- /dev/null +++ b/PYTHON/scapeWebsite/scrape_comics.py @@ -0,0 +1,75 @@ +import argparse +import os +from urllib.parse import urlparse + +import requests +from selenium import webdriver +from selenium.webdriver.common.by import By + +# Initialize argument parser to accept the website URL as an argument +parser = argparse.ArgumentParser(description="Download images from a comic website.") +parser.add_argument( + "url", type=str, help="The URL of the website to start downloading images from" +) +args = parser.parse_args() + +# Initialize WebDriver (Use the appropriate driver for your browser) +driver = webdriver.Chrome() + +# Open the website from the passed argument +url = args.url +print(f"Opening the website: {url}") +driver.get(url) + + +# A function to download images by URL +def download_image(url): + # Extract image name from URL + image_name = os.path.basename(urlparse(url).path) + + # Check if the image already exists + if os.path.exists(image_name): + print(f"Image {image_name} already exists, skipping download.") + return False + print(f"Downloading image from URL: {url}") + img_data = requests.get(url).content + with open(image_name, "wb") as handler: + handler.write(img_data) + print(f"Image {image_name} downloaded successfully") + return True + + +# No need to define a specific number of images now +count = 1 + +while True: + print(f"Processing image {count}...") + + # Find the image element by its ID + image_element = driver.find_element(By.ID, "cc-comic") + + # Get the image URL from the 'src' attribute + image_url = image_element.get_attribute("src") + print(f"Found image URL: {image_url}") + + # Download the image if it doesn't already exist + if download_image(image_url): + count += 1 # Increment count only if the image was downloaded + + # Try to find the 'Next' button by its class + try: + print("Clicking the 'Next' button to load the next image...") + next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next") + + # Navigate to the URL in the 'href' of the next button + next_button_url = next_button.get_attribute("href") + driver.get(next_button_url) + + except: + # If the 'Next' button is not found, it means we've reached the last image + print("No 'Next' button found. Reached the end of images.") + break + +# Close the browser +print("All images processed, closing the browser.") +driver.quit() diff --git a/PYTHON/screen_locker/screen_lock.py b/PYTHON/screen_locker/screen_lock.py index ddbfbaf..76e08a1 100755 --- a/PYTHON/screen_locker/screen_lock.py +++ b/PYTHON/screen_locker/screen_lock.py @@ -1,143 +1,142 @@ #!/usr/bin/env python3 -""" -Screen locker with workout verification for Arch Linux / i3wm +"""Screen locker with workout verification for Arch Linux / i3wm Requires user to log their workout to unlock the screen. """ -import tkinter as tk -import time -import sys -import re +from datetime import datetime import json import os -from datetime import datetime +import sys +import tkinter as tk class ScreenLocker: def __init__(self, demo_mode=True): # Set up log file path script_dir = os.path.dirname(os.path.abspath(__file__)) - self.log_file = os.path.join(script_dir, 'workout_log.json') - + self.log_file = os.path.join(script_dir, "workout_log.json") + # Check if already logged today if self.has_logged_today(): print("Workout already logged today. Skipping screen lock.") sys.exit(0) - + self.root = tk.Tk() self.root.title("Workout Locker" + (" [DEMO MODE]" if demo_mode else "")) self.demo_mode = demo_mode - self.lockout_time = 10 if demo_mode else 1800 # 10 seconds for demo, 30 minutes for production + self.lockout_time = ( + 10 if demo_mode else 1800 + ) # 10 seconds for demo, 30 minutes for production self.workout_data = {} - + # Get total screen dimensions across all monitors screen_width = self.root.winfo_screenwidth() screen_height = self.root.winfo_screenheight() - + # Override redirect to bypass window manager (needed for multi-monitor spanning) self.root.overrideredirect(True) - + # Position window at 0,0 and span all monitors - self.root.geometry(f'{screen_width}x{screen_height}+0+0') - + self.root.geometry(f"{screen_width}x{screen_height}+0+0") + # Make window fullscreen and on top - self.root.attributes('-fullscreen', True) - self.root.attributes('-topmost', True) - self.root.configure(bg='#1a1a1a', cursor='arrow') - + self.root.attributes("-fullscreen", True) + self.root.attributes("-topmost", True) + self.root.configure(bg="#1a1a1a", cursor="arrow") + if demo_mode: # Demo mode: only close button allowed # Add close button in top-left corner close_btn = tk.Button( self.root, text="✕ Close Demo", - font=('Arial', 12), - bg='#ff4444', - fg='white', + font=("Arial", 12), + bg="#ff4444", + fg="white", command=self.close, - cursor='hand2' + cursor="hand2", ) close_btn.place(x=10, y=10) - + # Create main container - self.container = tk.Frame(self.root, bg='#1a1a1a') - self.container.place(relx=0.5, rely=0.5, anchor='center') - + self.container = tk.Frame(self.root, bg="#1a1a1a") + self.container.place(relx=0.5, rely=0.5, anchor="center") + # Start with initial question self.ask_workout_done() - + # Force window to update and grab input after everything is set up self.root.update_idletasks() self.root.focus_force() self.root.grab_set_global() - + def clear_container(self): for widget in self.container.winfo_children(): widget.destroy() - + def ask_workout_done(self): self.clear_container() - + question = tk.Label( self.container, text="Did you work out today?", - font=('Arial', 36, 'bold'), - fg='white', - bg='#1a1a1a' + font=("Arial", 36, "bold"), + fg="white", + bg="#1a1a1a", ) question.pack(pady=30) - - button_frame = tk.Frame(self.container, bg='#1a1a1a') + + button_frame = tk.Frame(self.container, bg="#1a1a1a") button_frame.pack(pady=20) - + yes_btn = tk.Button( button_frame, text="YES", - font=('Arial', 24, 'bold'), - bg='#00aa00', - fg='white', + font=("Arial", 24, "bold"), + bg="#00aa00", + fg="white", width=10, command=self.ask_workout_type, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) - yes_btn.pack(side='left', padx=20) - + yes_btn.pack(side="left", padx=20) + no_btn = tk.Button( button_frame, text="NO", - font=('Arial', 24, 'bold'), - bg='#aa0000', - fg='white', + font=("Arial", 24, "bold"), + bg="#aa0000", + fg="white", width=10, command=self.lockout, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) - no_btn.pack(side='left', padx=20) - + no_btn.pack(side="left", padx=20) + def lockout(self): self.clear_container() - + self.lockout_label = tk.Label( self.container, text=f"Go work out!\nLocked for {self.lockout_time} seconds", - font=('Arial', 48, 'bold'), - fg='#ff4444', - bg='#1a1a1a' + font=("Arial", 48, "bold"), + fg="#ff4444", + bg="#1a1a1a", ) self.lockout_label.pack(pady=30) - + self.countdown_label = tk.Label( self.container, text=str(self.lockout_time), - font=('Arial', 120, 'bold'), - fg='white', - bg='#1a1a1a' + font=("Arial", 120, "bold"), + fg="white", + bg="#1a1a1a", ) self.countdown_label.pack(pady=30) - + self.remaining_time = self.lockout_time self.update_lockout_countdown() - + def update_lockout_countdown(self): if self.remaining_time > 0: self.countdown_label.config(text=str(self.remaining_time)) @@ -145,309 +144,365 @@ class ScreenLocker: self.root.after(1000, self.update_lockout_countdown) else: self.ask_workout_done() - + def ask_workout_type(self): self.clear_container() - + question = tk.Label( self.container, text="What type of workout?", - font=('Arial', 36, 'bold'), - fg='white', - bg='#1a1a1a' + font=("Arial", 36, "bold"), + fg="white", + bg="#1a1a1a", ) question.pack(pady=30) - - button_frame = tk.Frame(self.container, bg='#1a1a1a') + + button_frame = tk.Frame(self.container, bg="#1a1a1a") button_frame.pack(pady=20) - + running_btn = tk.Button( button_frame, text="RUNNING", - font=('Arial', 24, 'bold'), - bg='#0066cc', - fg='white', + font=("Arial", 24, "bold"), + bg="#0066cc", + fg="white", width=15, command=self.ask_running_details, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) - running_btn.pack(side='left', padx=20) - + running_btn.pack(side="left", padx=20) + strength_btn = tk.Button( button_frame, text="STRENGTH", - font=('Arial', 24, 'bold'), - bg='#cc6600', - fg='white', + font=("Arial", 24, "bold"), + bg="#cc6600", + fg="white", width=15, command=self.ask_strength_details, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) - strength_btn.pack(side='left', padx=20) - + strength_btn.pack(side="left", padx=20) + def ask_running_details(self): self.clear_container() - self.workout_data['type'] = 'running' - + self.workout_data["type"] = "running" + title = tk.Label( self.container, text="Running Details", - font=('Arial', 36, 'bold'), - fg='white', - bg='#1a1a1a' + font=("Arial", 36, "bold"), + fg="white", + bg="#1a1a1a", ) title.pack(pady=20) - + # Distance - dist_frame = tk.Frame(self.container, bg='#1a1a1a') + dist_frame = tk.Frame(self.container, bg="#1a1a1a") dist_frame.pack(pady=10) - tk.Label(dist_frame, text="Distance (km):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.distance_entry = tk.Entry(dist_frame, font=('Arial', 20), width=10) - self.distance_entry.pack(side='left', padx=10) - + tk.Label( + dist_frame, + text="Distance (km):", + font=("Arial", 20), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.distance_entry = tk.Entry(dist_frame, font=("Arial", 20), width=10) + self.distance_entry.pack(side="left", padx=10) + # Time - time_frame = tk.Frame(self.container, bg='#1a1a1a') + time_frame = tk.Frame(self.container, bg="#1a1a1a") time_frame.pack(pady=10) - tk.Label(time_frame, text="Time (minutes):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.time_entry = tk.Entry(time_frame, font=('Arial', 20), width=10) - self.time_entry.pack(side='left', padx=10) - + tk.Label( + time_frame, + text="Time (minutes):", + font=("Arial", 20), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.time_entry = tk.Entry(time_frame, font=("Arial", 20), width=10) + self.time_entry.pack(side="left", padx=10) + # Pace - pace_frame = tk.Frame(self.container, bg='#1a1a1a') + pace_frame = tk.Frame(self.container, bg="#1a1a1a") pace_frame.pack(pady=10) - tk.Label(pace_frame, text="Pace (min/km):", font=('Arial', 20), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.pace_entry = tk.Entry(pace_frame, font=('Arial', 20), width=10) - self.pace_entry.pack(side='left', padx=10) - + tk.Label( + pace_frame, + text="Pace (min/km):", + font=("Arial", 20), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.pace_entry = tk.Entry(pace_frame, font=("Arial", 20), width=10) + self.pace_entry.pack(side="left", padx=10) + # Timer countdown label self.timer_label = tk.Label( - self.container, - text="", - font=('Arial', 16), - fg='#ffaa00', - bg='#1a1a1a' + self.container, text="", font=("Arial", 16), fg="#ffaa00", bg="#1a1a1a" ) self.timer_label.pack(pady=10) - + self.submit_btn = tk.Button( self.container, text="SUBMIT (locked)", - font=('Arial', 24, 'bold'), - bg='#666666', - fg='white', + font=("Arial", 24, "bold"), + bg="#666666", + fg="white", width=15, - state='disabled', - cursor='hand2' if self.demo_mode else '' + state="disabled", + cursor="hand2" if self.demo_mode else "", ) self.submit_btn.pack(pady=10) - + # Back button back_btn = tk.Button( self.container, text="← BACK", - font=('Arial', 18), - bg='#666666', - fg='white', + font=("Arial", 18), + bg="#666666", + fg="white", width=15, command=self.ask_workout_type, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) back_btn.pack(pady=10) - + # Start 30 second timer self.submit_unlock_time = 30 self.entries_to_check = [self.distance_entry, self.time_entry, self.pace_entry] self.submit_command = self.verify_running_data self.update_submit_timer() - + def verify_running_data(self): try: distance = float(self.distance_entry.get()) time_mins = float(self.time_entry.get()) pace = float(self.pace_entry.get()) - + # Sanity checks if distance <= 0 or distance > 100: self.show_error("Distance seems unrealistic (0-100 km)") return - + if time_mins <= 0 or time_mins > 600: self.show_error("Time seems unrealistic (0-600 minutes)") return - + if pace <= 0 or pace > 20: self.show_error("Pace seems unrealistic (0-20 min/km)") return - + # Calculate expected pace and check if close enough expected_pace = time_mins / distance pace_diff = abs(pace - expected_pace) tolerance = expected_pace * 0.15 # 15% tolerance - + if pace_diff > tolerance: - self.show_error(f"Pace doesn't match! Expected ~{expected_pace:.2f} min/km, got {pace:.2f}") + self.show_error( + f"Pace doesn't match! Expected ~{expected_pace:.2f} min/km, got {pace:.2f}" + ) return - + # Data looks good self.unlock_screen() - + except ValueError: self.show_error("Please enter valid numbers") - + def ask_strength_details(self): self.clear_container() - self.workout_data['type'] = 'strength' - + self.workout_data["type"] = "strength" + title = tk.Label( self.container, text="Strength Training Details", - font=('Arial', 36, 'bold'), - fg='white', - bg='#1a1a1a' + font=("Arial", 36, "bold"), + fg="white", + bg="#1a1a1a", ) title.pack(pady=20) - + # Exercises - ex_frame = tk.Frame(self.container, bg='#1a1a1a') + ex_frame = tk.Frame(self.container, bg="#1a1a1a") ex_frame.pack(pady=10) - tk.Label(ex_frame, text="Exercises (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.exercises_entry = tk.Entry(ex_frame, font=('Arial', 18), width=30) - self.exercises_entry.pack(side='left', padx=10) - + tk.Label( + ex_frame, + text="Exercises (comma-separated):", + font=("Arial", 18), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.exercises_entry = tk.Entry(ex_frame, font=("Arial", 18), width=30) + self.exercises_entry.pack(side="left", padx=10) + # Sets per exercise - sets_frame = tk.Frame(self.container, bg='#1a1a1a') + sets_frame = tk.Frame(self.container, bg="#1a1a1a") sets_frame.pack(pady=10) - tk.Label(sets_frame, text="Sets per exercise (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.sets_entry = tk.Entry(sets_frame, font=('Arial', 18), width=20) - self.sets_entry.pack(side='left', padx=10) - + tk.Label( + sets_frame, + text="Sets per exercise (comma-separated):", + font=("Arial", 18), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.sets_entry = tk.Entry(sets_frame, font=("Arial", 18), width=20) + self.sets_entry.pack(side="left", padx=10) + # Reps per set - reps_frame = tk.Frame(self.container, bg='#1a1a1a') + reps_frame = tk.Frame(self.container, bg="#1a1a1a") reps_frame.pack(pady=10) - tk.Label(reps_frame, text="Reps per set (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.reps_entry = tk.Entry(reps_frame, font=('Arial', 18), width=20) - self.reps_entry.pack(side='left', padx=10) - + tk.Label( + reps_frame, + text="Reps per set (comma-separated):", + font=("Arial", 18), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.reps_entry = tk.Entry(reps_frame, font=("Arial", 18), width=20) + self.reps_entry.pack(side="left", padx=10) + # Weights - weights_frame = tk.Frame(self.container, bg='#1a1a1a') + weights_frame = tk.Frame(self.container, bg="#1a1a1a") weights_frame.pack(pady=10) - tk.Label(weights_frame, text="Weight per exercise in kg (comma-separated):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.weights_entry = tk.Entry(weights_frame, font=('Arial', 18), width=20) - self.weights_entry.pack(side='left', padx=10) - + tk.Label( + weights_frame, + text="Weight per exercise in kg (comma-separated):", + font=("Arial", 18), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.weights_entry = tk.Entry(weights_frame, font=("Arial", 18), width=20) + self.weights_entry.pack(side="left", padx=10) + # Total weight lifted - total_frame = tk.Frame(self.container, bg='#1a1a1a') + total_frame = tk.Frame(self.container, bg="#1a1a1a") total_frame.pack(pady=10) - tk.Label(total_frame, text="Total weight lifted (kg):", font=('Arial', 18), fg='white', bg='#1a1a1a').pack(side='left', padx=10) - self.total_weight_entry = tk.Entry(total_frame, font=('Arial', 18), width=15) - self.total_weight_entry.pack(side='left', padx=10) - + tk.Label( + total_frame, + text="Total weight lifted (kg):", + font=("Arial", 18), + fg="white", + bg="#1a1a1a", + ).pack(side="left", padx=10) + self.total_weight_entry = tk.Entry(total_frame, font=("Arial", 18), width=15) + self.total_weight_entry.pack(side="left", padx=10) + # Timer countdown label self.timer_label = tk.Label( - self.container, - text="", - font=('Arial', 16), - fg='#ffaa00', - bg='#1a1a1a' + self.container, text="", font=("Arial", 16), fg="#ffaa00", bg="#1a1a1a" ) self.timer_label.pack(pady=10) - + self.submit_btn = tk.Button( self.container, text="SUBMIT (locked)", - font=('Arial', 24, 'bold'), - bg='#666666', - fg='white', + font=("Arial", 24, "bold"), + bg="#666666", + fg="white", width=15, - state='disabled', - cursor='hand2' if self.demo_mode else '' + state="disabled", + cursor="hand2" if self.demo_mode else "", ) self.submit_btn.pack(pady=10) - + # Back button back_btn = tk.Button( self.container, text="← BACK", - font=('Arial', 18), - bg='#666666', - fg='white', + font=("Arial", 18), + bg="#666666", + fg="white", width=15, command=self.ask_workout_type, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) back_btn.pack(pady=10) - + # Start 30 second timer self.submit_unlock_time = 30 - self.entries_to_check = [self.exercises_entry, self.sets_entry, self.reps_entry, self.weights_entry, self.total_weight_entry] + self.entries_to_check = [ + self.exercises_entry, + self.sets_entry, + self.reps_entry, + self.weights_entry, + self.total_weight_entry, + ] self.submit_command = self.verify_strength_data self.update_submit_timer() - + def verify_strength_data(self): try: - exercises = [e.strip() for e in self.exercises_entry.get().split(',')] - sets = [int(s.strip()) for s in self.sets_entry.get().split(',')] - reps = [int(r.strip()) for r in self.reps_entry.get().split(',')] - weights = [float(w.strip()) for w in self.weights_entry.get().split(',')] + exercises = [e.strip() for e in self.exercises_entry.get().split(",")] + sets = [int(s.strip()) for s in self.sets_entry.get().split(",")] + reps = [int(r.strip()) for r in self.reps_entry.get().split(",")] + weights = [float(w.strip()) for w in self.weights_entry.get().split(",")] total_weight = float(self.total_weight_entry.get()) - + # Check all lists have same length if not (len(exercises) == len(sets) == len(reps) == len(weights)): - self.show_error("Number of exercises, sets, reps, and weights must match") + self.show_error( + "Number of exercises, sets, reps, and weights must match" + ) return - + # Check for empty or lazy entries if any(len(ex) < 3 for ex in exercises): self.show_error("Exercise names too short - be specific") return - + # Sanity checks if any(s < 1 or s > 20 for s in sets): self.show_error("Sets should be between 1-20") return - + if any(r < 1 or r > 100 for r in reps): self.show_error("Reps should be between 1-100") return - + if any(w < 0 or w > 500 for w in weights): self.show_error("Weights should be between 0-500 kg") return - + # Calculate expected total weight - expected_total = sum(sets[i] * reps[i] * weights[i] for i in range(len(exercises))) + expected_total = sum( + sets[i] * reps[i] * weights[i] for i in range(len(exercises)) + ) weight_diff = abs(total_weight - expected_total) tolerance = expected_total * 0.15 # 15% tolerance - + if weight_diff > tolerance: - self.show_error(f"Total weight doesn't match! Expected ~{expected_total:.1f} kg, got {total_weight:.1f}") + self.show_error( + f"Total weight doesn't match! Expected ~{expected_total:.1f} kg, got {total_weight:.1f}" + ) return - + # Data looks good self.unlock_screen() - + except ValueError: self.show_error("Please enter valid data in correct format") - + def update_submit_timer(self): """Update countdown timer and check if submit can be enabled""" # Check if widgets still exist (user might have clicked back) try: if self.submit_unlock_time > 0: - self.timer_label.config(text=f"Submit available in {self.submit_unlock_time} seconds...") + self.timer_label.config( + text=f"Submit available in {self.submit_unlock_time} seconds..." + ) self.submit_unlock_time -= 1 self.root.after(1000, self.update_submit_timer) else: # Timer finished, check if all entries have data all_filled = all(entry.get().strip() for entry in self.entries_to_check) - + if all_filled: # Enable submit button self.submit_btn.config( text="SUBMIT", - state='normal', - bg='#00aa00', - command=self.submit_command + state="normal", + bg="#00aa00", + command=self.submit_command, ) self.timer_label.config(text="You can now submit!") else: @@ -457,18 +512,18 @@ class ScreenLocker: except tk.TclError: # Widgets were destroyed (user clicked back), stop the timer pass - + def check_entries_filled(self): """Continuously check if entries are filled after timer expires""" try: all_filled = all(entry.get().strip() for entry in self.entries_to_check) - + if all_filled: self.submit_btn.config( text="SUBMIT", - state='normal', - bg='#00aa00', - command=self.submit_command + state="normal", + bg="#00aa00", + command=self.submit_command, ) self.timer_label.config(text="You can now submit!") else: @@ -477,120 +532,120 @@ class ScreenLocker: except tk.TclError: # Widgets were destroyed (user clicked back), stop checking pass - + def show_error(self, message): self.clear_container() - + error_label = tk.Label( self.container, text="ERROR", - font=('Arial', 48, 'bold'), - fg='#ff4444', - bg='#1a1a1a' + font=("Arial", 48, "bold"), + fg="#ff4444", + bg="#1a1a1a", ) error_label.pack(pady=20) - + msg_label = tk.Label( self.container, text=message, - font=('Arial', 24), - fg='white', - bg='#1a1a1a', - wraplength=800 + font=("Arial", 24), + fg="white", + bg="#1a1a1a", + wraplength=800, ) msg_label.pack(pady=20) - + retry_btn = tk.Button( self.container, text="TRY AGAIN", - font=('Arial', 24, 'bold'), - bg='#0066cc', - fg='white', + font=("Arial", 24, "bold"), + bg="#0066cc", + fg="white", width=15, command=self.ask_workout_done, - cursor='hand2' if self.demo_mode else '' + cursor="hand2" if self.demo_mode else "", ) retry_btn.pack(pady=30) - + def unlock_screen(self): # Save workout data to log self.save_workout_log() - + self.clear_container() - + success_label = tk.Label( self.container, text="Great job! 💪", - font=('Arial', 48, 'bold'), - fg='#00ff00', - bg='#1a1a1a' + font=("Arial", 48, "bold"), + fg="#00ff00", + bg="#1a1a1a", ) success_label.pack(pady=30) - + unlock_label = tk.Label( self.container, text="Screen Unlocked!", - font=('Arial', 36), - fg='white', - bg='#1a1a1a' + font=("Arial", 36), + fg="white", + bg="#1a1a1a", ) unlock_label.pack(pady=20) - + self.root.after(1500, self.close) - + def has_logged_today(self): """Check if workout has been logged today""" if not os.path.exists(self.log_file): return False - + try: - with open(self.log_file, 'r') as f: + with open(self.log_file) as f: logs = json.load(f) - - today = datetime.now().strftime('%Y-%m-%d') + + today = datetime.now().strftime("%Y-%m-%d") return today in logs - except (json.JSONDecodeError, IOError): + except (OSError, json.JSONDecodeError): return False - + def save_workout_log(self): """Save workout data to log file""" # Load existing logs logs = {} if os.path.exists(self.log_file): try: - with open(self.log_file, 'r') as f: + with open(self.log_file) as f: logs = json.load(f) - except (json.JSONDecodeError, IOError): + except (OSError, json.JSONDecodeError): logs = {} - + # Add today's workout - today = datetime.now().strftime('%Y-%m-%d') + today = datetime.now().strftime("%Y-%m-%d") logs[today] = { - 'timestamp': datetime.now().isoformat(), - 'workout_data': self.workout_data + "timestamp": datetime.now().isoformat(), + "workout_data": self.workout_data, } - + # Save updated logs try: - with open(self.log_file, 'w') as f: + with open(self.log_file, "w") as f: json.dump(logs, f, indent=2) - except IOError as e: + except OSError as e: print(f"Warning: Could not save workout log: {e}") - + def close(self): self.root.destroy() sys.exit(0) - + def run(self): self.root.mainloop() -if __name__ == '__main__': +if __name__ == "__main__": # Check for --production flag demo_mode = True # Default to demo mode for safety - - if len(sys.argv) > 1 and sys.argv[1] == '--production': + + if len(sys.argv) > 1 and sys.argv[1] == "--production": demo_mode = False - + locker = ScreenLocker(demo_mode=demo_mode) locker.run() diff --git a/PYTHON/screen_locker/workout_log.json b/PYTHON/screen_locker/workout_log.json new file mode 100644 index 0000000..7b624cc --- /dev/null +++ b/PYTHON/screen_locker/workout_log.json @@ -0,0 +1,20 @@ +{ + "2025-11-27": { + "timestamp": "2025-11-27T19:45:38.894904", + "workout_data": { + "type": "strength" + } + }, + "2025-11-28": { + "timestamp": "2025-11-28T12:46:09.077724", + "workout_data": { + "type": "strength" + } + }, + "2025-11-30": { + "timestamp": "2025-11-30T12:12:44.884093", + "workout_data": { + "type": "strength" + } + } +} diff --git a/PYTHON/split/split_x_into_n_symmetrically.py b/PYTHON/split/split_x_into_n_symmetrically.py new file mode 100644 index 0000000..939cae8 --- /dev/null +++ b/PYTHON/split/split_x_into_n_symmetrically.py @@ -0,0 +1,57 @@ +def calculate_symmetric_weights(N, middle_weight, factors=None): + """Calculate symmetric weights for both even and odd N. + + N: Number in which to split. + middle_weight: The middle value for symmetry. + factors: If provided, controls the difference in weights (used for the `split_x_into_n_symmetrically` function). + Must have length N // 2 or N // 2 - 1 depending on N. + """ + half_N = N // 2 + weights_left = [middle_weight] + + if factors: + for factor in factors: + next_weight = weights_left[-1] + factor + weights_left.append(next_weight) + else: + for i in range(half_N - 1): + weights_left.append(middle_weight - (i + 1)) + + if N % 2 == 0: + weights = weights_left[::-1] + weights_left + else: + weights = weights_left[::-1] + [middle_weight] + weights_left + + return weights + + +def scale_to_total(X, weights): + """Scale the weights so that their sum is proportional to X. + + X: Total value to distribute. + weights: The list of weights to be scaled. + """ + total_weight = sum(weights) + base_unit = X / total_weight + distances = [base_unit * weight for weight in weights] + + return distances + + +def split_x_into_n_symmetrically(X, N, factors): + """X: Total value to distribute. + N: Number in which we split. + factors: List controlling the difference in weights between consecutive days. + Must have length of N // 2 (if N is odd) or (N // 2 - 1) (if N is even). + """ + weights = calculate_symmetric_weights(N, middle_weight=1, factors=factors) + return scale_to_total(X, weights) + + +def split_x_into_n_middle(X, N, middle_value): + """X: Total value to distribute. + N: Number in which we split. + middle_value: Value of the middle number (the biggest weight). + """ + weights = calculate_symmetric_weights(N, middle_weight=middle_value) + return scale_to_total(X, weights) diff --git a/PYTHON/stockfish_analysis/README.md b/PYTHON/stockfish_analysis/README.md index 940150d..364a3ae 100644 --- a/PYTHON/stockfish_analysis/README.md +++ b/PYTHON/stockfish_analysis/README.md @@ -21,9 +21,11 @@ python3 PYTHON/analyze_chess_game.py lichess_bot_game_8GSdY3Ci.log ``` Options: + - `--engine /path/to/stockfish` to specify a custom engine path - `--time 0.2` seconds per evaluation (default) - `--depth 12` fixed depth instead of time The script prints a table with, for each ply: + - side to move, SAN move, eval before/after from mover's POV, delta, classification, and Stockfish best move suggestion. diff --git a/PYTHON/stockfish_analysis/analyze_chess_game.py b/PYTHON/stockfish_analysis/analyze_chess_game.py old mode 100644 new mode 100755 index 3912811..6d90b91 --- a/PYTHON/stockfish_analysis/analyze_chess_game.py +++ b/PYTHON/stockfish_analysis/analyze_chess_game.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -""" -Analyze a chess game's moves using a local Stockfish engine and rate each move. +"""Analyze a chess game's moves using a local Stockfish engine and rate each move. Usage: python3 PYTHON/analyze_chess_game.py @@ -22,11 +21,10 @@ from __future__ import annotations import argparse import io +import multiprocessing import os import re import sys -from typing import Optional, Tuple -import multiprocessing try: import psutil # type: ignore @@ -37,13 +35,15 @@ try: import chess import chess.engine import chess.pgn -except Exception as e: # pragma: no cover +except Exception: # pragma: no cover print("Missing dependency. Please install python-chess:", file=sys.stderr) - print(" pip install -r PYTHON/stockfish_analysis/requirements.txt", file=sys.stderr) + print( + " pip install -r PYTHON/stockfish_analysis/requirements.txt", file=sys.stderr + ) raise -def extract_pgn_text(raw: str) -> Optional[str]: +def extract_pgn_text(raw: str) -> str | None: """Try to extract a PGN block from a possibly noisy file. Strategies tried in order: @@ -79,7 +79,9 @@ def extract_pgn_text(raw: str) -> Optional[str]: return None -def score_to_cp(score: chess.engine.PovScore, pov_white: bool) -> Tuple[Optional[int], Optional[int]]: +def score_to_cp( + score: chess.engine.PovScore, pov_white: bool +) -> tuple[int | None, int | None]: """Return tuple (cp, mate_in) from a PovScore for the given POV color. If it's a mate score, cp will be None and mate_in will be +/-N (positive means mate for POV side). @@ -93,7 +95,7 @@ def score_to_cp(score: chess.engine.PovScore, pov_white: bool) -> Tuple[Optional return s.score(mate_score=None), None -def classify_cp_loss(cp_loss: Optional[int]) -> str: +def classify_cp_loss(cp_loss: int | None) -> str: """Classify move quality using Lichess-like centipawn loss bands. Loss is best_eval(cp) - played_eval(cp), from the mover's POV (positive is worse). @@ -120,7 +122,7 @@ def classify_cp_loss(cp_loss: Optional[int]) -> str: return "Blunder" -def fmt_eval(cp: Optional[int], mate_in: Optional[int]) -> str: +def fmt_eval(cp: int | None, mate_in: int | None) -> str: if mate_in is not None: sign = "+" if mate_in > 0 else "" return f"M{sign}{mate_in}" @@ -130,7 +132,7 @@ def fmt_eval(cp: Optional[int], mate_in: Optional[int]) -> str: return f"{cp/100.0:+.2f}" -def _parse_threads(value: str) -> Optional[int]: +def _parse_threads(value: str) -> int | None: v = value.strip().lower() if v in ("auto", "max", ""): # auto-detect return None @@ -141,7 +143,7 @@ def _parse_threads(value: str) -> Optional[int]: raise argparse.ArgumentTypeError("--threads must be an integer or 'auto'") -def _parse_hash_mb(value: str) -> Optional[int]: +def _parse_hash_mb(value: str) -> int | None: v = value.strip().lower() if v in ("auto", "max", ""): # auto-detect return None @@ -152,7 +154,7 @@ def _parse_hash_mb(value: str) -> Optional[int]: raise argparse.ArgumentTypeError("--hash-mb must be an integer (MB) or 'auto'") -def _detect_total_mem_mb() -> Optional[int]: +def _detect_total_mem_mb() -> int | None: # Prefer psutil if available if psutil is not None: try: @@ -161,7 +163,7 @@ def _detect_total_mem_mb() -> Optional[int]: pass # Fallback: Linux /proc/meminfo try: - with open("/proc/meminfo", "r", encoding="utf-8", errors="ignore") as f: + with open("/proc/meminfo", encoding="utf-8", errors="ignore") as f: for line in f: if line.startswith("MemTotal:"): parts = line.split() @@ -183,7 +185,7 @@ def _auto_hash_mb(threads_wanted: int, engine_options) -> int: opt = engine_options.get("Hash") max_allowed = None try: - max_allowed = getattr(opt, "max") if opt is not None else None + max_allowed = opt.max if opt is not None else None except Exception: max_allowed = None if isinstance(max_allowed, int): @@ -195,27 +197,61 @@ def _auto_hash_mb(threads_wanted: int, engine_options) -> int: def main(): - ap = argparse.ArgumentParser(description="Analyze a chess game's moves with Stockfish and rate each move.") + ap = argparse.ArgumentParser( + description="Analyze a chess game's moves with Stockfish and rate each move." + ) ap.add_argument("file", help="Path to a PGN file or a log containing a PGN section") - ap.add_argument("--engine", default="stockfish", help="Path to stockfish executable (default: stockfish)") + ap.add_argument( + "--engine", + default="stockfish", + help="Path to stockfish executable (default: stockfish)", + ) # Exactly one of time or depth may be provided; default to time - ap.add_argument("--time", type=float, default=0.5, help="Analysis time per evaluation in seconds (default: 0.5)") - ap.add_argument("--depth", type=int, default=None, help="Fixed depth per evaluation (overrides --time)") + ap.add_argument( + "--time", + type=float, + default=0.5, + help="Analysis time per evaluation in seconds (default: 0.5)", + ) + ap.add_argument( + "--depth", + type=int, + default=None, + help="Fixed depth per evaluation (overrides --time)", + ) # Performance knobs - ap.add_argument("--threads", type=_parse_threads, default=None, metavar="auto|N", - help="Engine threads to use (default: auto = all logical cores)") - ap.add_argument("--hash-mb", type=_parse_hash_mb, default=None, metavar="auto|MB", - help="Hash table size in MB (default: auto = up to half RAM, capped)") - ap.add_argument("--multipv", type=int, default=2, help="Number of principal variations to compute (default: 1)") - ap.add_argument("--last-move-only", action="store_true", - help="Analyze only the last move of the main line (reports its eval and the best move)") + ap.add_argument( + "--threads", + type=_parse_threads, + default=None, + metavar="auto|N", + help="Engine threads to use (default: auto = all logical cores)", + ) + ap.add_argument( + "--hash-mb", + type=_parse_hash_mb, + default=None, + metavar="auto|MB", + help="Hash table size in MB (default: auto = up to half RAM, capped)", + ) + ap.add_argument( + "--multipv", + type=int, + default=2, + help="Number of principal variations to compute (default: 1)", + ) + ap.add_argument( + "--last-move-only", + action="store_true", + help="Analyze only the last move of the main line (reports its eval and the best move)", + ) args = ap.parse_args() if not os.path.isfile(args.file): print(f"Input not found: {args.file}", file=sys.stderr) sys.exit(1) - with open(args.file, "r", encoding="utf-8", errors="replace") as f: + with open(args.file, encoding="utf-8", errors="replace") as f: raw = f.read() pgn_text = extract_pgn_text(raw) @@ -233,7 +269,10 @@ def main(): engine = chess.engine.SimpleEngine.popen_uci([args.engine]) except FileNotFoundError: print(f"Could not launch engine at: {args.engine}", file=sys.stderr) - print("Ensure Stockfish is installed and in PATH, or specify with --engine.", file=sys.stderr) + print( + "Ensure Stockfish is installed and in PATH, or specify with --engine.", + file=sys.stderr, + ) sys.exit(4) # Configure engine performance options if available @@ -243,7 +282,9 @@ def main(): options = {} # Threads - wanted_threads = args.threads if args.threads is not None else (multiprocessing.cpu_count() or 1) + wanted_threads = ( + args.threads if args.threads is not None else (multiprocessing.cpu_count() or 1) + ) # Respect engine bounds if present if "Threads" in options: try: @@ -307,18 +348,26 @@ def main(): result = game.headers.get("Result", "*") print(f" {white} vs {black} Result: {result}") print() - print("Columns: ply side move played_eval best_eval loss class best_suggestion") + print( + "Columns: ply side move played_eval best_eval loss class best_suggestion" + ) # Brief performance summary (best-effort) try: thr_show = int(wanted_threads) except Exception: thr_show = 1 try: - hash_show = int(engine.options.get("Hash").value) if hasattr(engine, "options") and engine.options.get("Hash") else None + hash_show = ( + int(engine.options.get("Hash").value) + if hasattr(engine, "options") and engine.options.get("Hash") + else None + ) except Exception: hash_show = None if hash_show is not None: - print(f"Using engine options: Threads={thr_show}, Hash={hash_show} MB, MultiPV={effective_mpv}") + print( + f"Using engine options: Threads={thr_show}, Hash={hash_show} MB, MultiPV={effective_mpv}" + ) else: print(f"Using engine options: Threads={thr_show}, MultiPV={effective_mpv}") @@ -339,10 +388,20 @@ def main(): # If this is the final move in the mainline, analyze it and stop. if not move_node.variations: # Analyse current position to get engine best move suggestion - info_root_raw = engine.analyse(board, limit=limit, multipv=effective_mpv) - info_root = info_root_raw[0] if isinstance(info_root_raw, list) else info_root_raw + info_root_raw = engine.analyse( + board, limit=limit, multipv=effective_mpv + ) + info_root = ( + info_root_raw[0] + if isinstance(info_root_raw, list) + else info_root_raw + ) best_move = None - if info_root is not None and "pv" in info_root and info_root["pv"]: + if ( + info_root is not None + and "pv" in info_root + and info_root["pv"] + ): best_move = info_root["pv"][0] if best_move is None: res = engine.play(board, limit) @@ -353,29 +412,47 @@ def main(): # Evaluate played move board_played = board.copy() board_played.push(move) - info_played_raw = engine.analyse(board_played, limit=limit, multipv=effective_mpv) - info_played = info_played_raw[0] if isinstance(info_played_raw, list) else info_played_raw + info_played_raw = engine.analyse( + board_played, limit=limit, multipv=effective_mpv + ) + info_played = ( + info_played_raw[0] + if isinstance(info_played_raw, list) + else info_played_raw + ) if info_played is None or "score" not in info_played: played_cp, played_mate = None, None else: - played_cp, played_mate = score_to_cp(info_played["score"], pov_white=mover_white) + played_cp, played_mate = score_to_cp( + info_played["score"], pov_white=mover_white + ) # Evaluate best move position (for mover POV) - best_san = board.san(best_move) if best_move is not None else "?" + best_san = ( + board.san(best_move) if best_move is not None else "?" + ) if best_move is not None: board_best = board.copy() board_best.push(best_move) - info_best_raw = engine.analyse(board_best, limit=limit, multipv=effective_mpv) - info_best = info_best_raw[0] if isinstance(info_best_raw, list) else info_best_raw + info_best_raw = engine.analyse( + board_best, limit=limit, multipv=effective_mpv + ) + info_best = ( + info_best_raw[0] + if isinstance(info_best_raw, list) + else info_best_raw + ) if info_best is None or "score" not in info_best: best_cp, best_mate = None, None else: - best_cp, best_mate = score_to_cp(info_best["score"], pov_white=mover_white) + best_cp, best_mate = score_to_cp( + info_best["score"], pov_white=mover_white + ) else: best_cp, best_mate = None, None # Compute loss/classification - cp_loss: Optional[int] = None + cp_loss: int | None = None classification = "Unknown" if best_mate is not None or played_mate is not None: if best_mate is not None and played_mate is not None: @@ -397,10 +474,9 @@ def main(): classification = "Blunder" else: classification = "Blunder" - else: - if best_cp is not None and played_cp is not None: - cp_loss = max(0, best_cp - played_cp) - classification = classify_cp_loss(cp_loss) + elif best_cp is not None and played_cp is not None: + cp_loss = max(0, best_cp - played_cp) + classification = classify_cp_loss(cp_loss) side = "W" if mover_white else "B" print( @@ -422,8 +498,14 @@ def main(): mover_white = board.turn # Analyse position to get engine best move suggestion - info_root_raw = engine.analyse(board, limit=limit, multipv=effective_mpv) - info_root = info_root_raw[0] if isinstance(info_root_raw, list) else info_root_raw + info_root_raw = engine.analyse( + board, limit=limit, multipv=effective_mpv + ) + info_root = ( + info_root_raw[0] + if isinstance(info_root_raw, list) + else info_root_raw + ) best_move = None if info_root is not None and "pv" in info_root and info_root["pv"]: best_move = info_root["pv"][0] @@ -436,29 +518,45 @@ def main(): san = board.san(move) board_played = board.copy() board_played.push(move) - info_played_raw = engine.analyse(board_played, limit=limit, multipv=effective_mpv) - info_played = info_played_raw[0] if isinstance(info_played_raw, list) else info_played_raw + info_played_raw = engine.analyse( + board_played, limit=limit, multipv=effective_mpv + ) + info_played = ( + info_played_raw[0] + if isinstance(info_played_raw, list) + else info_played_raw + ) if info_played is None or "score" not in info_played: played_cp, played_mate = None, None else: - played_cp, played_mate = score_to_cp(info_played["score"], pov_white=mover_white) + played_cp, played_mate = score_to_cp( + info_played["score"], pov_white=mover_white + ) # Evaluate best move position (for mover POV) best_san = board.san(best_move) if best_move is not None else "?" if best_move is not None: board_best = board.copy() board_best.push(best_move) - info_best_raw = engine.analyse(board_best, limit=limit, multipv=effective_mpv) - info_best = info_best_raw[0] if isinstance(info_best_raw, list) else info_best_raw + info_best_raw = engine.analyse( + board_best, limit=limit, multipv=effective_mpv + ) + info_best = ( + info_best_raw[0] + if isinstance(info_best_raw, list) + else info_best_raw + ) if info_best is None or "score" not in info_best: best_cp, best_mate = None, None else: - best_cp, best_mate = score_to_cp(info_best["score"], pov_white=mover_white) + best_cp, best_mate = score_to_cp( + info_best["score"], pov_white=mover_white + ) else: best_cp, best_mate = None, None # Compute centipawn loss bands - cp_loss: Optional[int] = None + cp_loss: int | None = None classification = "Unknown" # Handle mate cases first if best_mate is not None or played_mate is not None: @@ -486,10 +584,9 @@ def main(): else: # Losing a forced mate or missing one classification = "Blunder" - else: - if best_cp is not None and played_cp is not None: - cp_loss = max(0, best_cp - played_cp) - classification = classify_cp_loss(cp_loss) + elif best_cp is not None and played_cp is not None: + cp_loss = max(0, best_cp - played_cp) + classification = classify_cp_loss(cp_loss) side = "W" if mover_white else "B" print( diff --git a/PYTHON/stockfish_analysis/requirements.txt b/PYTHON/stockfish_analysis/requirements.txt index 4f12b6e..e2c0b21 100644 --- a/PYTHON/stockfish_analysis/requirements.txt +++ b/PYTHON/stockfish_analysis/requirements.txt @@ -1,2 +1,2 @@ +psutil>=5.9 python-chess>=1.999 -psutil>=5.9 \ No newline at end of file diff --git a/PYTHON/tagDivider/README.md b/PYTHON/tagDivider/README.md new file mode 100644 index 0000000..2312381 --- /dev/null +++ b/PYTHON/tagDivider/README.md @@ -0,0 +1,15 @@ +# tagDivider + +Python script creating two directories, showing images in the script directory and putting those images into one of those directories depending on user input + +How to use: + +1. Install opencv for python3 + a) Linux: sudo apt-get install python3-opencv +2. Put the script into whatever folder you have images in +3. Run the script + python3 tagDivider.py +4. Enter folders names in terminal +5. Click "a" or "d" accordingly to folder you want image to be in + +If you want to change default buttons just modify script diff --git a/PYTHON/tagDivider/tagDivider.py b/PYTHON/tagDivider/tagDivider.py new file mode 100644 index 0000000..f6bba84 --- /dev/null +++ b/PYTHON/tagDivider/tagDivider.py @@ -0,0 +1,72 @@ +import os # for: os.getcwd; os.mkdir; os.listdir; +from os import path # for: os.path.abspath +import shutil # for: shutil.move + +import cv2 # for: cv2.imread; cv2.namedWindow; cv2.imshow; cv2.waitKey; cv2.destroyAllWindows; cv2.IMREAD_COLOR + +IMAGE_EXTENSION = ( + ".bmp", + ".dib", + ".jpeg", + ".jpg", + ".jpe", + ".jp2", + ".png", + ".pbm", + ".pgm", + ".ppm", + ".pxm", + ".pnm", + ".pfm", + ".sr", + ".ras", + ".tiff", + ".tif", + ".exr", + ".hdr", + ".pic", +) # Stolen from here: https://docs.opencv.org/4.5.2/d4/da8/group__imgcodecs.html I didn't include .webp because if the image is animated shit does not work +LEFT_FOLDER_CODE = 100 # Default 100 - 'd' +RIGHT_FOLDER_CODE = 97 # Default 97 - 'a' +# Change by checking: https://www.ascii-code.com/ + +firstFolderName = input("Enter first folder name: [a] ") +secondFolderName = input("Enter second folder name: [d] ") + +currentPath = os.path.abspath( + os.getcwd() +) # Stolen from: https://stackoverflow.com/q/3430372 +os.chdir(currentPath) # Change working directory to the path where the python file is + +if ( + path.isdir(firstFolderName) != 1 +): # Check if folder already exists, if it does not make it + os.mkdir(firstFolderName) +if path.isdir(secondFolderName) != 1: + os.mkdir(secondFolderName) + +for filename in os.listdir( + os.getcwd() +): # Go through every file in the working directory + if (filename.lower()).endswith( + IMAGE_EXTENSION + ): # If the file name ends with image extension + print(filename) + image = cv2.imread(filename, cv2.IMREAD_COLOR) + window_name = filename.split(".")[0] + cv2.namedWindow(window_name) # Window name is the same as image file name + cv2.imshow(window_name, image) + key = cv2.waitKey() + if key == RIGHT_FOLDER_CODE: + shutil.move( + currentPath + "/" + filename, + currentPath + "/" + firstFolderName + "/" + filename, + ) + elif key == LEFT_FOLDER_CODE: + shutil.move( + currentPath + "/" + filename, + currentPath + "/" + secondFolderName + "/" + filename, + ) + # else: + # print(key) + cv2.destroyAllWindows() diff --git a/TS/battery-status/README.md b/TS/battery-status/README.md index b25f8cf..15865a7 100644 --- a/TS/battery-status/README.md +++ b/TS/battery-status/README.md @@ -15,5 +15,6 @@ npm run dev Then open the printed local URL (default http://localhost:5173). Notes: + - The Battery Status API may be unavailable or disabled in some browsers for privacy reasons. In that case, the app will show a helpful message. -- On laptops it typically works in Chromium-based browsers; mobile support varies. \ No newline at end of file +- On laptops it typically works in Chromium-based browsers; mobile support varies. diff --git a/TS/battery-status/package.json b/TS/battery-status/package.json index 2ed28e9..d3e5565 100644 --- a/TS/battery-status/package.json +++ b/TS/battery-status/package.json @@ -4,9 +4,9 @@ "private": true, "type": "module", "scripts": { - "dev": "vite", - "typecheck": "tsc --noEmit", - "build": "vite build", + "dev": "vite", + "typecheck": "tsc --noEmit", + "build": "vite build", "preview": "vite preview --strictPort --port 5173" }, "dependencies": { @@ -14,7 +14,7 @@ "react-dom": "^18.3.1" }, "devDependencies": { - "@vitejs/plugin-react": "^4.3.1", + "@vitejs/plugin-react": "^4.3.1", "@types/react": "^18.3.5", "@types/react-dom": "^18.3.0", "typescript": "^5.5.4", diff --git a/TS/champions_leauge_scores/README.md b/TS/champions_leauge_scores/README.md index d0eb6cb..5b85a6b 100644 --- a/TS/champions_leauge_scores/README.md +++ b/TS/champions_leauge_scores/README.md @@ -1,12 +1,13 @@ # Champions League Live Scores (React + TS) This app displays live and today's UEFA Champions League results. It uses: + - React + TypeScript (Vite) for the frontend - A tiny Express proxy server that calls football-data.org to fetch match data ## Setup -1) Create a `.env` file in `TS/champions_leauge_scores/`: +1. Create a `.env` file in `TS/champions_leauge_scores/`: ``` FOOTBALL_DATA_API_KEY=your_api_token_here @@ -15,7 +16,7 @@ PORT=8787 Sign up at https://www.football-data.org/ to get a free API token. Free tier has rate limits. -2) Install dependencies and run both servers: +2. Install dependencies and run both servers: ``` npm install @@ -26,9 +27,11 @@ npm run dev - API Proxy: http://localhost:8787 ## Notes + - Live endpoint: `GET /api/live` - Today endpoint: `GET /api/matches` (uses today's date by default) - Edit polling intervals in `src/App.tsx` if needed. ## License -MIT \ No newline at end of file + +MIT diff --git a/TS/champions_leauge_scores/package.json b/TS/champions_leauge_scores/package.json index 3a0fc12..58e4a7f 100644 --- a/TS/champions_leauge_scores/package.json +++ b/TS/champions_leauge_scores/package.json @@ -4,29 +4,29 @@ "private": true, "type": "module", "scripts": { - "dev": "concurrently \"vite\" \"npm:server:dev\"", + "dev": "concurrently \"vite\" \"npm:server:dev\"", "build": "vite build", "preview": "vite preview", - "server:dev": "tsx watch server/src/server.ts" + "server:dev": "tsx watch server/src/server.ts" }, "dependencies": { "axios": "^1.7.2", "dotenv": "^16.4.5", - "cors": "^2.8.5", + "cors": "^2.8.5", "express": "^4.19.2", "react": "^18.3.1", "react-dom": "^18.3.1" }, "devDependencies": { - "@vitejs/plugin-react": "^4.3.1", + "@vitejs/plugin-react": "^4.3.1", "@types/express": "^4.17.21", - "@types/cors": "^2.8.17", + "@types/cors": "^2.8.17", "@types/node": "^20.12.12", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "concurrently": "^8.2.2", - "ts-node-dev": "^2.0.0", - "tsx": "^4.19.2", + "ts-node-dev": "^2.0.0", + "tsx": "^4.19.2", "typescript": "^5.4.5", "vite": "^5.3.3" } diff --git a/TS/two-inputs/angular.json b/TS/two-inputs/angular.json new file mode 100644 index 0000000..60bd5d4 --- /dev/null +++ b/TS/two-inputs/angular.json @@ -0,0 +1,92 @@ +{ + "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "version": 1, + "newProjectRoot": "projects", + "projects": { + "two-inputs": { + "projectType": "application", + "schematics": { + "@schematics/angular:component": { + "style": "scss" + } + }, + "root": "", + "sourceRoot": "src", + "prefix": "app", + "architect": { + "build": { + "builder": "@angular-devkit/build-angular:application", + "options": { + "outputPath": "dist/two-inputs", + "index": "src/index.html", + "browser": "src/main.ts", + "polyfills": ["zone.js"], + "tsConfig": "tsconfig.app.json", + "inlineStyleLanguage": "scss", + "assets": ["src/favicon.ico", "src/assets"], + "styles": [ + "@angular/material/prebuilt-themes/indigo-pink.css", + "src/styles.scss" + ], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "outputHashing": "all" + }, + "development": { + "optimization": false, + "extractLicenses": false, + "sourceMap": true + } + }, + "defaultConfiguration": "production" + }, + "serve": { + "builder": "@angular-devkit/build-angular:dev-server", + "configurations": { + "production": { + "buildTarget": "two-inputs:build:production" + }, + "development": { + "buildTarget": "two-inputs:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "builder": "@angular-devkit/build-angular:extract-i18n", + "options": { + "buildTarget": "two-inputs:build" + } + }, + "test": { + "builder": "@angular-devkit/build-angular:karma", + "options": { + "polyfills": ["zone.js", "zone.js/testing"], + "tsConfig": "tsconfig.spec.json", + "inlineStyleLanguage": "scss", + "assets": ["src/favicon.ico", "src/assets"], + "styles": [ + "@angular/material/prebuilt-themes/indigo-pink.css", + "src/styles.scss" + ], + "scripts": [] + } + } + } + } + } +} diff --git a/TS/two-inputs/package.json b/TS/two-inputs/package.json new file mode 100644 index 0000000..642045d --- /dev/null +++ b/TS/two-inputs/package.json @@ -0,0 +1,40 @@ +{ + "name": "two-inputs", + "version": "0.0.0", + "scripts": { + "ng": "ng", + "start": "ng serve", + "build": "ng build", + "watch": "ng build --watch --configuration development", + "test": "ng test" + }, + "private": true, + "dependencies": { + "@angular/animations": "^17.0.0", + "@angular/cdk": "^17.3.5", + "@angular/common": "^17.0.0", + "@angular/compiler": "^17.0.0", + "@angular/core": "^17.0.0", + "@angular/forms": "^17.0.0", + "@angular/material": "^17.3.5", + "@angular/platform-browser": "^17.0.0", + "@angular/platform-browser-dynamic": "^17.0.0", + "@angular/router": "^17.0.0", + "rxjs": "~7.8.0", + "tslib": "^2.3.0", + "zone.js": "~0.14.2" + }, + "devDependencies": { + "@angular-devkit/build-angular": "^17.0.3", + "@angular/cli": "^17.0.3", + "@angular/compiler-cli": "^17.0.0", + "@types/jasmine": "~5.1.0", + "jasmine-core": "~5.1.0", + "karma": "~6.4.0", + "karma-chrome-launcher": "~3.2.0", + "karma-coverage": "~2.2.0", + "karma-jasmine": "~5.1.0", + "karma-jasmine-html-reporter": "~2.1.0", + "typescript": "~5.2.2" + } +} diff --git a/TS/two-inputs/src/app/app.component.html b/TS/two-inputs/src/app/app.component.html new file mode 100644 index 0000000..a6a7a6e --- /dev/null +++ b/TS/two-inputs/src/app/app.component.html @@ -0,0 +1,30 @@ + + min + + + + max + + + + step + + + + targetValue + +
+ + inputOne + + + + +
+ + inputTwo + + + + +
diff --git a/TS/two-inputs/src/app/app.component.ts b/TS/two-inputs/src/app/app.component.ts new file mode 100644 index 0000000..2db98d5 --- /dev/null +++ b/TS/two-inputs/src/app/app.component.ts @@ -0,0 +1,138 @@ +import { Component } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { RouterOutlet } from '@angular/router'; +import { MatInputModule } from '@angular/material/input'; +import {MatButtonModule} from '@angular/material/button'; +import { FormsModule } from '@angular/forms'; + +@Component({ + selector: 'app-root', + standalone: true, + imports: [CommonModule, RouterOutlet, MatInputModule, FormsModule, MatButtonModule], + templateUrl: './app.component.html', + styleUrls: ['./app.component.scss'] +}) +export class AppComponent { + inputOne: number | null = null; // Default initialization to 50 + inputTwo: number | null = null; // Default initialization to 50 + min: number | null = 100; + max: number | null = 250; + step: number | null = 25; + targetValue: number | null = 325; + indexOne: number = 0; + indexTwo: number = 0; + possibleValues: Array<[number, number]> | null = []; + + constructor() { + this.possibleValues = AppComponent.findValidPairs(this.step, this.min, this.max, this.targetValue); + } + + ngOnInit() { + this.possibleValues = AppComponent.findValidPairs(this.step, this.min, this.max, this.targetValue); + } + + public updateInput() { + this.possibleValues = AppComponent.findValidPairs(this.step, this.min, this.max, this.targetValue); + } + + private changeIndex(currentValue: number, direction: boolean) { + this.possibleValues = AppComponent.findValidPairs(this.step, this.min, this.max, this.targetValue); + const length = this.possibleValues?.length; + if(typeof length !== "undefined") { + if(direction) { + if(currentValue + 1 > length - 1) { + return 0; + } + return currentValue + 1; + } else { + if(currentValue - 1 < 0) { + return length - 1; + } + return currentValue - 1; + } + } else { + console.error(`appComponent, changeIndex, length is undefined!`, length); + } + return currentValue; + } + + updateTwoValue() { + if(this.possibleValues !== null) { + this.inputOne = this.possibleValues[this.indexOne][0]; + if(typeof this.inputOne !== "undefined" && this.inputOne !== null) { + const result = AppComponent.findCorrespondingValue(this.possibleValues, this.inputOne); + if(result !== null) { + [this.inputTwo, this.indexTwo] = result; + return; + } + console.error(`result is null!`); + } + console.error(`this.inputOne is null or undefined!: `, this.inputOne, this.possibleValues, this.indexOne); + } + } + + upOne() { + this.indexOne = this.changeIndex(this.indexOne, true); + this.updateTwoValue(); + } + + downOne() { + this.indexOne = this.changeIndex(this.indexOne, false); + this.updateTwoValue(); + } + + upTwo() { + this.indexTwo = this.changeIndex(this.indexTwo, true); + if(this.possibleValues !== null) { + this.inputTwo = this.possibleValues[this.indexTwo][1]; + const result = AppComponent.findCorrespondingValue(this.possibleValues, this.inputTwo); + if(result !== null) { + [this.inputOne, this.indexOne] = result; + } + } + } + + downTwo() { + this.indexTwo = this.changeIndex(this.indexTwo, false); + if(this.possibleValues !== null) { + this.inputTwo = this.possibleValues[this.indexTwo][1]; + const result = AppComponent.findCorrespondingValue(this.possibleValues, this.inputTwo); + if(result !== null) { + [this.inputOne, this.indexOne] = result; + } + } + } + + private static findCorrespondingValue(pairs: Array<[number, number]>, number: number): [number, number] | null { + for (let index = 0; index < pairs.length; index += 1) { + if (pairs[index][0] === number) { + return [pairs[index][1], index]; // Return n2 if the given number matches n1 + } else if (pairs[index][1] === number) { + return [pairs[index][0], index]; // Return n1 if the given number matches n2 + } + } + console.error("No corresponding value found for the provided number in the pairs.", pairs, number); + return null; // Return null if no matching number is found +} + + private static findValidPairs(x: number | null, y: number | null, z: number | null, ml: number | null): Array<[number, number]> | null { + if (x === null || y === null || z === null || ml === null) { + console.error("findValidPairs, some value is null"); + return null; + } + + const results: Array<[number, number]> = []; + + // Iterate through possible values of n1, which must be multiples of x, at least y, and not more than z + for (let n1 = y; n1 <= ml - y && n1 <= z; n1 += x) { + let n2 = ml - n1; + // Ensure n2 is also a multiple of x, n2 >= y, and n2 <= z + if (n2 % x === 0 && n2 >= y && n2 <= z) { + results.push([n1, n2]); + } + } + + return results; +} + +} diff --git a/TS/two-inputs/tsconfig.app.json b/TS/two-inputs/tsconfig.app.json new file mode 100644 index 0000000..84f1f99 --- /dev/null +++ b/TS/two-inputs/tsconfig.app.json @@ -0,0 +1,10 @@ +/* To learn more about this file see: https://angular.io/config/tsconfig. */ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./out-tsc/app", + "types": [] + }, + "files": ["src/main.ts"], + "include": ["src/**/*.d.ts"] +} diff --git a/TS/two-inputs/tsconfig.json b/TS/two-inputs/tsconfig.json new file mode 100644 index 0000000..d9ae4ec --- /dev/null +++ b/TS/two-inputs/tsconfig.json @@ -0,0 +1,29 @@ +/* To learn more about this file see: https://angular.io/config/tsconfig. */ +{ + "compileOnSave": false, + "compilerOptions": { + "outDir": "./dist/out-tsc", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "esModuleInterop": true, + "sourceMap": true, + "declaration": false, + "experimentalDecorators": true, + "moduleResolution": "node", + "importHelpers": true, + "target": "ES2022", + "module": "ES2022", + "useDefineForClassFields": false, + "lib": ["ES2022", "dom"] + }, + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/TS/two-inputs/tsconfig.spec.json b/TS/two-inputs/tsconfig.spec.json new file mode 100644 index 0000000..47e3dd7 --- /dev/null +++ b/TS/two-inputs/tsconfig.spec.json @@ -0,0 +1,9 @@ +/* To learn more about this file see: https://angular.io/config/tsconfig. */ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./out-tsc/spec", + "types": ["jasmine"] + }, + "include": ["src/**/*.spec.ts", "src/**/*.d.ts"] +} diff --git a/articles/.gitignore b/articles/.gitignore index 6eddaa1..b99cebb 100644 --- a/articles/.gitignore +++ b/articles/.gitignore @@ -1,2 +1,2 @@ data/* -uploads/* \ No newline at end of file +uploads/* diff --git a/articles/README.md b/articles/README.md index 5f91d58..eb6aff2 100644 --- a/articles/README.md +++ b/articles/README.md @@ -9,7 +9,9 @@ Mini Articles (<=14KB) - Local persistence via localStorage (no server required) How to open + - Open `site/index.html` in a browser. Tests + - `pytest` includes a test to enforce the 14KB budget for `index.html`. diff --git a/articles/cppcheck.txt b/articles/cppcheck.txt index 2b07c1d..60b6990 100644 --- a/articles/cppcheck.txt +++ b/articles/cppcheck.txt @@ -476,4 +476,3 @@ server_c.c:44:12: style: The function 'append_file_line' is never used. [unusedF static int append_file_line(const char* path, const char* line){ FILE* f=fopen(path,"ab"); if(!f) return -1; size_t n=fwrite(line,1,strlen(line),f); n+=fwrite("\n",1,1,f); fclose(f); return (int)n>=0?0:-1; } ^ nofile:0:0: information: Active checkers: 117/966 (use --checkers-report= to see details) [checkersReport] - diff --git a/articles/data/articles.json b/articles/data/articles.json index 9e2b89c..35d8357 100644 --- a/articles/data/articles.json +++ b/articles/data/articles.json @@ -1 +1,24 @@ -[{"id":"29176c917f1a66c3","title":"full featuresd article","author":"author","body":"\"image\"

This is an important image of course :)

and nother:
\"image\"
","thumb":"/uploads/29176c9e7818ee30.jpg","createdAt":1757331025041},{"id":"199259f41b8933b8","title":"Whats heveier","body":"A kilogrem of stel or kilogrem of fethers","thumb":"/uploads/27d6402e78a09d65.jpg","createdAt":1757272818104},{"id":"19925965dead21e7","title":"UwU its my first article","body":":)))

","thumb":"/uploads/27d640310e163446.jpg","createdAt":1757272235498}] \ No newline at end of file +[ + { + "id": "29176c917f1a66c3", + "title": "full featuresd article", + "author": "author", + "body": "\"image\"

This is an important image of course :)

and nother:
\"image\"
", + "thumb": "/uploads/29176c9e7818ee30.jpg", + "createdAt": 1757331025041 + }, + { + "id": "199259f41b8933b8", + "title": "Whats heveier", + "body": "A kilogrem of stel or kilogrem of fethers", + "thumb": "/uploads/27d6402e78a09d65.jpg", + "createdAt": 1757272818104 + }, + { + "id": "19925965dead21e7", + "title": "UwU its my first article", + "body": ":)))

", + "thumb": "/uploads/27d640310e163446.jpg", + "createdAt": 1757272235498 + } +] diff --git a/articles/test_server_api.py b/articles/test_server_api.py index 3e945ac..104e0e3 100644 --- a/articles/test_server_api.py +++ b/articles/test_server_api.py @@ -1,11 +1,11 @@ import json import os -import time -import urllib.request -import urllib.error -import subprocess -import socket from pathlib import Path +import socket +import subprocess +import time +import urllib.error +import urllib.request def _req(url, method="GET", data=None): @@ -40,47 +40,55 @@ def test_crud_roundtrip(tmp_path): # wait briefly for server to be ready for _ in range(30): try: - with urllib.request.urlopen(base + "/api/articles", timeout=0.2) as resp: + with urllib.request.urlopen( + base + "/api/articles", timeout=0.2 + ) as resp: resp.read() break except Exception: time.sleep(0.05) # Create - code, body = _req(base+"/api/articles", method="POST", data={ - "title": "T1", - "body": "

Hello

", - "thumb": "data:image/png;base64,xyz" - }) + code, body = _req( + base + "/api/articles", + method="POST", + data={ + "title": "T1", + "body": "

Hello

", + "thumb": "data:image/png;base64,xyz", + }, + ) assert code == 201 created = json.loads(body) art_id = created["id"] # List - code, body = _req(base+"/api/articles") + code, body = _req(base + "/api/articles") assert code == 200 items = json.loads(body) assert any(a["id"] == art_id for a in items) # Get one - code, body = _req(base+f"/api/articles/{art_id}") + code, body = _req(base + f"/api/articles/{art_id}") assert code == 200 got = json.loads(body) assert got["title"] == "T1" # Update - code, body = _req(base+f"/api/articles/{art_id}", method="PUT", data={"title": "T2"}) + code, body = _req( + base + f"/api/articles/{art_id}", method="PUT", data={"title": "T2"} + ) assert code == 200 updated = json.loads(body) assert updated["title"] == "T2" # Delete - code, _ = _req(base+f"/api/articles/{art_id}", method="DELETE") + code, _ = _req(base + f"/api/articles/{art_id}", method="DELETE") assert code == 204 # Ensure gone try: - _req(base+f"/api/articles/{art_id}") + _req(base + f"/api/articles/{art_id}") assert False, "Expected 404" except urllib.error.HTTPError as e: assert e.code == 404 diff --git a/articles/test_site_size.py b/articles/test_site_size.py index 1a8e18e..46928ea 100644 --- a/articles/test_site_size.py +++ b/articles/test_site_size.py @@ -4,7 +4,7 @@ import os BUDGET = 14 * 1024 # 14 KiB HERE = os.path.dirname(__file__) -SITE_FILE = os.path.join(HERE, 'index.html') +SITE_FILE = os.path.join(HERE, "index.html") def test_site_file_exists(): diff --git a/lint_python.sh b/lint_python.sh new file mode 100755 index 0000000..d1d1cc6 --- /dev/null +++ b/lint_python.sh @@ -0,0 +1,352 @@ +#!/usr/bin/env bash +# ============================================================================== +# Python Linting Script - Run ALL linters with aggressive settings +# ============================================================================== +# Usage: +# ./lint_python.sh # Lint all Python files +# ./lint_python.sh --fix # Lint and auto-fix where possible +# ./lint_python.sh # Lint specific file +# ./lint_python.sh --quick # Quick lint (ruff + mypy only) +# ./lint_python.sh --report # Generate detailed reports +# ============================================================================== + +set -euo pipefail + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +MAGENTA='\033[0;35m' +CYAN='\033[0;36m' +NC='\033[0m' # No Color +BOLD='\033[1m' + +# Configuration +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="${SCRIPT_DIR}" +PYTHON_PATHS=( + "PYTHON" + "articles" + "poker-modifier-app" + "tests" +) +EXCLUDE_PATHS=( + ".venv" + "__pycache__" + ".git" + "Bash/ffmpeg-build" + ".pytest_cache" + ".ruff_cache" + ".mypy_cache" +) + +# Build exclude pattern for find +EXCLUDE_PATTERN="" +for path in "${EXCLUDE_PATHS[@]}"; do + EXCLUDE_PATTERN="${EXCLUDE_PATTERN} -path '*/${path}/*' -prune -o" +done + +# Parse arguments +FIX_MODE=false +QUICK_MODE=false +REPORT_MODE=false +TARGET_FILES="" +VERBOSE=false + +while [[ $# -gt 0 ]]; do + case $1 in + --fix|-f) + FIX_MODE=true + shift + ;; + --quick|-q) + QUICK_MODE=true + shift + ;; + --report|-r) + REPORT_MODE=true + shift + ;; + --verbose|-v) + VERBOSE=true + shift + ;; + --help|-h) + echo "Usage: $0 [OPTIONS] [FILES...]" + echo "" + echo "Options:" + echo " --fix, -f Auto-fix issues where possible" + echo " --quick, -q Quick mode (ruff + mypy only)" + echo " --report, -r Generate detailed reports to ./lint-reports/" + echo " --verbose, -v Show verbose output" + echo " --help, -h Show this help message" + echo "" + echo "Examples:" + echo " $0 # Lint all Python files" + echo " $0 --fix # Lint and auto-fix" + echo " $0 PYTHON/ # Lint specific directory" + echo " $0 --quick --fix # Quick lint with auto-fix" + exit 0 + ;; + *) + TARGET_FILES="${TARGET_FILES} $1" + shift + ;; + esac +done + +# If no target specified, use default paths +if [[ -z "${TARGET_FILES}" ]]; then + TARGET_FILES="${PYTHON_PATHS[*]}" +fi + +# Create reports directory if needed +if [[ "${REPORT_MODE}" == true ]]; then + mkdir -p "${PROJECT_ROOT}/lint-reports" +fi + +# Track overall status +OVERALL_STATUS=0 +FAILED_TOOLS=() + +# ============================================================================== +# Helper functions +# ============================================================================== + +print_header() { + echo "" + echo -e "${BOLD}${BLUE}══════════════════════════════════════════════════════════════${NC}" + echo -e "${BOLD}${BLUE} $1${NC}" + echo -e "${BOLD}${BLUE}══════════════════════════════════════════════════════════════${NC}" +} + +print_subheader() { + echo "" + echo -e "${CYAN}──────────────────────────────────────────────────────────────${NC}" + echo -e "${CYAN} $1${NC}" + echo -e "${CYAN}──────────────────────────────────────────────────────────────${NC}" +} + +print_success() { + echo -e "${GREEN}✓${NC} $1" +} + +print_warning() { + echo -e "${YELLOW}⚠${NC} $1" +} + +print_error() { + echo -e "${RED}✗${NC} $1" +} + +print_info() { + echo -e "${BLUE}ℹ${NC} $1" +} + +run_tool() { + local tool_name="$1" + local tool_cmd="$2" + local report_file="${PROJECT_ROOT}/lint-reports/${tool_name}.txt" + + print_subheader "Running ${tool_name}..." + + if [[ "${REPORT_MODE}" == true ]]; then + if eval "${tool_cmd}" 2>&1 | tee "${report_file}"; then + print_success "${tool_name} passed" + return 0 + else + print_error "${tool_name} found issues (see ${report_file})" + FAILED_TOOLS+=("${tool_name}") + return 1 + fi + else + if eval "${tool_cmd}"; then + print_success "${tool_name} passed" + return 0 + else + print_error "${tool_name} found issues" + FAILED_TOOLS+=("${tool_name}") + return 1 + fi + fi +} + +check_tool() { + if command -v "$1" &> /dev/null; then + return 0 + else + print_warning "$1 not found, skipping..." + return 1 + fi +} + +# ============================================================================== +# Main linting process +# ============================================================================== + +print_header "Python Linting Suite - Aggressive Mode" +echo "" +print_info "Target: ${TARGET_FILES}" +print_info "Fix mode: ${FIX_MODE}" +print_info "Quick mode: ${QUICK_MODE}" +print_info "Report mode: ${REPORT_MODE}" + +cd "${PROJECT_ROOT}" + +# ============================================================================== +# RUFF - Primary linter and formatter +# ============================================================================== +if check_tool ruff; then + if [[ "${FIX_MODE}" == true ]]; then + run_tool "ruff-lint" "ruff check --fix --show-fixes ${TARGET_FILES}" || OVERALL_STATUS=1 + run_tool "ruff-format" "ruff format ${TARGET_FILES}" || OVERALL_STATUS=1 + else + run_tool "ruff-lint" "ruff check ${TARGET_FILES}" || OVERALL_STATUS=1 + run_tool "ruff-format-check" "ruff format --check ${TARGET_FILES}" || OVERALL_STATUS=1 + fi +fi + +# ============================================================================== +# MYPY - Static type checking +# ============================================================================== +if check_tool mypy; then + run_tool "mypy" "mypy --strict --ignore-missing-imports ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# Quick mode exits here +if [[ "${QUICK_MODE}" == true ]]; then + print_header "Quick Lint Complete" + if [[ ${#FAILED_TOOLS[@]} -gt 0 ]]; then + print_error "Failed tools: ${FAILED_TOOLS[*]}" + exit 1 + else + print_success "All quick checks passed!" + exit 0 + fi +fi + +# ============================================================================== +# PYLINT - Comprehensive linting +# ============================================================================== +if check_tool pylint; then + run_tool "pylint" "pylint --rcfile=pyproject.toml --jobs=0 --fail-under=0 ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# BANDIT - Security linting +# ============================================================================== +if check_tool bandit; then + run_tool "bandit" "bandit -c pyproject.toml -r ${TARGET_FILES} --severity-level low --confidence-level low" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# VULTURE - Dead code detection +# ============================================================================== +if check_tool vulture; then + run_tool "vulture" "vulture --min-confidence 80 ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# FLAKE8 - Traditional linter +# ============================================================================== +if check_tool flake8; then + run_tool "flake8" "flake8 --max-line-length=88 --extend-ignore=E203,W503 --max-complexity=10 ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# PYCODESTYLE - PEP 8 style checker +# ============================================================================== +if check_tool pycodestyle; then + run_tool "pycodestyle" "pycodestyle --max-line-length=88 --ignore=E203,W503 ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# PYDOCSTYLE - Docstring style checker +# ============================================================================== +if check_tool pydocstyle; then + run_tool "pydocstyle" "pydocstyle --convention=google ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# RADON - Complexity metrics +# ============================================================================== +if check_tool radon; then + print_subheader "Running radon (complexity analysis)..." + echo "" + echo -e "${MAGENTA}Cyclomatic Complexity:${NC}" + radon cc -a -s ${TARGET_FILES} || true + echo "" + echo -e "${MAGENTA}Maintainability Index:${NC}" + radon mi -s ${TARGET_FILES} || true + + if [[ "${REPORT_MODE}" == true ]]; then + radon cc -a -s ${TARGET_FILES} > "${PROJECT_ROOT}/lint-reports/radon-cc.txt" 2>&1 || true + radon mi -s ${TARGET_FILES} > "${PROJECT_ROOT}/lint-reports/radon-mi.txt" 2>&1 || true + fi +fi + +# ============================================================================== +# INTERROGATE - Docstring coverage +# ============================================================================== +if check_tool interrogate; then + run_tool "interrogate" "interrogate -v --fail-under=0 ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# PYRIGHT - Microsoft's type checker (optional, very strict) +# ============================================================================== +if check_tool pyright; then + run_tool "pyright" "pyright ${TARGET_FILES}" || OVERALL_STATUS=1 +fi + +# ============================================================================== +# AUTOFLAKE - Unused imports/variables (fix mode only) +# ============================================================================== +if [[ "${FIX_MODE}" == true ]] && check_tool autoflake; then + print_subheader "Running autoflake (removing unused imports)..." + find ${TARGET_FILES} -name "*.py" -type f -exec autoflake --in-place --remove-all-unused-imports --remove-unused-variables {} \; + print_success "autoflake completed" +fi + +# ============================================================================== +# PYUPGRADE - Upgrade Python syntax (fix mode only) +# ============================================================================== +if [[ "${FIX_MODE}" == true ]] && check_tool pyupgrade; then + print_subheader "Running pyupgrade (upgrading syntax to Python 3.10+)..." + find ${TARGET_FILES} -name "*.py" -type f -exec pyupgrade --py310-plus {} \; + print_success "pyupgrade completed" +fi + +# ============================================================================== +# CODESPELL - Spell checking +# ============================================================================== +if check_tool codespell; then + if [[ "${FIX_MODE}" == true ]]; then + run_tool "codespell" "codespell -w --skip='*.json,*.lock,.git,__pycache__,.venv' ${TARGET_FILES}" || OVERALL_STATUS=1 + else + run_tool "codespell" "codespell --skip='*.json,*.lock,.git,__pycache__,.venv' ${TARGET_FILES}" || OVERALL_STATUS=1 + fi +fi + +# ============================================================================== +# Summary +# ============================================================================== +print_header "Linting Summary" +echo "" + +if [[ ${#FAILED_TOOLS[@]} -gt 0 ]]; then + print_error "The following tools reported issues:" + for tool in "${FAILED_TOOLS[@]}"; do + echo " - ${tool}" + done + echo "" + if [[ "${REPORT_MODE}" == true ]]; then + print_info "Detailed reports saved to: ${PROJECT_ROOT}/lint-reports/" + fi + print_info "Run with --fix to auto-fix issues where possible" + exit 1 +else + print_success "All linting checks passed!" + exit 0 +fi diff --git a/poker-modifier-app/README.md b/poker-modifier-app/README.md index ffadc1a..18f45bc 100644 --- a/poker-modifier-app/README.md +++ b/poker-modifier-app/README.md @@ -47,7 +47,10 @@ A fun web application that randomly applies modifiers to Texas Hold'em poker gam You can easily add new modifiers by using the `addModifier()` method: ```javascript -window.pokerApp.addModifier("Your Modifier Name", "Description of what it does"); +window.pokerApp.addModifier( + "Your Modifier Name", + "Description of what it does", +); ``` ## Browser Compatibility diff --git a/poker-modifier-app/README_python.md b/poker-modifier-app/README_python.md index 846c6ac..a1ce5f9 100644 --- a/poker-modifier-app/README_python.md +++ b/poker-modifier-app/README_python.md @@ -32,6 +32,7 @@ python poker_modifier_app.py ## Modifiers Included ### Classic Poker Modifiers + - **High Stakes**: All bets are doubled - **Wild Card**: Next card can be used as any card - **Bluff Master**: See one opponent's card before betting @@ -51,6 +52,7 @@ python poker_modifier_app.py ## Modifiers Included ### Classic Poker Modifiers + - **High Stakes**: All bets are doubled - **Wild Card**: Next card can be used as any card - **Bluff Master**: See one opponent's card before betting @@ -68,6 +70,7 @@ python poker_modifier_app.py - **Chip Challenge**: Winner gets extra house chips ### Drinking Game Modifiers + - **Red or Black**: Guess community card colors for double winnings - **Pocket Rockets**: Pocket Aces trigger drinks for everyone else - **Rainbow Flop**: 3-suit flop boosts flush draws diff --git a/poker-modifier-app/index.html b/poker-modifier-app/index.html index 9789b27..34a1f19 100644 --- a/poker-modifier-app/index.html +++ b/poker-modifier-app/index.html @@ -18,15 +18,15 @@ 30% - +

Click "Start Round" to begin!

- + - +
Rounds Played: @@ -39,7 +39,7 @@
- + diff --git a/poker-modifier-app/poker_modifier_app.py b/poker-modifier-app/poker_modifier_app.py index ed89c70..72dba76 100644 --- a/poker-modifier-app/poker_modifier_app.py +++ b/poker-modifier-app/poker_modifier_app.py @@ -27,7 +27,7 @@ class PokerModifierApp: "name": "High Card Hero", "description": "Win with just high card: collect your normal winnings + 1 chip from each player." }, - + # Card Enhancement Modifiers { "name": "Face Card Power", @@ -49,13 +49,13 @@ class PokerModifierApp: "name": "Steel Cards", "description": "Random rank chosen: {steel_rank}. All {steel_rank}s beat everything this hand!" }, - + # Ante-Based Effects (Clear Money Source) { "name": "Bonus Pool", "description": "Everyone puts 2 chips in bonus pool. First person to make any pair wins it all." }, - + # Deck Manipulation (Balatro-style) { "name": "Deck Shuffle", @@ -69,13 +69,13 @@ class PokerModifierApp: "name": "Phantom Cards", "description": "Deal 6 community cards, but randomly remove 1 before showdown." }, - + # Special Betting Rules (Realistic Economics) { "name": "Escalation", "description": "Each raise must be at least 2x the previous raise (not just matching)." }, - + # Position and Action Modifiers { "name": "Button Bonus", @@ -85,7 +85,7 @@ class PokerModifierApp: "name": "Call Penalty", "description": "Anyone who only calls (never raises) pays 1 chip penalty to pot." }, - + # Information Warfare { "name": "Poker Face", @@ -99,7 +99,7 @@ class PokerModifierApp: "name": "Open Book", "description": "Everyone plays with one hole card face-up." }, - + # Drinking Game Integration { "name": "Liquid Courage", @@ -117,7 +117,7 @@ class PokerModifierApp: "name": "Drink Tax", "description": "Each red card in your final hand = one sip (reveal afret play) ." }, - + # Wild and Chaos Effects { "name": "Joker's Wild", @@ -135,7 +135,7 @@ class PokerModifierApp: "name": "Time Warp", "description": "Play the hand completely backwards: showdown first, then remove random cards from table !" }, - + # Economic Effects (Clear Money Sources) { "name": "Poverty Mode", @@ -149,7 +149,7 @@ class PokerModifierApp: "name": "Charity Case", "description": "Player with fewest chips get ther ente funded by richest player." }, - + # Penalty-Based Modifiers (Clear Consequences) { "name": "Fold Tax", @@ -167,7 +167,7 @@ class PokerModifierApp: "name": "Talk Tax", "description": "Every word spoken during betting costs 1 chip to the pot." }, - + # Skill Challenges (With Clear Rewards/Penalties) { "name": "Memory Challenge", @@ -185,7 +185,7 @@ class PokerModifierApp: "name": "Prediction Pool", "description": "Everyone puts 1 chip in pool. Guess the river card exactly = win the pool." }, - + # Partnership Modifiers { "name": "Buddy System", @@ -208,7 +208,7 @@ class PokerModifierApp: "description": "If both partners make it to showdown, they both get +1 chip bonus from other players (revalt at the end of round)." }, ] - + # Separate endgame modifiers for special handling self.endgame_modifiers = [ # Classic Endgame Modifiers @@ -228,7 +228,7 @@ class PokerModifierApp: "name": "Double or Nothing", "description": "Winner gets double payout, but everyone else pays double penalty." }, - + # High Stakes Endgame { "name": "All In Madness", @@ -242,7 +242,7 @@ class PokerModifierApp: "name": "Last Stand", "description": "Player with fewest chips gets to act last in ALL betting rounds." }, - + # Dramatic Reversals { "name": "Underdog Victory", @@ -279,7 +279,7 @@ class PokerModifierApp: "name": "Lightning Round", "description": "Deal all 5 community cards at once. Betting happens after each card revealed." }, - + # Psychological Warfare { "name": "Confession Booth", @@ -293,7 +293,7 @@ class PokerModifierApp: "name": "Poker Face Off", "description": "Staring contest: losers must reveal one hole card to the table." }, - + # Endgame Economics { "name": "Wealth Redistribution", @@ -307,7 +307,7 @@ class PokerModifierApp: "name": "Final Ante", "description": "Everyone must put in their last 2 chips before seeing cards. No backing out." }, - + # Apocalypse Modifiers { "name": "Nuclear Option", @@ -321,7 +321,7 @@ class PokerModifierApp: "name": "Solar Flare", "description": "All suits become the same suit (dealer's choice)." }, - + # Legacy Modifiers { "name": "Hall of Fame", @@ -335,7 +335,7 @@ class PokerModifierApp: "name": "Photo Finish", "description": "Take a photo of the winning hand - it goes in the poker hall of fame." }, - + # Chaos Theory { "name": "Butterfly Effect", @@ -350,11 +350,11 @@ class PokerModifierApp: "description": "Deal 2 separate boards. Players choose which board to play after seeing both." } ] - + # Remove endgame modifiers from regular modifier list endgame_modifier_names = [mod['name'] for mod in self.endgame_modifiers] self.modifiers = [mod for mod in self.modifiers if mod['name'] not in endgame_modifier_names] - + # Game state tracking self.rounds_played = 0 self.modifiers_applied = 0 @@ -362,9 +362,9 @@ class PokerModifierApp: self.endgame_threshold = 0.8 # Start endgame modifiers at 80% of total rounds self.debug_mode = False self.force_endgame = False - + self.setup_gui() - + def setup_gui(self): # Create main window self.root = tk.Tk() @@ -372,15 +372,15 @@ class PokerModifierApp: self.root.geometry("650x750") self.root.configure(bg='#0f4c3a') self.root.resizable(True, True) - + # Configure style style = ttk.Style() style.theme_use('clam') - + # Main container main_frame = tk.Frame(self.root, bg='#0f4c3a', padx=20, pady=20) main_frame.pack(fill=tk.BOTH, expand=True) - + # Title title_label = tk.Label( main_frame, @@ -390,7 +390,7 @@ class PokerModifierApp: bg='#0f4c3a' ) title_label.pack(pady=(0, 20)) - + # Settings frame settings_frame = tk.LabelFrame( main_frame, @@ -402,11 +402,11 @@ class PokerModifierApp: bd=2 ) settings_frame.pack(fill=tk.X, pady=(0, 20), padx=10, ipady=10) - + # Probability setting prob_frame = tk.Frame(settings_frame, bg='#1a6b4d') prob_frame.pack(fill=tk.X, padx=10, pady=5) - + tk.Label( prob_frame, text="Modifier Probability:", @@ -414,7 +414,7 @@ class PokerModifierApp: fg='white', bg='#1a6b4d' ).pack(side=tk.LEFT) - + self.prob_var = tk.IntVar(value=30) self.prob_scale = tk.Scale( prob_frame, @@ -430,7 +430,7 @@ class PokerModifierApp: activebackground='#ffd700' ) self.prob_scale.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(10, 5)) - + self.prob_label = tk.Label( prob_frame, text="30%", @@ -440,11 +440,11 @@ class PokerModifierApp: width=5 ) self.prob_label.pack(side=tk.RIGHT) - + # Debug controls frame debug_frame = tk.Frame(settings_frame, bg='#1a6b4d') debug_frame.pack(fill=tk.X, padx=10, pady=5) - + # Debug mode toggle self.debug_var = tk.BooleanVar(value=False) debug_check = tk.Checkbutton( @@ -460,7 +460,7 @@ class PokerModifierApp: font=('Arial', 10, 'bold') ) debug_check.pack(side=tk.LEFT, padx=(0, 15)) - + # Force endgame button (only visible in debug mode) self.force_endgame_button = tk.Button( debug_frame, @@ -473,11 +473,11 @@ class PokerModifierApp: bd=2 ) # Initially hidden - + # Game length setting length_frame = tk.Frame(settings_frame, bg='#1a6b4d') length_frame.pack(fill=tk.X, padx=10, pady=5) - + tk.Label( length_frame, text="Total Game Rounds:", @@ -485,7 +485,7 @@ class PokerModifierApp: fg='white', bg='#1a6b4d' ).pack(side=tk.LEFT) - + self.length_var = tk.IntVar(value=20) self.length_scale = tk.Scale( length_frame, @@ -501,7 +501,7 @@ class PokerModifierApp: activebackground='#ffd700' ) self.length_scale.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(10, 5)) - + self.length_label = tk.Label( length_frame, text="20", @@ -511,7 +511,7 @@ class PokerModifierApp: width=5 ) self.length_label.pack(side=tk.RIGHT) - + # Result display frame self.result_frame = tk.Frame( main_frame, @@ -522,7 +522,7 @@ class PokerModifierApp: ) self.result_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 20), padx=10) self.result_frame.pack_propagate(False) - + # Initial result text self.result_label = tk.Label( self.result_frame, @@ -534,11 +534,11 @@ class PokerModifierApp: justify=tk.CENTER ) self.result_label.pack(expand=True, fill=tk.BOTH, padx=20, pady=20) - + # Button frame for Start and Reset button_frame = tk.Frame(main_frame, bg='#0f4c3a') button_frame.pack(fill=tk.X, pady=(0, 20), padx=10) - + # Start button self.start_button = tk.Button( button_frame, @@ -554,7 +554,7 @@ class PokerModifierApp: cursor='hand2' ) self.start_button.pack(side=tk.LEFT, fill=tk.X, expand=True, ipady=10, padx=(0, 5)) - + # Reset button self.reset_button = tk.Button( button_frame, @@ -570,11 +570,11 @@ class PokerModifierApp: cursor='hand2' ) self.reset_button.pack(side=tk.RIGHT, ipady=10, padx=(5, 0)) - + # Statistics frame stats_frame = tk.Frame(main_frame, bg='#0f4c3a') stats_frame.pack(fill=tk.X, padx=10) - + # Rounds played rounds_frame = tk.LabelFrame( stats_frame, @@ -586,7 +586,7 @@ class PokerModifierApp: bd=2 ) rounds_frame.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 3)) - + self.rounds_label = tk.Label( rounds_frame, text="0", @@ -595,7 +595,7 @@ class PokerModifierApp: bg='#1a6b4d' ) self.rounds_label.pack(pady=10) - + # Modifiers applied mods_frame = tk.LabelFrame( stats_frame, @@ -607,7 +607,7 @@ class PokerModifierApp: bd=2 ) mods_frame.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(3, 3)) - + self.mods_label = tk.Label( mods_frame, text="0", @@ -616,7 +616,7 @@ class PokerModifierApp: bg='#1a6b4d' ) self.mods_label.pack(pady=10) - + # Game phase indicator phase_frame = tk.LabelFrame( stats_frame, @@ -628,7 +628,7 @@ class PokerModifierApp: bd=2 ) phase_frame.pack(side=tk.RIGHT, fill=tk.X, expand=True, padx=(3, 0)) - + self.phase_label = tk.Label( phase_frame, text="Early", @@ -637,16 +637,16 @@ class PokerModifierApp: bg='#1a6b4d' ) self.phase_label.pack(pady=10) - + def update_prob_display(self, value): """Update the probability percentage display""" self.prob_label.config(text=f"{value}%") - + def update_length_display(self, value): """Update the game length display""" self.length_label.config(text=str(value)) self.total_game_rounds = int(value) - + def toggle_debug_mode(self): """Toggle debug mode and show/hide debug controls""" self.debug_mode = self.debug_var.get() @@ -657,7 +657,7 @@ class PokerModifierApp: self.force_endgame_button.pack_forget() self.force_endgame = False print("🐛 Debug mode disabled") - + def toggle_force_endgame(self): """Toggle forced endgame mode for testing""" self.force_endgame = not self.force_endgame @@ -667,40 +667,40 @@ class PokerModifierApp: else: self.force_endgame_button.config(text="Force Endgame", bg='#ff6b6b') print("🎯 Normal modifier selection restored") - + def is_endgame(self): """Determine if we're in endgame phase""" if self.debug_mode and self.force_endgame: return True - + endgame_round = int(self.total_game_rounds * self.endgame_threshold) return self.rounds_played >= endgame_round - + def start_round(self): """Start a new poker round and determine if modifier should be applied""" # Button animation effect self.start_button.config(relief=tk.SUNKEN) self.root.after(100, lambda: self.start_button.config(relief=tk.RAISED)) - + # Update round counter self.rounds_played += 1 self.rounds_label.config(text=str(self.rounds_played)) - + # Update game phase indicator self.update_phase_indicator() - + # Get current probability modifier_chance = self.prob_var.get() - + # Determine if modifier should be applied random_value = random.random() * 100 should_apply_modifier = random_value < modifier_chance - + if should_apply_modifier: self.apply_random_modifier() else: self.show_no_modifier() - + def update_phase_indicator(self): """Update the game phase indicator based on current round""" if self.is_endgame(): @@ -711,13 +711,13 @@ class PokerModifierApp: self.phase_label.config(text="Mid", fg='#ffeb3b') else: self.phase_label.config(text="Early", fg='#4CAF50') - + def apply_random_modifier(self): """Apply a random modifier and update display""" # Update modifier counter self.modifiers_applied += 1 self.mods_label.config(text=str(self.modifiers_applied)) - + # Determine which modifier pool to use if self.is_endgame(): modifier_pool = self.endgame_modifiers @@ -727,22 +727,22 @@ class PokerModifierApp: modifier_pool = self.modifiers modifier_type = "🎲" bg_color = '#2d4a2d' # Green for normal - + # Select random modifier from appropriate pool selected_modifier = random.choice(modifier_pool).copy() - + # Special handling for Steel Cards - randomize the rank if selected_modifier['name'] == 'Steel Cards': ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] steel_rank = random.choice(ranks) selected_modifier['description'] = selected_modifier['description'].format(steel_rank=steel_rank) - + # Update result frame styling for modifier self.result_frame.config(bg=bg_color, highlightbackground='#ffd700', highlightthickness=2) - + # Update display with modifier info modifier_text = f"{modifier_type} {selected_modifier['name']}\n\n{selected_modifier['description']}" - + # Add endgame indicator if applicable if self.is_endgame(): rounds_left = self.total_game_rounds - self.rounds_played @@ -750,19 +750,19 @@ class PokerModifierApp: modifier_text += f"\n\n⚠️ Endgame Phase - {rounds_left} rounds left" else: modifier_text += f"\n\n⚠️ FINAL ROUND!" - + self.result_label.config( text=modifier_text, fg='#ffd700', bg=bg_color, font=('Arial', 14, 'bold') ) - + def show_no_modifier(self): """Show no modifier message""" # Update result frame styling for no modifier self.result_frame.config(bg='#2d2d2d', highlightbackground='#666666', highlightthickness=1) - + # Update display self.result_label.config( text="No modifier this round\n\nPlay normally", @@ -770,18 +770,18 @@ class PokerModifierApp: bg='#2d2d2d', font=('Arial', 14) ) - + def reset_game(self): """Reset the game to initial state""" self.rounds_played = 0 self.modifiers_applied = 0 self.force_endgame = False - + # Update displays self.rounds_label.config(text="0") self.mods_label.config(text="0") self.phase_label.config(text="Early", fg='#4CAF50') - + # Reset result frame self.result_frame.config(bg='#2d2d2d', highlightbackground='#666666', highlightthickness=1) self.result_label.config( @@ -790,22 +790,22 @@ class PokerModifierApp: bg='#2d2d2d', font=('Arial', 14) ) - + # Reset force endgame button if visible if self.debug_mode: self.force_endgame_button.config(text="Force Endgame", bg='#ff6b6b') - + print("🔄 Game reset to initial state") - + def add_modifier(self, name, description): """Add a new modifier to the list""" self.modifiers.append({"name": name, "description": description}) - + def get_stats(self): """Get current statistics""" modifier_rate = 0 if self.rounds_played == 0 else (self.modifiers_applied / self.rounds_played) * 100 rounds_remaining = max(0, self.total_game_rounds - self.rounds_played) - + return { "rounds_played": self.rounds_played, "modifiers_applied": self.modifiers_applied, @@ -816,7 +816,7 @@ class PokerModifierApp: "debug_mode": self.debug_mode, "force_endgame": self.force_endgame } - + def run(self): """Start the application""" print("🃏 Texas Hold'em Modifier App started!") diff --git a/poker-modifier-app/script.js b/poker-modifier-app/script.js index c0c598f..30d5bf6 100644 --- a/poker-modifier-app/script.js +++ b/poker-modifier-app/script.js @@ -62,15 +62,15 @@ class PokerModifierApp { description: "Winner gets extra chips from the house!" } ]; - + this.roundsPlayed = 0; this.modifiersApplied = 0; - + this.initializeElements(); this.attachEventListeners(); this.updateChanceDisplay(); } - + initializeElements() { this.startButton = document.getElementById('startRoundBtn'); this.resultDisplay = document.getElementById('resultDisplay'); @@ -79,60 +79,60 @@ class PokerModifierApp { this.roundsCountDisplay = document.getElementById('roundsCount'); this.modifiersCountDisplay = document.getElementById('modifiersCount'); } - + attachEventListeners() { this.startButton.addEventListener('click', () => this.startRound()); this.modifierChanceSlider.addEventListener('input', () => this.updateChanceDisplay()); } - + updateChanceDisplay() { const chance = this.modifierChanceSlider.value; this.chanceValueDisplay.textContent = `${chance}%`; } - + startRound() { // Add button animation this.startButton.style.transform = 'scale(0.95)'; setTimeout(() => { this.startButton.style.transform = ''; }, 150); - + // Update round counter this.roundsPlayed++; this.roundsCountDisplay.textContent = this.roundsPlayed; - + // Get current probability const modifierChance = parseInt(this.modifierChanceSlider.value); - + // Determine if a modifier should be applied const randomValue = Math.random() * 100; const shouldApplyModifier = randomValue < modifierChance; - + if (shouldApplyModifier) { this.applyRandomModifier(); } else { this.showNoModifier(); } - + // Add some visual feedback with animation this.resultDisplay.style.opacity = '0'; this.resultDisplay.style.transform = 'scale(0.8)'; - + setTimeout(() => { this.resultDisplay.style.opacity = '1'; this.resultDisplay.style.transform = 'scale(1)'; }, 200); } - + applyRandomModifier() { // Update modifier counter this.modifiersApplied++; this.modifiersCountDisplay.textContent = this.modifiersApplied; - + // Select random modifier const randomIndex = Math.floor(Math.random() * this.modifiers.length); const selectedModifier = this.modifiers[randomIndex]; - + // Update display this.resultDisplay.className = 'result-display has-modifier'; this.resultDisplay.innerHTML = ` @@ -140,7 +140,7 @@ class PokerModifierApp {
${selectedModifier.description}
`; } - + showNoModifier() { this.resultDisplay.className = 'result-display no-modifier'; this.resultDisplay.innerHTML = ` @@ -148,12 +148,12 @@ class PokerModifierApp {
Play normally
`; } - + // Method to add new modifiers (for future expansion) addModifier(name, description) { this.modifiers.push({ name, description }); } - + // Method to get statistics getStats() { return { @@ -167,7 +167,7 @@ class PokerModifierApp { // Initialize the app when the page loads document.addEventListener('DOMContentLoaded', () => { window.pokerApp = new PokerModifierApp(); - + // Add some console info for developers console.log('🃏 Texas Hold\'em Modifier App loaded!'); console.log('Access the app instance via window.pokerApp'); diff --git a/poker-modifier-app/style.css b/poker-modifier-app/style.css index 0b660b8..ddf1eff 100644 --- a/poker-modifier-app/style.css +++ b/poker-modifier-app/style.css @@ -209,25 +209,25 @@ h1 { .container { padding: 1rem; } - + h1 { font-size: 2rem; } - + .game-area { padding: 1.5rem; } - + .setting { flex-direction: column; align-items: stretch; } - + .setting label { min-width: auto; text-align: center; } - + .stats { flex-direction: column; } diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..93d2f8c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,299 @@ +[project] +name = "testsandmisc" +version = "0.1.0" +description = "Collection of miscellaneous tests and scripts" +requires-python = ">=3.10" + +# ============================================================================ +# RUFF - Extremely fast Python linter and formatter (written in Rust) +# ============================================================================ +[tool.ruff] +line-length = 88 # Black-compatible line length (stricter) +target-version = "py310" +# Include all Python files +include = ["*.py", "**/*.py"] +# Exclude vendored/build directories +exclude = [ + ".git", + ".venv", + "__pycache__", + "build", + "dist", + ".eggs", + "Bash/ffmpeg-build", # Vendored FFmpeg tools +] + +[tool.ruff.lint] +# AGGRESSIVE: Select ALL rules from all categories +select = ["ALL"] +# Minimal ignores - only conflicting rules +ignore = [ + "D203", # 1 blank line required before class docstring (conflicts with D211) + "D213", # Multi-line docstring summary should start at second line (conflicts with D212) + "COM812", # Trailing comma missing (conflicts with formatter) + "ISC001", # Implicit string concatenation (conflicts with formatter) + "ANN101", # Missing type annotation for self (deprecated) + "ANN102", # Missing type annotation for cls (deprecated) +] + +# Allow ALL rules to be auto-fixed +fixable = ["ALL"] +unfixable = [] + +# Per-file ignores for test files +[tool.ruff.lint.per-file-ignores] +"**/tests/**/*.py" = [ + "S101", # Allow assert in tests + "PLR2004", # Allow magic values in tests + "D100", # Allow missing module docstring in tests + "D103", # Allow missing function docstring in tests +] +"**/conftest.py" = [ + "D100", # Allow missing module docstring + "D103", # Allow missing function docstring +] + +[tool.ruff.lint.pydocstyle] +convention = "google" # Use Google docstring convention + +[tool.ruff.lint.isort] +force-single-line = false +force-sort-within-sections = true +known-first-party = ["PYTHON"] + +[tool.ruff.lint.flake8-quotes] +docstring-quotes = "double" +inline-quotes = "double" + +[tool.ruff.lint.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.lint.pylint] +max-args = 5 +max-branches = 12 +max-returns = 6 +max-statements = 50 + +[tool.ruff.lint.mccabe] +max-complexity = 10 + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" +skip-magic-trailing-comma = false +line-ending = "auto" +docstring-code-format = true + +# ============================================================================ +# MYPY - Static type checker (most aggressive settings) +# ============================================================================ +[tool.mypy] +python_version = "3.10" +# Strict mode enables most checks +strict = true +# Additional aggressive settings +warn_return_any = true +warn_unused_configs = true +disallow_untyped_defs = true +disallow_incomplete_defs = true +check_untyped_defs = true +disallow_untyped_decorators = true +no_implicit_optional = true +warn_redundant_casts = true +warn_unused_ignores = true +warn_no_return = true +warn_unreachable = true +# Extra strict settings +disallow_any_unimported = true +disallow_any_explicit = false # Too aggressive for practical use +disallow_any_generics = true +disallow_subclassing_any = true +strict_equality = true +extra_checks = true +# Allow missing imports for third-party packages +ignore_missing_imports = true +# Show error codes +show_error_codes = true +# Enable colored output +color_output = true +# Exclude vendored directories +exclude = [ + "Bash/ffmpeg-build/", + ".venv/", +] + +# ============================================================================ +# PYLINT - Comprehensive Python linter +# ============================================================================ +[tool.pylint.main] +# Analyse import fallback blocks +analyse-fallback-blocks = true +# Pickle collected data for later comparisons +persistent = true +# Jobs to use for parallel execution (0 = auto) +jobs = 0 +# Minimum Python version +py-version = "3.10" +# Ignore vendored directories +ignore = ["Bash", ".venv", "__pycache__"] +# Ignore patterns +ignore-patterns = [".*\\.pyi$"] + +[tool.pylint.messages_control] +# Enable all checks by disabling disable +enable = "all" +# Minimal disabled checks - only truly problematic ones +disable = [ + "raw-checker-failed", + "bad-inline-option", + "locally-disabled", + "file-ignored", + "suppressed-message", + "useless-suppression", + "deprecated-pragma", + "use-symbolic-message-instead", + "too-few-public-methods", # Often false positive for data classes +] + +[tool.pylint.format] +# Maximum line length (same as ruff/black) +max-line-length = 88 +# Maximum module lines +max-module-lines = 1000 + +[tool.pylint.design] +# Maximum arguments for function/method +max-args = 5 +# Maximum local variables +max-locals = 15 +# Maximum return statements +max-returns = 6 +# Maximum branches in function body +max-branches = 12 +# Maximum statements in function body +max-statements = 50 +# Maximum parents for a class +max-parents = 7 +# Maximum attributes for a class +max-attributes = 10 +# Maximum public methods for a class +max-public-methods = 20 +# Minimum public methods for a class +min-public-methods = 1 + +[tool.pylint.similarities] +# Minimum lines for similarity check +min-similarity-lines = 4 +# Ignore comments when computing similarities +ignore-comments = true +# Ignore docstrings when computing similarities +ignore-docstrings = true +# Ignore imports when computing similarities +ignore-imports = true + +[tool.pylint.spelling] +# No spelling dictionary to avoid false positives +spelling-dict = "" + +[tool.pylint.typecheck] +# Ignore missing members for dynamic modules +ignored-modules = ["chess", "pygame", "cv2", "PIL"] + +# ============================================================================ +# BANDIT - Security linter +# ============================================================================ +[tool.bandit] +# Exclude test directories and vendored code +exclude_dirs = ["tests", ".venv", "Bash/ffmpeg-build"] +# Use all tests +tests = [] +# No skipped tests (most aggressive) +skips = [] +# Severity level: all (1=low, 2=medium, 3=high) +severity = 1 +# Confidence level: all +confidence = 1 + +# ============================================================================ +# BLACK - Code formatter (for comparison/fallback) +# ============================================================================ +[tool.black] +line-length = 88 +target-version = ["py310"] +include = '\.pyi?$' +extend-exclude = ''' +/( + \.git + | \.venv + | __pycache__ + | Bash/ffmpeg-build +)/ +''' + +# ============================================================================ +# ISORT - Import sorting (for comparison/fallback, ruff handles this) +# ============================================================================ +[tool.isort] +profile = "black" +line_length = 88 +force_single_line = false +force_sort_within_sections = true +known_first_party = ["PYTHON"] +skip = [".venv", "__pycache__", "Bash/ffmpeg-build"] + +# ============================================================================ +# PYTEST - Testing framework configuration +# ============================================================================ +[tool.pytest.ini_options] +testpaths = ["tests", "PYTHON", "articles"] +python_files = ["test_*.py", "*_test.py"] +python_classes = ["Test*"] +python_functions = ["test_*"] +addopts = [ + "-v", + "--strict-markers", + "--strict-config", + "-ra", +] +filterwarnings = [ + "error", + "ignore::DeprecationWarning", +] + +# ============================================================================ +# COVERAGE - Code coverage configuration +# ============================================================================ +[tool.coverage.run] +source = ["PYTHON", "articles", "poker-modifier-app"] +branch = true +omit = [ + "*/__pycache__/*", + "*/tests/*", + "*/.venv/*", + "Bash/*", +] + +[tool.coverage.report] +# Fail under this percentage +fail_under = 0 +show_missing = true +skip_covered = false +exclude_lines = [ + "pragma: no cover", + "def __repr__", + "raise AssertionError", + "raise NotImplementedError", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] + +# ============================================================================ +# VULTURE - Dead code detection +# ============================================================================ +# Note: Vulture uses command-line args, but we can document settings here +# vulture --min-confidence 80 --exclude ".venv,Bash" . + +# ============================================================================ +# PYDOCSTYLE - Docstring style checker (ruff handles this, but for standalone) +# ============================================================================ +# Configured in ruff.lint.pydocstyle above diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..09d9d85 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,191 @@ +# ============================================================================== +# Python Development Dependencies - Linting, Formatting, and Testing +# ============================================================================== +# Install with: pip install -r requirements-dev.txt +# ============================================================================== + +# Include base requirements +-r requirements.txt + +# add-trailing-comma - Add trailing commas +add-trailing-comma>=3.1.0 + +# autoflake - Remove unused imports and variables +autoflake>=2.2.0 + +# autopep8 - PEP 8 formatting (alternative formatter) +autopep8>=2.0.0 + +# ============================================================================== +# SECURITY LINTERS +# ============================================================================== + +# Bandit - Security linter +bandit>=1.7.0 + +# Black - The uncompromising code formatter (fallback/comparison) +black>=24.0.0 + +# ============================================================================== +# SPELL CHECKING +# ============================================================================== + +# codespell - Fix common misspellings +codespell>=2.2.0 + +# Coverage.py - Code coverage measurement +coverage>=7.4.0 + +# darglint - Check docstrings match function signatures +darglint>=1.8.0 + +# dead - Find dead code +dead>=1.5.0 + +# docformatter - Formats docstrings +docformatter>=1.7.0 + +# fixit - Auto-fix linting errors +fixit>=2.1.0 + +# Flake8 - Linting tool (wraps pyflakes, pycodestyle, mccabe) +flake8>=7.0.0 +flake8-annotations>=3.0.0 # Type annotation checks +flake8-bandit>=4.1.0 # Security checks via bandit + +# Flake8 plugins for maximum coverage +flake8-bugbear>=24.0.0 # Additional bug detection +flake8-comprehensions>=3.14.0 # Better list/dict/set comprehensions +flake8-docstrings>=1.7.0 # Docstring checks +flake8-eradicate>=1.5.0 # Dead code detection +flake8-pie>=0.16.0 # Miscellaneous lints +flake8-print>=5.0.0 # Detect print statements +flake8-pyi>=24.0.0 # Type stub file checks +flake8-pytest-style>=2.0.0 # Pytest style checks +flake8-return>=1.2.0 # Better return statement checks +flake8-simplify>=0.21.0 # Simplification suggestions + +# Hypothesis - Property-based testing +hypothesis>=6.98.0 + +# ============================================================================== +# IMPORT CHECKING +# ============================================================================== + +# importlib-metadata for import analysis +importlib-metadata>=7.0.0 + +# ============================================================================== +# DOCUMENTATION +# ============================================================================== + +# pep257 - PEP 257 docstring checker (legacy, use pydocstyle) +# interrogate - Check docstring coverage +interrogate>=1.5.0 + +# isort - Import sorting (ruff handles this, but useful standalone) +isort>=5.13.0 + +# mccabe - McCabe complexity checker +mccabe>=0.7.0 + +# ============================================================================== +# TYPE CHECKING +# ============================================================================== + +# MyPy - Static type checker +mypy>=1.8.0 + +# pip-audit - Audit Python packages for known vulnerabilities +pip-audit>=2.6.0 + +# pipdeptree - Show dependency tree +pipdeptree>=2.14.0 + +# ============================================================================== +# PRE-COMMIT +# ============================================================================== + +# pre-commit - Git hook management +pre-commit>=3.6.0 + +# prospector - Python static analysis tool +prospector>=1.10.0 + +# pycodestyle - Python style guide checker (PEP 8) +pycodestyle>=2.11.0 + +# pydocstyle - Docstring style checker (PEP 257) +pydocstyle>=6.3.0 + +# pyflakes - Passive checker of Python programs +pyflakes>=3.2.0 + +# pylama - Code audit tool (wraps multiple linters) +pylama>=8.4.0 + +# ============================================================================== +# LINTERS +# ============================================================================== + +# Pylint - Comprehensive Python linter +pylint>=3.0.0 + +# Pyright - Microsoft's type checker (very strict) +pyright>=1.1.350 + +# ============================================================================== +# TESTING +# ============================================================================== + +# pytest - Testing framework +pytest>=8.0.0 + +# pytest plugins +pytest-cov>=4.1.0 # Coverage plugin +pytest-randomly>=3.15.0 # Randomize test order +pytest-sugar>=1.0.0 # Better test output +pytest-timeout>=2.2.0 # Test timeouts +pytest-xdist>=3.5.0 # Parallel test execution + +# ============================================================================== +# ADDITIONAL TOOLS +# ============================================================================== + +# pyupgrade - Upgrade Python syntax +pyupgrade>=3.15.0 + +# Radon - Code metrics (complexity, maintainability) +radon>=6.0.0 + +# reorder-python-imports - Reorder imports +reorder-python-imports>=3.12.0 + +# ============================================================================== +# CODE FORMATTERS +# ============================================================================== + +# Ruff - Extremely fast Python linter and formatter (replaces many tools) +ruff>=0.8.0 + +# Safety - Check dependencies for known security vulnerabilities +safety>=2.3.0 +types-python-dateutil>=2.8.0 +types-PyYAML>=6.0.0 + +# Type stubs for common packages +types-requests>=2.31.0 +types-setuptools>=69.0.0 + +# ============================================================================== +# CODE QUALITY & DEAD CODE DETECTION +# ============================================================================== + +# Vulture - Find dead code +vulture>=2.10 + +# xenon - Monitor code complexity +xenon>=0.9.0 + +# yapf - Yet Another Python Formatter (Google's formatter) +yapf>=0.40.0 diff --git a/requirements.txt b/requirements.txt index 7f01542..237e7a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -python-chess>=1.999 pytest>=7.0 +python-chess>=1.999