Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 335 336 [337] 338 339 ... 796

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

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5040 on: September 26, 2013, 10:30:32 pm »

With perlin noise, you know what given seeds will generate so check that the seeds do indeed generate that. Inject the randomness somehow because otherwise tests will occasionally fail. That's bad and flaky tests are very annoying. I have a dislike for randomness in tests outside of monkey testing. A false positive is a bug, a false negative is a regular annoyance and time sink.

If you are ever refactoring/optimising the algorithm and the test breaks, it means you broke the algorithm.

So, brute force it by comparing two entire images, the result and the expected? But in order to generate the expected image to verify my perlin noise algorithm, I have to have a working perlin noise algorithm. That doesn't make any sense.

I think the best that can be done is test the individual functions that the algorithm uses. Perlin noise generally uses interpolation (I prefer cosine) and a pseudo RNG. I think I could test if an individual perlin value works (one pixel). It's just that I don't understand how testing an entire image would work. At least for automatic tests.

How often do you do manual tests? I remember trying them with something once, I forget what it was. It would open an image in a web browser, but that was too annoying to work with. Hmm, I think the best solution to this perlin noise problem I've brought up is to render a lot of images on screen and see if they look sane.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #5041 on: September 27, 2013, 07:01:46 am »

It depends what you want.

If you want something that generates values that have certain properties, you can inspect the result to make sure it has those properties (though I'd still insist on using a constant seed for the pRNG).

If you want, specifically, an implementation of the Perlin Noise algorithm, then it's perfectly reasonable to say "in these scenarios perlin noise does this, therefore I will test that the algorithm I have does that".
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5042 on: September 27, 2013, 12:49:31 pm »

I know what I have is horribly broken, but I have no idea how I would set up a system here that separates individual words, then tries to identify them as commands while still allowing those different words to affect how the command is played out (if that makes sense).

Code: [Select]
int getInputs(string strStartRead, string strEndRead) //changes variables based on HFC Input.dat
{
ifstream inf("HFC Input.dat");

//need to make upkeep function (population consuming food, etc)

while(true)
{
string strFindStart;
getline(inf, strFindStart);
if (strFindStart == strStartRead) break;
}
int nTemporaryPopulation = data["Population:"];
vector<string> tokens;
for(int iii = 0; iii < (sizeof tokens); ++iii)
{
string strCommand;
getline(inf, strCommand);
if (strCommand == strEndRead) break;
istringstream iss(strCommand);
copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter<vector<string> >(tokens));
int nX = 0;
if (nTemporaryPopulation < 1) break;
int y;
y = strToInt(tokens[nX + 1]);
nTemporaryPopulation = nTemporaryPopulation - y;

if (tokens[nX] == "build")
{
if (tokens[nX + 2] == "towns")
data["towns:"]= data["towns:"] + y;

if (tokens[nX + 2] == "farms")
data["farms:"] = data["farms:"] + y;

if (tokens[nX + 2] == "stables")
data["stables:"] = data["stables:"] + y;

if (tokens[nX + 2] == "naturepreserves")
data["nature preserves:"] = data["nature preserves:"] + y;

if (tokens[nX + 2] == "fisheries")
data["fisheries:"] = data["fisheries:"] + y;

if (tokens[nX + 2] == "mines")
data["mines:"] = data["mines:"] + y;

if (tokens[nX + 2] == "forges")
data["forges:"] = data["forges:"] + y;

if (tokens[nX + 2] == "subterraneanfarms")
data["subterranean farms:"] = data["subterranean farms:"] + y;

if (tokens[nX + 2] == "fortresses")
data["fortresses:"] = data["fortresses:"] + y;

if (tokens[nX + 2] == "graveyards")
data["graveyards:"] = data["graveyards:"] + y;

if (tokens[nX + 2] == "unicorn pens")
data["unicorn pens:"] = data["unicorn pens:"] + y;
}

if (tokens[nX] == "work")
{
if (tokens[nX + 2] == "towns")
{
if (strStartRead == "*Human Empire"|| strStartRead == "*Kobold Empire")
data["Population:"] = data["Population:"] + (2 * y);
else
data["Population:"] = data["Population:"] + y;
}

if (tokens[nX + 2] == "farms"|| tokens[nX + 2] == "fisheries"|| (tokens[nX + 2] == "subterranean" && tokens[nX + 3] == "farms"))
{
if (strStartRead == "*Halfling Empire")
data["food:"] = data["food:"] + (y * 6);
else
data["food:"] = data["food:"] + (y * 2);
}

if (tokens[nX + 2] == "stables")
data["horses:"] = data["horses:"] + y;

if (tokens[nX + 2] == "nature" && tokens[nX + 3] == "preserves")
data["food:"] = data["food:"] + (y * 2);

if (tokens[nX + 2] == "graveyards")
data["Population:"] = data["Population:"] - 1;
//+ 1 undead army

if (tokens[nX + 2] == "unicorn" && tokens[nX + 3] == "pens")
data["unicorns"] = data["unicorns"] + y;

//need to do a thing for mines
}

if (tokens[nX] == "smelt")
{
if (tokens[nX + 2] == "iron")
{
data["Iron Ore:"] = data["Iron Ore:"] - y;
data["Coal:"] = data["Coal:"] - y;
data["Iron Bars:"] = data["Iron Bars:"] + y;
}
if (tokens[nX + 2] == "steel")
{
data["Iron Bars"] = data["Iron Bars"] - (y * 2);
data["Coal:"] = data["Coal:"] - (y * 2);
data["Steel Bars:"] = data["Steel Bars:"] + y;
}
if (tokens[nX + 2] == "mithril")
{
data["Raw Mithril:"] = data["Raw Mithril"] - y;
data["Coal:"] = data["Coal:"] - (y * 10);
data["Mithril Bars:"] = data["Mithril Bars:"] + y;
}
}

if (tokens[nX] == "forge")
{
if (tokens[nX + 2] == "iron")
{
if (tokens[nX + 3] == "armor")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 5);
data["Iron Armor:"] = data["Iron Armor:"] + y;
}
if (tokens[nX + 3] == "weapons")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 3);
data["Iron Weapons:"] = data["Iron Weapons:"] + y;
}
}
if (tokens[nX + 2] == "steel")
{
if (tokens[nX + 3] == "armor")
{
data["Steel Bars:"] = data["Steel Bars:"] - (y * 5);
data["Steel Armor:"] = data["Steel Armor:"] + y;
}
if (tokens[nX + 3] == "weapons")
{
data["Steel Bars:"] = data["Steel Bars:"] - (y * 3);
data["Steel Weapons:"] = data["Steel Weapons:"] + y;
}
}
if (tokens[nX + 2] == "mithril")
{
if (tokens[nX + 3] == "armor")
{
data["Mithril Bars:"] = data["Mithril Bars:"] - (y * 5);
data["Mithril Armor:"] = data["Mithril Armor:"] + y;
}
if (tokens[nX + 3] == "weapons")
{
data["Mithril Bars:"] = data["Mithril Bars:"] - (y * 3);
data["Mithril Weapons:"] = data["Mithril Weapons:"] + y;
}
}
}
}
inf.close();
return 0;
}
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5043 on: September 27, 2013, 02:37:43 pm »

