Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 442 443 [444] 445 446 ... 796

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

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6645 on: November 19, 2014, 03:41:05 am »

Class wants me to script in perl.

For a "higher level" language,

Importing self-made modules is BULLSHIT.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6647 on: November 19, 2014, 04:31:27 am »

Class wants me to script in perl.

For a "higher level" language,

Importing self-made modules is BULLSHIT.

How so?
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

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6648 on: November 19, 2014, 04:42:25 am »

That really doesn't seem like a language you'd want to use for a timed competition.

Why not?

Prolog is actually a pretty powerful and nice language when you learn to stop forcing imperative programming practices on it.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6649 on: November 19, 2014, 04:44:30 am »

I looked that up because Putman mentioned it and... uh.  I can see why programming a game in that might be difficult.  It looks like a whole different mindset; I'm still not sure how it could be useful.  Too damn much homework tonight to wrap my head around it.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6650 on: November 19, 2014, 04:49:29 am »

$perl
Except when it's @perl.

How so?

So for C and C++ you just go

Code: [Select]
#include <stdlib.h>
#include "file.h"
or, at worst,
Code: [Select]
#include "/path/to/file.h"
in the .c or .cpp file you use right? Works for ruby, too.

For perl, it's just

Code: [Select]
use Foo;

But you see, to use the module, you gotta be all (and this is the whole sample module mind you)
Code: [Select]
package Foo;
use strict;
use warnings;
use Exporter;

our @ISA= qw( Exporter );

# these CAN be exported.
our @EXPORT_OK = qw( export_me export_me_too );

# these are exported by default.
our @EXPORT = qw( export_me );

sub export_me {
    # stuff
}

sub export_me_too {
    # stuff
}

1;

And "use Foo" would only give you export_me.
To get export_me_too, you gotta be all
Code: [Select]
use Foo qw( export_me export_me_too );

Fucking hell perl. And god forbid your .pm (perl module) file is in a different directory, then it's a whole new level of bull.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6651 on: November 19, 2014, 04:54:54 am »

That ... is very bad. :|

I like Python's way, you do import <filename in same directory excluding the .py> and all of the functions and variables in the file are put into the "filename" namespace.
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

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6652 on: November 19, 2014, 05:34:14 am »

And then you can place packages in folders. Make a folder, put a file called __init.py__ in there, then you can do import <folder name> and all files in that folder will be put into the "folder name" namespace, with any files in the folder basically following the same import logic (E.G variable baz in file bar in folder foo would be accessed with foo.bar.baz after "foo" is imported)

BlindKitty

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6653 on: November 19, 2014, 05:35:34 am »

Well, I've got to use it for my A-level project, and I'm too far through it to even attempt to switch to a new language, let alone learn a new one.

2000+ lines of code and counting.

Having said that, I am intending to learn c#.

I would say that learning C# is a very good idea for starters. With opened .NET platform it might be mighty useful soon, but more importantly, it is relatively easy to set up the env and start programming, if you are on Windows. It is also quite beginner friendly, has good documentation, and is rather fast.
What is more, telling someone to learn C/C++ at the start is like telling them to learn mathematical analysis first, and the easy stuff like multiplication will give then no trouble at all later. There is always time to learn C/C++ if you need it; C# is much better. Of course, it is not the only good choice for beginner (Python comes to mind, especially if you are developing on Linux), but it is one of the better ones, I think. Also, Java seems terrible from what I know about it (it's not very much, true). Building anything major written in Java seems kinda painful.
Logged
My little roguelike craft-centered game thread. Check it out.

GENERATION 10: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6654 on: November 19, 2014, 05:37:29 am »

And I can't recommend ruby too much. As a scripting language and an alternative to perl, it just makes so much more sense.

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6655 on: November 19, 2014, 08:07:05 am »

I looked that up because Putman mentioned it and... uh.  I can see why programming a game in that might be difficult.  It looks like a whole different mindset; I'm still not sure how it could be useful.  Too damn much homework tonight to wrap my head around it.

