Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 573 574 [575] 576 577 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882554 times)

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8610 on: December 07, 2015, 04:48:32 pm »

Hello! The idiot is back!

I played around with Python, I think I'm starting to get the hand of it and I made a small console-based rpg character generator (Need to add a lot of details and such, and the code is probably anything but tidy). I was curious about what other projects I could do to learn more?
Logged

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8611 on: December 07, 2015, 04:55:03 pm »

...One of my favorites is a comprehensive dice roller that reads input (in-program or command line argument, the difference is negligible) and rolls xdx with various restrictions and factors, such as exploding dice, showing the rolls, or counting "successes". I usually do it to learn string manipulation in any new language I learn.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8612 on: December 07, 2015, 05:07:24 pm »

Having written a dice roller like that in PHP I can tell you that it is a surprisingly involved thing to create, so that could indeed be a good experiment.

My latest implementation even went to Enterprise level unnecessary complexity by decomposing expressions into parse trees so that it could evaluate the expression using math operators without having to resort to eval() or any of its kin.  It helped that I'd written a BBCode parser that I was able to reuse the parsing guts from.

I swear, the compiler class I took in grad school has been the most directly useful programming class I think I've ever taken.  I've used the parser design knowledge I gained from that in at least 4 or 5 substantial projects, including some stuff at my regular job.  Of course, we started using AngularJS a couple of years later, but I was proud of that HTML templating engine.
« Last Edit: December 07, 2015, 05:09:03 pm by Telgin »
Logged
Through pain, I find wisdom.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8613 on: December 07, 2015, 05:09:20 pm »

Yeah, dice rollers are always cool. My favourite was one I made for D&D, incorporating a bunch of metamagic effects.

Personally, I like to take something I don't know/understand too well (e.g. file i/o) and write a couple of programs that combine the new concept with old ones that I already understand (e.g. list comprehensions). It's not really a project per se, but you can easily tack it on to an existing game or something, unless you're like me and prefer to have a few lines of code sitting around as an example.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8614 on: December 07, 2015, 05:21:52 pm »

https://repl.it/B6hl/0

Well, I made something. I just need to find a way to make it loop (I guess a quick google will yield results) and something to make it less awfuly boring.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8615 on: December 07, 2015, 05:33:31 pm »

do you really want a 3d6 to only come up with multiples of 3...

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8616 on: December 07, 2015, 05:37:04 pm »

To loop it, stick the whole thing inside a while loop.

Also, what Putnam mentioned, right now your code only rolls one dice and multiplies the result. What you want to do is roll the dice multiple times, in a for loop.
Code: [Select]
for i in range (diceNumber):
    #do dice rolly stuff

There are two other things I recommend looking in to: the first is seeing if you can have the user enter everything in one line (e.g. "3d6") and have the program process that.
The other is adding error handling (i.e. try::except blocks) so that if the user presses r instead of 5, they'll get a message saying "invalid input" or the like, and an opportunity to try again, rather than simply having the program crash.
The second is probably easier if you don't know too much about string manipulation, but both are important skills. Of course, we can help whenever you get stuck too.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8617 on: December 07, 2015, 05:38:55 pm »

do you really want a 3d6 to only come up with multiples of 3...

Ugh, I just noticed that... Give me a minute...
Logged

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8618 on: December 07, 2015, 06:00:23 pm »

DISREGARD THIS, I FIXED IT WITH A RECURSION

https://repl.it/B6hl/4
« Last Edit: December 07, 2015, 06:22:40 pm by DrunkGamer »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8619 on: December 07, 2015, 06:36:16 pm »

Recursion's when you call a function within said function, not whatever you're calling recursion.
Code: [Select]
for i in range (int(diceNumber)):
result = random.randint(0,int(diceFaces))
input("Your roll is " + str(result) + ".")
Probably better to do this:

Code: [Select]
result=0
for i in range (int(diceNumber)):
result+=random.randint(0,int(diceFaces))
input("Your roll is " + str(result) + ".")

DrunkGamer

  • Bay Watcher
  • Beer loving Gentledwarf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8620 on: December 07, 2015, 06:37:14 pm »

Recursion's when you call a function within said function, not whatever you're calling recursion.
Code: [Select]
for i in range (int(diceNumber)):
result = random.randint(0,int(diceFaces))
input("Your roll is " + str(result) + ".")
Probably better to do this:

Code: [Select]
result=0
for i in range (int(diceNumber)):
result+=random.randint(0,int(diceFaces))
input("Your roll is " + str(result) + ".")

Welp, thanks. Fixing it asap and adding some more things to practice.
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8621 on: December 08, 2015, 04:56:32 am »

should be while !exit and then when the user press N you set exit = true

or you gonna get a stack overflow (too many calls)

kudos for using recursion tho!!
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8622 on: December 08, 2015, 07:09:18 am »

yeah, using recursion to just restart the same function causes memory to get cluttered, because you've got an ever-growing number of copies of the function still in memory - that's because when you call the next diceRoller, the current one hasn't ended yet, so you get multiple copies all with their own local data, and none of them get cleared until the moment you exit the final diceRoller.

Recursion is a powerful and compact notation, but it's very costly in terms of memory and speed. There's basically no situation where you're not better off just using a loop in terms of computer resources. But it could have readability advantages some times for code that you know won't get called very often, so saving in programmer time.
« Last Edit: December 08, 2015, 07:10:57 am by Reelya »
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8623 on: December 08, 2015, 11:56:00 am »

Hey is there a place on the web where (wanna-be) programmers could post snippets of their code so that other programmers examined the code in respect of how much clear, readable, efficient etc it is?
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #8624 on: December 08, 2015, 11:58:33 am »

I've always wanted such a place.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward
Pages: 1 ... 573 574 [575] 576 577 ... 796