Jump to content
• Advertisement

# arkane7

Member

29

213 Neutral

• Rank
Member
1. ## more tic tac toe

if you do not put a break in that if statement, it will change ALL empty spaces into O's, rather than the first one you find If (board =='') { board = 'O'; break; } that way only one empty is changed also i think you may want 9 instead of 8; arent there 9 tiles in TTT?
2. ## Define loop?

in basic terms a loop is a conditional setting for code within in the loop to continue to repeat execution. say you want to count the number of 1's in an array (an array is just a simple "list" of data in successive memory) int [50] myNum = { 1,2, 1, 3 1, ..... //fill in the rest with whatever you want}' now lets count the number of 1's int numOf1 = 0; int indexOfArray = 0; while (indexOfArray != 50) // this is a condition to stop the loop. when indexOfArray equals 50, the repeition of the following will stop //everything in the brackets will be repeated as long as the above condition is true { if (myNum[indexOfArray] == 1) numOf1++; //this counts the number of 1s indexOfArray++; //this allows you to go to the next element in the array; } now it will first start off with both variables at 0; then it will run the code in the middle. indexOfArray will only be at 1 after the first run through, so indexOfArray !=50 will be true. this will repeat the inner code 50 times, updating the variable everyloop; indexOfArray increments EVERY loop. while numOf1 only increments when it finds a 1 inside the array if you just did this while (true) {} this will run FOREVER because it is always true. while (false) {...} this will NEVER run becuase it is always false another example index = 0; while (myNum [index] != 1) { index++;} now it will only stop when you find a 1 inside the myNum array. if there is no 1 it will run forever sorry for not knowing python this is in C++
3. ## Sorting vectors

reference: http://www.engr.sjsu...tt/SortFind.htm just go down until it talks about sort this is how i learned to use sort. using a function that is basically a < operator bool lessOp (YourClass a, YourClass b) { if (a.attribute < b.attribute) return true else return false; } then with your vector of objects, just call the sort function from the algorithm library sort (vec.begin(), vec.end(), lessOp); /////// you're passing a pointer to your lessOp function; using this pointer sort will call your function to compare the objects or doing what the website i used above, just create a operator< function for you classes like this: bool operator<(const Cperson& left, const Cperson& right) { return left.lastname < right.lastname; } now whenever you have 2 objects of that class and you do this var1<var2 it will called that function above. just use your class instead of Cperson
4. ## Comparing strings

To add on what RulerOfNothing noted, there is a C standard function called strcmp use it like this if ( strcmp(str1, str2) == 0) do some stufff..... it returns an integer, if it is zero then the strings are equivalent. From what i know it will return a negative number if the first string is less than the second (the first nonequivalent letter/character has an ASCII number less than the second strings) and will return positive if the first nonequivalent letter in the first has an ASCII number greater than the seconds basically like this: strcmp(s1, s2) < 0 /// s1 < s2 strcmp(s1, s2) > 0 //// s1> s2 strcmp(s1, s2) ==0 //// s1 == s2 the library for this function should be string.h or stdlib.h (possibly cctype.h) but im not really sure and as stated by rip-off, it is much safer to use C++ strings instead. You dont need a special function, s1==s2 is defined for those strings, as is a multitude of varying features. just #include <string> (not string.h, the C-style one) and you're ready to go.
5. ## Inventory System

why do you have c and temp objects? and more importantly, do you only have 1 type of items for each type of class? What I'm asking is wont you have like 3 different swords with various stats? or different clothing/gear? The way you have set this up it seems like you can't expand it, the user can't have 5 differente unique weapons or clothing, only 1 of each. What i can see is that you edit the temp ones to accomodate changes to the particular item, where it can be a different weapon entirely, but this still prevents the user from having many different items of the same type. If this system serves your purposes that is fine, but this seems like a bad design approach. It is very limited and if you want to make changes to it later you most likely have to change the entire thing. The point of recommending inheritance to aid you was to make this whole thing simlper, shorter and cleaner. Your item hierarchy is fine, its just your c and temp globals that make this very unusual. I will come back to this thread and post an expanded example of how you may approach this (i am currently at work and cannot risk typing on here for an extended amount of time). Like i said if your layout is working fine and your game is behaving how you want, you can just ignore me. I just want to help
6. ## Deleting from vector with iterator (99% sure this is the way to go)

