• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Ambitious

Member
I'm working on a project in a team of five for uni this semester. It's a middleware for managing files stored at multiple cloud storage providers, like Dropbox. In particular, we have to support three specific providers and implement RAID1/RAID5 logic. Users should be presented with a simple file management UI including selection of the RAID mode.

So, just before the Christmas holidays, one of the other team members volunteered to implement RAID 5. Two weeks ago, I noticed a commit indicating he was finished. Cool. While I'm of course aware of how RAID 5 works in general (I'm using it myself with my NAS), I didn't know how it works in detail - like how exactly files are split up, how parity data is calculated, how files can be recovered and so on. So, curiously, I had a look at his source code and realized that guy had no clue either.

His code splits files into two halves. One half is uploaded to the first provider, the second half to the second one, and the complete file is uploaded to the third one. The information of which part is which is encoded in the file name: originalName_1, originalName_2, originalName_3. If the file at one of the first two providers is missing or corrupted, the code returns the complete file from the third one. On the other hand, if the file stored with the third provider is missing or corrupted, it concatenates the data from providers 1 and 2.

And all of this is hardcoded. He literally creates an array of size 3 and assigns the parts to data[0], data[1] and data[2]. I can't fucking believe it.
The part where he uploads the parts is a simple loop, iterating through the provider list. Imagine something like providers.upload(fileNames + "_" + i, data). Which means if you set up more than three accounts, the data array is accessed beyond its upper bound. The language is JavaScript, so there's no exception or crash. It returns "undefined".

So if you've got more than three accounts (in other words: if you've got more than three hard disks), only the files in the first three will contain actual data. All files in all accounts starting with the fourth will contain "undefined". Fucking great RAID logic.

Funny enough, the assignment descriptions says the UI needs to show how parity information is distributed. I wonder what he's going to do when he sees that line. If he ever sees that line. I don't think he has ever read the assignment description.

For instance, the description clearly says we also need to support version management for both RAID modes. The way I see it, we should write a version manager class which is supplied with one of the two raid controllers. Three layers: Version Manager --uses--> (one) Raid Controller --uses--> (many) Cloud Storage Connector

But no, that fucking guy implemented version management directly in the RAID 5 controller. Only in the RAID 5 controller. What a goddamn mess.
 
I'm working on a project in a team of five for uni this semester. It's a middleware for managing files stored at multiple cloud storage providers, like Dropbox. In particular, we have to support three specific providers and implement RAID1/RAID5 logic. Users should be presented with a simple file management UI including selection of the RAID mode.

So, just before the Christmas holidays, one of the other team members volunteered to implement RAID 5. Two weeks ago, I noticed a commit indicating he was finished. Cool. While I'm of course aware of how RAID 5 works in general (I'm using it myself with my NAS), I didn't know how it works in detail - like how exactly files are split up, how parity data is calculated, how files can be recovered and so on. So, curiously, I had a look at his source code and realized that guy had no clue either.

His code splits files into two halves. One half is uploaded to the first provider, the second half to the second one, and the complete file is uploaded to the third one. The information of which part is which is encoded in the file name: originalName_1, originalName_2, originalName_3. If the file at one of the first two providers is missing or corrupted, the code returns the complete file from the third one. On the other hand, if the file stored with the third provider is missing or corrupted, it concatenates the data from providers 1 and 2.

And all of this is hardcoded. He literally creates an array of size 3 and assigns the parts to data[0], data[1] and data[2]. I can't fucking believe it.
The part where he uploads the parts is a simple loop, iterating through the provider list. Imagine something like providers.upload(fileNames + "_" + i, data). Which means if you set up more than three accounts, the data array is accessed beyond its upper bound. The language is JavaScript, so there's no exception or crash. It returns "undefined".

So if you've got more than three accounts (in other words: if you've got more than three hard disks), only the files in the first three will contain actual data. All files in all accounts starting with the fourth will contain "undefined". Fucking great RAID logic.

Funny enough, the assignment descriptions says the UI needs to show how parity information is distributed. I wonder what he's going to do when he sees that line. If he ever sees that line. I don't think he has ever read the assignment description.

For instance, the description clearly says we also need to support version management for both RAID modes. The way I see it, we should write a version manager class which is supplied with one of the two raid controllers. Three layers: Version Manager --uses--> (one) Raid Controller --uses--> (many) Cloud Storage Connector

