5
\$\begingroup\$

I posted this question two days ago and received some helpful answers.

I was advised to try to incorporate the suggestions into the code and also to try to code the steps of the "game task" that I skipped and so I did.

I thought that a new question would be convenient since the code is quite different...

Anyway, here is the new code:

It should be noted that I have left several comments for you to read to get a better understanding of my problems. Of course, you also got a pretty long and detailed description in the linked question above, which I recommend you to read to understand the thing that I am trying to accomplish. Generally though, what can I do to make the code as efficient/smart/clean as possible?

import random

def checkForBum(tal, bummer):
    if(not tal%bummer) or str(bummer) in str(tal):
        return "BUM!"
    else:
        return tal

def getInput(text, min, max):
    var = input(text)
    while int(var) < min or int(var) > max:
        var = input("Whoops, that is not valid - Try again: ")
    return int(var)

def isDead(pcurrNum, pbumNum):
        if(checkForBum(pcurrNum, pbumNum) == "BUM!"):
            return True
        else:
            return False

def gameon():
    print("Instructions: \nPossibilities for Boom number(s): 1-100\nPossibilities for number of players: 1-20\nEach partipipant must say the first upcoming number but gets the option to count up to two numbers extra.")
    bumnum = getInput("Choose the bum: ", 1, 100)
    extrabt = getInput("Extra bumnum(0 = none): ", 1, 100)
    pnum = []#default list
    for q in range(1, getInput("Number of player(s) (2+=2): ", 1, 20)+1):
        pnum.append(q)
    isAI = False
    if(len(pnum) == 1):#singleplayer
        isAI = True

    currNum = 0
    for x in range(1, 101):
        for player in pnum[:]:
            #check if the current player is the winner
            if(len(pnum) == 1 and isAI == False):
                print("WE GOT A WINNER! CONGRATS PLAYER %s" % player)
                pnum.remove(player)
                continue

            currNum += 1
            #check diff between raw_input and input
            if(isDead(currNum, bumnum)):
                print("OH! Player %s said BUM! and is eliminated from the game!" % player)
                pnum.remove(player)
                continue#this statement works though because the goal is to affect the enclosing loop
            else:
                print("Player %s is safe and sound - said %s" % (player, currNum))
            countto=raw_input("Player %s - How much do you wish to count up to?(%s, %s or %s to skip) " % (player, currNum+1, currNum+2, currNum))
            #if the same number as currNum is chosen, the range in the following for loop will be (currNum, currNum) meaning it is skipped(correct me if I am wrong, please)
            for left in range(currNum, int(countto)):
                currNum+=1
                if(isDead(currNum, bumnum)):
                    #further counting interrupted by the boom
                    print("Uhh that was an unlucky shot, you made yourself say BUM! and player %s is eliminated." % player)
                    pnum.remove(player)
                    continue#would like to continue the outer loop at all the points where i used continue. The following prints had to be commented out because they would be called in all iterations(aka help please)
                else:
                    print("Player %s has counted up to %s" % (player, currNum))
                    continue
                #print("Player %s has chosen to skip" % player)#if the continue statement is ignored, the current partipipant has chosen to skip
        if(isAI and len(pnum)>0):
            #singleplayer chosen - let the bot take a turn
            currNum+=1
            if(isDead(currNum, bumnum)):
                print("Oh! The bot has been slayed! You won, congrats :)")
            else:
                print("The bot is safe and sound - said %s" % currNum)
            #if the same number as currNum is chosen, the range in the following for loop will be (currNum, currNum) meaning it is skipped(correct me if I am wrong, please)
            for left in range(currNum, random.randint(currNum, currNum+2)):
                currNum+=1
                if(isDead(currNum, bumnum)):
                    #further counting interrupted by the boom
                    print("Oh! The bot has been slayed! You won, congrats :)")
                else:
                    print("The bot has counted up to %s" % currNum)
                    continue
                #print("The bot has chosen to skip")#if the continue statement is ignored, the current partipipant has chosen to skip

gameon()

