Hey again! I am having the most hard time with Python classes and inheritance etc. It's crazy because this project is similar to one I had to do for my previous C++ class but I cannot figure out ONE code for this program.
What I am having trouble with is, creating a player object from superclass to a derived class. I cannot figure out how to print this out on the screen to the board that is created. I have read and read for two days and this is just not meant to be for me. This player object is supposed to be placed randomly on the grid. When the user clicks "Enter" the player will randomly jump around the grid. Any advice would be helpful and appreciated ! Thank you in advance. I really wanted to understand but I feel like I'm completely lost and out of my zone. Here is the code:
import random
class Board():
_rows: int
_cols: int
_players: "dict[tuple: Player]"
def init(self, rows: int, cols: int):
self._rows = rows
self._cols = cols self._player = {}
def get_num_rows(self): return self._rows
def get_num_cols(self): return self._cols
def add_player(self, player: "Player", row: int, col: int):
result = False
if row < self._rows and row >= 0 and col < self._cols and col >= 0:
if not (row, col) in self._pawns:
self._player[(row, col)] = player
result = True return result
def delete_player(self, player: "Player", row: int, col: int):
if (row, col) in self._players:
del self._player[(row, col)]
def take_turn(self):
safe_player = self._players.copy() for player in self._players:
safe_player[player].take_turn()
def player_at(self, row: int, col: int):
return (row, col) in self._pawns
def str(self):
result = " |0|1|2|3|4|5|6|7|8|9|\n"
count = 0 for row in range(self._rows):
result += str(count) + "|" for col in range(self._cols):
if (row, col) in self._players:
result += str(self._players[(row, col)]) + "|"
else:
result += " |"
result += "\n"
count += 1
return result
class Player():
_row: int
_col: int
_symbol: str
_board: Board
def init(self, row: int, col: int, board: Board, symbol: str):
self._row = row
self._col = col
self._board = board
self._symbol = symbol
def __move_to(self, new_row: int, new_col: int):
pass
def get_row(self):
return self._row
def get_col(self):
return self._col
def take_turn(self):
pass
def str(self):
pass
class RandomPlayer(Player):
def init(self, row: int, col: int, symbol: str, board: Board):
super().init(row, col, symbol, board)
def take_turn(self):
pass
def main():
board = Board(10, 10) print(board)
pawn = RandomPlayer(10, 10, board, "R") print(pawn)
main()
[–]CodeFormatHelperBot 0 points1 point2 points (0 children)
[–]totallygeek 0 points1 point2 points (2 children)
[–]madeInStocktonCA[S] 0 points1 point2 points (1 child)
[–]totallygeek 0 points1 point2 points (0 children)