2011-02-23 17:20:51
<?php
 
class bullshit {
 
    public $path;
    public $total_engineers;
    public $total_drinks;
    public $engineer = array();
    public $drink = array();
    public $scoreboard;
    public $enable_output = true;
    private $io;
 
    public function __construct($p = NULL) {
        // setup the default variables
        $this->total_engineers = 0;
        $this->total_drinks = 0;
        $this->path = (isset($p)) ? $p : die("Please check the file path is valid or not.");
 
        // initialization
        $this->__readfile();
        $this->__readline(10);
        $this->__assignDrinkToEngineer();
    }
 
    /*
     * read file
     */
 
    public function __readfile() {
        $this->io = fopen($this->path, "r") or die("the path that you've assigned is invalid.({$this->path})");
        flock($this->io, 2);
    }
 
    /*
     * set global total drinks number
     */
 
    private function __setTotalDrinks($value) {
        $this->total_drinks = $value;
    }
 
    /*
     * set global total engineers number
     */
 
    private function __setTotalEngineers($value) {
        $this->total_engineers = $value;
    }
 
    /*
     * convert from string to array using seperator
     */
 
    public function __getValueBySeperator($string, $seperator) {
        $string_array = explode(",", $string);
        return $string_array;
    }
 
    /*
     * We read each line to:
     *      1. define first line to set the total engineer and total drinks ( seperator by a space ) ( total engineer is always even number )
     */
 
    public function __readline($line) {
        $current_row = 0;
        for ($i = 0; !feof($this->io); $i++) { // read to the end of the file
            try {
                // handling the maximum line is permited
                if ($current_row < $line) {
                    if ($current_row == 0) { // handling the first row
                        $element = fgetcsv($this->io, 1000, " ");
                        $this->__setTotalEngineers($element[0]);
                        $this->__setTotalDrinks($element[1]);
 
                        if ($this->total_engineers % 2 != 0)
                            die("Total engineers must be an even number");
                    } else if ($current_row > 0 && $current_row < ($this->total_drinks + 1)) { // rows between total drink range
                        $element = fgetcsv($this->io, 1000, "\t"); // get element by seperator tab
                        array_push($this->drink, $element[1]); // push the value into the drink array
                    } else if ($current_row >= ($this->total_drinks + 1) && $current_row <= ($this->total_engineers + $this->total_drinks + 1)) { // row between engineer ranges
                        $element = fgetcsv($this->io, 1000, "\t"); // get element by seperator tab
                        array_push($this->engineer, $element[1]); // push the value into the engineer array
                    }
 
                    if ($this->enable_output) {
                        echo $element[0] . " " . $element[1] . "<br/>";
                    }
                }
 
                $current_row++; // increase the current row by one
            } catch (Exception $e) {
                echo "The input file is not well-formed: " . $e->getMessage();
            }
        }
    }
 
    /*
     * We need to assign drink number into 2 dimension array which first dimension is id of engineer
     * second dimension indicates id the engineer's drink of choice
     * eg.
     *      Engineer[0][0] = 1
     *      Engineer[0][1] = 0
     *      this engineer has chosen his first drink is 1. second drink of his is 0. And so on.
     */
 
    public function __assignDrinkToEngineer() {
 
        foreach ($this->engineer as $Name => $value) {
            $text = "<br/>";
            if (strlen($value) > 0) {
                if ($this->enable_output) { // debug
                    $text .= "Engineer id[" . $Name . "] = {$value} ";
                }
 
                $drink = $this->__getValueBySeperator($value, ",");
                foreach ($drink as $N => $DrinkValue) {
                    if (strlen($DrinkValue) > 0) {
                        if ($this->enable_output) { // debug
                            $text .= $this->drink[$DrinkValue] . "($DrinkValue) ,";
                        }
 
                        $this->engineer[$Name][$N] = $DrinkValue;
                    }
                }
 
                if ($this->enable_output) { // debug
                    $text = substr($text, 0, -1);
                    echo $text;
                }
            }
        }
    }
 
