THREE EQUAL SIGNS!?JavaScript said:But we use === (three equal signs) to check if one variable's value is "equal"
Codeacademy rocks. Some of the lessons are better than others, but it's been my favorite way to learn a language so far.Is it weird that I'm having an absolute blast with the codecademy website? I wish could have found a tutorial site like this for ActionScript when I was trying to get into flash game programming.
//Beginning Game Programming, 2nd Edition
//Chapter 2
//HelloWorld program
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, "Motoko Kusanagi has hacked your system!", "Public Security Section 9", MB_OK | MB_ICONEXCLAMATION);
}
items {
description: "string to use after examining item"
locationData: "String for room to use to describe item"
canTake: true/false //determines if player can pick up the item
}
locations {
description: "string to use when entering location"
items: [array of items inside of location]
exits: [array of directions that lead to new locations]
}
player {
inventory: [array of items in player's inventory]
equiped: [array of items the player is wearing]
}
//The player should be able to input [VERB] [OBJECT] syntax for commands.
var commandLine = ""; //the player's input
var commandVerb = ""; //the player's action
var commandObject = ""; //object the action is on
var parseCommandLine(commandLine){ //splits the player's command into a verb and object
this.verbStop = commandLine.indexOf(" ");
commandVerb = commandLine.subString(0, this.verbStop);
commandObject = commandLine.substring(this.verbstop + 1, commandLine.length);
performAction(commandVerb, commandObject);
this.getItem = function(commandObject){ //moves item to player's inventory
player.inventory.push(commandObject);
for (var i = 0; i < locations.items.length; i++){
if (locations.items[i] === commandObject){
locations.items.splice(i, 1);
break;
}
}
}
}
//Takes an action and calls for the appropriate response from the targeted object
var performAction = function(commandVerb, commandObject){
switch(commandVerb) {
case "examine":
console.log(commandObject.description);
break;
case "take":
if(commandObject.cantake === true){
getItem(commandObject);
} else { console.log("You cannot take the " + commandObject); };
break;
case "inventory":
for(var i = 0; i < player.inventory.length; i++){
console.log(player.inventory[i]);
};
default:
console.log("I don't understand what you're saying.");
}
}
I'm in way late here, but if you consider C++ to be "C with Classes" (I'll admit that was literally the early name for the language) you are doing a MASSIVE disservice to C++ as it is today. Templates alone offer a staggering amount of flexibility, though as with most things in C/C++, they give you enough rope to hang yourself with, and then some (and hang your friends, and your neighbors, and your city, and...). But I always feel like the language trusts YOU to be "not an idiot" rather than giving you the scissors that can't cut butter, which is how I feel in the "coffee" languages. (I saw the "coffee" term when talking about any language on a runtime, such as on the JVM or the CLI, and thought it was great)C is the low level language. Direct access to memory makes it powerful and efficient. If you need performance you want to write in C or for Object Oriented programming, it's younger brother C++. Things that are easy in nearly every other languages need to be implemented manually (or with helper libraries) in C/C++. A great language to know, not one I would recommend now if you're just starting out.
The internetname things you can't do in C++ (language, not library, though low-hanging fruit there is OK) that you can in Java/C#. And if you try memory management, I'll pwn you.
And that's the fallacy I run into a lot, even at work. There's an old saying that "An engineer can write FORTRAN in any programming language." And that's what C++ used to be capable of too, and that was best practices even. But it isn't anymore. There's very little intrinsically in the coffee languages that you can't write "like" in C++. Want easier memory management? Declare all your variables with "new" and store them in a shared_ptr<> for everything. Boom, you have "garbage collection" of a sort. You'll never need to call delete again. Care about array out-of-bounds? Use the "at" method of the standard containers so that you catch array-out-of-bounds at runtime (ref here). And that's 90% of the argument for the coffee languages shot to hell right there. Those are the two most common programming mistakes out there for amateurs that the coffee languages will catch at runtime (well, kinda catch, in that they still throw exceptions. You're still just as screwed if you're doing it wrong in either type of language) but C++ wouldn't by default. Now it does if you use it correctly.Low level languages are amazing for their ability to get down into the guts of what's going on. They require a LOT of expertise to properly write and take a ton of time and effort to do so. They show a hardware/execution-side efficiency.
char* array = malloc(sizeof(char) * 256); // 256 item array of chars, a.k.a. a string
char* array2d[256]; // array of pointers, gives you an array of arrays
char** another_array2d; // pointer to pointers is another way of having 2d arrays
#include <vector>
using namespace std; // so I don't need to put "std::" in front of everything. NEVER use this in header files, but it's usually OK in .cpp files
int main(int argc, char** argv)
{
vector<int> fibon = {0, 1, 1, 2, 3, 5, 8}; // check your compiler to make sure it supports this syntax. Most do these days
for(auto x: fibon) // need C++11 for this one GCC 4.7 or VS11 beta
{
cout << x << ", ";
}
cout << "done!" << endl;
// Jagged now
vector<vector<int> > jagged2D;
vector<int> tmp = {0, 1, 2, 3, 4};
jagged2D.emplace_back(move(tmp)); // C++11 again, but you can just push_back() the other vector too. I'm trying to be efficient here with std::move() and emplace_back()
tmp.clear();
for(int i = 0; i < 10; i++)
{
tmp.push_back(i);
}
jagged2D.emplace_back(move(tmp));
// Now you have a 2D array with 5 elements in the first row, and 10 in the 2nd.
cout << "jagged2D[0][4] is: " << jagged2D[0][4] << endl;
cout << "jagged2D[1][8] is: " << jagged2D[1][8] << endl;
//cout << "jagged2D[0][8] is: " << jagged2D[0][8] << endl; // Don't do! No array index checking on the [] operator so undefined behavior. You may be accessing anything at this point.
cout << "jagged2D.at(0).at(8) is: " << jagged2D[0][8] << endl; // throws a std::out_of_range exception
return 0;
}
The biggest problem with learning that one IMO is that you may get into the wrong mindset with arrays. I remember that was my biggest problem with Matlab when I used it in school (Engineering) was that Matlab uses math-style vector/matrix indexes (starts at 1) and every other programming language anywhere uses zero-based indexes (starts at zero) and the number of times I screwed it up because I've been programming a long time was embarrassing. So anybody trying to learn on that language may get into habits there that will screw them over in a big way anywhere else. Given that one-off errors are some of the most common anyway (in any language), starting somebody off in a language that's different than all others seems inadvisable.
- Matlab: If you have a copy, go for it. You can see immediate results, see the values of variables as the program runs, and with the click of a mouse, you can stop the program anywhere in the middle of its run. The syntax is easy, and it's very close to both C(++) and Fortran. You can be procedural or object oriented. This is my #1 pick for new languages IF you have something to practice on.