#How/where should I make a "Retry?" functionality since recursion does not really seem to be optimal here.
#after elimination
#How would you check if the "count up to" replies are numbers that are neither more or less than the possibilities I gave?
#How would you do so that if an extra boom number is chosen, the game updates and uses as an extra boom number?

I have also tried to add the AI and (technically) unlimited amount of player functionality, however, there still are few things things that I need, which I commented at the bottom :)

\$\endgroup\$
  • \$\begingroup\$ @200_success can I request answers from anybody? \$\endgroup\$ – noflow Sep 3 '17 at 10:51
  • \$\begingroup\$ @200_success Like on quora, you can request answers from certain people and is that also an option here? I mean, apparently, there are more people than just me who would love a good answer. Therefore, if you could request answers, I could generate traffic to the ones who have the abilities to answer this question in probably the best way possible. \$\endgroup\$ – noflow Sep 3 '17 at 13:52
  • \$\begingroup\$ We're all doing this for "fake Internet points" — basically for fun. Requesting specific people to work for you is considered impolite here. It is up to you to make your question attractive and interesting to others. \$\endgroup\$ – 200_success Sep 3 '17 at 13:57
  • \$\begingroup\$ @200_success I see. Then, I would hope somebody would find this question interesting :) \$\endgroup\$ – noflow Sep 3 '17 at 14:03
2
\$\begingroup\$

This is really an interesting question and thus I will give it a shot.

I am not quite sure how you should implement a retry functionality, but you could try to split things up in more functions than what you currently have, so like you would have a retry() function etc. Your gameon() looks like it could be simplified and if this is done correctly, you can probably have will probably have a function instead of those for loops where you want to break out of the enclosing for loop which ultimately will enable you to the the print for when someone has skipped the turn.

Note: I know that my answer is not the most helpful one but I would just like to share some thoughts and also try to reach out for a complete answer.

I REALLY hope that someone else who knows more about Python that what I do answers this question and where that blessed person then really tries to look at your code and applies his knowledge. I cross my fingers for more answers :)

\$\endgroup\$
  • \$\begingroup\$ oh thanks. The problem that I have sort of thought of the same, but could not really figure how to accomplish it... But yea, let's hope that someone who really knows his stuff takes a look at this :))) \$\endgroup\$ – noflow Sep 3 '17 at 11:59
  • \$\begingroup\$ Please note that asking for code to be written is off-topic for Code Review. \$\endgroup\$ – 200_success Sep 3 '17 at 13:50
1
\$\begingroup\$

Well, I'm a beginner programmer too, so I won't be able to answer all your questions, but I have some suggestions:

Some minor changes:

1) This part:

def isDead(pcurrNum, pbumNum):
        if(checkForBum(pcurrNum, pbumNum) == "BUM!"):
            return True
        else:
            return False

Could be simplified to:

def isDead(pcurrNum, pbumNum):
    return checkForBum(pcurrNum, pbumNum) == "BUM!"

2) When the program asks for a value to the extra bum, it says you can choose 0 for no extra bum, but you set the minimum input allowed to 1. So you must change min to 0.

Answering some questions:

How would you check if the "count up to" replies are numbers that are neither more or less than the possibilities I gave?

You could check if countto are allowed numbers using your own function getInput. For that, you can change:

countto=raw_input("Player %s - How much do you wish to count up to?(%s, %s or %s to skip) " % (player, currNum+1, currNum+2, currNum))

to:

text = "Player {0} - How much do you wish to count up to?({1}, {2} or {3} to skip)".format(player, currNum+1, currNum+2, CurrNum)

countto = getInput(text, currNum, currNum+2)

(I used .format here because it's the way I know, but it's not ok to mix format and "%" styles in the code. You should change that for the sake of coherence!)

How/where should I make a "Retry?" functionality since recursion does not really seem to be optimal here.

When you say retry you mean something like a "play again"? In this case you could use something like:

if __name__ == '__main__':
    while True:        
        gameon()
        if not getInput("Do you want to play again? Yes(1) or No(0)?",0,1):
            break

Other suggestions

Well, the code has some bugs and I agree with what Fac Pam said about splitting the code into more functions since it's a little hard to follow it's flow.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.