Code: [Select]
// Base command class
class Command {
public:
void execute(vector<string> & tokens, map<string, int> & data) = 0;
}

// Example command
class ForgeCommand: public Command {
public:
void execute(vector<string> & tokens, map<string, int> & data) {
if (tokens[2] == "iron")
{
if (tokens[3] == "armor")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 5);
data["Iron Armor:"] = data["Iron Armor:"] + y;
}
if (tokens[nX + 3] == "weapons")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 3);
data["Iron Weapons:"] = data["Iron Weapons:"] + y;
}
}
}
}

class CommandController {
public:
static void addCommand(string verb, Command * command) {
commands[verb] = command;
}
static void runCommand (vector<string> & tokens, map<string, int> & data) {
if (commands.find(tokens[0]) != commands.end()) {
commands[tokens[0]]->execute(tokens, data);
} else {
// Invalid token, do something about it here
}
}
private:
static map<string, Command*> commands;
}

Usage: When the program starts, run CommandController::addCommand() once for every command you have (for example CommandController::addCommand("forge", new ForgeCommand());

Then when you have a set of tokens that make a command, you just call CommandController::runCommand(tokens, data) instead of going through your crazy conditional structure.
Logged

Aklyon

  • Bay Watcher
  • Fate~
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5044 on: September 27, 2013, 09:46:54 pm »

Well, considering Roguebasin has both (among other things) a python roguelike tutorial (which links to a C++ one on the main tut page), I would say yes, as long as you think you know enough to start one. I've been following (for the most part) the python one.
Logged
Crystalline (SG)
Sigtext
Quote from: RedKing
It's known as the Oppai-Kaiju effect. The islands of Japan generate a sort anti-gravity field, which allows breasts to behave as if in microgravity. It's also what allows Godzilla and friends to become 50 stories tall, and lets ninjas run up the side of a skyscraper.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5045 on: September 27, 2013, 09:48:10 pm »

C++ is a good programming language for anything sufficiently complex. A roguelike definitely qualifies as complex enough.
Logged

lue

  • Bay Watcher
  • [PREFSTRING:missing right bracket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5046 on: September 27, 2013, 10:12:19 pm »

C++ is very good for rougelikes. (One of the most popular roguelikes, Nethack, is programmed in C, so doing something similar with C++'s features should definitely be no problem.)

Since you first went with Java, I'm going to take a wild guess and say you're programming with some sort of GUI in C++. If I'm wrong, please ignore the rest of this paragraph :) . So, looks like I guessed correctly :D . I would recommend getting away from doing GUI stuff and focus on doing commandline based programs for the time being. Trying to work with a GUI while learning programming is, I predict, not the best way to get in the hang of things. Especially if you're exploring something brand new ("today I want to get my head around polymorphism!" for example), it's best to stick to printing text to a terminal for the time being.

Since you're beginning with C++, I would strongly advise against reading C tutorials and articles unless/until you know the differences between what C and C++ offer, their respective strengths, and so on. As a quick example, where C prints stuff to the terminal with printf(), C++ would do the same with std::cout . There are of course times where you might want to/have to do things the way they're done in C, but in my opinion what works well in C++ is different from what works well in C. Stick to C++ resources.

Also, some quick C++ tips:
  • For the love of all that is good, do not use using namespace std; ever. Namespaces (such as the std namespace) exist to avoid name conflicts. While it's slightly less likely for another C++ library to conflict with names in std, it just a bad habit to make namespaces useless. You can, however, import certain names you use often, for example using std::cout;. This means you can type cout instead of std::cout, but you still have to put std:: in font of other names. Details here.
  • If you click on the link in the above bullet point, that C++ FAQ is a wonderful thing to keep handy. :)

If you have anything more specific you need help on, I'm sure we'll be happy to help.
Logged
Post not guaranteed accurate or pristine for all of time.
Sigtext. Enticing, yes? If you do not know where things I have "sigged" go, this page will explain.

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5047 on: September 28, 2013, 12:52:08 am »

So I've finally decided to learn myself some Java (because, you know, college, and being a well-rounded individual and no time like the present all that jazz), and I'd like to find a good tutorial. My only experience was two trimesters of Java programming with Karel at the age of 13-14, and I didn't "get it" then, but now that I'm the ripe old age of 18 I'm feeling the urge to yell at some misplaced semicolons to get off my lawn. I might as well be a beginner. Does anyone have any suggestions?

[EDIT: Update on the situation: apparently there's no particular reason to start with Java...I'd be equally happy with Python or C++.]

[Update to the update on the situation: it looks like I'm going for Python. Google led me to Codeacademy, but would anything else be better?]

Update to the update to the update: how do I compile in Notepad++?
« Last Edit: September 28, 2013, 01:46:55 am by FearfulJesuit »
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5048 on: September 28, 2013, 01:09:40 am »

I am pondering what to do to help in a college-bound portfolio :v Other than the game I'm working on, that xD
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

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5049 on: September 28, 2013, 07:49:36 am »

Update to the update to the update: how do I compile in Notepad++?
You need a Python interpreter (Notepad++ doesn't come with one) which you need to run on the code file. Install one of those, then name your file "something.py" and make sure that its first line is "#!python". Then you should be able to double click the file to run it.
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5050 on: September 28, 2013, 07:52:40 am »

Just use IDLE. Should have come with your Python download.
Logged

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5051 on: September 28, 2013, 12:30:01 pm »

Update to the update to the update: how do I compile in Notepad++?
You need a Python interpreter (Notepad++ doesn't come with one) which you need to run on the code file. Install one of those, then name your file "something.py" and make sure that its first line is "#!python". Then you should be able to double click the file to run it.

OK, I've downloaded Python 3.3.2...and when I click on my .py file (with that as the first line-but isn't it a comment?) the command prompt opens up for the smallest fraction of a second, barely enough to show the note "SyntaxError: Invalid Syntax." For the record, this is just the Hello, World! program, so the only line in it is

print "Hello, World!"

(aside from the aforementioned #!python and another comment.)
« Last Edit: September 28, 2013, 12:32:02 pm by FearfulJesuit »
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5052 on: September 28, 2013, 12:53:34 pm »

Python is incompatible with itself, sort of.

In python 2 you do
Code: [Select]
print "Hello, World!"while in python 3 you do
Code: [Select]
print("Hello, World!")
Logged

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5053 on: September 28, 2013, 01:04:57 pm »

Python is incompatible with itself, sort of.

In python 2 you do
Code: [Select]
print "Hello, World!"while in python 3 you do
Code: [Select]
print("Hello, World!")

That's not really the problem...it's that I have no idea how to turn my .py files into .exes from Notepad++, really, so I can debug and goof around with them. I installed the plugin NppExec, and I don't really understand it.
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

gigaraptor487

  • Bay Watcher
  • Escaped Lunatic now civilised
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5054 on: September 28, 2013, 01:21:49 pm »

There is a package called Pyinstaller which can turn your Python files into Executables, i havent used it in about 8 months though.
Logged
Hehe, you thought there would be an interesting sig here

I now run a program which brings old multiplayer games back to life : click
Pages: 1 ... 335 336 [337] 338 339 ... 796