Skip to main content
GameDev.net gamedev.net
🔒 Locked

Make the code game!

Started by Alpha_ProgDes Mar 2, 2007 at 8:32 PM 6 replies 1.2k views
Original Post
Alpha_ProgDes
Alpha_ProgDes
Well the Lounge has been home to games that tell a story one word, one command, or one sentence at a time. So I wondered why not have a game where everyone adds code to existing code. The code doesn't have to have a point. But! It has to work, ie. be compilable and runnable. We'll use C++, because everyone to some degree knows it. Rules:
  1. It needs to be compilable and runnable!
  2. You can add code -- simple or complex.
  3. You can use any API, but please don't require us to download anything.
  4. This means images, sounds, or stuff in that realm.
  5. (optional) A link to how to setup "x" API would be helpful.
  6. (optional) Explain anything that might seem "advanced".
  7. {As best you can) alter or add code to the code right above you.
  8. If you alter anyone's code, please say note where or whose code it was.
  9. If someone is using prestandard-C++ (ex: ), then please notify them of it. Or feel free to post the correct way yourself.
  10. It needs to be compilable and runnable!
I'll start it off:

#include <iostream>

using namespace std;

int main()
{
   cout << Hello, GameDev! << endl;

   return 0;
}

You can make changes or addition but we should all be able to copy and paste it into our favorite editor+compiler and run it. [Edited by - Alpha_ProgDes on March 3, 2007 9:46:20 PM]
Beginner in Game Development?  Read here. And read here.  
Ravuya
Ravuya
#include <iostream>using namespace std;namespace HelloWorld {   class HelloRunner {   public:      HelloRunner(std::ostream& out) {          out << "Hello, GameDev!" << std::endl;      }   };}int main(){   HelloWorld::HelloRunner g(std::cout);   return 0;}

Someone should probably add templates.
jouley
jouley
Quote:
Original post by Ravuya
*** Source Snippet Removed ***
Someone should probably add templates.

Not it!

#include <iostream>#include <string>using namespace std;namespace HelloWorld {	std::string strUser;	class HelloRunner {	public:		HelloRunner(std::ostream& out) {			out << "Hello, GameDev!" << std::endl;			out << "What's your name, stranger? ";		}		std::string GetInput(std::istream& in) {			std::string strTheInput;			in >> strTheInput;			return strTheInput;		}	};}int main(){	HelloWorld::HelloRunner g(std::cout);	HelloWorld::strUser = g.GetInput(std::cin);	return 0;}
jfclavette
jfclavette
Templates hmm ? We need some friggin object-orientation goodness too.

#include <iostream>#include <string>using namespace std;namespace HelloWorld {        class InputNameProvider {    public:        virtual string getName() = 0;    };    class CachingConsoleInputNameProvider : public InputNameProvider {    string _name;    public:        CachingConsoleInputNameProvider() : _name("") {}        virtual string getName() {            if (_name == "") {                cout << "Enter your name: ";                 cin >> _name;            }            return _name;        }    };    //Every time you instantiate this with a negative integer template argument, God kills a kitten.    template <int N>        class HelloRunnerLoop {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {            HelloRunnerLoop<N-1>    runnerIter(out, nameProvider);            out << "Hello, "<< nameProvider.getName() <<".  Welcome to GameDev!" << std::endl;        }    };    template <>    class HelloRunnerLoop<0> {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {}    };    typedef HelloRunnerLoop<1> HelloRunner;}int main(){    HelloWorld::CachingConsoleInputNameProvider nameProvider;    HelloWorld::HelloRunnerLoop<10> g(std::cout, nameProvider);        return 0;}



Does Code::Block even TRY to be consistent with tabulations/spaces ? I really need to reinstall VC++ Express