But no, that fucking guy implemented version management directly in the RAID 5 controller. Only in the RAID 5 controller. What a goddamn mess.


Got to love group work. I just about managed to drag my group to a good mark in one of my software engineering assignment last year. Did well to get 80+ considering.
 

Kyuur

Member
There's lots of little quirks with Intellisense when you go digging around in the armpits of C++ like this. Nested classes is another big weak point.

So it seems. It's alright though, it sort of got me thinking about how my classes were structured and I was able to make something much more streamlined anyways!
 
Hey so I posted this in OT but didn't get the info I was looking for regarded my programming questions, perhaps some of you can offer insight?
Here's the relevant excerpt:

Also, when I was younger I was pretty interested in programming. I used to be pretty decent but that was almost 5 years ago and due to the extreme time commitment sports required I was unable to pursue it further. I was fortunate enough to take a course in university for distribution but that was pretty basic and I breezed through it. I'm pretty jealous of some of my friends who now have degrees in that field given how lucrative the industry has become (not to mention that it's interesting).
Which languages are "in" nowadays?
Which areas of programming are the most lucrative or require the most skill?
If you had an abundance of time and were able to specialize in a language, which would you choose and which areas?
One of my friends who is quite deft at programming has a job lined up at a big company when he graduates that uses C# and visual basic, (and html/javascript/css for web development), is that indicative of the industry as a whole?
For example, which languages and areas do the new hires at Google use?

Any feedback is appreciated.
 

tokkun

Member
Hey so I posted this in OT but didn't get the info I was looking for regarded my programming questions, perhaps some of you can offer insight?
Here's the relevant excerpt:



Any feedback is appreciated.

I can answer your question about Google. In approximately descending order of importance:

C++ is used for the majority of services, backend, and infrastructure.
Java is used for much of Android.
Javascript for web interface elements.
Python is used for some scripting.
Go is used sparingly for some tasks that might otherwise be done with C++.

Before you decide that means you should run off and study C++, take heed. Google's development architecture is a product of its time, size, and organizational structure. Newer companies and startups are more likely to use more modern languages that allow for faster prototyping.
 

NotBacon

Member
Why is my intro to Java course focusing primarily on bitwise operators?

Bitwise operations are very good to know, especially if you end up taking more C/Assembly classes later on, but you guys shouldn't dwell on it for over a week in an intro class...
 

GK86

Homeland Security Fail
Is anyone good with web scrapers? I'm learning how to create/use them for my internship. I started with using Google doc's to grab url's.

I have a question. I'm testing out and learning by playing around on Craig list. I did a random search for furniture. I'm trying to grab the url's from the different listings. My problem is in the source code on Craig list, it lists the URLs starting from "/mhn/837737" instead of "Craigslist.com/mhn/8377737". So when the URLs are listed on the Google docs, it lists the former. Is there a way to force Google docs to list the latter?

If anyone has any good tutorials for web scraping, I would appreciate.

Thanks.
 
Why is my intro to Java course focusing primarily on bitwise operators?
As someone said, it's good to know. Let me ask you a simple question. How would you know if a number is a power of two? There are multiple ways to do this, some even requiring multiple lines. But with a simple bitwise operator, it's only one line a code and it's beautiful:

Code:
bool powerOfTwo(int check){
	return ((check != 0) && !(check & (check - 1)));
}
Explanation: Say check is 4: it's represented by: 0100. check-1 is then: 0011. Do an & operation, and it should be all zeroes. If it's not all zeroes, it's not a power of two.
For a more detail explanation, refer to this: http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2/

Bitwise operators are awesome. I sometimes use the bitwise inclusive OR, which is this symbol in Java: "|=". But you usually go more in depth in this in your Architecture/ C & Assembly class.
 
As someone said, it's good to know. Let me ask you a simple question. How would you know if a number is a power of two? There are multiple ways to do this, some even requiring multiple lines. But with a simple bitwise operator, it's only one line a code and it's beautiful:

Code:
bool powerOfTwo(int check){
	return ((check != 0) && !(check & (check - 1)));
}

Code:
bool powerOfTwo(int check) {
    return __popcnt(check) == 1;
}

I'll show myself out.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Is anyone good with web scrapers? I'm learning how to create/use them for my internship. I started with using Google doc's to grab url's.

I have a question. I'm testing out and learning by playing around on Craig list. I did a random search for furniture. I'm trying to grab the url's from the different listings. My problem is in the source code on Craig list, it lists the URLs starting from "/mhn/837737" instead of "Craigslist.com/mhn/8377737". So when the URLs are listed on the Google docs, it lists the former. Is there a way to force Google docs to list the latter?

If anyone has any good tutorials for web scraping, I would appreciate.

Thanks.

Can you just run your output through a function to prepend "Craigslist.com" onto the front?

Also I'm fairly certain that Craigslist is super against scraping, so if this is anything outside of a learning project keep that in mind.
 

GK86

Homeland Security Fail
Can you just run your output through a function to prepend "Craigslist.com" onto the front?

Also I'm fairly certain that Craigslist is super against scraping, so if this is anything outside of a learning project keep that in mind.

I honestly wouldn't know. I started learning this morning. I read Google docs would be the easiest starting point. I plan to try with Python when I try to pick that language up. I will look into it though.

I picked CL since a video tutorial used it as an example. Would make sense since in the video, they were able to grab the whole URL (video is 3 years old).

Thank you.
 
Is anyone good with web scrapers? I'm learning how to create/use them for my internship. I started with using Google doc's to grab url's.

I have a question. I'm testing out and learning by playing around on Craig list. I did a random search for furniture. I'm trying to grab the url's from the different listings. My problem is in the source code on Craig list, it lists the URLs starting from "/mhn/837737" instead of "Craigslist.com/mhn/8377737". So when the URLs are listed on the Google docs, it lists the former. Is there a way to force Google docs to list the latter?

If anyone has any good tutorials for web scraping, I would appreciate.

Thanks.

Chances are the google docs stuff can be downloaded as CSV so why don't you just grab it and use some simple shell scripts to do the rest?
 

Onemic

Member
As someone said, it's good to know. Let me ask you a simple question. How would you know if a number is a power of two? There are multiple ways to do this, some even requiring multiple lines. But with a simple bitwise operator, it's only one line a code and it's beautiful:

Code:
bool powerOfTwo(int check){
	return ((check != 0) && !(check & (check - 1)));
}
Explanation: Say check is 4: it's represented by: 0100. check-1 is then: 0011. Do an & operation, and it should be all zeroes. If it's not all zeroes, it's not a power of two.
For a more detail explanation, refer to this: http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2/

Bitwise operators are awesome. I sometimes use the bitwise inclusive OR, which is this symbol in Java: "|=". But you usually go more in depth in this in your Architecture/ C & Assembly class.

Didn't know that, Interesting. Guess I gotta bite the bullet.
 

GK86

Homeland Security Fail
Chances are the google docs stuff can be downloaded as CSV so why don't you just grab it and use some simple shell scripts to do the rest?

Sage advice. Web scraping should be the last resort. In this case there is an official API allowing sheets to be read directly.

https://developers.google.com/google-apps/spreadsheets/

Thank you, guys! It is the company that wanted to do web scraping. I don't know what methods they tried before this. But web scraping is their current project, which I joined yesterday.
 
I am having problems calling overloaded constructor, can someone tell what i am doing wrong?

Code:
public: Banner(){ 
	cout << "Banner's Constructor" << endl;
	favoriteProgram = "C++ ";
}

public: Banner(string param1) {
	cout << "Banner's Overloaded Constructor" << endl;
	favoriteProgram = param1;
}

public: Banner(string param2) {
	cout << "Banner's Overloaded Constructor 2 " << endl;
	favoriteProgram = param2;

I get this error:
Error 1 error C2535: 'Banner::Banner(std::string)' : member function already defined or declared

I can call one overloaded constructor, but not more than 1. Please help

Never-mind I figured it out
 

poweld

Member
I am having problems calling overloaded constructor, can someone tell what i am doing wrong?

Code:
public: Banner(){ 
	cout << "Banner's Constructor" << endl;
	favoriteProgram = "C++ ";
}

public: Banner(string param1) {
	cout << "Banner's Overloaded Constructor" << endl;
	favoriteProgram = param1;
}

public: Banner(string param2) {
	cout << "Banner's Overloaded Constructor 2 " << endl;
	favoriteProgram = param2;

I get this error:

I can call one overloaded constructor, but not more than 1. Please help

Never-mind I figured it out

Rule #1 about asking programming questions on the internet: if you solve it yourself, do not just say "nvm", post what you figured out.

The reason you were having this issues is because the constructor method signature is comprised of the name, the cardinality of the parameter list, and the types of the parameters.

In this case, you had two methods with the same name with one string parameter, causing a conflict.
 
Not really sure if this is a good question at all. I'm trying to wrap my head around object oriented programming in python. Let's say I was making some Pokemon-esque game.

I make a class called Pokemon.

Code:
class Pokemon(object):
	def __init__(self, name, hp, type1, type2, move1, move2, move3, move4):
		self.name = name 
		self.hp = hp
		self.type1 = type1
		self.type2 = type2 
		self.max_hp = hp
		self.move1 = move1
		self.move2 = move2 
		self.move3 = move3
		self.move4 = move4

Now here I only give Pokemon an HP, two types, and 4 moves. I plan on just simplifying things and have damage values tied to attacks rather than to Pokemon. And give weaker Pokemon weaker attacks.

Now for the Pokemon in my game, should they all be created by doing something like:

Code:
class Caterpie(Pokemon):
    Caterpie.name = 'Caterpie'
    Caterpie.hp = 5
    Caterpie.move1 = 'tackle'

Each Pokemon getting its own class and inheriting from the Pokemon parent class?

Meanwhile lets say I have a function that makes one Pokemon attack a defending Pokemon. Should I put this function in the Pokemon Class, since it is an action I want Pokemon to be able to do? I guess I'm really not to sure on how to use classes.
 

poweld

Member
Not really sure if this is a good question at all. I'm trying to wrap my head around object oriented programming in python. Let's say I was making some Pokemon-esque game.

I make a class called Pokemon.

Now here I only give Pokemon an HP, two types, and 4 moves. I plan on just simplifying things and have damage values tied to attacks rather than to Pokemon. And give weaker Pokemon weaker attacks.

Now for the Pokemon in my game, should they all be created by doing something like:

Each Pokemon getting its own class and inheriting from the Pokemon parent class?

Meanwhile lets say I have a function that makes one Pokemon attack a defending Pokemon. Should I put this function in the Pokemon Class, since it is an action I want Pokemon to be able to do? I guess I'm really not to sure on how to use classes.

Don't be so self-doubting! It sounds like you've got a handle on it. You made a class of object, the Pokemon, and created a subclass, Caterpie, which satisfies the "is a" relationship to its parent class.

In terms of attack, you're right, it's an action that any Pokemon can initiate, so it belongs in the Pokemon class. Something like this:

Code:
def attack(self, other, move):
  other.takeDamage(calculateDamage(self, other, move))

edit: A method for taking damage would be necessary since of course the Pokemon would have to d... faint if hp <= 0!
 
Don't be so self-doubting! It sounds like you've got a handle on it. You made a class of object, the Pokemon, and created a subclass, Caterpie, which satisfies the "is a" relationship to its parent class.

In terms of attack, you're right, it's an action that any Pokemon can initiate, so it belongs in the Pokemon class. Something like this:

Code:
def attack(self, other, move):
  other.takeDamage(calculateDamage(self, other, move))

edit: A method for taking damage would be necessary since of course the Pokemon would have to d... faint if hp <= 0!

Thanks for the confirmation/help. I'll have to figure out how to simulate fainting. Damage can be done (I think) by doing something like: other.hp - move.dmg
 

poweld

Member
Thanks for the confirmation/help. I'll have to figure out how to simulate fainting. Damage can be done (I think) by doing something like: other.hp - move.dmg

Wouldn't damage also have to take into consideration the defending Pokemon type? (I'm not an expert)

Good luck!
 

OceanBlue

Member
Don't be so self-doubting! It sounds like you've got a handle on it. You made a class of object, the Pokemon, and created a subclass, Caterpie, which satisfies the "is a" relationship to its parent class.

Should Caterpies be an instance or a subclass of the Pokemon class if it's going to have the species name and types as constructor parameters?

Edit: I'm probably being really dumb about this lol.
 
Wouldn't damage also have to take into consideration the defending Pokemon type? (I'm not an expert)