Prolog wouldnt be so great for the low level game stuff, like graphics rendering, networking, input/output etc. Prolog would excel at game logic though, particularly rule-heavy games like turn based strategies, TCG's, etc. Prolog's function-like strucures are called "rules" for a reason :P.

It requires you to think very differently about how you program. For example, a "contains" function that takes 2 arguments, a list A, and an element B, that returns true if A is in B.

If you are doing it in an imperative style, you think "go through each element in A and see if its the same as B, if so return true."

Code: [Select]
foreach(var I in A)
    if (B == I) return true;
return false;

But for something like prolog you think "If the statement 'B is contained in A' is true, either the first element of A is B, or B is contained in the rest of A".

Code: [Select]
contains([AHead|ARest], AHead).
contains([AHead|ARest], B) :- contains(ARest, B).

%calling it like so in the interperator, results in true
contains([1,2,3,4], 3).

The thing that makes the language real interesting is the fact that it is a First Order Logic solver. For the above example, if you pass an "uninitialised" variable as one of the arguments, it tries to work out what values could make it true. If you call it like

contains ([1,2,3,4], Value).

It will deduce that value must be either 1, 2, 3 or 4. Or

contains(List, 4).

It will work out every single possible list that could contain the number 4, and tells you. This is a lot of lists (an infinite ammount, actually).
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6656 on: November 19, 2014, 09:08:34 am »

Code: [Select]
contains([AHead|ARest], AHead).
contains([AHead|ARest], B) :- contains(ARest, B).

This is the standard functional approach to doing anything on a list, and can be exceedingly slow if either the interpreter doesn't support list slices or you do something that means it can't use slicing, as you potentially create a new N-1 sized list for every iteration, aka O(N!) complexity O.o

C++ variadic templates (this is a whole extra level of C++ you want to avoid if you're new) are pretty much pure functional (but operate solely at compile time), and look at this: http://ideone.com/6A83T6
Code: [Select]
template<int i_find, int... i_rest>
struct contains;

template<int i_find>
struct contains<i_find> : std::false_type {};

template<int i_find, int... i_rest>
struct contains<i_find, i_find, i_rest...> : std::true_type {};

template<int i_find, int i_head, int... i_rest>
struct contains<i_find, i_head, i_rest...> : contains<i_find, i_rest...> {};

The 1st one defines contains as a template that takes an integer to find and a 0+ element "list" of integers, but doesn't define it to do anything.
The 2nd one defines our "false" case: an element to find but an empty list
The 3rd is our "true" case: the element to find and the first element of the list are the same
The 4th is our recursion: contains with an element to find that doesn't match the list head is the same as contains with the element to find and the rest of the list

Note that after compilation, this is a compile-time true/false, which is always awesome.

Just another example that C++ is in fact all programming styles at once.
« Last Edit: November 19, 2014, 09:12:46 am by Thief^ »
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6657 on: November 19, 2014, 05:24:43 pm »

<stuff against Perl>

Okay, first thing you're doing wrong is hating on Perl before being really good at Perl, or at least having asked a person who is really good at Perl. Here's a really simple import script:

Code: (Foo.pm) [Select]
package Foo;

sub bar {...}

1;
Code: (main.pl) [Select]
use Foo;

Foo::bar(42);

That's literally all you need. "package Foo;" selects the namespace, and since the main program's code isn't written in the Foo namespace, you need to call Foo::bar instead of just bar. Simple as that.
Aaactually, it gets even simpler if you prefer not to use namespaces and want to do everything in the main namespace (horrible coding practice tho):

Code: (Foo.pm) [Select]
sub bar {...}

1;
Code: (main.pl) [Select]
use Foo;

bar(42);

Now here's the deal about namespaces: What Perl doesn't have is a "public" keyword for subroutines. And there is no default "using namespace" or "import" method to clutter your namespace. There are good reasons for this:
First of all, Perl actually allows you to redefine subroutines at runtime. This is because subroutine names (such as &foo) are technically also variables and you can overwrite them at any time (note that assigning to subroutines has different syntax than assigning to other variables). So defining them as "public" would just make no sense.
But before you complain that they shouldn't be variables: Perl's import mechanism allows a module to give its user fine control over which variables and methods should be thrown into the user's namespace (by overwriting the appropriate subroutines with the module's own). Note that this functionality needs to be defined by the module itself, which is a bit of extra work for the module author.
But before you complain that it shouldn't be like that: Consider that there are usually many more people that use a module than people who write it. And consider that it is very awesome to just be able to write "Hey, I'd like to use this module and I'd like to import this set of functions and that set, but not that one" using a single uncluttered readable line. And that is actually feasible in a Perl module, because Perl is awesome like that. You can of course abuse this functionality for many other cool tricks and improvements of user comfortability, but that is not in the scope of this rant.
If you're worrying that writing that functionality is hard, then that's what the Exporter module is there for, it is a part of the standard Perl library and basically does all the work for you, you just need to tell it which methods to export, and you do that using @EXPORT, and if you want to allow individual things to be exported on demand, you do that using @EXPORT_OK, and if you want to group things into sets which can be exported all at once, you use %EXPORT_TAGS. That's literally all you have to do.

