Page 1 of 1

Better way to write this

PostPosted: Mon Apr 30, 2012 2:14 pm
by Fojam
is there a better way to write the first part of this code?

http://pastebin.com/Fg79UNrj

basically im comparing the scores of the 4 players and deciding whether to send them into sudden death. if there are 2 pairs of players with the same score, then the higher pair gets sent into sudden death and the lower pair just receives the same rank (i changer winners[0] to 1 when that happens). If all four of them are tied, then all four of them receive the same score and it just says "4 way tie"(i change winners[0] to 2 when that happens).

here are the comments for the suddenDeathPlayers array so you can understand what i did there.

Code: Select all
//suddenDeathPlayers comments
    //0 defines which is the first player that isnt in the sudden death
        //0 means that there are no players that are not in the sudden death
        //1 or more means the player's playerNo thats in the sudden death
    //1 defines the other player that is not in the sudden death
        //0 means that there are no players that are not in the sudden death
        //1 or more means the player's playerNo thats in the sudden death
    //2 defines the amount of players in the sudden death (the new charAmount)
    //3 defines the original charAmount
    //4 defines the fist player's playerNo that is in the sudden death
    //5 defines the second player's playerNo that is in the sudden death
    //6 defines the third player's playerNo that is in the sudden death
        //0 means that there is no third player in the sudden death

Re: Better way to write this

PostPosted: Mon Apr 30, 2012 4:02 pm
by skydereign
Yeah, there are better ways to write that. Essentially what you want the game to know is which actors won (and if more than one, send to sudden death). Here is a simple way of going about it (just to get the idea across [namely it isn't optimized as much as it could be]).
Code: Select all
int max_score = 0;
int player_win[4] = {0};
int win_count = 0; // counts the number of wins
int i;

for(i=0;i<4;i++)
{
    max_score = max(score[i], max_score); // finds the max score
}

for(i=0;i<4;i++)
{
    if(score[i]==max_score) // if this player has the highest score
    {
        player_win[i]=1; // the player won
        win_count++;
    }
}

// now you can check if(win_count is greater than 1 [sudden death])
// else, the player in player_win won

Re: Better way to write this

PostPosted: Mon Apr 30, 2012 5:16 pm
by Fojam
A loop! damn i cant believe i didnt even think of using a loop.. now i feel dumb.