First of all, I'm aware that Mathematica StackExchange prefer specific questions, not discussions, but I need some enlightenment with this issue for I have nowhere else to ask :)

The thing is, our teacher wants to implement a program that simulates a game (The Fish Game, of The Cloud Institute for Sustainability Education) with N rounds, and each round have N players, and each player does N actions in its turn. The results of the game (e.g. fish catched by each player, cumulative catch and population over time) are important stats, but even more important is the fact that each step should be present and not omitted. So it's a game more than a simulation with graphs and sliders to see tendencies and behaviors, because each iteration asks for the user to do something (catch fish). This is a fully procedural approach.

After some research, I've found that loops are not efficient nor desirables in Mathematica and there are some others built-ins able to do the job. I've been fiddling around with the Do loop, and got myself a 'decent' text-based simulation. However, I'm not sure if there is a more appropriate way to represent the nature of the game. Do looping the rounds, and nesting another Do loop for the players is what I have now.

I know you need some code insight, so here's an extract:

    Print["Starts round ", round, "!"];
    suma = {};
        Print["Starts Player ", player, " turn!"];
        catch = RandomInteger[{1,3}];

        popafterdraw = actualpop - catch;

        If[actualpop >= catch, (* Validate population before draw *)
            Print["Player ", player, " draws ", catch, " fish!"];

It's basically looping players times each player's action, which loops rounds times or till fish is depleted.

Full code is available here. Be careful, it's a mess.

So, long story short, my question is: do you think this could be changed to a more functional approach? or should I stand up and tell the whole class that Core Language is not the way to do it and I'll get way further programming a Python script?

If, in any case, there's another way (and a more efficient one) to approach this program, then I'll be more than glad to hear your recommendations; changing programming paradigms is kinda difficult to do if all your life procedural was the way to go.

And, if my question doesn't belong here or is not specific enough to be in a StackExchange site, I apologize in advance. Please moderate as usual.

Thank you.

PS: If anyone is intrigued why we're using Mathematica for this, is because "the software is cool but expensive and we need to use it along iPads to create iOS applications". CDF Player is not even available in iOS, so...

  • $\begingroup$ Rather than AppendTo which will start to grind if the lists become large (everytime you append the entire list is copied) I would suggest using Sow using player as the tag. Then Reap will give you each players catches. $\endgroup$ – Ymareth May 6 '14 at 9:55
  • $\begingroup$ And maybe InputField instead of Input, that succession of Input is not handy, several InputField and a Run button would be easier. $\endgroup$ – Öskå May 6 '14 at 10:05
  • 2
    $\begingroup$ While it's true that new users tend to use procedural loops when alternative structures would be more appropriate, it is an over-simplification to state that loops are undesirable. If you have an algorithm which is most naturally expressed as a procedural loop, forcing a functional approach can obscure the underlying logic and make the code harder to write and to reason about. You are asking the correct question - "what is the most appropriate way to represent the nature of the game?" If the answer is "as a loop" then my advice would be to code it as a loop. $\endgroup$ – Simon Woods May 6 '14 at 11:06
  • $\begingroup$ @Ymareth thank you for your input, I'll give Reap and Sow a try. @Öskå I tried using InputField, but didn't quite work. I suppose because I didn't include a Run button. @SimonWoods I thought exactly the same. I´m doing another code in Python following a more traditional approach in case something is not working in the future. Thank you again for your answers :) $\endgroup$ – Saxo Miko Mola May 6 '14 at 16:41

Your Answer

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

Browse other questions tagged or ask your own question.