Hi toad1359,
I mentioned the firstline feature so you could edit your posts, so we could refer to your code with the correct line numbers. Can you also fix the tabs? In your IDE set indenting to be 4 spaces - not tabs, then reformat it, & edit your post by re-pasting your code . Some places in your code has 24 spaces worth of indenting.
If there is a bunch of code that is very similar for p1 & p2, can you figure out a way of writing one function that deals with both? If you find yourself repeating code, then you are probably doing something wrong.
You could have an array of strings that represent each tile in your monopoly game
(could have put that in the topic title). Then you could have a function that prints out the appropriate string based on it's number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
unsigned short Player1Roll = 0; // Always initialise variables
const std::string[] Tile = {
"You are on Jacobs field.",
"You are on Community chest. Please take a card.",
// etc for all the rest
}
void PrintTile(const unsigned short TileNum) {
std::cout << Tile[TileNum] << "\n";
//calling the function
PrintTile(Player1Roll);
}
|
Declare you all your functions before main - hopefully in the same order that you call them, then define them in the same order after main. If you do this, you might find you have less variables in main, as more of them could be local to the functions.
Also with variable & function names, don't abbreviate them too much. Well named variables assist greatly in self documenting the code, and in other peoples understanding. People shouldn't have to ask what r1t or r2t means. Names like Player1, Player2, Player1Roll are much better names.
With functions, it is a good idea to have a divide & conquer approach. There was a rule that any function (including main) should be no longer than 80 LOC, this was so a function could be printed on 1 A4 page. These days 40LOC is more acceptable. Candidates for functions could include the compound statements that go with if-else if- else, loops, and switches. So rather than have a bunch of code in an if statement, call a function instead. With switches, have each case call a function. This idea also helps with understanding, as one can have a higher level view rather than a bunch of detail.
Hopefully all this info will help you write better code :+)