~piotr-machura/python-miniprojects

642a5cc9df523a440454e749057ab111509eb085 — Piotr Machura 1 year, 7 months ago d0b8a1e
Code refactor
12 files changed, 22 insertions(+), 31 deletions(-)

M README.md
D blackjack/__init__.py
R blackjack/{main.py => __main__.py}
M blackjack/deck.py
M blackjack/exception.py
M blackjack/player.py
D collatz/__init__.py
R collatz/{main.py => __main__.py}
M collatz/app.py
D tictactoe/__init__.py
R tictactoe/{main.py => __main__.py}
M tictactoe/game.py
M README.md => README.md +6 -4
@@ 1,10 1,12 @@
# Python miniprojects
This is a collection of small projects I've made to learn some Python in my spare time.

- :heavy_check_mark: __Tic-tac-toe__ - a command line version of a well known minigame.
- ✅ **Tic-tac-toe** - a command line version of a well known minigame.

- :x: __Blackjack__ - a simplified, command line version of a well known card game played against a computer.
- ❌ **Blackjack** - a simplified, command line version of a well known card game played against a computer.

- :heavy_check_mark: __Collatz conjecture checker__ - A Qt5 GUI for checking the [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) on your favourite numbers. Icon made by [DinosoftLabs](https://www.flaticon.com/authors/dinosoftlabs) from [Flaticon](https://www.flaticon.com/). Code refactoring still in the works.
- ✅ **Collatz conjecture checker** - A Qt5 GUI for checking the [Collatz
  conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) on your favorite numbers. Icon made by
  [DinosoftLabs](https://www.flaticon.com/authors/dinosoftlabs) from [Flaticon](https://www.flaticon.com/).

Projects marked with :heavy_check_mark: are considered finished, while those with :x: are a work in progress.
Projects marked with ✅ are considered finished, while those with ❌ are a work in progress.

D blackjack/__init__.py => blackjack/__init__.py +0 -0
R blackjack/main.py => blackjack/__main__.py +2 -1
@@ 26,7 26,8 @@ def bet_or_exit():
    if choice == '0':
        raise FoldException()
    elif choice == 'q':
        raise ExitException()
        print('Goodbye!')
        sys.exit(0)
    else:
        PLAYER.bet(int(choice))


M blackjack/deck.py => blackjack/deck.py +0 -1
@@ 2,7 2,6 @@
"""
from random import randrange


class Card:
    """The Card class. Contains fields `symbol` (string) and a `value` (tuple).
    The static `TYPES` dictonary contains pairs of all of the valid symbols and

M blackjack/exception.py => blackjack/exception.py +1 -3
@@ 1,6 1,4 @@
"""This module contains the exceptions used to manage game flow"""
from player import Player


class BlackJackException(Exception):
    """The base BlackJackException used to manage game flow."""


@@ 21,6 19,6 @@ class FoldException(BlackJackException):
class WinException(BlackJackException):
    """Used to signify winning."""

    def __init__(self, player: Player):
    def __init__(self, player):
        super().__init__()
        self.winning_player = player

M blackjack/player.py => blackjack/player.py +1 -1
@@ 56,7 56,7 @@ class Player:
        except ValueError:
            # TODO: Handle error when the deck is empty
            pass
        if self.calculate_points() > 21:
        if self.calculate_points()[0] > 21:
            raise BustedException



D collatz/__init__.py => collatz/__init__.py +0 -4
@@ 1,4 0,0 @@
""" This is a samll GUI for checking the Collatz conjecture on your
favourite natural numbers and seeing the steps performed to reach 1.
It utilizes the Qt5 framework.
"""

R collatz/main.py => collatz/__main__.py +0 -0
M collatz/app.py => collatz/app.py +8 -10
@@ 46,7 46,7 @@ class CollatzApp(QtWidgets.QMainWindow):
        self.steps_display.setFont(font_)
        # Set tab width to 4 spaces
        self.steps_display.setTabStopDistance(
            QtGui.QFontMetricsF(font_).horizontalAdvance(' ') * 4)
            QtGui.QFontMetricsF(font_).horizontalAdvance(' ') * 8)
        self.steps_display.setLineWrapMode(QtWidgets.QPlainTextEdit.NoWrap)
        self.steps_display.setReadOnly(True)



@@ 82,20 82,18 @@ class CollatzApp(QtWidgets.QMainWindow):
        """Perform a recursive check with the Collatz sequence
        and print each step to `stepsDisplay`.

        Raises `Exception` and terminates if the `number` is 1.
        Terminates if the `number` is 1.
        """

        self.steps_display.insertPlainText(str(number))
        if number == 1:
            self.steps_display.insertPlainText('\n')
        else:
            self.counter += 1
        while number != 1:
            self.steps_display.insertPlainText(str(number))
            if number % 2 == 0:
                self.steps_display.insertPlainText('\t| /2\n')
                self._sequence(int(number / 2))
                number = int(number / 2)
            else:
                self.steps_display.insertPlainText('\t| *3 + 1\n')
                self._sequence(int(3 * number + 1))
                number = 3 * number + 1
            self.counter += 1


    @QtCore.pyqtSlot()
    def _on_click_start(self):

D tictactoe/__init__.py => tictactoe/__init__.py +0 -4
@@ 1,4 0,0 @@
"""This is a small tic-tac-toe game played in command line.
Players choose positions by inputing numbers from the board.
Normal tic-tac-toe rules for winning/drawing apply.
"""

R tictactoe/main.py => tictactoe/__main__.py +0 -0
M tictactoe/game.py => tictactoe/game.py +4 -3
@@ 5,7 5,6 @@ import sys
# Global variable board is a 0-9 list, which eventually get replaced with x/o
BOARD = list(range(9))


def print_board():
    """Clear the terminal screen and print the board."""



@@ 25,7 24,7 @@ def input_position(player):

    while True:
        try:
            pos_string = input(f'{player}: ')
            pos_string = input(f'{player}\'s turn: ')
            if pos_string == 'q':
                sys.exit()
            position = int(pos_string)


@@ 35,12 34,14 @@ def input_position(player):
            raise ValueError
        except ValueError:
            print('Invalid position, try again or "q" to quit.')
        except KeyboardInterrupt:
            print()
            sys.exit(0)


def check_win():
    """Check if there is a winner (or draw) and end the game if there is."""

    global BOARD
    h_index = 0
    v_index = 0
    # Check horizontal lines with h_index and vertical lines with v_index