Minigame domino eror

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 727
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Minigame domino eror

#1 Post by Andredron »

Hi everyone, I need your help. I'm doing code minigame dominoes, before I wrote the code, the screen displayed what dominoes at the player and the computer, worked without problems, tried to add conditions, consider the rules of the real game, like too the screen started. I wrote more improvements and all the game hung.

Code: Select all

init python:  # Initializes Python code block
    import random  # Imports the random module for shuffling and making random choices

    class Domino: # Defines a class named Domino to represent individual domino tiles
        def __init__(self, value1, value2): # Constructor method to initialize Domino objects
            self.value1 = value1 # Sets the first value of the domino
            self.value2 = value2 # Sets the second value of the domino

        def __str__(self):  # Defines a method to return a string representation of the Domino object
            return f"[{self.value1}|{self.value2}]" # Returns a string with the domino values enclosed in square brackets

    # Creates a list of all possible dominoes (28 in total) using the Domino class constructor
    dominoes = [Domino(0, 0), Domino(0, 1), Domino(0, 2), Domino(0, 3), Domino(0, 4), Domino(0, 5), Domino(0, 6), Domino(1, 1), Domino(1, 2), Domino(1, 3), Domino(1, 4), Domino(1, 5), Domino(1, 6), Domino(2, 2), Domino(2, 3), Domino(2, 4), Domino(2, 5), Domino(2, 6), Domino(3, 3), Domino(3, 4), Domino(3, 5), Domino(3, 6), Domino(4, 4), Domino(4, 5), Domino(4, 6), Domino(5, 5), Domino(5, 6), Domino(6, 6)]

    # Initializes the game state and variables
    def init_game():
        global player_dominoes, computer_dominoes, boneyard, current_player, board, player_score, computer_score
        player_dominoes = [] # Initializes an empty list to store the player's dominoes
        computer_dominoes = [] # Initializes an empty list to store the computer's dominoes
        boneyard = list(dominoes) # Creates a copy of the dominoes list to serve as the boneyard
        random.shuffle(boneyard) # Shuffles the boneyard randomly
        for _ in range(7): # Deals 7 dominoes to the player and the computer from the boneyard
            player_dominoes.append(boneyard.pop()) # Removes a domino from the boneyard and adds it to the player's hand
            computer_dominoes.append(boneyard.pop())  # Removes a domino from the boneyard and adds it to the computer's hand
        current_player = "player"  # Sets the current player to the player
        board = [] # Initializes an empty list to represent the game board
        setup_board(board, boneyard) # Sets up the initial state of the board
        player_score = 0 # Initializes the player's score to 0
        computer_score = 0 # Initializes the computer's score to 0

    def setup_board(board, boneyard): # Sets up the initial state of the game board
        doubles = [domino for domino in boneyard if domino.value1 == domino.value2] # Finds all double dominoes in the boneyard
        if doubles: # If there are double dominoes available
            board.append(doubles.pop()) # Adds a double domino to the board
            boneyard.remove(board[0]) # Removes the added domino from the boneyard
        else: # If there are no double dominoes available
            board.append(boneyard.pop()) # Adds a random domino to the board

    def make_move(domino): # Validates and executes the player's move
        global player_dominoes, computer_dominoes, current_player, board, player_score, computer_score
        if valid_move(domino, board): # Checks if the chosen domino can be played legally
            player_dominoes.remove(domino) # Removes the chosen domino from the player's hand
            add_to_board(domino, board) # Adds the chosen domino to the board
            if not player_dominoes: # If the player has no more dominoes left
                player_score += sum(d.value1 + d.value2 for d in computer_dominoes) # Calculates the player's score
                show_result("player_wins", player_score, computer_score) # Displays the result
                return # Exits the function
            current_player = "computer" # Switches the current player to the computer
            computer_move() # Executes the computer's move
        else: # If the chosen move is invalid
            renpy.notify("Invalid move. Please choose a domino that matches the ends of the board.") # Notifies the player

    def computer_move(): # Executes the computer's move
        global player_dominoes, computer_dominoes, current_player, board, player_score, computer_score
        valid_moves = [domino for domino in computer_dominoes if valid_move(domino, board)] # Finds all valid moves for the computer
        if valid_moves: # If there are valid moves available
            move = random.choice(valid_moves) # Chooses a random valid move
            computer_dominoes.remove(move) # Removes the chosen domino from the computer's hand
            add_to_board(move, board) # Adds the chosen domino to the board
            if not computer_dominoes: # If the computer has no more dominoes left
                computer_score += sum(d.value1 + d.value2 for d in player_dominoes) # Calculates the computer's score
                show_result("computer_wins", player_score, computer_score) # Displays the result
                return # Exits the function
            current_player = "player" # Switches the current player to the player
        else: # If there are no valid moves for the computer
            current_player = "player" # Switches the current player to the player

    def valid_move(domino, board): # Checks if a move is valid
        if not board: # If the board is empty
            return True # Any domino can be played
        return domino.value1 in [board[0].value1, board[-1].value2] or domino.value2 in [board[0].value1, board[-1].value2] # Checks if the chosen domino matches either end of the board

    def add_to_board(domino, board): # Adds a domino to the board
        if not board: # If the board is empty
            board.append(domino) # Adds the domino to the board
        elif domino.value1 == board[0].value1: # If the domino matches the start of the board
            board.insert(0, domino) # Inserts the domino at the start of the board
        elif domino.value1 == board[-1].value2: # If the domino matches the end of the board
            board.append(domino) # Appends the domino to the end of the board
        elif domino.value2 == board[0].value1: # If the flipped domino matches the start of the board
            board.insert(0, Domino(domino.value2, domino.value1)) # Inserts the flipped domino at the start of the board
        else: # If the flipped domino matches the end of the board
            board.append(Domino(domino.value2, domino.value1)) # Appends the flipped domino to the end of the board

    def show_result(winner, player_score, computer_score):  # Displays the game result
        renpy.hide_screen("domino_game") # Hides the domino game screen
        if winner == "player_wins": # If the player wins
            renpy.notify(f"You won! Your score: {player_score}, Computer score: {computer_score}") # Notifies the player
        else: # If the computer wins
            renpy.notify(f"Computer won! Your score: {player_score}, Computer score: {computer_score}")  # Notifies the player
        renpy.call_in_new_context("restart_game") # Calls the restart_game label in a new context
