Skip to content

refactoring of battleship_oo2 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

refactoring of battleship_oo2 #1

wants to merge 18 commits into from

Conversation

pythonlectures
Copy link
Owner

added comments
some variables are kept private via _ convention
added Cell class

added comments
some variables are kept private via _ convention
added Cell class
self.shooter = self._player1
self.receiver = self._player2
self._shooter = self._player1
self._receiver = self._player2

def play_game(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only method I can see you use from outside Game which means all other Game methods should be private.
I think there are other examples for the same ie. public methods that should be private.

Consider creating all methods as private by default and only make those methods public that you especially want to expose to the outside world.

def __init__(self, coordinates: 'Coordinates', status: str = None):
self._coordinates = coordinates
self._status = status
self._allowed_statuses = {'Hit', 'Not Hit'}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using an enum for constants like 'Hit' and 'Not Hit' to eliminate hardcoding these strings multiple times in the code. With an enum you can define valid statuses and use those same enum values across the board.

return False


class Board:
"""This class keeps track of the shots taken by a Player """
def __init__(self, board_len):

def __init__(self, board_len: int):
self.coordinates = [Coordinates(x=x, y=y) for x in range(1, board_len) for y in range(1, board_len)]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You populate coordinates but don't use it anywhere instead you use the shots_taken set.
Although this works with the current features, you should remove any unused code and I suggest you remove the shots_taken set and keep coordinates storing Cells instead of Coordinates.

If the Board consists of Cells you can register a shot on the board by setting the status of the cell to something like 'Undiscovered' by default and make it 'Empty' or 'Hit' after a shot and it's also easy to verify if a shot is valid on the board or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants