|
They're probably trying to get you to write it using recursion. For example, have a function that takes as input the current player and scores, and then execute your turn logic. If a player has won return the results, otherwise call the same function again with the updated values. Aside, when a 1 is rolled do you really accumulate the turn score and not go bust? If so, why would you ever not roll?
|
# ? May 4, 2024 03:44 |
|
|
# ? May 4, 2024 16:24 |
|
moosferatu posted:They're probably trying to get you to write it using recursion. For example, have a function that takes as input the current player and scores, and then execute your turn logic. If a player has won return the results, otherwise call the same function again with the updated values. that's the right interpretation or be an rear end in a top hat and write it as an event-driven score system using a single for loop which has its value change to the current highest score after each iteration; each iteration emits a currentPlayerRoll event handled by a currentPlayerRollHandler which does all the score/turn keeping and has a handy function that also updates the variable tracking the for loop and current player so the game ends when a single unbroken for loop runs to completion to 100 and it's completely over-engineered in response to "no while loops"
|
# ? May 4, 2024 04:02 |
|
Yeah those requirements definitely point towards the prof wanting you to use a specific technique, probably based on whatever's been in the lectures recently. Avoiding loops entirely by using recursion is probably the easiest way to do it. Still I'd be kinda surprised if a prof handed an assignment like that out without specifically saying "use recursion", especially with the weird restriction that for loops are ok but while loops are not. Other notes if you wanna take a non-recursive approach: Instead of break, you can always get out of a loop with return. of course this requires separating each loop out into its own function instead of putting them all in one big one. If you're a real sicko you can do it by raising and catching exceptions too! (Don't do that.) if you want an infinite loop without using while, you can use generator functions, for example Python code:
But this is also kinda stupid and is basically just a dumber way to write a while loop. Unless your teacher specifically wanted you to write/use a generator function just for the sake of writing one, so again I guess check the recent lecture topics? Tempora Mutantur posted:that's the right interpretation in python, unlike C, you can't overwite the variable controlling a for loop. Every loop is basically range-based for. Though you can still hack around it with a generator that depends on some external state, like like this one made with iter: Python code:
RPATDO_LAMD fucked around with this message at 04:30 on May 4, 2024 |
# ? May 4, 2024 04:07 |
|
code:
|
# ? May 4, 2024 05:39 |
|
every time I've tried to use recursion in a non-toy piece of code I've immediately run out of stack space and been forced to rewrite it as a while loop lol
|
# ? May 4, 2024 05:48 |
|
cheetah7071 posted:every time I've tried to use recursion in a non-toy piece of code I've immediately run out of stack space and been forced to rewrite it as a while loop lol
|
# ? May 4, 2024 06:02 |
|
also depends on what exactly you're doing, like recursively walking a tree or doing any kind of binary search thing will only use log(n) stack space
|
# ? May 4, 2024 06:18 |
|
I've been doing a lot of work with data-driven execution in shaders and let me tell you, not having a call stack at all breaks you in ways I can't even describe. I can flatten a directed acyclic graph into a for loop in my sleep. Structs? Maybe if you don't care about cache coherency. We only do arrays of 4 component vectors here, buddy!
|
# ? May 4, 2024 06:44 |
|
|
# ? May 4, 2024 16:24 |
|
A Festivus Miracle posted:Hey Goons, your gracious assistance please. As others mentioned, figure out what your professor is trying to get the class to do and do that. This is an extremely dumb way to try to teach people to use recursion if that is indeed the case; there's so many better examples that aren't terrible! Also, saying "we don't use break statements" makes me want to say "ok cool, code:
I'm not using break here or while loops! We're good, right?" Volmarias fucked around with this message at 07:21 on May 4, 2024 |
# ? May 4, 2024 07:11 |