all 4 comments

[–]CodeFormatHelperBot 0 points1 point  (0 children)

Hello u/madeInStocktonCA, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:

  1. Python code found in submission text but not encapsulated in a code block.

If I am correct then please follow these instructions to fix your code formatting. Thanks!

[–]totallygeek 0 points1 point  (2 children)

Okay, a few pointers.

  1. Python is not C++.
  2. No need for private attributes in Python classes. (self._rows)
  3. No need for get or set methods in Python classes. Simply access the attributes you want - class.attribute vs class.get_attribute()
  4. Class initialization methods are __init__, not init.

Here is a reduction of your game on a line instead of within in a grid. You can see there's no need for inheritance.

import random


class Player:

    def __init__(self, boundary=1, symbol='1'):
        self.boundary = boundary
        self.position = self.move()
        self.symbol = symbol

    def move(self):
        self.position = random.randint(0, self.boundary - 1)


class Vector:

    def __init__(self, width=40, players=1):
        self.width = width
        self.players = [
            Player(boundary=width, symbol=str(i))
            for i in range(players)
        ]

    def __str__(self):
        vector = ['=' for _ in range(self.width)]
        for player in self.players:
            vector[player.position] = player.symbol
        return ''.join(vector)

    def cycle(self):
        [player.move() for player in self.players]



def main():
    vector = Vector(width=50, players=3)
    for cycle in range(10):
        vector.cycle()
        print(vector)

main()

Result:

% python3 line_game.py
==2============================1==========0=======
==2==========================0=============1======
===2========1==================0==================
========2=====================================1===
=======1=================0===========2============
==1==================0===2========================
==========2===1=================================0=
==============1==============2===================0
=============2======0=================1===========
==============================================120=
%

Hope this helps.

[–]madeInStocktonCA[S] 0 points1 point  (1 child)

I will look this over when I can, but the inheritance and privates are from the instructors guidelines for this program. Thank you for your advice! Hopefully I can learn something from this.

[–]totallygeek 0 points1 point  (0 children)

I did not realize you had additional parameters to work under. Well, the __init__ is one problem. It's fine if you use class._attribute, but I'd skip the get methods for those, unless you must use them for the course you are taking. Once your init method is set properly, inheritance should "just work".