# Defines the domino_game screen
screen domino_game:
    vbox:  # Arranges children vertically
        text "Player Score: [player_score]"  # Displays the player's score
        text "Computer Score: [computer_score]"  # Displays the computer's score
        null height 20  # Adds a space for layout
        hbox:  # Arranges children horizontally
            for domino in board:  # Iterates over each domino on the board
                text str(domino)  # Displays the string representation of the domino
        null height 20  # Adds a space for layout
        text "Your Dominoes:"  # Displays a label for the player's dominoes
        hbox:  # Arranges children horizontally
            for domino in player_dominoes:  # Iterates over each domino in the player's hand
                textbutton str(domino) action make_move(domino)  # Displays the domino as a button to make a move
        null height 20  # Adds a space for layout
        text "Computer's Dominoes:"  # Displays a label for the computer's dominoes
        hbox:  # Arranges children horizontally
            for domino in computer_dominoes:  # Iterates over each domino in the computer's hand
                text str(domino)  # Displays the string representation of the domino
        null height 20  # Adds a space for layout
        textbutton "Restart" action init_game()  # Displays a button to restart the game and calls the init_game function

label start:
    scene bg room
    "Welcome to the Domino Game!"
    $ init_game()
    $ renpy.show_screen("domino_game")
    "test"

label restart_game:
    "pause"
And here's the error code:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 117, in script
    pause 1.0
  File "renpy/common/000statements.rpy", line 460, in execute_pause
    renpy.pause(delay)
Exception: renpy.restart_interaction() was called 100 times without processing any input.

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 117, in script
    pause 1.0
  File "C:\renpy-8.0.0-sdk\renpy\ast.py", line 1968, in execute
    self.call("execute")
  File "C:\renpy-8.0.0-sdk\renpy\ast.py", line 1950, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "C:\renpy-8.0.0-sdk\renpy\statements.py", line 349, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 460, in execute_pause
    renpy.pause(delay)
  File "C:\renpy-8.0.0-sdk\renpy\exports.py", line 1661, in pause
    rv = renpy.ui.interact(mouse='pause', type='pause', roll_forward=roll_forward, pause=delay, pause_modal=modal)
  File "C:\renpy-8.0.0-sdk\renpy\ui.py", line 301, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\renpy-8.0.0-sdk\renpy\display\core.py", line 2163, in interact
    raise Exception("renpy.restart_interaction() was called 100 times without processing any input.")
Exception: renpy.restart_interaction() was called 100 times without processing any input.

Windows-10-10.0.19045 AMD64
Ren'Py 8.2.0.24012702
domino_test 1.0
Mon Apr 29 22:51:10 2024
The error “Exception: renpy.restart_interaction() was called 100 times without processing any input” indicates that your game loop may hang when renpy.restart_interaction()is called without waiting for player input. And I don't understand how to implement a proper step-by-step loop so that renpy doesn't swear. or how to remove renpy.restart_interaction() since this function is used to refresh the screen and wait for player input.

please help me

User avatar
Andredron
Miko-Class Veteran
Posts: 727
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Minigame domino eror

#2 Post by Andredron »

Code: Select all

init python:
    import random

    class Domino:
        def __init__(self, value1, value2):
            self.value1 = value1
            self.value2 = value2

        def __str__(self):
            return f"[{self.value1}|{self.value2}]"

    dominoes = [Domino(0, 0), Domino(0, 1), Domino(0, 2), Domino(0, 3), Domino(0, 4), Domino(0, 5), Domino(0, 6), Domino(1, 1), Domino(1, 2), Domino(1, 3), Domino(1, 4), Domino(1, 5), Domino(1, 6), Domino(2, 2), Domino(2, 3), Domino(2, 4), Domino(2, 5), Domino(2, 6), Domino(3, 3), Domino(3, 4), Domino(3, 5), Domino(3, 6), Domino(4, 4), Domino(4, 5), Domino(4, 6), Domino(5, 5), Domino(5, 6), Domino(6, 6)]

    def init_game():
        global player_dominoes, computer_dominoes, boneyard, current_player, board, player_score, computer_score
        player_dominoes = []
        computer_dominoes = []
        boneyard = list(dominoes)
        random.shuffle(boneyard)
        for _ in range(7):
            player_dominoes.append(boneyard.pop())
            computer_dominoes.append(boneyard.pop())
        current_player = "player"
        board = []
        setup_board(board, boneyard)
        player_score = 0
        computer_score = 0

    def setup_board(board, boneyard):
        doubles = [domino for domino in boneyard if domino.value1 == domino.value2]
        if doubles:
            board.append(doubles.pop())
            boneyard.remove(board[0])
        else:
            board.append(boneyard.pop())

    def make_move(domino):
        global player_dominoes, computer_dominoes, current_player, board, player_score, computer_score
        if valid_move(domino, board):
            player_dominoes.remove(domino)
            add_to_board(domino, board)
            if not player_dominoes:
                player_score += sum(d.value1 + d.value2 for d in computer_dominoes)
                show_result("player_wins", player_score, computer_score)
                return
            current_player = "computer"
            computer_move()
        else:
            renpy.notify("Invalid move. Please choose a domino that matches the ends of the board.")

    MakeMove = renpy.curry(make_move)

    def computer_move():
        global player_dominoes, computer_dominoes, current_player, board, player_score, computer_score
        valid_moves = [domino for domino in computer_dominoes if valid_move(domino, board)]
        if valid_moves:
            move = random.choice(valid_moves)
            computer_dominoes.remove(move)
            add_to_board(move, board)
            if not computer_dominoes:
                computer_score += sum(d.value1 + d.value2 for d in player_dominoes)
                show_result("computer_wins", player_score, computer_score)
                return
            current_player = "player"
        else:
            current_player = "player"

    def valid_move(domino, board):
        if not board:
            return True
        return domino.value1 in [board[0].value1, board[-1].value2] or domino.value2 in [board[0].value1, board[-1].value2]

    def add_to_board(domino, board):
        if not board:
            board.append(domino)
        elif domino.value1 == board[0].value1:
            board.insert(0, domino)
        elif domino.value1 == board[-1].value2:
            board.append(domino)
        elif domino.value2 == board[0].value1:
            board.insert(0, Domino(domino.value2, domino.value1))
        else:
            board.append(Domino(domino.value2, domino.value1))

    def show_result(winner, player_score, computer_score):
        renpy.hide_screen("domino_game")
        if winner == "player_wins":
            renpy.notify(f"You won! Your score: {player_score}, Computer score: {computer_score}")
        else:
            renpy.notify(f"Computer won! Your score: {player_score}, Computer score: {computer_score}")
        renpy.call_in_new_context("restart_game")

screen domino_game:
    vbox:
        text "Player Score: [player_score]"
        text "Computer Score: [computer_score]"
        null height 20
        hbox:
            for domino in board:
                text str(domino)
        null height 20
        text "Your Dominoes:"
        hbox:
            for domino in player_dominoes:
                textbutton str(domino) action MakeMove(domino)
        null height 20
        text "Computer's Dominoes:"
        hbox:
            for domino in computer_dominoes:
                text str(domino)
        null height 20
        textbutton "Restart" action init_game()

label start:
    scene bg room
    "Welcome to the Domino Game!"
    $ init_game()
    $ renpy.show_screen("domino_game")
    "test"

label restart_game:
    "pause"
We were advised to make a change

The make_move function is not bound to the button action directly. Instead, a new MakeMove function is created using renpy.curry(make_move).
In the domino_game screen, buttons for player dominoes use MakeMove(domino) as the action instead of make_move(domino)
You can't bind functions to buttons and other actions, you need to bring them to the appropriate form:

def some_func(message):
__renpy.notify(message)
SomeFunc = renpy.curry(some_func)

screen test:
__textbutton "test" action SomeFunc("OK")

User avatar
Andredron
Miko-Class Veteran
Posts: 727
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Minigame domino eror

#3 Post by Andredron »


Code: Select all

init python:
    import random

    ###This code defines a class Domino, representing a single domino tile. It has an initializer (__init__) that sets the values of value1 and value2 for a domino tile. The __str__ method returns a string representation of the domino tile. The valid_move method checks if the domino tile can be placed on the board by checking if its values match the ends of the board.
    class Domino: 
        def __init__(self, value1, value2):
            self.value1 = value1
            self.value2 = value2

        def __str__(self):
            return f"({self.value1}, {self.value2})"

        def valid_move(self, board):
            if not board:
                return True
            return self.value1 in [board[0].value1, board[-1].value2] or self.value2 in [board[0].value1, board[-1].value2]

### This function creates a set of domino tiles by generating all possible combinations of values from 0 to 6 for value1 and value2 in a domino tile.
    def create_domino_set():
        return [Domino(i, j) for i in range(7) for j in range(i, 7)]
###This function initializes the game by setting up various global variables. It creates a set of domino tiles, shuffles them randomly, and assigns dominoes to the player, computer, and the boneyard (remaining dominoes). The board is initialized with the highest double value from the player's and computer's dominoes.
    def init_game():
        global player_dominoes, computer_dominoes, boneyard, current_player, board, player_score, computer_score
        dominoes = create_domino_set()
        random.shuffle(dominoes)
        player_dominoes = dominoes[:7]
        computer_dominoes = dominoes[7:14]
        boneyard = dominoes[14:]

        # Initialize board with the highest double
        start_double_max = max([domino for domino in player_dominoes + computer_dominoes if domino.value1 == domino.value2], default=None, key=lambda x: x.value1)
        if start_double_max:
            board = [start_double_max]
            (player_dominoes if start_double_max in player_dominoes else computer_dominoes).remove(start_double_max)
        else:
            board = [boneyard.pop()]

        current_player = "player"
        player_score = 0
        computer_score = 0
