Skip to content

Commit ea99a8e

Browse files
authored
Merge pull request #222 from ikostan/exercism-sync/4ef1694079777879
[Sync Iteration] python/ellens-alien-game/1
2 parents 870a470 + 9708046 commit ea99a8e

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

ellens-alien-game/classes.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class Alien:
55
"""
6-
Create an Alien object with location x_coordinate and y_coordinate.
6+
Alien located at given coordinates.
77
88
Attributes
99
----------
@@ -17,37 +17,74 @@ class Alien:
1717
-------
1818
hit(): Decrement Alien health by one point.
1919
is_alive(): Return a boolean for if Alien is alive (if health is > 0).
20-
teleport(new_x_coordinate, new_y_coordinate): Move Alien object to new coordinates.
20+
teleport(new_x_coordinate, new_y_coordinate): Move Alien object
21+
to new coordinates.
2122
collision_detection(other): Implementation TBD.
2223
"""
2324

2425
total_aliens_created: int = 0
2526

2627
def __init__(self, x_coordinate: int, y_coordinate: int):
28+
"""
29+
Initialize a new Alien at the provided coordinates.
30+
31+
Sets health to 3, assigns x and y coordinates, and increments
32+
Alien.total_aliens_created.
33+
34+
:param x_coordinate: Position on the x-axis.
35+
:param y_coordinate: Position on the y-axis.
36+
:return: None
37+
"""
2738
self.health: int = 3
2839
self.x_coordinate = x_coordinate
2940
self.y_coordinate = y_coordinate
3041
Alien.total_aliens_created += 1
3142

3243
def is_alive(self):
44+
"""
45+
Return whether the alien is alive.
46+
47+
:return: True if health > 0, else False.
48+
"""
3349
return self.health > 0
3450

3551
def hit(self):
52+
"""
53+
Decrement the alien's health by one point.
54+
55+
:return: None
56+
"""
3657
self.health -= 1
3758

3859
def teleport(self, new_x_coordinate, new_y_coordinate):
60+
"""
61+
Decrement the alien's health by one point.
62+
63+
:return: None
64+
"""
3965
self.x_coordinate = new_x_coordinate
4066
self.y_coordinate = new_y_coordinate
4167

4268
def collision_detection(self, other):
43-
pass
69+
"""
70+
Determine whether this Alien collides with another.
71+
72+
Implementation TBD; currently returns None.
73+
74+
:param other: Another Alien or object to check for
75+
positional overlap.
76+
:return: None
77+
78+
`Source: /ellens-alien-game/classes_test.py`
79+
"""
80+
pass # pylint: disable=unnecessary-pass
4481

4582

4683
def new_aliens_collection(
4784
alien_start_positions: list[tuple[int, int]],
4885
) -> list:
4986
"""
50-
Creates a list of Alien() objects, given a list of positions (as tuples).
87+
Create a list of Alien objects from starting positions.
5188
5289
:param alien_start_positions: given a list of positions
5390
:return: a list of Alien() objects
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Solution to Ellen's Alien Game exercise."""
2+
3+
4+
class Alien:
5+
"""
6+
Create an Alien object with location x_coordinate and y_coordinate.
7+
8+
Attributes
9+
----------
10+
(class)
11+
total_aliens_created: int
12+
x_coordinate: int - Position on the x-axis.
13+
y_coordinate: int - Position on the y-axis.
14+
health: int - Number of health points.
15+
16+
Methods
17+
-------
18+
hit(): Decrement Alien health by one point.
19+
is_alive(): Return a boolean for if Alien is alive (if health is > 0).
20+
teleport(new_x_coordinate, new_y_coordinate): Move Alien object to new coordinates.
21+
collision_detection(other): Implementation TBD.
22+
"""
23+
24+
total_aliens_created: int = 0
25+
26+
def __init__(self, x_coordinate: int, y_coordinate: int):
27+
self.health: int = 3
28+
self.x_coordinate = x_coordinate
29+
self.y_coordinate = y_coordinate
30+
Alien.total_aliens_created += 1
31+
32+
def is_alive(self):
33+
return self.health > 0
34+
35+
def hit(self):
36+
self.health -= 1
37+
38+
def teleport(self, new_x_coordinate, new_y_coordinate):
39+
self.x_coordinate = new_x_coordinate
40+
self.y_coordinate = new_y_coordinate
41+
42+
def collision_detection(self, other):
43+
pass
44+
45+
46+
def new_aliens_collection(
47+
alien_start_positions: list[tuple[int, int]],
48+
) -> list:
49+
"""
50+
Creates a list of Alien() objects, given a list of positions (as tuples).
51+
52+
:param alien_start_positions: given a list of positions
53+
:return: a list of Alien() objects
54+
"""
55+
return list(Alien(pos[0], pos[1]) for pos in alien_start_positions)

0 commit comments

Comments
 (0)