Better way to write this

Talk about making games.

Better way to write this

Postby Fojam » Mon Apr 30, 2012 2:14 pm

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
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: Better way to write this

Postby skydereign » Mon Apr 30, 2012 4:02 pm

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
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Better way to write this

Postby Fojam » Mon Apr 30, 2012 5:16 pm

A loop! damn i cant believe i didnt even think of using a loop.. now i feel dumb.
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest