0

I'm curious if it's possible for me to change the functions from different files in Python.

What psrock.py file does is that it receives two datas from each file that will go against each other in rock paper scissors and deciding which player have won. Of course, psrock.py file contains other functions, too, but I just inserted one function since other ones don't really matter for my question.

I'm trying to edit the function that's in psrock.py file so that team3's (there are team1, team2, team3 and they play rock paper scissors against one another. (i.e. team1 and team 2 go against each other then team 1 and 3 after. vice versa)) result will always be rock and the opponent's result will be scissors so that team3 can win no matter what.

However, I am struggling and don't know what to do.. :( I barely started learning Python and it's pretty challenging task for me to do. I would love it if you can help me out a little bit ;)

# This is a function from psrock.py file
import team3

def round(player1, player2, history1='', history2=''):

    # Get player 1's move. 
    move1 = player1(my_history=history1, their_history=history2)
    # Get player 2's move. 
    move2 = player2(my_history=history2, their_history=history1)

    if valid(move1) and valid(move2):      
        if move1 == move2:
            score1, score2 = 0, 0
        elif move1+move2 in ['rs', 'sp', 'pr']:
            score1, score2 = 1, -1
        else:
            score1, score2 = -1, 1
    else: #one of the moves was invalid
        if valid(move1): # only move2 was invalid
            move2 = 'x'
            score1, score2 = 1, -1
        elif valid(move2): # only move1 was invalid
            move1 = 'x'
            score1, score2 = -1, 1
        else: # Both moves invalid
            move1, move2 = 'x', 'x'
            score1, score2 = -1, -1

    return move1, move2, score1, score2

...And I'm trying to edit this function from another file named team3...

# Team 3 File
# -*- coding: utf-8 -*-
import psrock

def round(player1, player2, history1='', history2=''):
    move1 = player1(my_history=history1, their_history=history2)
    if player1 == team3:
        move1 = 'r'
        move2 = 's'
    elif player2 == team3:
        move1 = 's'
        move2 = 'r'

The Files:


  1. Download the File
  2. Extract the zip file
  3. Open all of the files in the same tab
  4. Play the psrock_play.py file

https://drive.google.com/file/d/0BxNi5bq6Cvnea0c4aVVIWUxZRUE/view?usp=sharing

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
David Lim
  • 1
  • 2
  • What exactly do you mean by "edit this function from another file"? – Jonathan March May 23 '16 at 18:44
  • @JonathanMarch So basically you know how there's a function that's called "round(player1, player2, history1='', history2='')" in the first file? I'm trying to change what it does from another file when it runs (or something like that) instead of manually changing what my function is going to do in file #1. So basically in file #2, I'm trying to make it so that if player 1 or 2 is equal to team3, it will result a 'r' for the team 3 and 's' for the other player. (move1 is for player 1, move 2 is for player2.) – David Lim May 25 '16 at 03:47
  • Sorry for the vague explanation. I'm still struggling to find a way for me to describe this problem as well as possible. :( – David Lim May 25 '16 at 03:49
  • player1 is a function as i understand and how are you equating it to team3 what is team3 datatype ? – Harwee May 25 '16 at 04:13
  • @Harwee What the function in psrock.py file does is that it receives two files (player1, player2) and compare their results just like rock-paper-scissors. So when the function starts to compare, I want the function to make team3 file's result to be rock and other player to be scissors that team3 will always win the game. I think it's better to upload a canopy file for me to make it easier to understand. (I'm having a little bit of trouble explaining this x( ) – David Lim May 25 '16 at 14:49

1 Answers1

0

As I understand your question, one simple clean solution might be to change your round function so that when called, one of its parameters is a reference to a "callback function". This function would be defined in the module that calls round, but would actually be executed inside of round.

For more on callback functions generally: https://en.wikipedia.org/wiki/Callback_(computer_programming)#Python Implementing a callback in Python - passing a callable reference to the current function or search further for "python callback function"

Community
  • 1
  • 1
Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • That's a great solution if I were to edit the psrock.py file, however, the rule is that I cannot change anything from the first file with round function.. :( Thank you so much for your effort though! x) I really appreciate it. – David Lim May 31 '16 at 14:53