• What do you need help with? Version 1
    5,001 replies, posted
... Wow, it just randomly started working properly! Very odd. Thanks for your help And yes, I was doing it before the delete[]
Ok I have a quick question. I'm writing some JUnit test cases and I need to verify that a method throws an exception when it is supposed to. Is there an assert method that can do this or how would one go about this. This is in Java btw.
Can someone tell me how to get a random number between 0.12 and 0.19 done in C++? [code] if (menu == 2) { // Experience starts at 0. Enter the level of the monster // that was defeated to generate experience gained. int exp_gained; int mons_lvl; cout << "Enter Monster Level: "; cin >> mons_lvl; if (mons_lvl >= 8) { exp_gained = mons_lvl*/*Random number between 0.12 and 0.19 */*100; cout << exp_gained << endl; } else { break; } } [/code] I can't seem to figure out how to use rand() the way I want. I just need to know how to generate a random number between 0.12 and 0.19 then use it to do math stuff.
Do you want any real number between 0.12 and 0.19, i.e. 0.1333 and 0.1334 would both be valid, or do you only want eight different numbers (0.12, 0.13, 0.14, etc.)?
I don't want things like 0.133 or 0.1445. I just want things like 0.13 or 0.17
[cpp](rand() % 8 + 12) / 100.0[/cpp] [url]http://codepad.org/hLYQA7bB[/url]
[QUOTE=shill le 2nd;26128834][cpp](rand() % 8 + 12) / 100.0[/cpp] [url]http://codepad.org/hLYQA7bB[/url][/QUOTE] Thank you so much! [editline]18th November 2010[/editline] Ok need help again. This one is a bit more complicated (for me at least)... Here's what I want to do. The user will enter the player's strength (Example: 15) The program takes the strength and times it by 0.2 which will give the lowest damage it can deal (In this case, 3) It will then take the strength again and divide it by 0.6 which will give the highest damage it can deal (In this case, 25) Now what I want it to do is take 3 and 25 and pick a random number between the two, which will give what the player actually hits. Hope that makes sense.
[QUOTE=slayer20;26129080]Thank you so much! [editline]18th November 2010[/editline] Ok need help again. This one is a bit more complicated (for me at least)... Here's what I want to do. The user will enter the player's strength (Example: 15) The program takes the strength and times it by 0.2 which will give the lowest damage it can deal (In this case, 3) It will then take the strength again and divide it by 0.6 which will give the highest damage it can deal (In this case, 25) Now what I want it to do is take 3 and 25 and pick a random number between the two, which will give what the player actually hits. Hope that makes sense.[/QUOTE] [url]http://codepad.org/9Fqnpqam[/url]
Can't fucking get TinyXML++ to work for ages now. I'm using Visual Studio 2010 and used Premake4 with this""premake4 vs2010 [--unicode] [--ticpp-shared] [--dynamic-runtime]". When I compile the .vcprojx it says that it can't open the .lib files. How do I go about fixing this? I tried compiling in vs2008 and then converting the .vcproj but still, no cigar. I will switch for another format because this thing is driving me up the wall. If there's any good JSON C++ parsers with documentation then please link me.
shill you are awesome
[QUOTE=CANAD14N;26127392]Ok I have a quick question. I'm writing some JUnit test cases and I need to verify that a method throws an exception when it is supposed to. Is there an assert method that can do this or how would one go about this. This is in Java btw.[/QUOTE] [cpp] @Test(expected=OutOfCheeseException.class) public void testInsufficientCheese() { // … } [/cpp] Or, if you're using an older JUnit that doesn't use annotations, [cpp] public void testInsufficientCheese() { try { // … } catch (final OutOfCheeseException e) { return; // Complete the test successfully } Assert.fail("Expected a " + OutOfCheeseException.class.getName() + " but didn't get one"); } [/cpp]
How would I go about iterating through every pixel/tile within a variable radius? I'm trying to update each tile within a certain circle area of any given entity.
[QUOTE=NorthernGate;26133775]How would I go about iterating through every pixel/tile within a variable radius? More or less looking trying to update each tile within a circle range.[/QUOTE] iterate through the box starting at (center - radius,center + radius) and ending at (center + radius, center - radius). For each tile, do some quick trig. The center point and any point in that box will form a right triangle (except for ones directly up/down/left/right of the center, but the math still works). Find the length of the hypotenuse, compare it to your variable radius. getting the hypotenuse would work something like this: sqrt(abs(centerX - pointX)^2 + abs(centerY - pointY)^2) = hypotenuse
[QUOTE=Chris220;26120671]Maybe I should just roll my own[/QUOTE] A good idea.
[QUOTE=robmaister12;26134112]iterate through the box starting at (center - radius,center + radius) and ending at (center + radius, center - radius). For each tile, do some quick trig. The center point and any point in that box will form a right triangle (except for ones directly up/down/left/right of the center, but the math still works). Find the length of the hypotenuse, compare it to your variable radius. getting the hypotenuse would work something like this: sqrt(abs(centerX - pointX)^2 + abs(centerY - pointY)^2) = hypotenuse[/QUOTE] Or much faster and less redundant: [cpp](centerX - pointX)^2 + (centerY - pointY)^2 = hypotenuse^2[/cpp]
if centerX is less than pointX, the value become negative. same with the Y values. I suppose you could either check negative values as well, or just run abs on the whole thing. The equation is the same, I just singled out hypotenuse. Also, this is how it would look in C#: double hypotenuse = Math.Sqrt(Math.Abs(Math.Pow(centerX - pointX, 2) + Math.Pow(centerY - pointY, 2)))
[QUOTE=robmaister12;26134929]if centerX is less than pointX, the value become negative. same with the Y values. I suppose you could either check negative values as well, or just run abs on the whole thing. The equation is the same, I just singled out hypotenuse. Also, this is how it would look in C#: double hypotenuse = Math.Sqrt(Math.Abs(Math.Pow(centerX - pointX, 2) + Math.Pow(centerY - pointY, 2)))[/QUOTE] But the point he's trying to make is that you can pre-calculate Hypotenuse^2 out-side of the loop. You don't need the Abs() call because when you square a number the result is always positive. Also (IIRC) squaring is quicker than calculating the square root.
[QUOTE=yngndrw;26134962]But the point he's trying to make is that you can pre-calculate Hypotenuse^2 out-side of the loop. You don't need the Abs() call because when you square a number the result is always positive. Also (IIRC) squaring is quicker than calculating the square root.[/QUOTE] Oh, good point. didn't factor in calculation time... And why didn't I remember that squaring a number returns a positive value ? :iiam:
i² = -1 :smug:
[QUOTE=pikzen;26135510]i² = -1 :smug:[/QUOTE] Go and play with your imaginary friends somewhere else. :P
C++0x I'm trying to have my constructor support rvalue-references and const references without redundancy, so I wrote some test-code, which then should later replace this: [cpp]struct Foo { public: Foo(StringType&& string, ContainerType&& container):string(std::move(string)), container(std::move(container)){} Foo(const StringType& string, ContainerType&& container):string(string), container(std::move(container)){} Foo(StringType&& string, const ContainerType& container):string(std::move(string)), container(container){} Foo(const StringType& string, const ContainerType& container):string(string), container(container){} private: String string; Container container; };[/cpp] The test to figure out how it would work: [cpp]#include <iostream> template<typename type> struct printtype { std::string operator()(){ return typeid(type).name() + std::string(": unknown?"); } }; #define DEFTYPE(type) \ template<> \ struct printtype<type> \ { \ std::string operator()(){ return #type; } \ } DEFTYPE(std::string); DEFTYPE(std::string&); DEFTYPE(std::string&&); DEFTYPE(const std::string); DEFTYPE(const std::string&); DEFTYPE(const std::string&&); DEFTYPE(char*); DEFTYPE(char*&); DEFTYPE(char*&&); DEFTYPE(const char*); DEFTYPE(const char*&); DEFTYPE(const char*&&); DEFTYPE(char*const); DEFTYPE(char*const&); DEFTYPE(char*const&&); DEFTYPE(const char*const); DEFTYPE(const char*const&); DEFTYPE(const char*const&&); #undef DEFTYPE struct A { template<typename StringType> void func(StringType){ std::cout << "StringType: " << printtype<StringType>()() << std::endl; } }; int main() { A a; a.func("test"); a.func(std::string("test")); std::string ts("test"); a.func(ts); a.func(static_cast<const std::string&>(ts)); a.func(std::move(ts)); }[/cpp] output: [code]StringType: const char* StringType: std::string StringType: std::string StringType: std::string StringType: std::string[/code] Which is not quite what I expected. So then I tried [cpp]void func(StringType&&){ std::cout << "StringType: " << printtype<StringType&&>()() << std::endl;[/cpp] And that version outputs this: [code]StringType: A5_c: unknown? StringType: std::string&& StringType: std::string& StringType: const std::string& StringType: std::string&&[/code] Which is almost fine, but what's up with the unknown type? I DEFTYPE'd all forms of std::string and char* I could think of. I also tried using std::identity (like used for std::forward): [cpp]void func(typename std::identity<StringType>::type&&){ std::cout << "StringType: " << printtype<typename std::identity<StringType>::type&&>()() << std::endl; }[/cpp] Which fails to compile: [code]error: no matching function for call to &#8216;A::func(const char [5])&#8217; error: no matching function for call to &#8216;A::func(std::string)&#8217; error: no matching function for call to &#8216;A::func(std::string&)&#8217; error: no matching function for call to &#8216;A::func(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)&#8217; error: no matching function for call to &#8216;A::func(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)&#8217;[/code] [editline]10:28PM[/editline] [code]z33ky@mobileblahbuntu:~$ c++filt -t A5_c char [5][/code] heh... solved :)
So today my networking class was assigned to create a bot for UT2004 that would connect to the game server using our own TCP socket APIs. The professor purposely gave us no information on how to start this project other than the commands INIT and READY would be helpful to know about. Anyone know where I should look to get started on this?
[QUOTE=sirbinks;26143544]So today my networking class was assigned to create a bot for UT2004 that would connect to the game server using our own TCP socket APIs. The professor purposely gave us no information on how to start this project other than the commands INIT and READY would be helpful to know about. Anyone know where I should look to get started on this?[/QUOTE] Can't help you but that sounds like an awesome networking class.
I couldn't find the networking protocol online, so.. Look at the network packages sent (e.g. via the program Fiddler) while connecting to a server. If your bot shall be able to actually compete with the player .. have fun deciphering all the incoming data...
[QUOTE=ZeekyHBomb;26143678]I couldn't find the networking protocol online, so.. Look at the network packages sent (e.g. via the program Fiddler) while connecting to a server. If your bot shall be able to actually compete with the player .. have fun deciphering all the incoming data...[/QUOTE] nah, it doesn't have to be able to compete. I get a C just for connecting it to the server, a B for receiving game info from the server, an A if it walks, shoots, etc, and I get an A and a pizza if it does anything cool (navigates maps, targets players, dances, etc.)
sounds like an easy grade
Sounds like a pretty awesome networking class if you have to make a custom client for that game.
[QUOTE=sirbinks;26143846]and I get an A and a pizza if it does anything cool (navigates maps, targets players, dances, etc.)[/QUOTE] It keeps getting better and better :D Wish my school was like that instead of having people that say that the only difference between C++ and C are the input and output methods...
[QUOTE=Darwin226;26144280]It keeps getting better and better :D Wish my school was like that instead of having people that say that the only difference between C++ and C are the input and output methods...[/QUOTE] Who uses classes in C++ ? :v:
[QUOTE=Richy19;26144554]Who uses classes in C++ ? :v:[/QUOTE] I program C++ with functionless structs, because I'm just that badass.
Sorry, you need to Log In to post a reply to this thread.