Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 59 60 [61] 62 63 ... 796

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

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #900 on: January 26, 2012, 10:10:50 am »

So, we're doing Rock, Paper, Scissors in C++, and my teacher says that my random number generator will always generate the same thing, but I can't get it to do it. Could someone look this over for me, please?

Spoiler (click to show/hide)

Funny thing is, I was showing someone a little C++ a couple days ago and spent fifteen minutes writing rock, paper, scissors just to show that person an example of basic C++.

I get the feeling this is going to be a busy semester...oh well, I'll get to learn how to create my own programming language this semester.  That should at least be interesting.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #901 on: January 26, 2012, 10:12:08 am »

Any idea what sort of language you'll be building/want to build?
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #902 on: January 26, 2012, 10:38:36 am »

So, we're doing Rock, Paper, Scissors in C++, and my teacher says that my random number generator will always generate the same thing, but I can't get it to do it. Could someone look this over for me, please?

Spoiler (click to show/hide)

Funny thing is, I was showing someone a little C++ a couple days ago and spent fifteen minutes writing rock, paper, scissors just to show that person an example of basic C++.

I get the feeling this is going to be a busy semester...oh well, I'll get to learn how to create my own programming language this semester.  That should at least be interesting.

It can be fun, but it is very tedious. I used concepts from programming language and compiler design to write a very limited grammar for creating expressions for rolling dice, but that has been the extent of my use of that knowledge.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #903 on: January 26, 2012, 01:52:31 pm »

I rewrote the trajectory part as a function but for some reason it's breaking the game and I can't figure out why,

Spoiler (click to show/hide)
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #904 on: January 26, 2012, 01:58:40 pm »

Maybe I'm missing something, but... when you're moving that to a function, you're no longer setting ball1x and ball1y before testing them in the if statement.  Without seeing the rest of the code, that's my guess.
Logged
Through pain, I find wisdom.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #905 on: January 26, 2012, 02:10:48 pm »

thank you,

this forum is a lifesaver, really, thank you everyone for how much help you've all given me so far.

how would I go about doing that?
I don't know how to refer to variables set inside a void function outside of that void function.
« Last Edit: January 26, 2012, 02:13:19 pm by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #906 on: January 26, 2012, 02:43:34 pm »

Normally, when variables are passed into a function a copy is made. Anything you do to the variables inside the function is done to the copy. If you want to affect the variables directly, you should pass them in by reference.
Code: [Select]
Traject(int ball1x,int ball1y)
{
     ball1x++;
     ball1y++;
}
In this function, the variables ball1x and ball1y are passed in normally. When you increment them, you are only incrementing the copy. The variables outside the function are unchanged.
Code: [Select]
Traject(int &ball1x,int &ball1y)
{
     ball1x++;
     ball1y++;
}
In this function, the two variables are passed in by reference; notice the ampersand (a.k.a. demon sign) before the variable names in the parameter list. The changes made to the two variables here affect the "real" variables, rather than a copy.

So, just pass the ball1x and ball1y variables into the function by reference, and the code you posted should work.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #907 on: January 26, 2012, 03:00:51 pm »

kk thanks, ya I remember that now,  I had a hard time understanding that part of my book, but you just cleared up any questions I had about it.
I had deleted all the work I had done on converting the trajectories to functions since was having trouble with it (back before you gave such wonderful assistance) right now I'm writing the score and combo system, almost done with it, then going to go back and re-write the traject function then add the second ball.
I had to learn a little C for writing the scoring system, since it is soo much better at writing int / string combos, instead of learning stringstream which i tried for a while but was a nightmare.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #908 on: January 26, 2012, 03:08:07 pm »

IMHO, you should make a class Ball that has an x, y and maybe even trajectory information. This'll make it easier to have multiple balls as well.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #909 on: January 26, 2012, 03:20:15 pm »

Honestly I should have learned more c++ before jumping headfirst into SDL,
I was only on page 400 or so of my 1500 c++ book,
never got to the section that covered classes
although I'm considering moving the x and y positions to part of the struct
so I could do ball1.x and ball1.y
but it wouldn't really make too much of a difference, I wouldn't mind declaring each of the balls x and y's and ints
since there would only be 4 or 5 balls max.
I recently moved ballspeed and spinspeed out of that struct and made them global variables, since i'd never have them going different speeds.  working on graphics for score multipliers and misses right now.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #910 on: January 26, 2012, 03:34:51 pm »

I had to learn a little C for writing the scoring system, since it is soo much better at writing int / string combos, instead of learning stringstream which i tried for a while but was a nightmare.

So you prefer c-style strings to C++'s string class(es)?  You might want to reconsider this, the plain ol' string class (not stringstream) abstracts away a lot of the headaches and errors with c-style strings.

Of course, if what you're doing is dirt simple and / or using on string constants, it's not a big deal and might be overkill.
Logged
Through pain, I find wisdom.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #911 on: January 26, 2012, 04:13:17 pm »

no no no, don't get my wrong, I love the c++ string class,
but for SDL_ttf, in order to print any text to a surface it has to be in const char* type format.
I want to print a string and an int, that increases every so often "score"

with C this is as easy as

char charscore[15];
sprintf( charscore, "Score : %04d", score );

    score_ = TTF_RenderText_Solid( font, charscore, textColor );
    SDL_BlitSurface( score_ , NULL , screen , &scorepos );

I have no idea how to do this in C++, but I think it would be something along these lines

std::stringstream sstr;
sstr << my_int;
std::string str1 = sstr.str();

std::string str2 = boost::lexical_cast<std::string>(my_int);

which I was never able to get to work,
and that is just for the int to const char* conversion.

for everything else though c++ strings are the way to go.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #912 on: January 26, 2012, 04:15:11 pm »

stringvariable.c_str() will convert your string variable into a c string to pass as the function argument. No need to mess with c strings otherwise.


Also, if you add "using namespace std" to the top of the file you can get rid of all those std:: prefixes. Unless you're using a different namespace, of course.
« Last Edit: January 26, 2012, 04:17:23 pm by kaijyuu »
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #913 on: January 26, 2012, 05:33:52 pm »

Getting back into my roguelike for a general efficiency rebuild, I painlessly converted my MapGenerator, InterfaceObject, and MainScreen into static classes, so I can easily reference them from anywhere.  That's swell.

However, some of you who looked at my code mentioned that ConsoleKey data can be used in a switch statement?  Compiler says otherwise, that switches can only interpret primitives, which I recall is why I made a separate Interface to contain the gigantic if-else ladder I built to reinterpret ConsoleKey data as a byte to be passed to a switch statement.  Which is certainly duplication of effort, but with the Interface turned static I can use it anywhere and make some applications with it.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

nenjin

  • Bay Watcher
  • Inscrubtable Exhortations of the Soul
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #914 on: January 26, 2012, 05:35:44 pm »

I just wanted to add thanks for the references explanation. You feel really sheepish asking people who know what they're doing in coding fundamental questions like that, but sometimes that's the building block you need to move forward. You just need to hear it as plainly as possible.
Logged
Cautivo del Milagro seamos, Penitente.
Quote from: Viktor Frankl
When we are no longer able to change a situation, we are challenged to change ourselves.
Quote from: Sindain
Its kinda silly to complain that a friendly NPC isn't a well designed boss fight.
Quote from: Eric Blank
How will I cheese now assholes?
Quote from: MrRoboto75
Always spaghetti, never forghetti
Pages: 1 ... 59 60 [61] 62 63 ... 796