hi, can anyone help me with my code, any help will be much appreciated. The design should follow object oriented principles and use the UML tools. It should be a board game based on a “Monopoly-style” board made of 36 squares. The game is for two players who first enter their names and then take turns. On each turn, a die is thrown and the score dictates how many squares the player can move around the board. Each time a player moves past the "Start" square they collect a "token". Play continues until one player has collected five tokens. The user interface can be a console application with all input and output made through a “command line” prompt.
There should be submited: use case diagram(s), candidate and refined class names, classes, class diagram(s), either a collaboration diagram or a sequence diagram. A basic solution to the programming part of the task would operate in a console window with text prompts and outputs indicating what dice score had been rolled. At the design stage there can be produced CRC cards for the classes, and both the sequence diagram and the collaboration diagram. Here is the code but I think it needs some improvements to meet the criteria:
[

#include <iostream>
#include <ctime>

class cPlayer
{
      public:
             int pos;
             int pts;
};

cPlayer p1, p2;

int random(int nMin, int nMax)
{ return rand() % (nMax - nMin + 1) + nMin; }

int main()
{
    srand(static_cast<unsigned int>(time(NULL)));
    
    p1.pos = 0 ; p1.pts = 0 ;
    p2.pos = 0 ; p2.pts = 0 ;
    
    int i = 0 ;
    
    while(true)
    {
                 i = random(1,6);
                 std::cout << "Player 1 (is on the square " << p1.pos << ") rolled: " << i << std::endl;
                 while(i > 0)
                 {
                         p1.pos++;
                         if(p1.pos == 36)
                         {
                                   p1.pos = 0;
                                   p1.pts++;
                                   if(p1.pts == 5) break;
                         }
                         i--;
                 }
                 std::cout << "Player 1 came to square " << p1.pos << " (" << p1.pts << " token)" << std::endl;
                 if(p1.pts == 5) break;
                 
                 i = random(1,6);
                 std::cout << "Player 2 (is on the square " << p2.pos << ") rolled: " << i << std::endl;
                 while(i > 0)
                 {
                         p2.pos++;
                         if(p2.pos == 36)
                         {
                                   p2.pos = 0;
                                   p2.pts++;
                                   if(p2.pts == 5) break;
                         }
                         i--;
                 }
                 std::cout << "Player 2 came to square " << p2.pos << " (" << p2.pts << " token)" << std::endl;
                 if(p2.pts == 5) break;
    }
    
    if(p1.pts == 5) std::cout << "The winner is Player 1" << std::endl;
    if(p2.pts == 5) std::cout << "The winner is Player 2" << std::endl;
    
    system("PAUSE");
    return 0;
}
/]

Recommended Answers

All 16 Replies

If you can't tell if it needs improvements, how can we tell?

When posting code/questions, you need to tell us what you need help with, not leave it to us to figure out if something is wrong.

If you can't tell if it needs improvements, how can we tell?

When posting code/questions, you need to tell us what you need help with, not leave it to us to figure out if something is wrong.

The code seems to work as a computer to computer game, and I need a minimum one real player. There is also a problem because I do not known how to add any OO as required.

If you're skipping over the design phase completely you're doing yourself a disservice. All of the work with the use cases, CRC cards, etc. should be done before you write a single line of code, that way you can include the OO principles from the ground up.

commented: Absolutely! +20

Is there any possibility to change the code and have the OO etc added?

Is there any possibility to change the code and have the OO etc added?

Yes

Yes

Thank you, I thought so. May I ask you for some more help how to do it?

Check the thread "Error in code"

Check the thread "Error in code"

Thre is no error in the code showing while compiled. I just need to add some more things to the code as I wrote before and I need some help.

i don't get it

i don't get it

The code is working ok as a computer against computer game, but I need to change it to have a minimum one real player. There is also a problem because I do not known how to add any OO as required and the CRC cards for the classes, the sequence diagram and the collaboration diagram.

Why don't you make an asking system ... that's basic simple and stupid + it really works

Why don't you make an asking system ... that's basic simple and stupid + it really works

Thank you, but I have really no idea how to do it :( dick head at the moment :(

Changing it to use a human player simply means that any information that the computer player generates must come from the keyboard.

Where in the code do you generate the data for a player's move? Take that code and put an IF (player == computer) statement around it. Then make an else to accept input from the user.

Be sure to make a variable to keep track of the type of player, or make, say, player 1 always be the human. Change the IF accordingly.

Thre is no error in the code showing while compiled. I just need to add some more things to the code as I wrote before and I need some help.

I dont think, you wud hv seen the thread "Error In Code". There I hv given the solution for your "Board Game" thread . I hope you will be happy when u see that thread.

commented: Speak English! "wud hv" looks like swahili! -2

I dont think, you wud hv seen the thread "Error In Code". There I hv given the solution for your "Board Game" thread . I hope you will be happy when u see that thread.

honestly are you retarded? he just misspelled at the end /] , and that doesn't matter

honestly are you retarded? he just misspelled at the end /] , and that doesn't matter

Hi Robert(precocious), did I say he misspelled or there was an error in the code???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.