From 799c35bb6138c47c8a473209d36c6c233f31e82c Mon Sep 17 00:00:00 2001 From: danii Date: Tue, 12 Mar 2024 09:51:04 +0100 Subject: [PATCH] Now ShipBattle works --- .gitignore | 162 ------------------ .vscode/settings.json | 14 ++ HNC/Exercises/Ship_Battle/ShipField.py | 8 +- .../{test_ShipField.py => ShipField_test.py} | 13 +- .../__pycache__/ShipField.cpython-311.pyc | Bin 0 -> 8100 bytes ...hipField_test.cpython-311-pytest-8.1.1.pyc | Bin 0 -> 9601 bytes .../__pycache__/ShootResult.cpython-311.pyc | Bin 0 -> 549 bytes HNC/Exercises/Ship_Battle/main.py | 2 +- HNC/Exercises/Ship_Battle/test.py | 10 ++ 9 files changed, 38 insertions(+), 171 deletions(-) delete mode 100644 .gitignore create mode 100644 .vscode/settings.json rename HNC/Exercises/Ship_Battle/{test_ShipField.py => ShipField_test.py} (91%) create mode 100644 HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc create mode 100644 HNC/Exercises/Ship_Battle/__pycache__/ShipField_test.cpython-311-pytest-8.1.1.pyc create mode 100644 HNC/Exercises/Ship_Battle/__pycache__/ShootResult.cpython-311.pyc create mode 100644 HNC/Exercises/Ship_Battle/test.py diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 5d381cc..0000000 --- a/.gitignore +++ /dev/null @@ -1,162 +0,0 @@ -# ---> Python -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# 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/ - diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5732e9d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,14 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./HNC", + "-p", + "*_test.py" + ], + "python.testing.pytestEnabled": true, + "python.testing.unittestEnabled": false, + "python.testing.pytestArgs": [ + "." + ] +} \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipField.py b/HNC/Exercises/Ship_Battle/ShipField.py index ee60a59..a917394 100644 --- a/HNC/Exercises/Ship_Battle/ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField.py @@ -31,9 +31,9 @@ class ShipField: def target(self, row, col): + self.clear_marker() + if self.field_mode == 0: - self.clear_marker() - if self.check_possible(row, col): if self.ship_direction == 0: for r in range(row, row + self.ship_size): @@ -109,14 +109,10 @@ class ShipField: return True def set_ship_size(self, value): - if type(value) is str and value.isnumeric(): - value = int(value) if 1 <= value <= 4: self.ship_size = value def set_ship_direction(self, value): - if type(value) is str and value.isnumeric(): - value = int(value) if 0 <= value <= 1: self.ship_direction = value diff --git a/HNC/Exercises/Ship_Battle/test_ShipField.py b/HNC/Exercises/Ship_Battle/ShipField_test.py similarity index 91% rename from HNC/Exercises/Ship_Battle/test_ShipField.py rename to HNC/Exercises/Ship_Battle/ShipField_test.py index 36ce270..95b0cf4 100644 --- a/HNC/Exercises/Ship_Battle/test_ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField_test.py @@ -115,7 +115,16 @@ class TestShipField(TestCase): self.assertEqual(new_field_string, old_field_string) def test_set_ship(self): - self.fail() + ship_field = ShipField() + ship_field.set_ship_size(4) + ship_field.set_ship_direction(0) + + ship_field.set_ship(6, 3) + + self.assertEqual(ship_field.field[63].strip(), '1') + self.assertEqual(ship_field.field[73].strip(), '1') + self.assertEqual(ship_field.field[83].strip(), '1') + self.assertEqual(ship_field.field[93].strip(), '1') def test_check_possible(self): self.fail() @@ -127,5 +136,5 @@ class TestShipField(TestCase): self.fail() def test_toggle_ship_direction(self): - self.fail() + ship_field = ShipField() diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..aaba9ded41a2fb8002c36c00123e3fc8f453413b GIT binary patch literal 8100 zcmeGhOKcm*b(iE){F4$z{V0i)NK>LEOIGAxQY)@vN|xP5j$=DO4Fv?lrDa+)W$#jr z%TloqX+Z=6$^-$-04~4;sY}B?_^3k;e*_C_0IEg=-i`Nilr0O4eAO-QyS_F z#cDpLSnUH1MLmL_)UMF1?jjXrp5bGVu9`1iTU|}P8{^j!sc0RC-01ql^Cjrqqhb_% zNnO#f^vBc{EzW#J$7%sGtPY@_WdOFZdVmJD4WN-V05q{ifM(VN(88JlT3HJ~8*2q< zXKlcXy=uI8Eq-%89!s!MnJiLArnT^U{tke9)G{pZD>_4k;Yn(&2A-5JSS>s$3uSfi zgkO_J-pShq8--i27-V12Uak!g{ty0F_TU+66Nggr482icT{ZKTV_xmYw6Urj!wt2#cGxXPmhdSA@3Z!FDnsWmQqIhl%aTs(R$wR9~R zJ;X+m@pxisVe$0R-0c__jq@>n2_-)KRwR{5#7Lh=|HRE-RSn^AJQ+`g!|4u1D<tJ^Tux+wPmbnKNrH@Wrp!Od0~^kj9S!CfNPbC`gND!Ks= zxD`ax{tTUaRED|^cd4=T3ehRqg3*4%<=(SjMC46(0=i?tFVpRhlONH@7s|=4eFsS7m zh^OUNKdfq_s|g;dIJw&}gK}WX42M%jg$N1^nDOh#A!%uVY5@vS?{}|*mjNskdD1dq-mjyv zCM^wLXEgZbN%(Dmm^4w<)`jPYimE1q^X*S{U|qM1Dsrl(s%ub1v2FeZb&}v!>6=^^ z@D(&vEzv|Q!i8@{xD_x8D2ZG*0vmuT!$p$IG0utu#4wqm_mCli$o1i<8_{U4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qIMAQ5R>*1X!G!Sho<_mL+yr5zG4|=Y?gW*_TH#c*ns$!G!p?qU z)ft_;+c#E7tPA&U<5Hk)sY z+I9O{{#v66=tEIW|whgW6Kl0%EhAuV}KPabKwxN5YRNyD+oV8xQcKMV8M(lvgj$rm*)&{Ymr0D z!MF#~;@?Ur9v)obEZEzCSqs{%6f0{Z;h`1If?a%g*9vDr#7eQUNHqN33THt}G<+1Z z6@ct<%vJ!hCox-rmPGjd70!abc=$95S%JNE1Fe@$p^){!mWNGCy1^h5v-yIOFUm&a zhcmYYE0+{zZX-tq%>S8y@bdtbZqd9V88NHKh*rzu@GzrPmKtQXm{%}E(Nvi+DduXj zF~y1ef&>W^Dm!zCiQ;-9`Yt% literal 0 HcmV?d00001 diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShootResult.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShootResult.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..74d66eabe956c89d638e752dcdd3325174aa9e39 GIT binary patch literal 549 zcmZut%}T>S5T0#*TC4R35f6$7@zR4nfQV3StVJ8d+JhmmlEQMt( zl_{3SR7+ znxoWfb~vSJp3;ubB3JS`O0S}(JBkP$dyA}uoP-&K7w99xpu%?Tk_aO~8(zqTa9Zuq zZhNh5*7O|5wU5n1+qmYUusk?51L`sB*7*^Jk?}wIX05{ z)wq%rR^p>%aV@S&74wrMv#6vh$KeS%+flKCWWyT^$OgH+(eL)f61sA(!3jc7A%rBb Y^7ec6bd{|4;4j>5eaX}Be?r4%KT*Ac$N&HU literal 0 HcmV?d00001 diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py index b4022e3..63a69b4 100644 --- a/HNC/Exercises/Ship_Battle/main.py +++ b/HNC/Exercises/Ship_Battle/main.py @@ -39,7 +39,7 @@ def colorize(field, buttons): def keypress_handler(e): global active_field if e.keysym.isnumeric(): - active_field.set_ship_size(e.keysym) + active_field.set_ship_size(int(e.keysym)) else: if e.keysym == 'm': active_field.toggle_field_mode() diff --git a/HNC/Exercises/Ship_Battle/test.py b/HNC/Exercises/Ship_Battle/test.py new file mode 100644 index 0000000..71554bf --- /dev/null +++ b/HNC/Exercises/Ship_Battle/test.py @@ -0,0 +1,10 @@ +from ShipField import ShipField + +my_field = ShipField() + +my_field.set_ship_size(1) + +if my_field.ship_size == 1: + print("OK") +else: + print("ERROR") \ No newline at end of file