Good luck!

Oh you are correct. That is actually something I've been struggling with. I'm not really sure how to code in type effectiveness.

I tried something like just setting a bunch of stuff like "fire" < "water", and then assigning each move a type, and then doing a type comparison in the fight function to calculate damage.

But found that all that actually does it test which string has more characters (and it would report True in the case of "fire" < "water", but not for the reasons I want).
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
CornBurrito had it right. Not a Python expert so I can't comment on the syntax, but the structure looks spot on.

-------------
Oh you are correct. That is actually something I've been struggling with. I'm not really sure how to code in type effectiveness.

I tried something like just setting a bunch of stuff like "fire" < "water", and then assigning each move a type, and then doing a type comparison in the fight function to calculate damage.

But found that all that actually does it test which string has more characters (and it would report True in the case of "fire" < "water", but not for the reasons I want).

This would be fairly simple. Theres other ways to make this more elegant, but here's a long form so you hopefully understand.
Code:
if (attack) {    // if the Pokemon is attacking, enter the statement
    if ((enemy.type == 'grass') || (enemy.type == 'poison') || (enemy.type == 'psychc')) {   // if the move is super-effective, double the damage
        damage = move.damage * 2.0;
    } else if ((enemy.type == 'fight') || (enemy.type == 'fire') || (enemy.type == 'flying') || (enemy.type == 'ghost')) {    // if the move is not very effective, halve the damage
        damage = move.damage * 0.5;
    } else {    // otherwise, just use the moves damage value
        damage = move.damage;
    }
}

Hope that makes sense. (This is for gen 1, so it doesn't consider battlefield modifiers or the new types.)
 

Chris R

Member
Wouldn't damage also have to take into consideration the defending Pokemon type? (I'm not an expert)

Good luck!

Damage would also need to consider the current state of the battlefield (certain moves enhance/dampen the type of other moves), the state of both pokemon and any items that they are holding, ect.
 

survivor

Banned
Oh you are correct. That is actually something I've been struggling with. I'm not really sure how to code in type effectiveness.

I tried something like just setting a bunch of stuff like "fire" < "water", and then assigning each move a type, and then doing a type comparison in the fight function to calculate damage.

But found that all that actually does it test which string has more characters (and it would report True in the case of "fire" < "water", but not for the reasons I want).

I would just copy the actual type chart weakness. So make like a 18x18 matrix and look up the multiplier values there each time you do damage calculations.

But that's just a random guess, I'd imagine there might be more efficient or easier ways.
 

tariniel

Member
So how do you guys find side/practice stuff to do? I have this problem where I want to practice but can't think of anything I want to do as practice. I already graduated with a BSCS but my job doesn't actually have me writing a lot of code and I feel like I'm slowly forgetting everything. I also want some side projects that I can put on my resume some day.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
So how do you guys find side/practice stuff to do? I have this problem where I want to practice but can't think of anything I want to do as practice. I already graduated with a BSCS but my job doesn't actually have me writing a lot of code and I feel like I'm slowly forgetting everything. I also want some side projects that I can put on my resume some day.

Try to implement or recreate cool things that you see.

For web development, if I see a really neat site or effect, I'll try and recreate it or learn how they did it.
Or if I come across an interesting framework or API I'll attempt to build a site out of it.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I would just copy the actual type chart weakness. So make like a 18x18 matrix and look up the multiplier values there each time you do damage calculations.

If python supports enums (textual aliases for integers) like I'm familiar with, then damage calculation would be as simple as:

Code:
BaseDamage * Weakness[move.type][enemy.type1] * Weakness[move.type][enemy.type2] * <other modifiers>
Where Weakness is a 2D matrix of all the elemental weaknesses and strengths.

So you would have an elemental enum called FIRE that corresponds to the 1 (the second entry in a 0 indexed array). And you're matching it up against a WATER Pokemon, with WATER corresponding to 2 (the third entry in a 0 indexed array).

If you wrote it out in code, it would look something like:
Code:
BaseDamage * Weakness[FIRE][WATER] * Weakness[FIRE][BLANK] * ...

