Thread: Writing an algorithm that doesn't use to many if clauses

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    Writing an algorithm that doesn't use to many if clauses

    I am pretty new to c++ and I am trying to implement an efficient sorting algorithm. I want this to also work for four players but at the moment I am testing my program for 2 players. Each player has a goals and balls and an id. If for example player 1 has more goals than player 2 player two is the winner. If player 1 and player 2 have the same amount of goals then the player with the most amount of balls is the winner. If player 1 and player 2 have the same amount of goals and balls then the player with the highest id wins.
    This is what I have come up with if there are two players. It's a pretty long if.
    If there are 3 or 4 players I would end up with way to many if clauses which would make my code unreadable ?
    Is there a more efficient way to do this?
    I would really want to know since I plan to do it with three and four players.

    in my example player with id 2 would win since both of them have the same amount of goals but player with id: 2 has more balls.

    Code:
    results[2]; // array or vector of type int
    players[0].goals = 4;
    players[0].balls = 3;
    players[0].id = 1;
    players[1].goals = 4;
    players[1].balls = 4;
    players[1].id = 2;
    
    if(players[0].goals > players[1].goals) {
        results[0] = players[0].id;
        results[1] = players[1].id;
    }
    else if(players[1].goals > players[0].goals) {
        results[0] = players[1].id;
        results[1] = players[0].id;
    }
    if(players[0].goals == players[1].goals) {
        if(players[0].balls > players[1].balls) {
            results[0] = players[0].id;
            results[1] = players[1].id;
        }
        else if(players[1].balls > players[0].balls) {
            results[0] = players[1].id;
            results[1] = players[0].id;
        }
        else if(players[0].balls == players[1].balls) {
            if(players[0].id > players[1].id) {
                results[0] = players[0].id;
                results[1] = players[1].id;
            }
            else {
                 results[0] = players[1].id;
                results[1] = players[0].id;
            }
           
        }
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Instead of using a bunch of ifs, write a proper sort routine (selection or insertion sort, for example, which are good for small containers) or use std::sort.
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
     
    struct Player { int goals, balls, id; };
     
    int main() {
        vector<Player> players {
           { 4, 3, 1 },
           { 4, 4, 2 },
           { 5, 3, 3 },
           { 5, 3, 4 }
        };
     
        sort(players.begin(), players.end(),
            [](const auto& a, const auto& b) {
                if (a.goals > b.goals) return true;
                if (a.goals < b.goals) return false;
                if (a.balls > b.balls) return true;
                if (a.balls < b.balls) return false;
                return a.id > b.id;
            });
     
        for (auto& p: players)
            cout << p.goals << ' ' << p.balls << ' ' << p.id << '\n';
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    Hello, Thank yo for your answer.
    I don't really understand the sorting algorithm
    Why do we return true and why do we return false?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TGA RLE compressing ( writing ) algorithm on c++
    By Bloupies in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2014, 11:00 AM
  2. Replies: 7
    Last Post: 04-23-2012, 07:52 AM
  3. Writing to a file, it doesn't seem to be working for me...
    By porsche911nfs in forum C++ Programming
    Replies: 11
    Last Post: 04-06-2009, 02:57 PM
  4. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  5. Replies: 5
    Last Post: 03-16-2003, 05:58 PM

Tags for this Thread