Two functions for the ELU Rating system - Printable Version +- howtothings.co.uk (https://www.howtothings.co.uk) +-- Forum: Computing (https://www.howtothings.co.uk/forumdisplay.php?fid=4) +--- Forum: Programming Support, Graphic Design and Tutorials (https://www.howtothings.co.uk/forumdisplay.php?fid=14) +--- Thread: Two functions for the ELU Rating system (/showthread.php?tid=776) |
Two functions for the ELU Rating system - Drumm - 22-02-2011 Code for the ELU Rating system, explained here: http://mcompute.co.uk/showthread.php?tid=764&pid=6363#pid6363 Code requires math.h to be imported for the pow() and round() functions. Updated for a who's won picker. Also for those wondering (Pack3t) I've done some thinking, and there is a technical limit. When it gets to a point where the percentage chance of the other is > 0.5% (Due to the round function, so really 0%) then nothing changes. This program also allows for you to enter a long string, E.G I could enter 111 to signify Pack3t won 3 times or 2222 to signify Sharn won 4 times. [code=c] #include <stdio.h> #include <math.h> #include <stdlib.h> #define maxcandidates 20 #define maxlength 15 #define CONFIGURATION "configuration.txt" int elupercentage( int Ra, int Rb ); void eluNewBR( int *winner, int *loser, int winpercent, int losepercent ); int scanfile(); void savefile(int savelimit); int baserating[maxcandidates]; char candidate[maxcandidates][maxlength]; main(){ int percent1, percent2; int limit = scanfile(); int x, y; char c; int badchar = 0; do{ if(!badchar){ x = rand() % limit; y = rand() % limit; while ( x == y ) y = rand() % limit; printf("\n > For %s to win enter 1\n", candidate[x]); printf(" > For %s to win enter 2\n", candidate[y]); printf(" > Hit 3 to save and exit\n\n"); percent1 = elupercentage( baserating[x], baserating[y] ); percent2 = elupercentage( baserating[y], baserating[x] ); printf("Base Rating of %s: %d\n", candidate[x], baserating[x]); printf("Base Rating of %s: %d\n", candidate[y], baserating[y]); printf("Chance of %s winning: %d%% \n", candidate[x], percent1); printf("Chance of %s winning: %d%% \n", candidate[y], percent2); } badchar = 0; c = getchar(); if (c == '1') eluNewBR( &baserating[x], &baserating[y], percent1, percent2 ); else if (c == '2') eluNewBR( &baserating[y], &baserating[x], percent1, percent2 ); else if (c == '3') savefile(limit-1); else badchar = 1; }while(1); } int elupercentage( int Ra, int Rb ){ return round(100 / ( 1+pow(10, (((float)Rb - (float) Ra) / 400)))); } void eluNewBR( int *winner, int *loser, int winpercent, int losepercent ){ *winner += 100 - winpercent; *loser -= losepercent; } int scanfile(){ FILE *config = fopen(CONFIGURATION, "r"); int z, y, i, x = 0; char buffer[5]; //Allows for BR up to 99999 int username = 1; if (config == NULL){ printf("Error!\n"); exit(1); } for(i = 0;; i++){ z = fgetc(config); if (z == '\n' || z == EOF){ if (username == 1) username = 0; else if (username == 0){ baserating[x] = atoi(buffer); x++; for( y = 0; y <= 5; y++) buffer[y] = 0; username = 1; } i = -1; if ( z == EOF ) break; } else { if (username == 1){ candidate[x][i] = z; } else if(username == 0){ buffer[i] = z; } } } fclose(config); return x; } void savefile(int savelimit){ FILE *config = fopen(CONFIGURATION, "w"); int length, place, position = 0; char buffer[5]; //Matching previous buffer. for ( place = 0; place <= savelimit; position = 0, place++){ while ( candidate[place][position] != '\0' ){ fputc( candidate[place][position], config ); position++; } fputc('\n', config); length = sprintf ( buffer, "%d", baserating[place] ); for ( position = 0; position <= length; position++ ) fputc(buffer[position], config); fputc('\n', config); } fclose(config); exit(1); } [/code] RE: Two functions for the ELU Rating system - Drumm - 23-02-2011 Updated the code so it doesn't produce warnings when compiling. I did this by changing \% for %% in the printf statements. RE: Two functions for the ELU Rating system - Drumm - 24-02-2011 UPDATE: Changed my example of how the code can be used so that it allows you to pick a winner, see first post. RE: Two functions for the ELU Rating system - Drumm - 27-02-2011 UPDATE: Added a configuration.txt option, It MUST be formatted like so: Username Baserating Username Baserating Etc. etc. RE: Two functions for the ELU Rating system - Mark - 27-02-2011 Mark 10000000000000 RE: Two functions for the ELU Rating system - Drumm - 28-02-2011 I haven't got my same file with me, But if I remember correctly it's something like: Pack3t 700 Sharn 500 Latch 600 Mark 250 Drumm 100 RE: Two functions for the ELU Rating system - Mark - 28-02-2011 Wait.. Does that mean Sharn is more likely to get rated over myself and you? RE: Two functions for the ELU Rating system - Pack3t SynAck3r - 02-03-2011 It would appear that is the case.....but yet not over me. As a side note it would seem someone quietly took a lot of suggestions from an old fart, good times. RE: Two functions for the ELU Rating system - Drumm - 02-03-2011 You were my guidance on this Pack3t :tongue. When you ain't 100% confident in what you're doing, it's good to have someone that can say "Yeah, that will work" |