Skip to content

Instantly share code, notes, and snippets.

@naezith
Last active July 15, 2017 01:13
Show Gist options
  • Save naezith/0be8f89c62fbbf3fc829 to your computer and use it in GitHub Desktop.
Save naezith/0be8f89c62fbbf3fc829 to your computer and use it in GitHub Desktop.
2014 Fifa World Cup Simulation, Full C Source Code
// This code is pretty old so there might be very useless/wrong stuff.
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
struct Team {
char *name;
unsigned short MP;
unsigned short W;
unsigned short D;
unsigned short L;
unsigned short GF;
unsigned short GA;
unsigned short Pts;
unsigned short Skill;
unsigned short redrawMark;
unsigned short GoalsThisMatch;
};
struct Group {
struct Team Team[5];
};
void gotoXY(int x, int y);
void mainMenu();
void SetGroups();
void ShowRanks();
void RedrawGroups();
void Swap(struct Team *t1, struct Team *t2);
void SortGroupByPts(struct Group *g);
void PrintGroupStatus(struct Group g, int index);
void DisplayRoadMap();
void CheckMatchCount(int limit);
void StartGroupMatches();
void PlayMatch(struct Team *t1, struct Team *t2);
struct Team PlayStageMatch(struct Team *t1, struct Team *t2);
struct Group Group[9];
struct Group redrawGroup[9];
struct Team Round16Winners[9];
struct Team QuarterFinalWinners[5];
struct Team SemiFinalWinners[3];
struct Team FinalWinner;
int main() {
while(1) {
SetConsoleTitle("2 0 1 4 F I F A W O R L D C U P S i m u l a t i o n");
system("COLOR 0E");
SetGroups();
mainMenu();
}
}
void mainMenu() {
system("CLS");
printf(" _...----.._");
printf("\n , :':::::. `>. Tolga Ay");
printf("\n , ' |:::::;' |:::. ");
printf("\n / `'::' :::::\\ ");
printf("\n / _____ `::;\\");
printf("\n : /2014:\\ ` :");
printf("\n | , . /FIFA WC\\ | 2 0 1 4 F I F A W O R L D C U P");
printf("\n |;:::. `::::::;' | ");
printf("\n :::::: `::;' , . ; ");
printf("\n \\:::' , ::::/ S I M U L A T I O N ");
printf("\n \\ \\:::/ ");
printf("\n `. , :. :;' ");
printf("\n `-.::::::.. _.'' ");
printf("\n ```----''' * Skill levels are based on the world rankings *");
printf("\n Menu :\n 1 -> Start Group Matches\n 2 -> Redraw Groups\n 3 -> Rankings\n Selection : ");
int sel;
scanf("%d", &sel);
switch(sel) {
case 1:
StartGroupMatches();
break;
case 2:
RedrawGroups();
break;
case 3:
ShowRanks();
break;
}
}
void SetGroups() {
int i, j;
for(i = 1; i <= 8; i++) {
if(i <= 4)
QuarterFinalWinners[i].name = "<null>";
if(i <= 2)
SemiFinalWinners[i].name = "<null>";
Round16Winners[i].name = "<null>";
for(j = 1; j <= 4; j++) {
Group[i].Team[j].MP = 0;
Group[i].Team[j].W = 0;
Group[i].Team[j].D = 0;
Group[i].Team[j].L = 0;
Group[i].Team[j].GF = 0;
Group[i].Team[j].GA = 0;
Group[i].Team[j].Pts = 0;
Group[i].Team[j].redrawMark = 0;
Group[i].Team[j].GoalsThisMatch = 0;
}
}
FinalWinner.name = "<null>";
Group[1].Team[1].name = "BRAZIL";
Group[1].Team[1].Skill = 29;
Group[1].Team[2].name = "CROATIA";
Group[1].Team[2].Skill = 14;
Group[1].Team[3].name = "MEXICO";
Group[1].Team[3].Skill = 15;
Group[1].Team[4].name = "CAMEROON";
Group[1].Team[4].Skill = 3;
Group[2].Team[1].name = "SPAIN";
Group[2].Team[1].Skill = 32;
Group[2].Team[2].name = "NETHERLANDS";
Group[2].Team[2].Skill = 18;
Group[2].Team[3].name = "CHILE";
Group[2].Team[3].Skill = 20;
Group[2].Team[4].name = "AUSTRALIA";
Group[2].Team[4].Skill = 1;
Group[3].Team[1].name = "COLOMBIA";
Group[3].Team[1].Skill = 28;
Group[3].Team[2].name = "GREECE";
Group[3].Team[2].Skill = 23;
Group[3].Team[3].name = "COTE D'IVO";
Group[3].Team[3].Skill = 13;
Group[3].Team[4].name = "JAPAN";
Group[3].Team[4].Skill = 4;
Group[4].Team[1].name = "URUGUAY";
Group[4].Team[1].Skill = 27;
Group[4].Team[2].name = "COSTA RICA";
Group[4].Team[2].Skill = 8;
Group[4].Team[3].name = "ENGLAND";
Group[4].Team[3].Skill = 22;
Group[4].Team[4].name = "ITALY";
Group[4].Team[4].Skill = 24;
Group[5].Team[1].name = "SWITZERLAND";
Group[5].Team[1].Skill = 25;
Group[5].Team[2].name = "ECUADOR";
Group[5].Team[2].Skill = 10;
Group[5].Team[3].name = "FRANCE";
Group[5].Team[3].Skill = 17;
Group[5].Team[4].name = "HONDURAS";
Group[5].Team[4].Skill = 9;
Group[6].Team[1].name = "ARGENTINA";
Group[6].Team[1].Skill = 26;
Group[6].Team[2].name = "BOSN & HERZ";
Group[6].Team[2].Skill = 12;
Group[6].Team[3].name = "IRAN";
Group[6].Team[3].Skill = 7;
Group[6].Team[4].name = "NIGERIA";
Group[6].Team[4].Skill = 5;
Group[7].Team[1].name = "GERMANY";
Group[7].Team[1].Skill = 31;
Group[7].Team[2].name = "PORTUGAL";
Group[7].Team[2].Skill = 30;
Group[7].Team[3].name = "GHANA";
Group[7].Team[3].Skill = 6;
Group[7].Team[4].name = "USA";
Group[7].Team[4].Skill = 19;
Group[8].Team[1].name = "BELGIUM";
Group[8].Team[1].Skill = 21;
Group[8].Team[2].name = "ALGERIA";
Group[8].Team[2].Skill = 11;
Group[8].Team[3].name = "RUSSIA";
Group[8].Team[3].Skill = 16;
Group[8].Team[4].name = "KOREA REPUBLIC";
Group[8].Team[4].Skill = 2;
}
void ShowRanks() {
system("CLS");
printf("\n -RANKINGS-\n");
printf("\n 1 -> SPAIN 17 -> RUSSIA");
printf("\n 2 -> GERMANY 18 -> MEXICO");
printf("\n 3 -> PORTUGAL 19 -> CROATIA");
printf("\n 4 -> BRAZIL 20 -> COTE D' IVO");
printf("\n 5 -> COLOMBIA 21 -> BOSN & HERZ");
printf("\n 6 -> URUGUAY 22 -> AGERIA");
printf("\n 7 -> ARGENTINA 23 -> ECUADOR");
printf("\n 8 -> SWITZERLAND 24 -> HONDURAS");
printf("\n 9 -> ITALY 25 -> COSTA RICA");
printf("\n 10 -> GREECE 26 -> IRAN");
printf("\n 11 -> ENGLAND 27 -> GHANA");
printf("\n 12 -> BELGIUM 28 -> NIGERIA");
printf("\n 13 -> CHILE 29 -> JAPAN");
printf("\n 14 -> USA 30 -> CAMEROON");
printf("\n 15 -> NETHERLANDS 31 -> KOREA REPUBLIC");
printf("\n 16 -> FRANCE 32 -> AUSTRALIA");
printf("\n\n\n\n Press <ENTER> to display the Main Menu.");
getch();
}
void RedrawGroups() {
int i, j;
for(i = 1; i <= 8; i++) {
redrawGroup[i] = Group[i];
}
for(i = 1; i <= 8; i++)
for(j = 1; j <= 4; j++)
while(Group[i].Team[j].redrawMark == 0) { //randomly tries to put the team to some slot, then marks the team.
unsigned short rg = 1 + rand() % 8;
unsigned short rt = 1 + rand() % 4;
if(redrawGroup[rg].Team[rt].redrawMark == 0) {
Group[i].Team[j] = redrawGroup[rg].Team[rt];
redrawGroup[rg].Team[rt].redrawMark = 1;
Group[i].Team[j].redrawMark = 1;
}
}
printf("\n\n All Groups Randomly Redrawed!");
StartGroupMatches();
}
void Swap(struct Team *t1, struct Team *t2) {
struct Team temp = *t1;
*t1 = *t2;
*t2 = temp;
}
void SortGroupByPts(struct Group *g) {
int i, j;
for(i = 1; i <= 4; i++)
for(j = 1; j <= 3; j++) {
if(g->Team[j].Pts < g->Team[j+1].Pts)
Swap(&g->Team[j] , &g->Team[j+1]);
else if(g->Team[j].Pts == g->Team[j+1].Pts) {
if((g->Team[j].GF - g->Team[j].GA) < (g->Team[j+1].GF - g->Team[j+1].GA))
Swap(&g->Team[j] , &g->Team[j+1]);
}
}
}
void PrintGroupStatus(struct Group g, int index) {
char groupName;
static int currY = 0;
switch(index) {
case 1:
groupName = 'A';
break;
case 2:
groupName = 'B';
break;
case 3:
groupName = 'C';
break;
case 4:
groupName = 'D';
break;
case 5:
groupName = 'E';
break;
case 6:
groupName = 'F';
break;
case 7:
groupName = 'G';
break;
case 8:
groupName = 'H';
break;
}
gotoXY(0, currY+2);
printf(" -> GROUP %c MP W D L GF GA Pts", groupName);
gotoXY(0, currY+3);
printf(" __ _ _ _ __ __ ___");
int i;
for(i = 1; i <= 4; i++) {
gotoXY(0, currY+i+3);
printf(" %s", g.Team[i].name);
gotoXY(35, currY+i+3);
printf(" %d", g.Team[i].MP);
gotoXY(40, currY+i+3);
printf(" %d", g.Team[i].W);
gotoXY(44, currY+i+3);
printf(" %d", g.Team[i].D);
gotoXY(48, currY+i+3);
printf(" %d", g.Team[i].L);
gotoXY(52, currY+i+3);
printf(" %d", g.Team[i].GF);
gotoXY(57, currY+i+3);
printf(" %d", g.Team[i].GA);
gotoXY(63, currY+i+3);
printf(" %d", g.Team[i].Pts);
}
currY += 8;
}
void DisplayRoadMap() {
system("CLS");
int i;
for(i = 0; i <= 6; i += 2) {
gotoXY(11, 1+i*2.5);
printf("%c%c%c%c", 196, 196, 196, 191);
gotoXY(14, 1+i*2.5 + 1);
printf("%c%c", 195, 196); // First 4
gotoXY(11, 1+i*2.5 + 2);
printf("%c%c%c%c", 196, 196, 196, 217);
gotoXY(1, 1+i*2.5 + 2);
printf("%s", Group[i+2].Team[2].name);
gotoXY(1, 1+i*2.5);
printf("%s", Group[i+1].Team[1].name);
gotoXY(11, 21+i*2.5);
printf("%c%c%c%c", 196, 196, 196, 191);
gotoXY(14, 21+i*2.5 + 1);
printf("%c%c", 195, 196); // Second 4
gotoXY(11, 21+i*2.5 + 2);
printf("%c%c%c%c", 196, 196, 196, 217);
gotoXY(1, 21+i*2.5);
printf("%s", Group[i+1].Team[2].name);
gotoXY(1, 21+i*2.5 + 2);
printf("%s", Group[i+2].Team[1].name);
gotoXY(23, i*5+2);
printf("%c%c%c%c", 196, 196, 196, 191);
gotoXY(26, i*5+3);
printf("%c", 179);
gotoXY(26, i*5+4);
printf("%c%c", 195, 196); // Round 16
gotoXY(26, i*5+5);
printf("%c", 179);
gotoXY(26, i*5+6);
printf("%c", 179);
gotoXY(26, i*5+7);
printf("%c", 179);
gotoXY(23, i*5+7);
printf("%c%c%c%c", 196, 196, 196, 217);
gotoXY(16, i*5+7);
printf("%s", Round16Winners[i+2].name);
gotoXY(16, i*5+2);
printf("%s", Round16Winners[i+1].name);
if(i <= 2) {
gotoXY(36, i*10+4);
printf("%c%c%c%c", 196, 196, 196, 191);
gotoXY(39, i*10+5);
printf("%c", 179);
gotoXY(39, i*10+8);
printf("%c", 179);
gotoXY(39, i*10+6);
printf("%c", 179);
gotoXY(39, i*10+7);
printf("%c", 179);
gotoXY(39, i*10+9);
printf("%c%c", 195, 196); // Quarter
gotoXY(39, i*10+10);
printf("%c", 179);
gotoXY(39, i*10+11);
printf("%c", 179);
gotoXY(39, i*10+12);
printf("%c", 179);
gotoXY(39, i*10+13);
printf("%c", 179);
gotoXY(36, i*10+14);
printf("%c%c%c%c", 196, 196, 196, 217);
gotoXY(28, i*10+4);
printf("%s", QuarterFinalWinners[i+1].name);
gotoXY(28, i*10+14);
printf("%s", QuarterFinalWinners[i+2].name);
}
}
gotoXY(50, 9);
printf("%c%c%c%c", 196, 196, 196, 191);
for(i = 10; i <= 18; i++) {
gotoXY(53, i);
printf("%c", 179);
}
gotoXY(53, 19);
printf("%c%c%s", 195, 196, FinalWinner.name); // Semi Finals
for(i = 20; i <= 28; i++) {
gotoXY(53, i);
printf("%c", 179);
}
gotoXY(50, 29);
printf("%c%c%c%c", 196, 196, 196, 217);
gotoXY(41, 29);
printf("%s", SemiFinalWinners[2].name);
gotoXY(41, 9);
printf("%s", SemiFinalWinners[1].name);
gotoXY(50, 37);
}
void CheckMatchCount(int limit) {
static unsigned short matchCounter = 0;
matchCounter++;
if(matchCounter >= limit) {
matchCounter = 0;
int sel;
printf("\n\n\n\n MENU :\n\n 1 -> Play Next Games\n 2 -> Display the Road Map \n Selection : ");
scanf("%d", &sel);
if(sel == 2) {
system("CLS");
DisplayRoadMap();
printf("\n\n Press <ENTER> to play next games.");
getch();
system("CLS");
}
}
}
void StartGroupMatches() {
int matchShowLimit, matchCount = 0;
printf("\n Enter the number of games which would occur at the same time : ");
scanf("%d", &matchShowLimit);
int g, i, j;
int sel;
srand(time(NULL));
for(g = 1; g <= 8; g++) { //plays every group match one by one
for(i = 1; i <= 3; i++)
for(j = i+1; j <= 4; j++) {
PlayMatch(&Group[g].Team[i], &Group[g].Team[j]);
matchCount++;
if(matchCount >= matchShowLimit) { //shows menu
matchCount = 0;
printf("\n\n\n\n MENU :\n\n 1 -> Play Next Games\n 2 -> See the Group Status\n Selection : ");
scanf("%d", &sel);
if(sel == 2) {
system("CLS");
int f;
for(f = 1; f <= 8; f++)
PrintGroupStatus(Group[f], f);
printf("\n\n Press <ENTER> to play next games.");
getch();
}
}
}
SortGroupByPts(&Group[g]);
}
printf("\n\n\n\n MENU :\n\n 1 -> Start the Second Stage \n 2 -> See the Group Status\n Selection : ");
scanf("%d", &sel);
if(sel == 2) {
system("CLS");
int f;
for(f = 1; f <= 8; f++)
PrintGroupStatus(Group[f], f);
printf("\n\n Press <ENTER> to Start the Second Stage");
getch();
}
system("CLS");
printf("\n\n Enter the number of games which would occur at the same time : ");
scanf("%d", &matchShowLimit);
printf("\n\n\n\n -> ROUND OF 16\n\n");
for(i = 0; i <= 6; i += 2) {
Round16Winners[i/2 + 1] = PlayStageMatch(&Group[i+1].Team[1] , &Group[i+2].Team[2]);
CheckMatchCount(matchShowLimit);
Round16Winners[i/2 + 5] = PlayStageMatch(&Group[i+1].Team[2] , &Group[i+2].Team[1]);
CheckMatchCount(matchShowLimit);
}
printf("\n\n\n\n -> QUARTER FINAL\n\n");
for(i = 0; i <= 6; i += 2) {
QuarterFinalWinners[i/2 + 1] = PlayStageMatch(&Round16Winners[i+1] , &Round16Winners[i+2]);
CheckMatchCount(matchShowLimit);
}
printf("\n\n\n\n -> SEMI FINAL\n\n");
SemiFinalWinners[1] = PlayStageMatch(&QuarterFinalWinners[1] , &QuarterFinalWinners[2]);
CheckMatchCount(matchShowLimit);
SemiFinalWinners[2] = PlayStageMatch(&QuarterFinalWinners[3] , &QuarterFinalWinners[4]);
CheckMatchCount(matchShowLimit);
printf("\n\n\n\n -> FINAL\n\n");
FinalWinner = PlayStageMatch(&SemiFinalWinners[1] , &SemiFinalWinners[2]);
printf("\n\n\n -> %s WINS THE CUP! <-", FinalWinner.name);
printf("\n\n Press <ENTER> to display the Road Map.");
getch();
DisplayRoadMap();
printf("\n\n\n\n Press <ENTER> to display the Main Menu.");
getch();
}
void PlayMatch(struct Team *t1, struct Team *t2) {
int i, j;
t1->GoalsThisMatch = (rand() % 8) * t1->Skill/32;
t2->GoalsThisMatch = (rand() % 8) * t2->Skill/32;
t1->GF += t1->GoalsThisMatch;
t1->GA += t2->GoalsThisMatch;
t2->GF += t2->GoalsThisMatch;
t2->GA += t1->GoalsThisMatch;
t1->MP++;
t2->MP++;
printf("\n\n %s %d - %d %s\n", t1->name, t1->GoalsThisMatch, t2->GoalsThisMatch, t2->name);
if(t1->GoalsThisMatch > t2->GoalsThisMatch) {
printf(" %s Wins the match!", t1->name);
t1->W++;
t2->L++;
t1->Pts += 3;
} else if(t1->GoalsThisMatch < t2->GoalsThisMatch) {
printf(" %s Wins the match!", t2->name);
t2->W++;
t1->L++;
t2->Pts += 3;
} else {
printf(" Teams are Tied!");
t1->D++;
t2->D++;
t1->Pts++;
t2->Pts++;
}
}
struct Team PlayStageMatch(struct Team *t1, struct Team *t2) {
int i, j;
t1->GoalsThisMatch = (rand() % 8) * t1->Skill/32;
t2->GoalsThisMatch = (rand() % 8) * t2->Skill/32;
printf("\n\n %s %d - %d %s\n", t1->name, t1->GoalsThisMatch, t2->GoalsThisMatch, t2->name);
if(t1->GoalsThisMatch > t2->GoalsThisMatch) {
printf(" %s Wins the match!", t1->name);
struct Team ret = *t1;
return ret;
} else if(t1->GoalsThisMatch < t2->GoalsThisMatch) {
printf(" %s Wins the match!", t2->name);
struct Team ret = *t2;
return ret;
} else {
printf(" Teams are Tied, playing extra time!");
t1->GoalsThisMatch += (rand() % 3) * t1->Skill/32;
t2->GoalsThisMatch += (rand() % 3) * t2->Skill/32;
if(t1->GoalsThisMatch > t2->GoalsThisMatch) {
printf("\n %s Wins the extra time by %d - %d !", t1->name, t1->GoalsThisMatch, t2->GoalsThisMatch);
struct Team ret = *t1;
return ret;
} else if(t1->GoalsThisMatch < t2->GoalsThisMatch) {
printf("\n %s Wins the extra time by %d - %d !", t2->name, t2->GoalsThisMatch, t1->GoalsThisMatch);
struct Team ret = *t2;
return ret;
} else {
printf("\n Teams are still Tied with %d - %d, Penalties!", t1->GoalsThisMatch, t2->GoalsThisMatch);
unsigned short t1PenaltyScore = 0;
unsigned short t2PenaltyScore = 0;
while(t1PenaltyScore != 5 && t2PenaltyScore != 5) {
t1PenaltyScore += rand() % 2;
t2PenaltyScore += rand() % 2;
if(t1PenaltyScore == 4 && t2PenaltyScore == 4) {
t1PenaltyScore = 0;
t2PenaltyScore = 0;
}
}
if(t1PenaltyScore > t2PenaltyScore) {
printf("\n %s Wins the penalties by %d - %d !", t1->name, t1PenaltyScore, t2PenaltyScore);
struct Team ret = *t1;
return ret;
} else if(t1PenaltyScore < t2PenaltyScore) {
printf("\n %s Wins the penalties by %d - %d !", t2->name, t2PenaltyScore, t1PenaltyScore);
struct Team ret = *t2;
return ret;
}
}
}
}
void gotoXY(int x, int y) {
static HANDLE h = NULL;
if(!h) h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h, c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment