[Python] how do i resolve a dependancy issue between two functions? [closed]

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    i am currently attempting to build a multi dimensional tictactoe game in pygame, in which each box contains a smaller 3x3 box, and the pattern continues (up to 3 dimensions). in order to store the mark of each box of differing degree ("x", "O" or None) i constructed a dictionary for each dimension, in the form of {(top_left_pos, bottom_right_pos) : mark}. My issue arises in my checking logic.

    the function get_marks_in_box() takes in the clicked cell position, the desired box_degree (1 for the smallest 3x3, 2 for the larger 3x3 made up of 3x3, etc.) and the relevant box dictionary. it then returns a 3x3 nested loop - marks - with the marks retrieved from the box dictionary.

    secondly, the function box_completed() takes in the clicked cell position, the games dimension (1 for a 3x3, 2 for a 9x9, 3 for a 27x27), the desired box degree, the marks from the get_marks_in_box() funxtion, and the relevant box dictionary. with this information it checks whether or not that box is completed (has 3 in a row), and updates the relevant box dictionary so that it can be marked appropriately.

    my issue is that get_marks_in_box() requires the box_dict to be updated which is done in the box_completed function. and my box_completed function requires the marks which is computed in the get_marks_in_box_function. below are the two functions. i am relatively new to coding, so understand there may be a few (potentially a lot of) innefficiencies and innacuracies. how should i refactor my code to improve logic flow? any assistance is greatly appreciated. thankyou in advance.

    def get_marks_in_box(self, coords, box_deg, box_dict):
    N = game dimensions # 1,2, or 3

    box_pos = (tl_pos, br_pos)

    marks = [[None for _ in range(3)] for _ in range(3)]
    for i in range(3):
    for j in range(3):
    # Calculate the coordinates of the current sub-box
    sub_topleft_pos = ((sub_tl_row),(sub_tl_col))
    sub_bottomright_pos = ((sub_br_row),(sub_br_col))

    # Create a key for this sub-box (degree + its coordinates)
    box_key = (sub_tl_pos, sub_br_pos)

    # write mark to marks
    marks[j] = box_dict[box_key]

    return marks


    def box_completed(self, coords, N, box_deg, marks, box_dict):

    # horizontal
    if horizantal 3 in a row:
    box_pos = (tl_pos, br_pos)
    box_dict[box_pos] = mark

    # vertical
    if vertical 3 in a row:
    box_pos = (tl_pos, br_pos)
    box_dict[box_pos] = mark

    # diagonal
    if diagonal 3 in a row:
    box_pos = (tl_pos, br_pos)
    box_dict[box_pos] = mark


    link to full code if needed: https://drive.google.com/file/d/1xxzltkjz3BAfGJRel5SG0Px7JZqxXieC/view?usp=sharing

    Continue reading...

Compartilhe esta Página