Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 29 30 [31] 32 33 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95954 times)

Mondark

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #450 on: November 15, 2010, 05:43:31 pm »

Oh, and what do the +=, -= *= and /= operators do?

in pretty much any language that has those operators,

'x += 1', is the same as 'x = x + 1'
'x -= 1', is the same as 'x = x -1'
*= and /= are the corresponding operations for multiply and divide.

Just shortcuts, really, but a bit more concise.
Logged
Fefeshnelmargalip

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #451 on: November 16, 2010, 12:17:01 am »

Depending on the language, there may even be several other shortcut operators, like %= for example.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #452 on: November 16, 2010, 04:17:21 am »

Thanks! I finally understand what my programming books were gettihng at, and this'll simplify my code by quite a bit!

It's C++ BTW.
Logged

IHateOutside

  • Bay Watcher
  • Fire safety is for wimps.
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #453 on: November 18, 2010, 04:55:03 pm »

Hi there! A bit of VB2010 for you.

I have assembled a program that calculates and adds all the factors of a number to a list that is drawn at runtime. My problem relates to duplicated factors. e.g 10 brings up the list :
10,1
5,2
2,5
1,10.

Obviously half these numbers are irrelevant but I can't work out a way to correctly locate the duplicate factors and remove them from the list.
Logged

Argembarger

  • Bay Watcher
    • View Profile
    • Not quite yet
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #454 on: November 18, 2010, 05:20:24 pm »

Sorry if this is a dumb suggestion, but, uh... Couldn't you have the program stop when a < b? Then you wouldn't have to remove the duplicates, they simply wouldn't be output to begin with.

Or, I guess maybe don't output b at all.
Logged
Quote from: penguinofhonor
Quote from: miauw62
This guy needs to write a biography about Columbus. I would totally buy it.
I can see it now.

trying to make a different's: the life of Columbus

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #455 on: November 18, 2010, 05:26:32 pm »

Going no higher than the square root of a number will give all non-duplicate factors of a number with exactly 2 factors.
Essentially you just need to replace the maximum number in your loop from aNumber to squareRoot of aNumber.
For 10, the max would then be 3*, so it checks 1, 2, 3, giving you 1,10 and 2,5.
For 100, the max would be 10, checking 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, giving you 1,100, 2,50, 4,25, 5,20, 10,10
It also has the advantage of making your algorithm take a square root of the amount of time it previously did.

This of course assumes you are getting 2 factors, rather than reducing to primes or anything else silly like that.

*I'm not entirely sure how variable casting works in BASIC; it would either be 3 or stick with the messy decimal number; either way, it makes no difference in this algorithm
« Last Edit: November 18, 2010, 05:37:21 pm by alway »
Logged

IHateOutside

  • Bay Watcher
  • Fire safety is for wimps.
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #456 on: November 19, 2010, 05:19:19 pm »

I actually did stumble across that and didn't notice.  :P It wasn't letting me use the Sqrt() function so I just used a half indices (By that I mean x ^ 1/2 = sqrt(x))
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Logged

IHateOutside

  • Bay Watcher
  • Fire safety is for wimps.
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #458 on: November 20, 2010, 02:56:43 pm »

Its sqr() for VB6 but for the .net versions it is sqrt(). I got it to work because I needed to add the line 'import system.math'
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #459 on: November 20, 2010, 03:25:17 pm »

Of course, quite a few languages require you to import a math library for that kind of stuff.
Logged

IHateOutside

  • Bay Watcher
  • Fire safety is for wimps.
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #460 on: November 20, 2010, 04:09:16 pm »

I kinda assumed it would work off the bat because it did in VB6. I shouldn't assume things.
Logged

moocowmoo

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #461 on: November 23, 2010, 10:47:51 pm »

I've been using this to learn Python and it's the best tutorial I've come across:

http://openbookproject.net//thinkCSpy/index.html

It's real thorough yet not overwhelming. The practice problems at the end of chapters are challenging but doable. I like how it slowly introduces knowledge in a logical fashion, and gives you vocabulary to understand technical terms.
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #462 on: November 25, 2010, 12:29:07 pm »

I'mma bumping this.

Oh, and here's work on a chargen for GURPS:

Code: [Select]
#include <iostream>
#include <cmath>

using namespace std;

int Chargen (int &nSt, int &nDx, int &nIq, int &nHt);
int CharDisplay (int nSt, int nDx, int nIq, int nHt, int &nBasicLift,
 float &fBasicSpeed, int &nHp, int &nWill, int &nPer, int &nFp);
int CharCalc(int nSt, int nDx, int nIq, int nHt, int &nCharPoints, int nOrigPoints);
int TestFight(int nSt, int nDx, int nIq, int nHt, int nBasicLift,
 float fBasicSpeed, int nHp, int nWill, int nPer, int nFp);

unsigned int PRNG() //Outputs a number. Use modulus function!
{
    static unsigned int nSeed = 5323;
    nSeed = ((8253729 * nSeed + 2396403)*(8253729 * nSeed + 2396403 *(8253729 * nSeed + 2396403))/3+1337);
    return nSeed;
}

int main(int argc, char *argv[])
{
int nCharPoints = 0;
int nOrigPoints = 0;

//The primary stats.
    int nSt = 0;
    int nDx = 0;
    int nIq = 0;
    int nHt = 0;
   
    //The secondary stats.
    int nBasicLift = 0;
    float fBasicSpeed = 0;
    int nHp = 0;
    int nWill = 0;
    int nPer = 0;
    int nFp = 0;
   
    cout << "GURPS 4ed Character Stuff. Testing C++ Code" << endl;
    cout << "By LN" << endl;
    cout << endl;
cout << "Enter desired amount of character points (Default = 100): ";
cin >> nCharPoints;
nOrigPoints = nCharPoints;
if (nCharPoints == 0)
{
nCharPoints = 100;
nOrigPoints = 100;
}
cout << endl;
do
{
Chargen(nSt, nDx, nIq, nHt);
} while (CharCalc(nSt,nDx, nIq, nHt, nCharPoints, nOrigPoints) == 1);
CharDisplay (nSt, nDx, nIq, nHt, nBasicLift, fBasicSpeed, nHp, nWill, nPer, nFp);
char cAns
/*do
{
        cout << "Do you want to test your character in a fistfight?" << endl;
        cout << "Y/N" << endl;
        cin >> cAns;
        if (cAns == 'y')
           TestFight(nSt, nDx, nIq, nHt, nBasicLift, fBasicSpeed, nHp, nWill, nPer, nFp);
        else
            cout << "Too bad." << endl;
    }while (nHp > 0)*/
    system("PAUSE");
    return EXIT_SUCCESS;
}

int Chargen(int &nSt, int &nDx, int &nIq, int &nHt)
{
    cout << "Input numbers for char stats." << endl;
    cout << endl;
    cout << "Strength: ";
    cin >> nSt;
    cout << endl;
    cout << "Dexterity: ";
    cin >> nDx;
    cout << endl;
    cout << "Intelligence: ";
    cin >> nIq;
    cout << endl;
    cout << "Health: ";
    cin >> nHt;
    cout << endl;
cout << endl;
return 0;
}

int CharDisplay (int nSt, int nDx, int nIq, int nHt, int &nBasicLift,
 float &fBasicSpeed, int &nHp, int &nWill, int &nPer, int &nFp)
{
cout << "Your primary stats are: " << endl;
cout << endl;
cout << "Strength: " << nSt << endl;
cout << "Dexterity: " << nDx << endl;
cout << "Intelligence: " << nIq << endl;
cout << "Health: " << nHt << endl;
    cout << endl;
cout << "Your secondary stats are: " << endl;
    cout << endl;
    nBasicLift = (nSt*nSt)/5;
    cout << "Basic Lift: " << nBasicLift << endl;
    fBasicSpeed = (nHt+nDx)/4;
    cout << "Basic Speed: " << fBasicSpeed << endl;
    nHp = nHt;
    cout << "Hitpoits: " << nHp << endl;
    nWill = nIq;
    cout << "Will: " << nWill << endl;
    nPer = nIq;
    cout << "Perception: " << nPer << endl;
    nFp = nHt;
    cout << "Fatigue Points: " << nFp << endl;
return 0;
}

int CharCalc(int nSt, int nDx, int nIq, int nHt, int &nCharPoints, int nOrigPoints)
{
if (nSt != 10)
nSt = (nSt-10)*10;
if (nDx != 10)
nDx = (nDx-10)*20;
if (nIq != 10)
nIq = (nIq-10)*20;
if (nHt != 10)
nHt = (nHt-10)*10;
nCharPoints = nCharPoints - nSt;
nCharPoints = nCharPoints - nDx;
nCharPoints = nCharPoints - nIq;
nCharPoints = nCharPoints - nHt;

if (nCharPoints < 0)
{
cout << "Number of character points is negative. Please recalculate." << endl << endl;
nCharPoints = nOrigPoints;
return 1;
}
else
return 0;
}