With BLANK being a placeholder type that has no elemental strengths or weaknesses, it's just there to handle Pokemon with only 1 type.
 

poweld

Member
Should Caterpies be an instance or a subclass of the Pokemon class if it's going to have the species name and types as constructor parameters?

Edit: I'm probably being really dumb about this lol.

Well, if you don't subclass the Caterpie, every time you instantiate one you'll have to pass in the specifics on how to create a Caterpie, i.e.:

Code:
caterpie = Pokemon('Caterpie', 20, Type.grass, Type.ground, Move.scratch, ...)

By subclassing, the creation of a new Caterpie should be as easy as:

Code:
caterpie = Caterpie()

So how do you guys find side/practice stuff to do? I have this problem where I want to practice but can't think of anything I want to do as practice. I already graduated with a BSCS but my job doesn't actually have me writing a lot of code and I feel like I'm slowly forgetting everything. I also want some side projects that I can put on my resume some day.

Check out https://projecteuler.net/problems and http://rosettacode.org/wiki/Category:Programming_Tasks

The latter is especially neat for learning/practicing in new languages since it provides the problems along with their solution in approximately a bajillion languages.

Oh you are correct. That is actually something I've been struggling with. I'm not really sure how to code in type effectiveness.

I tried something like just setting a bunch of stuff like "fire" < "water", and then assigning each move a type, and then doing a type comparison in the fight function to calculate damage.

But found that all that actually does it test which string has more characters (and it would report True in the case of "fire" < "water", but not for the reasons I want).

As mentioned by Haly, I'd use an enum (short for enumeration). This gives you distinct types without having to use strings, which can be error-prone and inefficient.

For finding out how different matchups "work", e.g. fire damage deals 2x damage to water types, I'd create a method like I mentioned earlier, calculateDamage(pokemonAttacking, pokemonDefending, attack). This method can reference a dictionary that stores each type mapped to each other type, the result of which is the damage multiplier.

In other words, something like this:

Code:
weaknessLookup = {
  Type.Fire: {
    Type.water: 2.0
    Type.grass: 1.0
    Type.ground: 0.5
    ...
  }
  ...
}

The calculate method would then just have to do the lookup based on the attack type and defending Pokemon type, and as mentioned, any other effects currently in play.
 
Will look into enums. Thanks for the help everyone.

In other words, something like this:

Code:
weaknessLookup = {
  Type.Fire: {
    Type.water: 2.0
    Type.grass: 1.0
    Type.ground: 0.5
    ...
  }
  ...
}

The calculate method would then just have to do the lookup based on the attack type and defending Pokemon type, and as mentioned, any other effects currently in play.

So this seems like the way I'd go about it. But this also seems like a ton of typing since I need to do it for each of the 18 types. I guess there's no way around that?


edit: Ah shit this seems to be Python 3.x stuff. I don't think Python 2.7 supports enum.

CornBurrito had it right. Not a Python expert so I can't comment on the syntax, but the structure looks spot on.

-------------


This would be fairly simple. Theres other ways to make this more elegant, but here's a long form so you hopefully understand.
Code:
if (attack) {    // if the Pokemon is attacking, enter the statement
    if ((enemy.type == 'grass') || (enemy.type == 'poison') || (enemy.type == 'psychc')) {   // if the move is super-effective, double the damage
        damage = move.damage * 2.0;
    } else if ((enemy.type == 'fight') || (enemy.type == 'fire') || (enemy.type == 'flying') || (enemy.type == 'ghost')) {    // if the move is not very effective, halve the damage
        damage = move.damage * 0.5;
    } else {    // otherwise, just use the moves damage value
        damage = move.damage;
    }
}

