Hi guys
I have a class called board in which I wrote a really messy function printBoard and another one called printBDrop which do almost the same thing. I can't really find a way of simplifying them so I thought I'd ask here.
Not only does it annoy me that I have two functions that vary only in detail but the function itself is way too long and hard too read.
There's a couple functions that u'r calling often and yes they are long. Since u call this one many times: (double)(myTiles[xCount][yCount].myBuilding.health)/(double)(myTiles[xCount][yCount].myBuilding.maxHealth)
if I'm not mistaken u could call it just once on line 27 and then store that value into something much smaller just as a descriptive placeholder for readability...
The other thing in //actual board is that it looks like all those ifs and else ifs are identical except for the player # that u'r testing. So why not just loop the player # too ?
2. going to change that up with the placeholder thx ;)
3. What I do after the //actual board comment is I check for the players and whether the current tile is a building
then in the nested if i check how much health a building has left.
so you can basically think of it this way:
player1: yellow background color, health>66%: green foreground SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x62);
then:
player1: yellow background color, health>33%: yellow foreground SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x6E);
then:
player1: yellow background color, health>0%: red foreground SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x64);
and so on with the different players
//EDIT:
also I'm thinking about merging the two functions by putting the printBDrop specific part (from line 167 on) into printBoard and making it accessible via a bool parameter? (does that make sense?)
//EDIT2:
and by the way how do people solve saving each version of their code (e.g. making a backup before an important change?). should i just copy the solution's folder or is there a more comfortable way of doing that?
Yes, it looks like the 2 functions should be 1. They both seem to basically have the same flow so unless they need to be called separately they should probably be together. That'll save a good 100 lines right there + make ur code run faster. I'm not sure what u mean by a bool parameter?
i just cycle a few flash drives for backup and backup of backup. nothing worse than losing all that hard work on a single harddisk :/
about 1. again i thought you meant by that that writing
1 2 3 4 5 6 7 8 9 10
if
{
}
else
{
if
{
}
}
for example is better style than
1 2 3 4 5 6 7
if
{
}
elseif
{
}
I've now compressed both functions into one, made the player detection a loop, introduced vectors/iterators for the color selection and created a new variable (percHealth) to keep health/maxHealth.
you can see in the switch statement what I meant by the bool parameter. the bool parameter controls whether the feature of the (initially) copied function is executed.
the part after bDrop is true still doesn't appeal too much to me though, have to change that up as well (also in my main cpp file i execute almost the very same if statements).
I have some ideas to fix the remaining stuff so maybe i'm getting along alone but i'll post again if anything is too hard to solve for me :S
I wrote a really messy function printBoard and another one called printBDrop which do almost the same thing. I can't really find a way of simplifying them so I thought I'd ask here.
Comment their purpose. [edit] so it's easier for us to understand them [/edit]