[Edited by - jfclavette on March 2, 2007 10:46:26 PM]
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Alpha_ProgDes
Alpha_ProgDes
#include <ctime>#include <cstdlib>#include <iostream>#include <string>using namespace std;class Timer {    public:                 Timer () : initTime(0), elapsedTime(0), lastTime(0)        {                    }        void startTime ()        {            initTime = lastTime;        }        clock_t endTime ()        {               lastTime = clock();            elapsedTime = lastTime - initTime;            initTime = lastTime;            return elapsedTime;        }            private:        clock_t initTime;        clock_t elapsedTime;        clock_t lastTime;};namespace HelloWorld {        class InputNameProvider {    public:        virtual string getName() = 0;    };    class CachingConsoleInputNameProvider : public InputNameProvider {    string _name;    public:        CachingConsoleInputNameProvider() : _name("") {}        virtual string getName() {            if (_name == "") {                cout << "Enter your name: ";                 cin >> _name;            }            return _name;        }    };    //Every time you instantiate this with a negative integer template argument, God kills a kitten.    template <int N>        class HelloRunnerLoop {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {            HelloRunnerLoop<N-1>    runnerIter(out, nameProvider);            out << "Hello, "<< nameProvider.getName() <<".  Welcome to GameDev!" << std::endl;        }    };    template <>    class HelloRunnerLoop<0> {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {}    };    typedef HelloRunnerLoop<1> HelloRunner;}int main(){    Timer GDTimer;    clock_t clock_ticks;        GDTimer.startTime();    HelloWorld::CachingConsoleInputNameProvider nameProvider;    HelloWorld::HelloRunnerLoop<10> g(std::cout, nameProvider);    clock_ticks = GDTimer.endTime();    int inSeconds = clock_ticks / CLOCKS_PER_SEC,         inMilliseconds = inSeconds * 1000;            cout << "It took you " << clock_ticks << " clock ticks and "          << inSeconds << " seconds and approximately "         << inMilliseconds << " milliseconds to say --Hello World--."          << endl;        return 0;}
Beginner in Game Development?  Read here. And read here.  
Crypter
Crypter
I was interested in this one, so gave it a shot.

I added stream handling, and a way of outputting to numerous
locations at once (took a little long):
#include <ctime>#include <cstdlib>#include <iostream>#include <string>#include <vector>using namespace std;class Timer {    public:                 Timer () : initTime(0), elapsedTime(0), lastTime(0)        {                    }        void startTime ()        {            initTime = lastTime;        }        clock_t endTime ()        {               lastTime = clock();            elapsedTime = lastTime - initTime;            initTime = lastTime;            return elapsedTime;        }            private:        clock_t initTime;        clock_t elapsedTime;        clock_t lastTime;};// derive classes from OutLocation and call _Out.AddLocation() to add more// output locations!class OutLocation {	public:		virtual void Write (const string)=0;};// stdoutclass Stdout : public OutLocation {	public:		virtual void Write (const string str) {			cout << str << cout.flush ();		};};class DestStream  : public basic_ostream <char>{	// stream buffer to reroute streams to our destination streams	class StreamBuf : public basic_streambuf <char> {			string m_curMsg;			DestStream* m_pDest;		public:			StreamBuf() : basic_streambuf<char>() {}			void SetDest (DestStream* r) {				m_pDest=r;			}			virtual int_type overflow (int_type c) {				// overloaded from basic_streambuf. automatically called				// when buffer flushes				if (! std::char_traits <char>::eq_int_type (c, char_traits<char>::eof())) {					m_curMsg+=c;					if (c=='\n') {						m_pDest->Write (m_curMsg);						m_curMsg=" ";					}				}				return std::char_traits <char>::not_eof (c);			}	};	vector <OutLocation*> m_locs;	StreamBuf m_stream;	public:		DestStream() : basic_ostream<char>(&m_stream) {			m_stream.SetDest (this);		}		virtual ~DestStream() {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it) {					delete *it;					*it=0;				}			m_locs.clear();		}		virtual void AddLocation (OutLocation* loc) {			if (!loc)				return;			m_locs.push_back (loc);		}		virtual void RemoveLocation (OutLocation* loc) {			if (!loc)				return;			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it == loc) {					m_locs.erase (it);					return;				}		}		virtual void Write (string str) {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				(*it)->Write (str);		}}_Out;namespace HelloWorld {        class InputNameProvider {    public:        virtual string getName() = 0;    };    class CachingConsoleInputNameProvider : public InputNameProvider {    string _name;    public:        CachingConsoleInputNameProvider() : _name("") {}        virtual string getName() {            if (_name == "") {                cout << "Enter your name: ";                 cin >> _name;            }            return _name;        }    };    //Every time you instantiate this with a negative integer template argument, God kills a kitten.    template <int N>        class HelloRunnerLoop {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {            HelloRunnerLoop<N-1>    runnerIter(out, nameProvider);            out << "Hello, "<< nameProvider.getName() <<".  Welcome to GameDev!" << std::endl;        }    };    template <>    class HelloRunnerLoop<0> {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {}    };    typedef HelloRunnerLoop<1> HelloRunner;}int main(){    Timer GDTimer;    clock_t clock_ticks;        GDTimer.startTime();    HelloWorld::CachingConsoleInputNameProvider nameProvider;    HelloWorld::HelloRunnerLoop<10> g(std::cout, nameProvider);    clock_ticks = GDTimer.endTime();    int inSeconds = clock_ticks / CLOCKS_PER_SEC,         inMilliseconds = inSeconds * 1000;	_Out.AddLocation (new Stdout);    _Out << "It took you " << clock_ticks << " clock ticks and "          << inSeconds << " seconds and approximately "         << inMilliseconds << " milliseconds to say --Hello World--."          << endl;    return 0;}

