Skip to content

Commit 1e0c253

Browse files
author
Peter Götz
committed
Initial commit
0 parents  commit 1e0c253

14 files changed

+443
-0
lines changed

.coverage

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = crlf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 4
10+

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.DS_Store
2+
3+
# Virtual Environment
4+
venv/
5+
6+
# Python
7+
__pycache__/
8+
*.pyc
9+
10+
# Python Setuptools
11+
out/
12+
dist/
13+
build/
14+
15+
# JetBrains
16+
.idea/
17+
*.iml
18+
*.iws
19+
*.ipr
20+
21+
# Visual Studio (code)
22+
.vs*
23+
/reports

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.6
2+
LABEL author="Peter Götz ([email protected])"
3+
4+
WORKDIR /torpydo
5+
6+
COPY requirements.txt /torpydo
7+
8+
RUN pip install --upgrade pip
9+
RUN pip install -r requirements.txt

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Torpydo
2+
3+
A simple game of Battleship, written in Python.
4+
5+
# Getting started
6+
7+
This project requires Python 3.6. To prepare to work with it, pick one of these
8+
options:
9+
10+
## Linux and macOS
11+
12+
Create and activate a [virtual environment][venv]. This assumes python3 is
13+
already installed.
14+
15+
```bash
16+
python3 -m venv venv
17+
source venv/bin/activate
18+
pip install -r requirements.txt
19+
export PYTHONPATH=.
20+
```
21+
22+
If you stop working on this project, close your shell, and return later, make
23+
sure you run the `source bin/venv/activate` command again.
24+
25+
[venv]:https://docs.python.org/3/library/venv.html
26+
27+
## Windows
28+
29+
[Download][pywin] and install Python 3.6 for Windows. Make sure python is on
30+
your `PATH`. Open a command prompt:
31+
32+
```commandline
33+
python -m venv venv
34+
venv\Scripts\activate.bat
35+
pip install -r requirements.txt
36+
set PYTHONPATH=.
37+
```
38+
39+
[pywin]:https://www.python.org/downloads/windows/
40+
41+
## Docker
42+
43+
If you don't want to install anything Python-related on your system, you can
44+
run the game inside Docker instead.
45+
46+
### Build the Docker Image
47+
48+
```bash
49+
docker build -t torpydo .
50+
```
51+
52+
### Run a Docker Container from the Image
53+
54+
```bash
55+
docker run -it --env PYTHONPATH=/torpydo -v ${PWD}:/torpydo torpydo bash
56+
```
57+
58+
# Launching the game
59+
60+
```bash
61+
python -m torpydo
62+
63+
# alternatively:
64+
python torpydo/battleship.py
65+
```
66+
67+
# Running the Tests
68+
69+
```
70+
nosetests --exe
71+
behave
72+
```
73+
74+
to run with coverage:
75+
```
76+
nosetests --exe --with-coverage
77+
```
78+
79+
Run behave tests with coverage:
80+
```
81+
https://stackoverflow.com/a/37447392/736079
82+
```

features/battleship.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Feature: IsShipValid
2+
In order to avoid cheating
3+
As a player
4+
I want to be notified if my ship has a invalid placement
5+
6+
Scenario: Valid ship placement
7+
Given I have a 5 ship with 5 positions
8+
When I check if the ship is valid
9+
Then the result should be True
10+
11+
Scenario: Invalid ship placement
12+
Given I have a 5 ship with 4 positions
13+
When I check if the ship is valid
14+
Then the result should be False

features/steps/battleship_steps.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from behave import given, when, then
2+
from torpydo.ship import Color, Letter, Ship
3+
4+
@given('I have a {ship_length:d} ship with {positions:d} positions')
5+
def step_impl(context, ship_length, positions):
6+
context.ship = Ship('Test', ship_length, Color.RED)
7+
8+
for i in range(positions):
9+
context.ship.add_position(f"A{i+1}")
10+
11+
@when('I check if the ship is valid')
12+
def step_impl(context):
13+
context.success = str(len(context.ship.positions) == context.ship.size)
14+
15+
@then('the result should be {result}')
16+
def step_impl(context, result):
17+
assert context.success == result

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
behave==1.2.5
2+
nose==1.3.7
3+
pylint==1.7.4
4+
coverage==4.4.2
5+
colorama==0.3.9

tests/test_battleship.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
3+
from torpydo.battleship import parse_position
4+
5+
class TestBattleship(unittest.TestCase):
6+
def test_parse_position_true(self):
7+
self.assertTrue(parse_position("A1"))
8+
9+
if '__main__' == __name__:
10+
unittest.main()

tests/test_game_controller.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
from torpydo.game_controller import GameController
4+
from torpydo.ship import Color, Letter, Position, Ship
5+
6+
class TestShip(unittest.TestCase):
7+
def setUp(self):
8+
self.ships = []
9+
self.ships.append(init_ship(Ship("Test", 2, Color.RED), [Position(Letter.A, 1), Position(Letter.A, 2)]))
10+
11+
def test_check_is_hit_true(self):
12+
self.assertTrue(GameController.check_is_hit(self.ships, Position(Letter.A, 1)))
13+
14+
def test_check_is_hit_false(self):
15+
self.assertFalse(GameController.check_is_hit(self.ships, Position(Letter.B, 1)))
16+
17+
def test_check_is_hit_position_none(self):
18+
with self.assertRaises(ValueError):
19+
self.assertRaises(GameController.check_is_hit(self.ships, None))
20+
21+
def test_check_is_hit_ship_none(self):
22+
with self.assertRaises(ValueError):
23+
self.assertRaises(GameController.check_is_hit(None, Position(Letter.A, 1)))
24+
25+
def test_is_ship_valid_false(self):
26+
self.assertFalse(GameController.is_ship_valid(Ship("Not Valid", 3, Color.RED)))
27+
28+
def test_is_ship_valid_true(self):
29+
self.assertTrue(GameController.is_ship_valid(self.ships[0]))
30+
31+
def init_ship(ship: Ship, positions: list):
32+
ship.positions = positions
33+
34+
return ship
35+
36+
if '__main__' == __name__:
37+
unittest.main()

0 commit comments

Comments
 (0)