Hope that makes sense. (This is for gen 1, so it doesn't consider battlefield modifiers or the new types.)

I'm actually not sure what "||" means. I'm guessing it is equivalent to Python's "or" but the types you picked as examples confuses me since grass, poison, and psychic don't share a common weakness.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I'm actually not sure what "||" means. I'm guessing it is equivalent to Python's "or" but the types you picked as examples confuses me since grass, poison, and psychic don't share a common weakness.

|| means 'or' in a lot of languages.

------

Of course. Looks like there is an API available for Pokemon, and it has a wrapper for Python.

http://pokeapi.co/docs/


.....now I want to make a Pokemon battle website.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
You would write something that looks like this (not familiar with Python so I'm just pulling things from docs):

Code:
weaknesses = [
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0, 1.0, 0.5, 1.0],
[1.0, 0.5, 0.5, 1.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 0.5, 1.0, 0.5, 1.0, 2.0, 1.0],
....
]

What this does is makes an array of arrays (effectively a Matrix) out of hardcoded values. This is then accessed using the notation
Code:
weaknesses[x][y]
Where x is the attack type (as you can see, the first array I wrote corresponds to the effects of Normal attacks on every other type) and y is the defense type.

For example, I want know the damage multiplier of a Fire attack on a Grass type.

Fire corresponds to 1 (you'll define this using python's enumerations)
Grass corresponds to 4 (same as above),

So in the damage calculation function, I would write
Code:
totalDamage = baseDamage * weaknesses[1][4]

In a real game, data like this might be stored in a separate file and then read by the game at run time to enable modding or tweaking without recompiling the entire project, but this is beyond the scope of the current topic.
 

poweld

Member
So this seems like the way I'd go about it. But this also seems like a ton of typing since I need to do it for each of the 18 types. I guess there's no way around that?

You could reduce the amount of data you have to enter into the dictionary by omitting the values that are most common, probably the ones with a multiplier of 1.0.

Then, your damage calculation method can check whether the value exists, and if not, assume it's a 1.0 multiplier. Something like this:

Code:
def getDamageMultiplier(damageType, pokemonType):
  if pokemonType not in damageMap[damageType].keys():
    return 1.0
  else:
    return damageMap[damageType][pokemonType]
 
Hmm so enums were added in Python 3.4. I'm using 2.7, so Haly I don't think I can go with your solution. :-[

Or I can find an alternative way of implementing your solution in Python.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
The only thing enum really does is make your code more legible. If you create your own "cheatsheet" by manually writing down what types correspond to which numbers, you can use my example verbatim.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
When you need to use an object for an indefinite amount of time, you need to allocate memory for it on the heap.

When you don't need it anymore, you need to free up that memory.

In C++, destructors are what's used to free that memory. Without them, memory would remain allocated to unused objects until the program is terminated, aka, a memory leak.


Refer to cpp_is_king's post below.
 
Generically, destructors are code that runs when an object goes out of scope -- no matter what the program flow is.

That makes them useful for cleaning up dynamic memory, but also other RAII tasks. (like cleaning up a temporary directory, for example)

It's not dissimilar from how you might use try {} finally {} in java, except languages that support proper RAII constructs (C#'s using, python's with, C++'s destructors) make that task a lot easier and cleaner.
 
The only thing enum really does is make your code more legible. If you create your own "cheatsheet" by manually writing down what types correspond to which numbers, you can use my example verbatim.

Thanks for the help. I basically can do it just by doing this:

Code:
weaknesses = [

[1.0, 1.0, 2.0, 2.0, 0.5],
[1.0, 1.0, 2.0,  0.5, 2.0],
[1.0, 1.0, 0.5, 2.0, 2.0],
[1.0, 0.5, 1.0, 2.0, 2.0],
[0.5, 1.0, 1.0, 2.0, 2.0]
]

Fire = 0
Water = 1
Grass = 2
Wind = 3
Heart = 4

def get_damage_multiplier(x,y):
	print weaknesses[x][y] 
	
get_damage_multiplier(Fire, Heart)
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Hmm so enums were added in Python 3.4. I'm using 2.7, so Haly I don't think I can go with your solution. :-[

Or I can find an alternative way of implementing your solution in Python.

Like Haly said, you don't actually need enums for his implementation. They just pretty up the code.

It seems like all of this is beyond your current ability though. I'd recommend doing some more research, scaling down your project idea, and starting with something simpler to get more comfortable.

edit: Damn, you got it.

can someone briefly explain c++ class destructors to me? thanks

Manual garbage collection essentially.

Woah huh this looks neat. Never heard of API/wrappers before. So I can basically just borrow code from this site that would fit in my game?

Not exactly. That is a RESTful API, so it's dealing with server calls. You can make a GET request to the server at the URL provided by the API, and if your data request is valid it will return with the information/data that you asked for. From there you can use that data in a program.
 
Top Bottom