The goal is this "simple game".
The game is all about that you choose a "boom number" and then count one by one from 1. Every time that the player hits a number that can be divided by the "boom number" resulting in an integer.or that the "boom number" is a part of the number, the current player must shout "BUM!".
So If the "boom number" was 3, the game could proceed like the following example:
1, 2, BUM!, 4, 5 BUM!, 7 ,8 BUM!, 10, 11, BUM!, BUM!, 14, BUM!
Here, 3,6,9, 12 and 15 cause "BUM!" because they can all be divided by 3, and 3, because 3 is a part of that number.
Each player can choose to count one number up to three times meaning that each player will be asked up two times if they want to keep counting( the current player must say/write the first time). Example - The user chooses 3 players - Player one says 1 (the must), gets asked if we want to keep counting and says no - now it is player 2's turn he must say 2 and counts the two optional times so now the number is 3 - now it is player 3's turn and he must count the first time but the "boom number" was set to 4 so instead he would say/write "BUM!" and he is out - now it is player 1's turn...
Specifications:
implement a loop that counts from 1 to 100
Implement the function "checkForBum()" that accepts two integers, "num" and "boomnum".
The function must return:
a. "BUM!" if "boomnum" can be divided with "num" resulting in an integer or if "num" is a part of "boomnum"
b. The first parameter "num" unchanged
Use the function "checkForNum()" in the loop, so that the program plays the Boom Game
Let the user choose the "boom number"
Let the user have the option to choose an extra "boom number" and update the program so that both "boom numbers" counts
The game can be played 1-4 players, where if 1 player is chosen, the player will play against a very simple AI that counts one by one a random number of times (same rules as for the players).
Edit: It is up to you what way to ask the user what he/she wants to count up to.
How would you accomplish this? The thing is that I am normally programming in C++ and set up this task to see how it could be accomplished in Python.. and I have tried to do some of the things that I wanted (only about the half of the task) but since I do not know that much about python, it does not feel optimal. Hence, I would love to see somebody accomplish this task to have a look at how such things can be solved in Python when you really know about the language.
How can this code be optimized?(an accomplishment of the task/game stated above would be preferable though)
Code:
def checkForBum(tal, bummer):
x = 0
while x <= 100:
if(x+int(bummer) <= 100):
x+=int(bummer)
if(int(tal) == x):
return "BUM!"
else:
break
return tal
def getInput(text):
var = input(text)
while not var.isdigit() and int(var) > 0:
var = input("Whoops, it has to be a valid number - Try again: ")
return int(var)
def gameon():
bumtal = getInput("Choose the bum: ")
extrabt = getInput("Extra bumtal(0 = none): ")
tal = 0
pnum = getInput("Number of player(s) (2+=2): ")
oner = 0
twoer = 0
turns = 1
countbool = ""
#recursion - prob overhead from function calls on the stack.. (negligible)..
#could mean less code though
for x in range(100):
if(oner == twoer):
countbool = input("1 player's turn - Count(Y/N)?")
if (countbool == "N" or turns > 2):
turns = 0
oner += 1
elif(oner > twoer):
if(pnum > 1):
countbool = input("2 player's turn - Count(Y/N)?")
else:
countbool = input("Bot's turn: Hit enter to let him count")
if (countbool == "N" or turns > 2):
twoer += 1
turns = 0
if(countbool == "Y"):
turns += 1
tal += 1
for num in range(tal):
isit = checkForBum(num+1, bumtal)
if(int(extrabt) > 0 and isit != "BUM!"):
isit = checkForBum(num+1, extrabt)
print(isit)
gameon()
Update: Please take a look at this linked question.