    public function __setupScoreBoard() {
 
        //Array for chosen engineers
        $chosen = array();
        $total_chosen = 0;
        $flag1 = 0;
 
        //Loop for the upper group of engineers
        for ($i = 0; $i < $this->total_engineers / 2; $i++) {
            //Loop for taking preference score between 2 Engineers
            for ($j = $this->total_engineers - 1; $j > $this->total_engineers / 2 - 1; $j--) {
                //Starting score
                $score = 0;
                //Loop for every drink in the upper group of engineers
                for ($k = 0; $k < $this->total_drinks; $k++) {
                    //Checking for chosen engineers
                    if ($total_chosen > 0) {
                        for ($x = 0; $x < $total_chosen; $x++) {
                            if ($j == $chosen[$x]) {
                                $flag1 = 1;
                            }
                        }
                        if ($flag1 == 1) {
                            $flag1 = 0;
                            break;
                        }
                    }
 
                    if ($this->engineer[$i][$k] == -1)
                        break;
 
                    //Loop for taking preference score between 2 drinks
                    for ($l = 0; $l < $this->total_drinks; $l++) {
                        if ($this->engineer[$j][$l] == -1)
                            break;
                        if ($this->engineer[$i][$k] == $this->engineer[$j][$l]) {
                            //check position
                            if ($k == $l) {
                                $score += ( $this->total_drinks - $k) * ($this->total_drinks - $k);
                                if ($this->enable_output) {
                                    echo("+" . ($this->total_drinks - $k) * ($this->total_drinks - $k) . "<br/>"); //Debug
                                }
                                break;
                            } else if ($k < $l) {
                                $score += ( $this->total_drinks - $k);
                                if ($this->enable_output) {
                                    echo("+" . ($this->total_drinks - $k) . "<br/>"); //Debug
                                }
                                break;
                            } else {
                                break;
                            }
                        } else {
                            if ($l == ($this->total_drinks - 1)) {
                                $score += ( $this->total_drinks - $k);
                                if ($this->enable_output) {
                                    echo("+" . ($this->total_drinks - $k) . "<br/>"); //Debug
                                }
                                break;
                            }
                        }
                    }
                }
 
                echo("E[" . $i . "] and E[" . $j . "] Preference Score:" . $score . "\n"); //Debug purpose
                //Score update
                if ($score >= $this->scoreboard[$i][0]) {
                    $this->scoreboard[$i][0] = $score;
                    $this->scoreboard[$i][1] = $j;
                }
            }
            //Add the chosen
            $chosen[$i] = $this->scoreboard[$i][1];
            echo("E[" . $i . "] choose E[" . $this->scoreboard[$i][1] . "] with Highest Preference Score:" + $this->scoreboard[$i][0] . "\n");
            $total_chosen++;
        }
    }
 
    public function __getEngineerDrinkNames($EngineerID) {
        $text = "";
        for ($i = 0; $i < 3; $i++) {
            $text .= $this->drink[$this->engineer[$EngineerID][$i]] . ",";
        }
        $text = substr($text, 0, -1);
        return $text;
    }
 
}
 
$bullshit = new bullshit("test.txt");
$bullshit->enable_output = true;
 
echo "<h1>DEBUG</h1>";
echo "Total Engineers: {$bullshit->total_engineers}<br/>";
echo "Total Drinks: {$bullshit->total_drinks}<br/>";
 
 
echo ("<h1>SCORED BOARD</h1>");
$bullshit->__setupScoreBoard();
 
echo "<br/>";
for ($i = 0; $i < ($bullshit->total_engineers / 2); $i++) {
    echo($i . " " . $bullshit->scoreboard[$i][1] . "<br/>");
}
 
// or
echo "<br/>";
for ($i = 0; $i < ($bullshit->total_engineers / 2); $i++) {
    echo( "E[{$i}] " . $bullshit->__getEngineerDrinkNames($i) . " _______ E[{$bullshit->scoreboard[$i][1]}] " . $bullshit->__getEngineerDrinkNames($bullshit->scoreboard[$i][1]) . "<br/>");
    echo "<br/>";
}
?>
Invalid Email or Password