Theres a little bug in it (It prints a number after each endl).
Cookie for anyone who can find it![smile]

(Wow--this is the most complex Hello, World! I ever seen..)
Alpha_ProgDes
Alpha_ProgDes
note: this doesn't have to stay as a "Hello World" app. it can evolve to whatever we like it. however the code has to follow the rules in the OP.

edit: I used Crypter's code with jfclavette's code. It's a slight change to what the output stream is.
#include <ctime>#include <cstdlib>#include <iostream>#include <string>#include <vector>using namespace std;class Timer {    public:                 Timer () : initTime(0), elapsedTime(0), lastTime(0)        {                    }        void startTime ()        {            initTime = lastTime;        }        clock_t endTime ()        {               lastTime = clock();            elapsedTime = lastTime - initTime;            initTime = lastTime;            return elapsedTime;        }            private:        clock_t initTime;        clock_t elapsedTime;        clock_t lastTime;};// derive classes from OutLocation and call _Out.AddLocation() to add more// output locations!class OutLocation {	public:		virtual void Write (const string)=0;};// stdoutclass Stdout : public OutLocation {	public:		virtual void Write (const string str) {			cout << str << cout.flush ();		};};class DestStream  : public basic_ostream <char>{	// stream buffer to reroute streams to our destination streams	class StreamBuf : public basic_streambuf <char> {			string m_curMsg;			DestStream* m_pDest;		public:			StreamBuf() : basic_streambuf<char>() {}			void SetDest (DestStream* r) {				m_pDest=r;			}			virtual int_type overflow (int_type c) {				// overloaded from basic_streambuf. automatically called				// when buffer flushes				if (! std::char_traits <char>::eq_int_type (c, char_traits<char>::eof())) {					m_curMsg+=c;					if (c=='\n') {						m_pDest->Write (m_curMsg);						m_curMsg=" ";					}				}				return std::char_traits <char>::not_eof (c);			}	};	vector <OutLocation*> m_locs;	StreamBuf m_stream;	public:		DestStream() : basic_ostream<char>(&m_stream) {			m_stream.SetDest (this);		}		virtual ~DestStream() {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it) {					delete *it;					*it=0;				}			m_locs.clear();		}		virtual void AddLocation (OutLocation* loc) {			if (!loc)				return;			m_locs.push_back (loc);		}		virtual void RemoveLocation (OutLocation* loc) {			if (!loc)				return;			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it == loc) {					m_locs.erase (it);					return;				}		}		virtual void Write (string str) {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				(*it)->Write (str);		}}_Out;namespace HelloWorld {        class InputNameProvider {    public:        virtual string getName() = 0;    };    class CachingConsoleInputNameProvider : public InputNameProvider {    string _name;    public:        CachingConsoleInputNameProvider() : _name("") {}        virtual string getName() {            if (_name == "") {                cout << "Enter your name: ";                 cin >> _name;            }            return _name;        }    };    //Every time you instantiate this with a negative integer template argument, God kills a kitten.    template <int N>        class HelloRunnerLoop {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {            HelloRunnerLoop<N-1>    runnerIter(out, nameProvider);            out << "Hello, "<< nameProvider.getName() <<".  Welcome to GameDev!" << std::endl;        }    };    template <>    class HelloRunnerLoop<0> {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {}    };    typedef HelloRunnerLoop<1> HelloRunner;}int main(){    Timer GDTimer;    clock_t clock_ticks;    _Out.AddLocation (new Stdout);        GDTimer.startTime();    HelloWorld::CachingConsoleInputNameProvider nameProvider;    HelloWorld::HelloRunnerLoop<10> g(_Out, nameProvider);    clock_ticks = GDTimer.endTime();    int inSeconds = clock_ticks / CLOCKS_PER_SEC,         inMilliseconds = inSeconds * 1000;    _Out << "It took you " << clock_ticks << " clock ticks and "          << inSeconds << " seconds and approximately "         << inMilliseconds << " milliseconds to say --Hello World--."          << endl;        system("PAUSE");    return 0;}


