[Python] OOP project: I have a problem with the Player class. Thereby the intervals have to be...

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    It's about Candy Crush, which I wanted to build. In order to do this, I used three classes: Board, Player and main

    In the class Board I create a m*n matrix with yellow, blue and pink fruits, which should represent the field.

    In the main class the board is created by the user. The player has to enter his old/new X, Y coordinates and the fruits should move (I'm not done yet).

    In the Player class, the coordinates which is entered by the player should be checked. "Are the inputted numbers valid?". For this I wrote the method "legal_length"

    Board-Class

    import numpy as np
    from Player import *

    class Board:

    def __init__(self, x=None, y=None):
    if x is None:
    x = int(input("Enter the width of the board: "))
    if y is None:
    y = int(input("Enter the height of the board: "))
    self.x = x
    self.y = y
    self.board_flaeche = np.zeros((x, y), dtype=str)
    fruits_liste = ["Y", "B", "P"]

    for i in range(x):
    for j in range(y):
    self.board_flaeche[j] = np.random.choice(fruits_liste)

    print(self.board_flaeche)
    # do I need it??
    self.player = Player(0, 0, 0, 0)



    def move(self):

    old_x = self.player.getOldX()
    old_y = self.player.getOldY()
    new_x = self.player.getNewX()
    new_y = self.player.getNewY()

    # Update position of the player instance
    # self.player.setPosition(new_x, new_y)


    Player-Class

    # from numpy import np
    import main
    from Board import *



    # from x, y to newX, newY
    class Player:
    def __init__(self, old_x, old_y, new_x, new_y):
    self.old_x = old_x
    self.old_y = old_y
    self.new_x = new_x
    self.new_y = new_y

    def getOldX(self):
    return self.old_x

    def getOldY(self):
    return self.old_y

    def getNewX(self):
    return self.new_x

    def getNewY(self):
    return self.new_y


    # switching positions - not finished yet
    # def move(self):
    # Board.board_flaeche[x][y] = Board.board_flaeche[newX][newY]

    # ToDo:
    # Task: Write a method legal_length where old_x, old_y, new_x, new_y
    # must be checked if they are within the self.board_area[j] range

    def legal_length(self):
    # ToDo Problem:
    # how do I get the attributes from another class in Python without creating an instance each time
    pass

    def setPosition(self, new_x, new_y):
    pass


    Main-Class

    from Board import *
    from Player import *

    if __name__ == "__main__":
    b = Board()

    while True:
    old_x_input = int(input("x: "))
    old_y_input = int(input("y: "))
    new_x_input = int(input("move to newX: "))
    new_y_input = int(input("move to newY: "))
    p = Player(old_x_input - 1, old_y_input - 1, new_x_input - 1, new_y_input - 1)
    print(b.board_flaeche)
    print(b.move())


    can someone help me with the "legal_length" method

    Continue reading...

Compartilhe esta Página