Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 240 241 [242] 243 244 ... 796

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

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3615 on: December 20, 2012, 09:41:22 am »

Code: (Pseudocode) [Select]
Color interpolateColor(Color[] colorList, float[] weightList, int numColors) {
  float totalWeight = 0f;
  for (int i = 0..numColors-1) {
    totalWeight += weightList[i];
  }
  Color returnColor = new Color(0f, 0f, 0f);
  if (totalWeight == 0f) return returnColor;
  for (int i = 0..numColors-1) {
    returnColor.r += colorList[i].r * weightList[i];
    returnColor.g += colorList[i].g * weightList[i];
    returnColor.b += colorList[i].b * weightList[i];
  }
  returnColor.r /= totalWeight;
  returnColor.g /= totalWeight;
  returnColor.b /= totalWeight;
  return returnColor;
}
Logged

DrKillPatient

  • Bay Watcher
  • The yak falls infinitely
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3616 on: December 22, 2012, 12:05:11 am »

I'm trying to write a program in C++ that reads external data files for monsters in a roguelike, formatted like so...

Code: [Select]
wolf
    desc = A slavering wolf. Beware!
    health = 100
    attacks
        bites = 5-10 pierce
        claws = 2-5 slash

I have some idea as to how I'd organize this, that is, with a series of nested key/value pairs, where the value can either be a string or another (list of) key/value pairs. But I don't know exactly how I'd implement this in C++. I'd like the result to be that I can just call some kind of 'fromFile' function and return a list of such value trees (e.g. "wolf", "troll", "goblin"), then step through them one by one and query values like "health". Another problem is how to return a list like "attacks", though. (Maybe there's a better way of doing this.)

Any ideas? I'm stumped entirely with this one.
« Last Edit: December 22, 2012, 12:21:16 am by DrKillPatient »
Logged
"Frankly, if you're hanging out with people who tell you to use v.begin() instead of &v[0], you need to rethink your social circle."
    Scott Meyers, Effective STL

I've written bash scripts to make using DF easier under Linux!

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3617 on: December 22, 2012, 01:25:47 am »

Quick attempt at a solution:

Code: [Select]
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <cstdlib>

using namespace std;

class Attack {
string name;
int minDamage, maxDamage;
string type;
public:
Attack(string n, int mind, int maxd, string t) : name(n), minDamage(mind), maxDamage(maxd) type(t) {}
string getName() { return name; }
int getMinDamage() { return minDamage; }
int getMaxDamage() { return maxDamage; }
string getType() { return type; }
};

class Monster {
string desc;
int health;
map<string, Attack> attacks;
public:
Monster(string d, int h, map<string, Attack> a) : desc(d), health(h), attacks(a) {}
string getDesc() { return desc; }
Attack getAttack(string s) { return attacks.at(s); }
};

map<string, Monster> monsters;

/*
wolf
    desc = A slavering wolf. Beware!
    health = 100
    attacks
        bites = 5-10 pierce
        claws = 2-5 slash
*/

void processMonster(stringstream data) {
string name, desc, healthstr;
int health;
map<string, Attack> attacks;
getline(data, name);
getline(data, desc);
desc = desc.substr(desc.find(" = "+4));
getline(data, healthstr);
healthstr = healthstr.substr(healthstr.find(" = "+4));
health = atoi(healthstr.c_str());
data.getline(); // discard the "attacks" line
string temp;
while(getline(data, temp)) {
string atkname = temp.substr(0, temp.find(" = "));
string dmg = temp.substr(temp.find(" = ")+4, temp.find(" ", temp.find(" = ")+4));
string type = temp.substr(temp.find(" ", temp.find(" = ")+4)+1);
int mindmg, maxdmg;
if(dmg.find("-") != string::npos) { // variable damage
mindmg = atoi(dmg.substr(0, dmg.find("-")).c_str());
maxdmg = atoi(dmg.substr(dmg.find("-")+1).c_str());
}
else { // static damage
mindmg = maxdmg = atoi(dmg.c_str());
}
attacks[atkname] = Attack(atkname, mindmg, maxdmg, type);
}
monsters[name] = Monster(name, desc, health, attacks);
}

void readFile(string fname) {
string line;
ifstream f(fname);
stringstream lines;
bool newMonsterFlag = true;
while(getline(f, line)) {
if(line.at(0) != " " && line.at(0) != "\t") {
processMonster(lines);
lines.str("");
}
lines << line << endl;
}
}

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3618 on: December 22, 2012, 01:54:07 am »

I'd suggest doing it like so:
Code: [Select]
#include <map>
#include <vector>
using namespace std;

enum ATTRIBUTES {HP = 1, MP, INITIATIVE /*so on so forth*/ }

struct attackInfo
{
string name;
int min;
int max;
};

class monster
{
/*other values */

string monsterName;
string description;
map<int, double> attributes;
vector<attackInfo> attacks;

/* other stuff */
};

So when you read things from the file...

Code: (file) [Select]
wolf
    desc = A slavering wolf. Beware!
    health = 100
    attacks
        bites = 5-10 pierce
        claws = 2-5 slash

you would put "wolf" in the monster::monsterName, put everything after the desc = into monster::description, put everything that comes after desc but isn't attacks into the attributes map (The format would be attributes.insert(pair<int, double>(ENUMNAME, VALUE);, where the ENUMNAME is the enumerated name in the ATTRIBUTES enum. For example. attributes.insert(pair<int, double>(HP, 100); would make the monster have an HP of 100.). Put everything after "attacks" into a attackInfo variable, which is pushed onto monster.attacks. The attackInfo struct has minimum, maximum, and the attack name. o_O

Aaand I think I made the paragraph really complicated >_< Sorry. I guess I'm not good at explaining my ideas ;_; The "monster" struct would be to make a instance of a monster that can be used. If you put it in a map that's part of a constant class, it could be used as a lookup table.
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

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3619 on: December 22, 2012, 02:01:48 am »

I think Java has ruined me to the point where I can't effectively code using anything except OOP. Curses be to you, Sun/Oracle/whoever the hell you are.

Also Skyrunner, programming languages and traditional languages don't mix very cleanly.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3620 on: December 22, 2012, 02:28:54 am »

What's bad about OOP?

Also, do you have any comments on my code? >.> I'm actually using something similar to that in my own simulator: a single generic class, where each object is differentiated from others solely by its attributes. That means that instead of having a superclass "Plant", from where "Base Herb", "Snowberry bush", and "Apple" inherit from, I have a single class "Herb", which has various variables which include things like "Name", "Growth Period", "Effect" ...
If it's a bad method, I should try and change it early on than later. I do think it's a sort of Object Factory design pattern.
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 #3621 on: December 22, 2012, 04:00:19 pm »

I think Mego is complaining about inability to write programs that aren't OOP, as opposed to complaining about OOP itself.

DrKillPatient

  • Bay Watcher
  • The yak falls infinitely
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3622 on: December 22, 2012, 04:33:05 pm »

I've found some source code that's similar to what I want to do, but it's in C# and I think I'm missing something when looking at the code. I've tried to write a simple version of the IndentationTree class with the Parse function in C++, but it doesn't seem to be working in the same way that the C# version does (only the root node is returned, totally empty). My understanding of the C# version is that it will take a series of lines, create a root node, and for each indent level, add those lines to the root node as new nodes, subnodes under those, etc. Does anyone here know enough C# to see what I'm misunderstanding?

https://bitbucket.org/munificent/bramble/src/5e40302278e4/Bramble.Core/IndentationTree.cs?at=default

EDIT: Here's my C++ version:

Spoiler (click to show/hide)
« Last Edit: December 22, 2012, 04:37:01 pm by DrKillPatient »
Logged
"Frankly, if you're hanging out with people who tell you to use v.begin() instead of &v[0], you need to rethink your social circle."
    Scott Meyers, Effective STL

I've written bash scripts to make using DF easier under Linux!

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3623 on: December 22, 2012, 07:04:34 pm »

Code: [Select]
IndentationTree root = IndentationTree(-1, "");Your root is local and is destroyed as the function returns.

Either have the function return a pointer to an IndentationTree
Code: [Select]
IndentationTree *root = new IndentationTree(-1, "");or have the function take a pointer or reference to an IndentationTree in its parameter list instead of returning it. In either case the Parse function is probably meant to be static (either that or rewrite it to be recursive).

Edit: Also, since your stack (oh, and don't name an object the same as a class) works on IndentationTree objects, the root is copied as you push it on the stack and isn't the same as the one you return.
« Last Edit: December 22, 2012, 07:08:12 pm by olemars »
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3624 on: December 22, 2012, 08:20:45 pm »

I think Mego is complaining about inability to write programs that aren't OOP, as opposed to complaining about OOP itself.

This. OOP is a wonderful design, but isn't necessary for everything. Unfortunately, when learning Java, it gets drilled into your head to the point that structural and procedural programming fall out.

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #3625 on: December 23, 2012, 08:36:09 am »

Of course a lot of OOP code has the problem of overusing inheritance. One of the reasons the Java standard library sucks from a design perspective is the flagrant abuse of inheritance in situations where it would be much cleaner to use composition.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3626 on: December 25, 2012, 12:29:54 am »

Okay, so that I stop throwing code at the wall and seeing what sticks, I'm just going to ask here:

How would I set up a program, preferably with a for loop, so that it would take a chunk of text, copy that, replace all instances of a certain word with a different one, then do that multiple times with a different word each time.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3627 on: December 25, 2012, 12:37:36 am »

Depends. What language are you using?
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3628 on: December 25, 2012, 12:42:35 am »

C++.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3629 on: December 25, 2012, 01:24:11 am »

Combining string::replace and string::find will probably be your best bet, if you're only using the standard library.
Pages: 1 ... 240 241 [242] 243 244 ... 796