PDA

View Full Version : My C++ games



OlegL
02-05-2011, 05:19 PM
This is one of the games I created in C++:

#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;

const int SIZE = 100;

void initGame(int[], const int);
void printGame(int[], const int);
int findMin(int[], const int);
int findMax(int[], const int);
int guess();
bool found(int[], const int, int);

int main()
{
srand(time(0));
int game[SIZE];
initGame(game, SIZE);
// printGame(game, SIZE);
cout << "The minimum value in the array is " << findMin(game, SIZE) << endl;
cout << "The maximum value is " << findMax(game, SIZE) << endl;
bool quit = false;
while (quit == false)
{
int value = guess();
bool value_found = found(game, SIZE, value);
if (value_found == true)
cout << "Your value was found in the game\n";
if (value_found == false)
cout << "Your value wasn't found in the game\n";
cout << "Do you want to guess again or quit?"
" If you want to quit, type q\n"
"If you want to guess again, type any other key: ";
char answer;
cin >> answer;
if (answer == 'q')
quit = true;
}
}
void initGame(int int_array[], const int length)
{
for (int i = 0; i < length; i++)
int_array[i] = rand() % 1000 + 1;
}


void printGame(int int_array[], const int length)
{
for (int i = 0; i < length; i++)
cout << int_array[i] << endl;
}

int findMin(int int_array[], const int length)
{
int smallest = int_array[0];
for (int i = 1; i < length; i++)
{
if (int_array[i] < smallest)
smallest = int_array[i];
}
return smallest;
}

int findMax(int int_array[], const int length)
{
int largest = int_array[0];
for (int i = 1; i < length; i++)
{
if (int_array[i] > largest)
largest = int_array[i];
}
return largest;
}

int guess()
{
int value = 1;
while (value != 0)
{
cout << "Please enter a value between 1 and 1000: ";
cin >> value;
if (value >= 1 && value <= 1000)
{
break;
}
if (value == 0)
{
cout << "You decided to exit the program. Bye.\n";
exit(0);
}
}
return value;
}

bool found(int int_array[], const int length, int value)
{
bool value_found = false;
for (int i = 0; i < length; i++)
{
if (value == int_array[i])
value_found = true;
}
return value_found;
}


Later, I will post some other games I created in C++.

tesco
02-05-2011, 05:50 PM
compile it and post.

OlegL
02-10-2011, 06:47 AM
By the way, that code has one error, which is this line: int_array[i] = rand() % 1001 + 1. It should be 1000 + 1, not 1001 + 1.

Disme
02-10-2011, 09:24 AM
What made you think anyone cared ...

OlegL
02-10-2011, 11:44 AM
Don't make strange comments please.

Disme
02-10-2011, 01:05 PM
Don't make strange posts please ...

bijoy
02-12-2011, 06:15 AM
Compile it & upload anywhere, not in bt sites & post the link here :)

whatcdfan
02-14-2011, 05:46 PM
Compile it & upload anywhere, not in bt sites & post the link here :)

Dont make serious posts in OlegL thread please....

bijoy
02-14-2011, 07:23 PM
Compile it & upload anywhere, not in bt sites & post the link here :)

Dont make serious posts in OlegL thread please....

I thought he was somewhat different in this thread. :unsure:

OlegL
02-15-2011, 03:52 PM
There is no need to post executables anywhere.

anon
02-15-2011, 04:05 PM
There is no need to post executables anywhere.

You should consider most people here won't have programming IDEs readied up to compile your code. Unless you want them to do something else besides saying "uh, nice", it'd be a pretty good idea to post a compiled version.

tesco
02-18-2011, 12:34 AM
There is no need to post executables anywhere.

You should consider most people here won't have programming IDEs readied up to compile your code. Unless you want them to do something else besides saying "uh, nice", it'd be a pretty good idea to post a compiled version.
:yup:

iLOVENZB
02-19-2011, 01:15 AM
OlegL is banned :(.

anon
02-19-2011, 01:26 AM
Nope, not banned but disabled. Again. After his last thread in the BT section (which apparently got trashed), I'm not surprised.

Snee
03-18-2011, 07:53 PM
OlegL is banned :(.

It didn't take :(

bijoy
03-21-2011, 02:42 PM
Can you give me an exe to this? Is very difficult to play.

Congratulation for your precious 14th post. :)
You are just 6 posts away from using PM system.

anon
03-21-2011, 03:45 PM
You are just 6 posts away from using PM system.

More like -9. The postcount requirement is 5 now. He doesn't have a star because he's not been here for 15 days yet.

Glaucon
04-05-2011, 09:51 PM
I felt like compiling this:


In function 'int main()':
line 16 | error: 'time' was not declared in this scope

The hell if I know what that line was doing there in the first place. Anyway, I removed it so here it is - all compiled and joyful: click (http://dl.dropbox.com/u/9861105/stuff/OlegLGame.exe)

OlegL
04-06-2011, 06:41 PM
Maybe Windows compilers require adding one more header file to the program - <ctime>, but g++ compiler on linux and freebsd doesn't need that header to compile it. But I am not sure 'cause I didn't try to compile it with a windows compiler.

Glaucon
04-06-2011, 07:19 PM
Maybe Windows compilers require adding one more header file to the program - <ctime>, but g++ compiler on linux and freebsd doesn't need that header to compile it. But I am not sure 'cause I didn't try to compile it with a windows compiler.

Strange - I reckon all compilers should require the ctime header. Anyhow, that was likely the case, yeah.

KFlint
08-29-2011, 06:33 PM
You should check into object-oriented programming and design patterns if you haven't already done it yet. If you ever want to do that as a job, that's the next step.

dash18
08-30-2011, 01:31 PM
that code u posted is 14years old hard ! I don't see the idea !
P.S.: silverlight owns

KFlint
08-30-2011, 10:26 PM
that code u posted is 14years old hard ! I don't see the idea !
P.S.: silverlight owns

Well, you have to start somewhere.

Dion
09-04-2011, 08:22 AM
I wouldn't exactly call this the best place to get support or encouragement when starting out with very basic programming...

But I'll give one suggestion: since this is C++ you *really* should learn about the standard libraries, then rewrite this without C-style arrays and maybe even using a class or two (just for the hell of it). ;)

[Edit: and yeah, design patterns...]

KFlint
09-04-2011, 12:59 PM
If you want even more fun, watch a tutorial on designing app on mobile devices like Android phones or IPhone (I'd suggest Android because it's easier to get into object-oriented programming in Java).

Something like : Developing Android Applications with Java.

That's not that hard really, their API is relatively easy to understand.

Btw : while (quit == false) is redundant, you can just write while (!quit) as it's already a boolean.

Glaucon
09-04-2011, 05:37 PM
If you want even more fun, watch a tutorial on designing app on mobile devices like Android phones or IPhone (I'd suggest Android because it's easier to get into object-oriented programming in Java).

Definitely a great idea. The mobile market is blooming and working as a mobile developer at this time can really pay off.

iLOVENZB
09-04-2011, 05:55 PM
Yes, sometimes I wonder how they do pay off. If pirates are still willing to pre 99c apps how the fuck can they still succeed (mobile devs)? To me, the mobile market looks like a high turnover/low profit industry. Where by you spend all your time and effort into creating a game, selling it for 99c, and then hope that someone will buy it before someone cracks it and uploads it on the interwebz.

OlegL
09-04-2011, 11:46 PM
then rewrite this without C-style arrays

I know about C++ vectors. :)

KFlint
09-05-2011, 01:03 AM
Yes, sometimes I wonder how they do pay off. If pirates are still willing to pre 99c apps how the fuck can they still succeed (mobile devs)? To me, the mobile market looks like a high turnover/low profit industry. Where by you spend all your time and effort into creating a game, selling it for 99c, and then hope that someone will buy it before someone cracks it and uploads it on the interwebz.

I agree on the game development on mobile devices being a low profit industry, I don't think there is much money to make unless you do a major hit. I read somewhere than the top 50 apps on Android make 90% of the money, in a sea of thousand apps, that's pretty discouraging.

A friend of mine coded a 3d puzzle game (it was a bit boring I believe) and made like 23$ in 4 months, not worth the time spent coding this, at all.

Still many companies want do develop utility apps for their clients for various purposes. That's an interesting field of programming.

The web is changing with the appearance of those mobile devices and both technologies will be even more intricate in the years to come, no matter what mobile devices will look like in 10 years, wheter the mobile device is in your hands, directly in a pair of glasses or whatever. Web backends, communication with web services and mobile interface, that's the future.

Anyway, there is more to it than selling games on the market place.

Dion
09-05-2011, 11:15 AM
I know about C++ vectors. :)

Hmmm, then why not something using them? :blink:
(Disclaimer: Code not compiled, tested, read after writing, or taken the least bit of care with...)

template<class T> T getresponse(std::string s)
{ // print s, then get and return the users response.
T t;
cout << s << std::endl;
cin >> t;
return t;
}

bool play() // returns true if play again
{
std::vector<int> array;
for (int i=0;i<100;++i)
array.push_back(rand()%1000+1);
std::sort(array.begin(),array.end());
cout << "Array has values from "<<array.front()<<" to "<<array.back() << std::endl;
int guess;
while ( guess=getresponse<int>("Enter guess (or 0 to exit): "))
{ // user didn't type 0.
if (find(array.begin(),array.end(),guess)!=array.end())
{ // found guess in the array.
cout <<"You got it OlegL!" << std::endl;
break;
}
cout << "Nope." << std::endl;
}
char answer = 'z';
while (answer!='y' && answer!='n')
answer = getresponse<char>("Play again? [y/n]: ");
return (answer=='y');
}

int main()
{
srand( time( 0 ) );
while (play()) ;
cout << "Bye."<<std::endl;
}

thaiduythe
10-02-2011, 03:25 PM
complete yet?