Bill Door, that is a similar but more elongated version of what Brother Bob was talking about. Why make a special delete function and use a for_each? that seems a waste of time, since you can just do the delete in the predicate function. this is what i did on a homework assignment where i had to delete dead game actors del_func (takes object type or type pointer) { if (dead) delete} iterator newEnd = remove_if (vec.begin(), vec.end(), del_func); vec.erase(newEnd, vec.end()); again this is all similar to what Brother Bob already mentioned
7. ## Inventory System

just as JDGamedev mentioned, using OOP is your best bet at making it simple, shorter, and efficient (in some sense, since inheritance = virtual call table) As he said, start with a base Item class that has everything that will be in common to everything in the inventory (i recommend not only name/proportionns etc but also a "type", so you can request anything that is an item what kind of item it is so you can do proper management) from there make classes that inherit Item to be more specialized (armour, weapons, potions etc). Just keep a nice hierarchy but dont go too deep, as everyone know too many levels means a huge virtual lookup table (this is know when to use a subclasses function instead of a base classes function) Then all you have to do is keep a vector/list/whathaveyou of Item pointers, then whenever something is added (like a Spear for example), just add its pointer to the list. Whenever you want to use an item, just find it (using its name or type) and use it; if its a weapon or potion that has specific functions that Item does not have, as long as you know it is the correct type you can use type-casting and call the function (Anyone here is free to correct me if I am wrong)
8. ## Syntax Error C2061 while declaring a struct as a function argument...

[sub] [/sub] [sub]i believe fntHeader is a VARIABLE of type _fntHeader. i think this might be a possible issue. I don't know if that was your intention or not but putting a name at the end also declares an object of that struct type. What i think you want is to have the struct be called fntHeader. otherwise whenevr you use fntHeader there might be problems of interpretting it as the variable[/sub] [sub]what i think you want is this:[/sub] [sub]struct fntHeader {[/sub] [sub]//your code here };[/sub] [sub]As for your issue in the header file of GUI, do what Serapth said, just declare it as struct fntHeader; inside GUI.h, just dont use it inside the header. do all your implementations and uses of fntHeader inside GUI.cpp while including reader.h[/sub]
9. ## Syntax Error C2061 while declaring a struct as a function argument...

you can pass as an argument, i would recommend using a const reference, like const struct& var. This saves memory and time of having to copy your struct onto the stack if you want you can also use a pointer, but const references relieve you of having to use pointer syntax Edit: Serapth beat me to it PS: instead of using a typedef, you could have just removed the "_" from _fntHeader
10. ## Returning an element from an array

I think SOAL and rnlf are correct, you need every player to have a reference not create its own (though personally I would use a pointer but its all the same) Make the player class have a reference to a M this is pefect, should easily solve the issue also SOAL you forgot to include a check inside the isValid function if it is less than 0. I don't know if std::vector does that check or not.
11. ## Memory Allocate new

Also don't forget that using malloc/calloc etc you must actually put how many bytes you want to allocate. int* c = (int *) malloc( sizeof(int) * 30); // this will give you 30 elements of integers
12. ## Memory Allocate new

Why were you downvoted??? you were spot on in everything you mentioned. oh and yeah i completely forgot about 2[var] syntax. I never use it since its confusing. What do you mean overloading new? I'm not sure i've come accross that use of new Also monkeyboi other things about memory management: When using new, as stated before, it goes to the heap. If you allocate too many items, you will run out of memory unless you deallocate them. When you continually allocate memory but never free it, this is called a Memory Leak To deallocate, you use the delete key word. say I put SomeClass* x = new SomeClass(); to deallocate it just use delete x; (keep in mind this calls the destructor for SomeClass and frees the memory that x was pointing to, allowing later allocations to use that memory) but if you allocate an array you have to do something else; in the case of char* ca = new char[3]; you will have to use delete [] ca;
13. ## Memory Allocate new

