Git Product home page Git Product logo

Comments (4)

L-A avatar L-A commented on May 14, 2024

Regarding exercise 118, using while budget > 0 like it's presented means the game will tolerate "overspending" on the last enemy in the list. I believe the code as you present it can already be considered a solution to the exercise (it won't get stuck, it will simply overspend on whatever the next enemy costs).

Am I missing something, or should we be looking at code that wants to retry chanceList:next() until the cost of the picked enemy fits within the remaining budget?

from blog.

a327ex avatar a327ex commented on May 14, 2024

You can solve that problem in multiple ways. The most straight forward to me would be to just not spawn an enemy if it overspends by adding if points - self.enemy_to_points[enemy] >= 0 before doing the points = points - self.enemy_to_points[enemy] calculation. However this is the kind of thing that is really really small in the grand scheme of things so realistically I wouldn't worry about one enemy spending a few extra points.

from blog.

L-A avatar L-A commented on May 14, 2024

I apologize, I think I haven't made my point very well.

I mean that this statement in the exercise isn't true regarding the code provided in the lesson:

The while loop that takes care of finding enemies to spawn has one big problem: it can get stuck indefinitely in an infinite loop. Consider the situation where there's only one point left, for instance, and enemies that cost 1 point (like a Rock) can't be spawned anymore because that difficulty doesn't spawn Rocks.

It doesn't get stuck in an infinite loop because even if there's only 1 points left, it tolerates spending 3 points on the last enemy to bring points to -2, after which the while loop ends as expected.

By the way, thank you so much for making and distributing these lessons!

from blog.

a327ex avatar a327ex commented on May 14, 2024

Ah, I see. So the line that I mentioned in the previous comment should have been there in the first place and I guess I forgot to place it there. Now that I understand your question better:

Am I missing something, or should we be looking at code that wants to retry chanceList:next() until the cost of the picked enemy fits within the remaining budget?

I decided to not focus on that in the question because I wanted to make sure the reader understood the notion of stopping loops like that after a number of runs since it's a useful idea in lots of situations. But yea, in a real scenario you can change the probabilities of enemies as the number of points left changes. Although even in a real game I personally wouldn't do that because it's way more complicated than just using the naive solution.

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.