From 642a5cc9df523a440454e749057ab111509eb085 Mon Sep 17 00:00:00 2001 From: Piotr Machura Date: Fri, 6 Aug 2021 15:43:15 +0200 Subject: [PATCH] Code refactor --- README.md | 10 ++++++---- blackjack/__init__.py | 0 blackjack/{main.py => __main__.py} | 3 ++- blackjack/deck.py | 1 - blackjack/exception.py | 4 +--- blackjack/player.py | 2 +- collatz/__init__.py | 4 ---- collatz/{main.py => __main__.py} | 0 collatz/app.py | 18 ++++++++---------- tictactoe/__init__.py | 4 ---- tictactoe/{main.py => __main__.py} | 0 tictactoe/game.py | 7 ++++--- 12 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 blackjack/__init__.py rename blackjack/{main.py => __main__.py} (97%) delete mode 100644 collatz/__init__.py rename collatz/{main.py => __main__.py} (100%) delete mode 100644 tictactoe/__init__.py rename tictactoe/{main.py => __main__.py} (100%) diff --git a/README.md b/README.md index 31ded2a..9456c4e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/blackjack/__init__.py b/blackjack/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/blackjack/main.py b/blackjack/__main__.py similarity index 97% rename from blackjack/main.py rename to blackjack/__main__.py index 123492b..7e6d4c1 100644 --- a/blackjack/main.py +++ b/blackjack/__main__.py @@ -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)) diff --git a/blackjack/deck.py b/blackjack/deck.py index ef7f61d..2f06453 100644 --- a/blackjack/deck.py +++ b/blackjack/deck.py @@ -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 diff --git a/blackjack/exception.py b/blackjack/exception.py index f90b545..3a22757 100644 --- a/blackjack/exception.py +++ b/blackjack/exception.py @@ -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 diff --git a/blackjack/player.py b/blackjack/player.py index 9ba5bcc..7a707c7 100644 --- a/blackjack/player.py +++ b/blackjack/player.py @@ -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 diff --git a/collatz/__init__.py b/collatz/__init__.py deleted file mode 100644 index c3493b4..0000000 --- a/collatz/__init__.py +++ /dev/null @@ -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. -""" diff --git a/collatz/main.py b/collatz/__main__.py similarity index 100% rename from collatz/main.py rename to collatz/__main__.py diff --git a/collatz/app.py b/collatz/app.py index a61803e..91c1de1 100644 --- a/collatz/app.py +++ b/collatz/app.py @@ -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): diff --git a/tictactoe/__init__.py b/tictactoe/__init__.py deleted file mode 100644 index 311de93..0000000 --- a/tictactoe/__init__.py +++ /dev/null @@ -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. -""" diff --git a/tictactoe/main.py b/tictactoe/__main__.py similarity index 100% rename from tictactoe/main.py rename to tictactoe/__main__.py diff --git a/tictactoe/game.py b/tictactoe/game.py index 63df7ff..be26fc1 100644 --- a/tictactoe/game.py +++ b/tictactoe/game.py @@ -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 -- 2.38.5