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

In summary, the player has to answer questions in a given time limit. If they answer correctly, they are given points, and if they answer incorrectly, they lose a point. There are a number of questions per player, and the default number can be changed by passing a different number on the command line when starting the program.
  • #1
nemisis
6
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
  • #2
man if this was in VB, it would take 2 mins
 
  • #3
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:
 
  • #4
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:
 
  • #5
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...
 
  • #6
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
 
  • #7
so anybody has anything to tell me? i don't have much time left with other things in hand......
 
  • #8
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
 

1. What is an object in C++?

An object in C++ is a self-contained unit that contains both data and functions. It is a fundamental concept in the object-oriented programming paradigm, where objects are used to model real-world entities and their interactions.

2. What is the difference between a class and an object in C++?

A class is a blueprint or template for creating objects, while an object is an instance of a class. In other words, a class defines the characteristics and behaviors of an object, while an object is a specific manifestation of those characteristics and behaviors.

3. How do you create an object in C++?

To create an object in C++, you need to first define a class with the desired properties and behaviors. Then, you can use the class to create objects by using the 'new' keyword and assigning the object to a pointer variable.

4. What is encapsulation in C++?

Encapsulation is a principle of object-oriented programming that involves bundling data and functions within a class and controlling access to them through public and private interfaces. This allows for better data protection and code organization.

5. How do you achieve inheritance in C++?

Inheritance in C++ is achieved by using the 'inheritance' keyword in the class definition. This allows a new class to inherit the characteristics and behaviors of an existing class, known as the base class. The new class is called the derived class.

Similar threads

  • Programming and Computer Science
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Math Proof Training and Practice
2
Replies
46
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
17
Views
9K
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top