###These functions handle various game logic, such as making moves, determining valid moves left, calculating scores, and determining the winner based on the game state.
    def make_move(domino):
        global player_dominoes, computer_dominoes, current_player, board, player_score, computer_score
        if current_player == "player" and domino in player_dominoes and domino.valid_move(board):
            player_dominoes.remove(domino)
            add_to_board(domino, board)
            current_player = "computer"
            renpy.notify("You have chosen this domino. Your move is confirmed.")
            computer_move()
        elif current_player == "computer" and domino in computer_dominoes and domino.valid_move(board):
            computer_dominoes.remove(domino)
            add_to_board(domino, board)
            current_player = "player"
            renpy.notify("Computer has moved.")
        else:
            if current_player == "player" and boneyard:
                player_dominoes.append(boneyard.pop())
                renpy.notify("You must draw a domino from the boneyard.")
            elif current_player == "computer" and boneyard:
                computer_dominoes.append(boneyard.pop())
                renpy.notify("Computer must draw a domino from the boneyard.")
            else:
                renpy.notify("Invalid move. Please choose a domino that matches the ends of the board.")

    def computer_move():
        global player_dominoes, computer_dominoes, current_player, board
        valid_moves = [domino for domino in computer_dominoes if domino.valid_move(board)]
        if valid_moves:
            move = random.choice(valid_moves)
            computer_dominoes.remove(move)
            add_to_board(move, board)
            renpy.notify("It's the computer's turn.")
        else:
            if boneyard:
                computer_dominoes.append(boneyard.pop())
                renpy.notify("Computer must draw a domino from the boneyard.")
            else:
                current_player = "player"
                renpy.notify("Computer has no more valid moves. It's your turn.")

    def add_to_board(domino, board):
        if domino.value1 == board[-1].value2:
            board.append(domino)
        elif domino.value2 == board[-1].value2:
            board.append(Domino(domino.value2, domino.value1))
        elif domino.value1 == board[0].value1:
            board.insert(0, Domino(domino.value2, domino.value1))
        elif domino.value2 == board[0].value1:
            board.insert(0, domino)

    def calculate_score(dominoes):
        return sum(d.value1 + d.value2 for d in dominoes)

    def valid_moves_left(dominoes, board):
        return any(domino.valid_move(board) for domino in dominoes)

    def determine_winner():
        global player_score, computer_score
        if not player_dominoes and not boneyard:
            player_score += calculate_score(computer_dominoes)
            renpy.notify("You have won the game!")
        elif not computer_dominoes and not boneyard:
            computer_score += calculate_score(player_dominoes)
            renpy.notify("Computer has won the game!")
        elif not valid_moves_left(player_dominoes, board) and not valid_moves_left(computer_dominoes, board):
            player_score += calculate_score(player_dominoes)
            computer_score += calculate_score(computer_dominoes)
            if player_score < computer_score:
                renpy.notify("You have won the game!")
            elif player_score > computer_score:
                renpy.notify("Computer has won the game!")
            else:
                renpy.notify("The game is a draw.")
###This defines a screen called domino_game using the Ren'Py visual novel engine. It displays the game interface, including scores, board, player's and computer's dominoes, and current player's turn. 
screen domino_game:
    vbox:
        text "Player Score: [player_score]"
        text "Computer Score: [computer_score]"
        null height 20
        hbox:
            if board:
                text "Board: "
                for domino in board:
                    text str(domino) xsize 50
            else:
                text "Board is empty"
        null height 20
        frame:
            text "Your Dominoes:"
        hbox:
            for domino in player_dominoes:
                if current_player == "player":
                    textbutton str(domino) action Function(make_move, domino)
                else:
                    text str(domino)
        null height 20
        frame:
            text "Computer's Dominoes:"
        hbox:
            for domino in computer_dominoes:
                if current_player == "computer":
                    textbutton str(domino) action Function(make_move, domino)
                else:
                    text str(domino)
        null height 20
        if current_player == "player":
            text "It's your turn"
        elif current_player == "computer":
            text "Computer is playing"
        if boneyard:
            textbutton "Draw Domino" action Function(make_move, None) xalign 0.5
        else:
            text "No more dominoes in the boneyard" xalign 0.5
        null height 20
        textbutton "End Turn" action Function(computer_move) xalign 0.5
        null height 20
        textbutton "Restart" action init_game()

label start:
    scene bg room
    "Welcome to the Domino Game!"
    $ init_game()
    $ renpy.call_screen("domino_game")
    "test"

label restart_game:
    "pause"
So far a lot of bugs, achieved more or less that the screen works but encountered a number of errors:

28 chips in dominoes, does not show how many chips are left in the bazaar

Tried to prescribe that the player would take chips if there is nothing to go, failure

Make so that the chips are not random number generators, failure

Clicks to end the round do not lead to a loss and get chips, failure.

Went to bed later I'll try more code to assign.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]