Finally, if you need to import modules from other directories, you really just need to write this:
Code: [Select]
use lib "path/to/lib";
use Foo;

And if you are really really bothered by having to list your subroutines manually in the @EXPORT variables, I made some "public"-style keywords for you so you don't need to do that. Just put this at the start of your module:
Code: [Select]
package YOURPACKAGEHERE;
use parent Exporter;
our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS) = ();

sub public {
  no strict "refs";
  my ($n, $s) = @_;
  *{$n} = $s;
  push @EXPORT, $n;
  push @EXPORT_OK, $n;
  return $n;
}

sub optional {
  no strict "refs";
  my ($n, $s) = @_;
  *{$n} = $s;
  push @EXPORT_OK, $n;
  return $n;
}

sub group {
  my ($g, @n) = @_;
  push @{$EXPORT_TAGS{$g}}, @n;
  return @n;
}

Then you can define your subroutines like this:
Code: [Select]
# Will always be exported
public bar => sub {
  ...stuff...
  ...things...
}; ##Don't forget a semicolon here

# Will be exported if required
optional baz => sub {
  ...thangs...
};

# Add subroutines to a group to be able to import all subroutines of the same group using "use Foo qw(:features1)":
group features1 => qw(bar baz quux);

# Or you can directly add subroutines to a group right when the subroutine is defined:
group features1 => public quux => sub {
  ...justquuxthings...
};

TL;DR: Perl is literally the opposite of a boilerplate language, focusing on usability more than anything else. If you're doing something and it's taking more code than in other languages, at least one of the following options is true:
1) You are experiencing the small downside to a well-placed tradeoff of some simplicity for much more flexibility. In that case, there exists either a standard library module that restores the simplicity, or at least fifty CPAN modules doing the same.
2) You are writing a public module using all the good coding practices (Perl needs lots of those because it's really easy to accidentally write perfectly unreadable code).
3) There are many many better ways to do what you're doing than how you're doing it right now.
« Last Edit: November 19, 2014, 05:27:15 pm by MagmaMcFry »
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6658 on: November 19, 2014, 05:28:46 pm »

Aaactually, it gets even simpler if you prefer not to use namespaces and want to do everything in the main namespace (horrible coding practice tho):

pls don't do this ever in any situation

i see python tutorials for things and they do this so much

i cry

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6659 on: November 19, 2014, 09:27:51 pm »



That c++ example won't work backwards, though, you can't use it to determine what values make that rule true.

For example if you write a pathfinder, you can use that to get a path from A to B, get all paths from A to B, get all paths from A to any node, get all paths from any node to B, get all possible paths between all nodes, or get the start and end node of a specified path (which probably isn't that useful, but it will still do it), Without writing a single line of extra code.

If you create some rules to validate a player's move in a game, that same rule can be used to get potential moves for an AI, for example.

I am pretty sure that example won't work on runtime lists too, making the use cases for it very different.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!
Pages: 1 ... 442 443 [444] 445 446 ... 796