I first came accross std::string before i learned about char arrays. This was way back in HS in intro programming. But i feel you are right he needs to understand pointers, allocation, and buffer limitations. Key thinks that monkeyboi needs to know about C-style strings (aka char arrays): There must be a zerobyte/null byte inside that determines the end of the string. '\0' is the ASCII symbol for it, but 0 is also fine. When using strlen it counts the number of characters up until it find this zerobyte. This is why he had odd answers since the char[] was undefined upon initialization. When you put char[4] tc = "123"; you are really putting '1', '2', '3', '\0', filling it up; this extra step of putting the zerobyte at the end is just a feature of c++ (and C i believe), but only when you put var = "___", if you do it by individual bytes (var[4] = 'b') you MUST put a zerobyte in manually (tc[4] = '\0') Even if it has a zerobyte, never access/overwrite an element outside of its bounds (dont ever use tc[-2] or tc[n], or any larger number, only 0->n-1 ). Keep in mind the compiler will not warn you if you go out-of-bounds, since it doesn't really know the size of the array, only in the case of declaration and putting something too big will it ever tell you something (char [4] tc = "1234", was too big since '\0' is a fifth additional byte added on) When you use new, it goes to the heap in the CPU memory, which is a special place for dynamically allocated memory, or run-time(?) allocation. Always remember that char[] is still a pointer, as are all arrays. tc itself is the pointer to the first element of the array, as in the case above *tc would be '1'; *(tc+1) is '2', *(tc+2) is '3' and *(tc+3) is '\0'. When you use tc[0] or tc[2], it is just a "shorter" way of doing *(tc+2). char* and char[] are in many ways equivalent, just char[] is explicity announcing its array property, while char* could just be a pointer to a single char or possibly pointing to the beginning of a char array edit: i put this in a post below but this needs to be mentioned along with this ----other things about memory management: When using new, as stated before, it goes to the heap. If you allocate too many items, you will run out of memory unless you deallocate them. When you continually allocate memory but never free it, this is called a Memory Leak To deallocate, you use the delete key word. say I put SomeClass* x = new SomeClass(); to deallocate it just use delete x; (keep in mind this calls the destructor for SomeClass and frees the memory that x was pointing to, allowing later allocations to use that memory) but if you allocate an array you have to do something else; in the case of char* ca = new char[3]; you will have to use delete [] ca;
14. ## Memory Allocate new

any time you use the "new" or malloc (C-style) allocation methods, it goes onto the heap, contrast to putting it on the stack (any local variables for example, like int f =3) So indeed your array IS on the heap, since you used new. What I think Ameise was getting at is that since it is on the heap there is less chance of encountering important data, such as things like returning addresses (the place to go back to after a function call for example) which are stored on the stack (if you overwrite a return address it may very well cause a page fault and crash the program) Edit: the point is that you do not want to go out of bounds of any array or invalid memory. Even if you overwrite harmless data, you risk the opportunity to contaminate either your own program executing, the operating system, or other programs. This is why std::string is much better alternative to C-style strings (char[]) because you don't directly deal with pointers. Also it has its own size() function, as well a multitude of manipulating functions. All in all std::string is more flexible, useful, and easier.
15. ## Memory Allocate new

I remember the program will automatically chech if it is out of bound, and if it is there will be an assertion or something. I also try char tc[4] = "123"; //char tc[4] = "1234";failed tc[5] = 0; // why this line could work?? [/quote] I think this is only because upon declaration the compiler KNOWS it only has 4 bytes in memory to use, so putting "1234" is actually 5 bytes since the zerobyte at the end. But when you just put tc[5], you are directly dereferencing a pointer and the compiler is not aware of the size of the memory is has 'access' to. I'm guessing the compiler basically says "Oh i see only 4 bytes not 5, error". But in the other case it just says "Oh put this byte in this memory address". it doesn't check to see if that is legal
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!