#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0

    Programming advice


    I am trying to stimulate games and print the results of many serves of the 4 players did, and I believe the winning rules that I changed for my game is correct because I needed it for one team to have more than 5 points, then they would win. Also, I am trying to show the progress of my program with animation later and I am having trouble finding a function that does that. May you guys help a new beginning program? Thanks!
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    It would help if you put your code into code tags.
    At the start use [code]
    At the end use the same but with a / in front of the word code.
    See ...
    http://forums.devshed.com/misc.php?do=bbcode#code
    Last edited by Dietrich; November 16th, 2013 at 02:08 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    I would suggest that you start with one player and get it to work as each player is the same code, just different variables. For example, a function used to add to the score
    Code:
    ## Instead of code like this for every player
    if serving == "A_1":
       if random() < probA_1:
           scoreA = scoreA + 1
    
    ##-------- use a function instead
    def score_it(prob, score):
        if random() < prob:
            score = score + 1
        return score
    
    scoreB = score_it(probB_1, scoreB)
    scoreA = score_it(probA_1, scoreA)

    There are what appear to be some problems with the code you posted like
    Code:
    serving = "B_1" or "B_2"
    print serving          ## not what you probably expect (B2 never serves)
    Last edited by dwblas; November 16th, 2013 at 02:20 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    i did do that for 2 players, but then i get confused since the program gets more abstract when I added more players.



    Originally Posted by dwblas
    I would suggest that you start with one player and get it to work as each player is the same code, just different variables. For example, a function used to add to the score
    Code:
    ## Instead of code like this for every player
    if serving == "A_1":
       if random() < probA_1:
           scoreA = scoreA + 1
    
    ##-------- use a function instead
    def score_it(prob, score):
        if random() < prob:
            score = score + 1
        return score
    
    scoreB = score_it(probB_1, scoreB)
    scoreA = score_it(probA_1, scoreA)

    There are what appear to be some problems with the code you posted like
    Code:
    serving = "B_1" or "B_2"
    print serving          ## not what you probably expect (B2 never serves)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    451
    Rep Power
    32
    If I understand the rules of the game, and I probably don't, you have two switches or booleans involved: Team A=True, Team B=False, and for each team, True=server #1, False = server #2. It is also good to use lists or dictionaries instead of a slew of variables. The following code uses a list of lists, with the [0] inner list being team A and the [1] inner list being team B. They both contain two elements, [0] = True/False for server #1 or #2, and [1]=score. You could also put the probabilities in this list or a separate list but I will leave that decision up to you. Also lists are mutable, meaning you don't have to pass them back and forth between functions as you would with a "normal" variable.
    Code:
    from random import random
    from random import randrange
    
    def gameOver(a, b):
        if(a > 5 or b > 5):
            return 1
        else:
            return 0
    
    def check_random(list_in, prob_1, prob_2, serving):
        random_prob = random() 
        if list_in[0]:     ## True = A/B_1
            prob = prob_1       ## use a common variable and only one calculation
        else:
            prob = prob_2
    
        print "     ", random_prob, "<?", prob
        if random_prob < prob:
            list_in[1] += 1          ## add to score
        else: 
            ## switch server on this team for next time
            list_in[0] = not list_in[0]
            serving = not serving     ## other team
    
        print "     ", random_prob, "<?", prob, serving
        return serving 
        
    def simOneGame(probA_1, probA_2, probB_1, probB_2):
        choose_serve = randrange(1,5)
        if choose_serve == 1:
            serving = True       ## A
            players_list[0][0] = True
        elif choose_serve == 2:
            serving = True       ## A
            players_list[0][0] = False
        elif choose_serve == 3:
            serving = False       ## B
            players_list[1][0] = True
        elif choose_serve == 4:
            serving = False       ## B
            players_list[1][0] = False
        
        while not gameOver(players_list[0][1], players_list[1][1]):
            if serving:     ## "A" team 
                serving = check_random(players_list[0], probA_1, probA_2, serving)
            else:           ## "B" team
                serving = check_random(players_list[1], probB_1, probB_2, serving)
            print players_list
                               
    players_list = [[True, 0], [True, 0]]
    simOneGame(0.1, 0.2, 0.3, 0.4)
    Last edited by dwblas; November 16th, 2013 at 08:38 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Thank you so much for your help, and I do really appreciate all of your help. But I was wondering how can I fix my code that I created since I haven't learned all of these wonderful operations. May you please help me out... please? Thanks!




    Originally Posted by dwblas
    You have two switches or booleans involved: Team A=True, Team B=False, and for each team, True=server #1, False = server #2. It is also good to use lists or dictionaries instead of a slew of variables. The following code uses a list of lists, with the [0] inner list being team A and the [1] inner list being team B. They both contain two elements, [0] = True/False for server #1 or #2, and [1]=score. You could also put the probabilities in this list or a separate list but I will leave that decision up to you. Also lists are mutable, meaning you don't have to pass them back and forth between functions as you would with a "normal" variable.
    Code:
    from random import random
    from random import randrange
    
    def gameOver(a, b):
        if(a > 5 or b > 5):
            return 1
        else:
            return 0
    
    def check_random(list_in, prob_1, prob_2, serving):
        random_prob = random() 
        if list_in[0]:     ## True = A/B_1
            prob = prob_1       ## use a common variable and only one calculation
        else:
            prob = prob_2
    
        print "     ", random_prob, "<?", prob
        if random_prob < prob:
            list_in[1] += 1          ## add to score
        else: 
            ## switch server on this team for next time
            list_in[0] = not list_in[0]
            serving = not serving     ## other team
    
        print "     ", random_prob, "<?", prob, serving
        return serving 
        
    def simOneGame(probA_1, probA_2, probB_1, probB_2):
        choose_serve = randrange(1,5)
        serving = True       ## A
        if choose_serve in (3, 4):     ## B
            serving = False   ## B
        
        while not gameOver(players_list[0][1], players_list[1][1]):
            if serving:     ## "A" team 
                serving = check_random(players_list[0], probA_1, probA_2, serving)
            else:           ## "B" team
                serving = check_random(players_list[1], probB_1, probB_2, serving)
            print players_list
                               
    players_list = [[True, 0], [True, 0]]
    simOneGame(0.1, 0.2, 0.3, 0.4)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    I tried to redo my code and now I thought I did it, but I ran into some counting errors which I don't know how to fix. Does my code seem to do what I want it to do? Thanks.
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    What errors occur? I modified your program a little bit (I almost always rewrite the other code---helps me a great deal to understand it). During rewrite I found a major flaw. Coming into this block of code your "serving" variable refers to a string. In this loop, the iteration variable "i" takes the value of each single character in the string of serving. Thus none of the conditions can possibly be true since the string of length 1 cannot match a string of length 3.
    Code:
        for i in serving:
            if i == "A_1":
                count_A1 = count_A1 + 1
            elif i == ...

    "but I ran into some counting errors which I don't know how to fix. Does my code seem to do what I want it to do?"
    Maybe this corrects your counting error. Also, your final summary reports
    count_A1, count_A2, count_B1, count_B2
    I presume you actually want cumulative values but there's no code to accumulate them. You've merely returned the counts of the final game.


    Another idea: use random.choice function to replace
    Code:
        choose_serve = randrange(1,5)
        if(choose_serve == 1):
            serving = "A_1"
            #print(serving, "is serving")
        elif(choose_serve == 2):
            serving = "A_2"
        elif(choose_serve == 3):
            serving = "B_1"
        else:
            serving = "B_2"
    serving = choice('A_1 A_2 B_1 B_2'.split())[/code]with
    Code:
        serving = choice('A_1 A_2 B_1 B_2'.split())

    Comments on this post

    • ckwan48 agrees
    Last edited by b49P23TIvg; November 17th, 2013 at 01:04 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    No errors occur, my bad! But when I try to count the serves of each player, it seems that I can't get my loop to count it. That is where I am having a lot of trouble, right now. May you you help me? Thanks!


    Originally Posted by b49P23TIvg
    What errors occur? I modified your program a little bit (I almost always rewrite the other code---helps me a great deal to understand it). During rewrite I found a major flaw. Coming into this block of code your "serving" variable refers to a string. In this loop, the iteration variable "i" takes the value of each single character in the string of serving. Thus none of the conditions can possibly be true since the string of length 1 cannot match a string of length 3.
    Code:
        for i in serving:
            if i == "A_1":
                count_A1 = count_A1 + 1
            elif i == ...

    "but I ran into some counting errors which I don't know how to fix. Does my code seem to do what I want it to do?"
    Maybe this corrects your counting error. Also, your final summary reports
    count_A1, count_A2, count_B1, count_B2
    I presume you actually want cumulative values but there's no code to accumulate them. You've merely returned the counts of the final game.


    Another idea: use random.choice function to replace
    Code:
        choose_serve = randrange(1,5)
        if(choose_serve == 1):
            serving = "A_1"
            #print(serving, "is serving")
        elif(choose_serve == 2):
            serving = "A_2"
        elif(choose_serve == 3):
            serving = "B_1"
        else:
            serving = "B_2"
    serving = choice('A_1 A_2 B_1 B_2'.split())[/code]with
    Code:
        serving = choice('A_1 A_2 B_1 B_2'.split())
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Sure looks like you read no more of my post than the first sentence. Helping you is not worthwhile. I had planned to ask what happens if the probabilities don't sum to 1 within a tiny tolerance.
    Code:
        a_1 = eval(input("What is the prob. player A_1 wins a serve? "))
        a_2 = eval(input("What is the prob. player A_2 wins a serve? "))
        b_1 = eval(input("What is the prob. player B_1 wins a serve? "))
        b_2 = eval(input("What is the prob. player B_2 wins a serve? "))
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    My apologizes! I am grateful for all of your help. I read your comments and got kind of confused. I wanted to fix up my program to make sure I understood the basics first. And I don't think that probability has to add to 1. I try to count each of my players serves and I never get any counts. Thanks for all of your help!


    Originally Posted by b49P23TIvg
    Sure looks like you read no more of my post than the first sentence. Helping you is not worthwhile. I had planned to ask what happens if the probabilities don't sum to 1 within a tiny tolerance.
    Code:
        a_1 = eval(input("What is the prob. player A_1 wins a serve? "))
        a_2 = eval(input("What is the prob. player A_2 wins a serve? "))
        b_1 = eval(input("What is the prob. player B_1 wins a serve? "))
        b_2 = eval(input("What is the prob. player B_2 wins a serve? "))
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Also, I replaced i with "iii" and it gave me an error, so I didn't know what to use to compare the strings with.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Is something wrong with my for loop since it will not count each individual serves of the players. May I get some help please? Thanks!


    Originally Posted by b49P23TIvg
    Sure looks like you read no more of my post than the first sentence. Helping you is not worthwhile. I had planned to ask what happens if the probabilities don't sum to 1 within a tiny tolerance.
    Code:
        a_1 = eval(input("What is the prob. player A_1 wins a serve? "))
        a_2 = eval(input("What is the prob. player A_2 wins a serve? "))
        b_1 = eval(input("What is the prob. player B_1 wins a serve? "))
        b_2 = eval(input("What is the prob. player B_2 wins a serve? "))

IMN logo majestic logo threadwatch logo seochat tools logo