[Edited by - Alpha_ProgDes on March 3, 2007 9:53:58 PM]
Beginner in Game Development?  Read here. And read here.  
Crypter
Crypter
Removed system() to increase portability. (used cin.get() method instead)

Also, fixed little bug from previous post (Everything works fine now.)
The bug was caused by using cout.flush() (instead std::flush) in Stdout::Write()
#include <ctime>#include <cstdlib>#include <iostream>#include <string>#include <vector>using namespace std;class Timer {    public:                 Timer () : initTime(0), elapsedTime(0), lastTime(0)        {                    }        void startTime ()        {            initTime = lastTime;        }        clock_t endTime ()        {               lastTime = clock();            elapsedTime = lastTime - initTime;            initTime = lastTime;            return elapsedTime;        }            private:        clock_t initTime;        clock_t elapsedTime;        clock_t lastTime;};// derive classes from OutLocation and call _Out.AddLocation() to add more// output locations!class OutLocation {	public:		virtual void Write (const string)=0;};// stdoutclass Stdout : public OutLocation {	public:		virtual void Write (const string str) {			// modified from cout.flush			cout << str << flush;		};};class DestStream  : public basic_ostream <char>{	// stream buffer to reroute streams to our destination streams	class StreamBuf : public basic_streambuf <char> {			string m_curMsg;			DestStream* m_pDest;		public:			StreamBuf() : basic_streambuf<char>() {}			void SetDest (DestStream* r) {				m_pDest=r;			}			virtual int_type overflow (int_type c) {				// overloaded from basic_streambuf. automatically called				// when buffer flushes				if (! std::char_traits <char>::eq_int_type (c, char_traits<char>::eof())) {					m_curMsg+=c;					if (c=='\n'||c=='\r') {						m_pDest->Write (m_curMsg);						m_curMsg=" ";					}				}				return std::char_traits <char>::not_eof (c);			}	};	vector <OutLocation*> m_locs;	StreamBuf m_stream;	public:		DestStream() : basic_ostream<char>(&m_stream) {			m_stream.SetDest (this);		}		virtual ~DestStream() {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it) {					delete *it;					*it=0;				}			m_locs.clear();		}		virtual void AddLocation (OutLocation* loc) {			if (!loc)				return;			m_locs.push_back (loc);		}		virtual void RemoveLocation (OutLocation* loc) {			if (!loc)				return;			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				if (*it == loc) {					m_locs.erase (it);					return;				}		}		virtual void Write (string str) {			vector <OutLocation*>::iterator it;			for (it=m_locs.begin(); it!=m_locs.end(); it++)				(*it)->Write (str);		}}_Out;namespace HelloWorld {        class InputNameProvider {    public:        virtual string getName() = 0;    };    class CachingConsoleInputNameProvider : public InputNameProvider {    string _name;    public:        CachingConsoleInputNameProvider() : _name("") {}        virtual string getName() {            if (_name == "") {                cout << "Enter your name: ";                 cin >> _name;            }            return _name;        }    };    //Every time you instantiate this with a negative integer template argument, God kills a kitten.    template <int N>        class HelloRunnerLoop {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {            HelloRunnerLoop<N-1>    runnerIter(out, nameProvider);            out << "Hello, "<< nameProvider.getName() <<".  Welcome to GameDev!" << endl;        }    };    template <>    class HelloRunnerLoop<0> {    public:        HelloRunnerLoop(std::ostream& out, InputNameProvider& nameProvider) {}    };    typedef HelloRunnerLoop<1> HelloRunner;}int main(){    Timer GDTimer;    clock_t clock_ticks;    _Out.AddLocation (new Stdout);        GDTimer.startTime();    HelloWorld::CachingConsoleInputNameProvider nameProvider;    HelloWorld::HelloRunnerLoop<10> g(_Out, nameProvider);    clock_ticks = GDTimer.endTime();    int inSeconds = clock_ticks / CLOCKS_PER_SEC,         inMilliseconds = inSeconds * 1000;    _Out << "It took you " << clock_ticks << " clock ticks and "          << inSeconds << " seconds and approximately "         << inMilliseconds << " milliseconds to say --Hello World--."          << endl;	_Out << "Press any key to quit..." << endl;	int c=cin.get ();	cin.get();    return 0;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.