int TestFight(int nSt, int nDx, int nIq, int nHt, int nBasicLift,
 float fBasicSpeed, int nHp, int nWill, int nPer, int nFp)
{
    int nMonSt = (PRNG() % 15);
    int nMonDx = (PRNG() % 15);
    int nMonIq = (PRNG() % 15);
    int nMonHt = (PRNG() % 15);
    int nMonHp = nMonHt;
   
}

int SwingThrust (int nSt, int nModeDmg) // 1 = Thrust, 0 = Swing!
{
    if (nModeDmg == 0)
       switch (nSt)
       {
           case 1: return ((PRNG() % 6)-5);
           case 2: return ((PRNG() % 6)-5);
           case 3: return ((PRNG() % 6)-4);
           case 4: return ((PRNG() % 6)-4);
           case 5: return ((PRNG() % 6)-3);
           case 6: return ((PRNG() % 6)-3);
           case 7: return ((PRNG() % 6)-2);
           case 8: return ((PRNG() % 6)-2);
           case 9: return ((PRNG() % 6)-1);
           case 10: return (PRNG() % 6);
           case 11: return ((PRNG() % 6)+1);
           case 12: return ((PRNG() % 6)+2);
           case 13: return ((PRNG() % 6)+(PRNG() % 6)-1);
           case 14: return ((PRNG() % 6)+(PRNG() % 6));
           case 15: return ((PRNG() % 6)+(PRNG() % 6)+1);
           case 16: return ((PRNG() % 6)+(PRNG() % 6)+2);
           case 17: return ((PRNG() % 6)+(PRNG() % 6)+(PRNG() % 6)-1);
           case 18: return ((PRNG() % 6)+(PRNG() % 6)+(PRNG() % 6));
           case 19: return ((PRNG() % 6)+(PRNG() % 6)+(PRNG() % 6)+1);
           case 20: return ((PRNG() % 6)+(PRNG() % 6)+(PRNG() % 6)+2);
           default: { if (nSt > 20) return ((PRNG() % 6)+(PRNG() % 6)+(PRNG() % 6)+2);
                      else cout << "ERROR: Strength (nSt) is zero or negative!";}
       };
       if (nModeDmg == 1)
       switch (nSt)
       {
           case 1: return ((PRNG() % 6)-6);
           case 2: return ((PRNG() % 6)-6);
           case 3: return ((PRNG() % 6)-5);
           case 4: return ((PRNG() % 6)-5);
           case 5: return ((PRNG() % 6)-4);
           case 6: return ((PRNG() % 6)-4);
           case 7: return ((PRNG() % 6)-3);
           case 8: return ((PRNG() % 6)-3);
           case 9: return ((PRNG() % 6)-2);
           case 10: return ((PRNG() % 6)-2);
           case 11: return ((PRNG() % 6)-1);
           case 12: return ((PRNG() % 6)-1);
           case 13: return (PRNG() % 6);
           case 14: return (PRNG() % 6);
           case 15: return ((PRNG() % 6)+1);
           case 16: return ((PRNG() % 6)+1);
           case 17: return ((PRNG() % 6)+2);
           case 18: return ((PRNG() % 6)+2);
           case 19: return ((PRNG() % 6)+(PRNG() % 6)-1);
           case 20: return ((PRNG() % 6)+(PRNG() % 6)-1);
           default: { if (nSt > 20) return ((PRNG() % 6)+(PRNG() % 6)-1);
                      else cout << "ERROR: Strength (nSt) is zero or negative!";}
       };
}

Still console based I'm afraid. Might make it into a full fledged game. Use if you want.

Oh, and any idea how to make a text parser for games a la Zork?
Logged

cartmann

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #463 on: November 25, 2010, 02:47:15 pm »

Lordnincompoop, you'll want to put "break;" at the end of your 'case' statements, else you'll get a number that returns by 'case 20:' every time.

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #464 on: November 25, 2010, 10:38:42 pm »

No, the return;s are good enough, there.
Logged
Eh?
Eh!
Pages: 1 ... 29 30 [31] 32 33 ... 78