C/C++ C++ Object-Oriented Assignment: Test Your Motion Knowledge

AI Thread Summary
The discussion centers around a C++ assignment that involves creating an interactive game where players respond to motion verbs associated with entities. The program requires players to answer within a half-second timeframe, scoring points for correct answers and losing points for incorrect ones or no response. The original poster seeks help with coding the game, expressing frustration with C++ compared to other programming languages. They share an initial code structure and some class definitions but encounter compilation errors related to string handling. The conversation highlights the challenges of object-oriented programming in C++ and the need for guidance on both coding and suitable compilers.
nemisis
Messages
5
Reaction score
0
Hi Everyone I have a object oriented C++ assignment which is due this week and I have no idea as to how start it....

This program prints a motion verb ( fly, run, swim, crawl, walk, or roll ), waits for a second,
then prints the name of an entity and repeats the verb (for example: fly ..1s.. pigeon fly!.. ).
The player has ½ second to type ‘y’ for yes or ‘n’ for no. (case insensitive)

- If the answer is correct, the player scores, if incorrect, or if there was no answer within the ½ second, the
player looses a point. When correct, all the motions the entity can perform are printed.

- When wrong, Ka..BOOM! is printed instead.

The default number of questions per player is 20, but the number can be changed by passing a different
number on the command line when starting the program.


At the end of the run the player’s score (and the score of previous players if there were any) is printed, the
question: “Another player (Y/N): “ is asked. The game exits if ‘n’ is typed.

The output should look something like this:-

Player 1 starting.
You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
Press [Enter] when ready to start:

1 – fly pigeon fly!.. y
- I walk - I fly
2 – swim Wheelbarrow swim.. n
- I roll
3 – crawl ball crawl!..
Ka...BOOM!
4 – fly plane fly!.. y
- I roll - I fly.
5 – run boat run!.. n
- I roll.
6 – walk engine walk!..
Ka..BOOM!
7 – roll stone roll!.. y
- I roll.
. . .
16- crawl time crawl!.. y
- I crawl - I fly.
17- swim goose swim!.. n
Ka..BOOM!
18- run lizard run!.. y
- I run - I crawl.
19- run nose run.. y
- I run
20- fly goose fly!.. y
- fly - I walk – I run – I swim.

Player 1 *** score: 16 ***

Well done! Start another player (Y or N)? y

Player 2 starting
You must answer with the letter ‘y’ for YES, or ‘n’ for NO.
Press [Enter] when ready to start:

1 – crawl giraffe crawl!.. y
Ka..BOOM!





I am not good at all in coding so i tried and made an overview of what the main driver should look like


main{
start_function(a,b)
{
check here wherther the player is old or new------>Connector

where a = value of yes or no, taken by cin>>
if the vaue is yes...
}
you are back in main >>>here

now start the for...while/Do...while[(condition for 1/2 min)& this shld take care of the initial 30 seconds also]
{
call function2(loop for 20 questions)
else if right answer add a point
Also call a function to print what the object does (for ball, it will be I ROLL)

keep adding ( + or -)points in a variable

End for loop of questions
}

display final score
ask to replay ------------>connector
}


But as said above I have no idea on the coding part of the whole program and would like some help here please

Also apart from this can anyone suggest a good compiler, I use Visual Basic C++ if that's good

Thanks in advance:cool:
 
Technology news on Phys.org
man if this was in VB, it would take 2 mins
 
If you were doing it in a non-OO language, one obvious way to go would be encode the definitions of what objects can do what actions in a 2-D array, and the names of the objects and actions in two 1-D arrays. Apart from the data, the program would then be about 20 lines long (including comments).

But in C++, you can make it far more complcated than that by using templates to create collections of classes with multiple inheritance, or whatever... :rolleyes:
 
yup C would have been easier, I got friends who are doing Java too..... but C++ is one language in which i can't get the codes, it just freaks me out. but I am still strying, working on the code and will post it when i haven made some(or any) progress at all.

Anyways thanks guys for replying still if any1 can will help a lot:rolleyes:
 
If you have borland turbo cpp 3, then you could use dos.h which has a delay(time in ms) function. However, I don't know what the corresponding function would be in ansi cpp or whatever.. I am still getting used to it...

Anyway, you could create a class which stores the question, and its answer, use the delay function (or one like it), and test the input... sorry, I am not much help...
 
Srry i use Microsoft visual studio 8...... hehehehehe guess i am too lost to remember that too, anyhow anybody who can code the game program in c++ still find it tough to understand
 
so anybody has anything to tell me? i don't have much time left with other things in hand......
 
here is something new

Motion.h

Code: ( cpp )

#ifndef MOTION_H
#define MOTION_H

#include <iostream>
#include <string>

using namespace std;

class Motion
{
friend ostream &operator << (ostream &out, const Motion &m);

public:
Motion();
virtual ~Motion(){};
virtual string *toString() const = 0;
virtual bool contains(const string *key) const = 0;

};


#endif

Motion.cc

Code: ( cpp )

#include <iostream>
#include <sstream>
#include <string>
#include "Motion.h"

ostream &operator << (ostream &out, const Motion &m)

{
out << "- I " << m.toString();

return out;
}FlyMotion.h

Code: ( cpp )

#ifndef FLYMOTION_H
#define FLYMOTION_H

#include <iostream>
#include <string>

using namespace std;


#include "Motion.h"

class FlyMotion : public Motion
{

public:
static FlyMotion* instance();
virtual string *toString() const;
virtual bool contains(const string *key) const;

protected:
FlyMotion();
FlyMotion(const FlyMotion&);
~FlyMotion(){};

private:
static FlyMotion *inst;
// return &inst;

};

#endif
FlyMotion.cc

Code: ( cpp )

#include <iostream>
#include <sstream>
#include <string>
#include "FlyMotion.h"

using namespace std;

FlyMotion* FlyMotion::inst = 0;

FlyMotion::FlyMotion()
{
inst = 0;
}

FlyMotion* FlyMotion::instance()
{

if(inst == 0)
{
inst = new FlyMotion;
}

return inst;

}

string* FlyMotion::toString() const
{
ostringstream outStr;

outStr << Motion::toString() << "fly " << flush;

return outStr.str();

}

After compiling FlyMotion.cc this is what i get

Flymotion.cc: In member function 'virtual std::string* FlyMotion::toString() const'
line30: cannot convert 'std::basic_string<char, std::char_traits<char> std::allocator<char> >' to 'std::string*' in return
 

Similar threads

Replies
1
Views
4K
Replies
6
Views
3K
Replies
2
Views
8K
Replies
9
Views
3K
Replies
5
Views
2K
Replies
9
Views
2K
Replies
2
Views
2K
Replies
46
Views
13K
Back
Top