Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2011 18:09
Show Gist options
  • Select an option

  • Save anonymous/1220007 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1220007 to your computer and use it in GitHub Desktop.
A program to match people up randomly with other people for a chess tournament.
//Chess tournament program
//a matchmaking program by Andrenator
#include <iostream>
#include <string>
using namespace std ;
void displayScores () ; //le functions
void displayTierArray () ;
void displayTierBattles ( int ) ;
int findPlayer (int ) ;
const int NumOfPlayers = 8 ; //the hub of the program, lolz. Replace 8 with number of players
int scoreArray[NumOfPlayers] ;
string nameArray[NumOfPlayers] ;
int tierCount[NumOfPlayers] ;
int seedNumber ;
int main ( )
{
string yesNo ;
cout << "Enter the player names, please." << endl ;
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
{
cout << "Player " << loopNum + 1 << "'s name: " ;
cin >> nameArray[loopNum] ;
}
//if players have scores, enter them. if not, everyone gets a 1
do
{
cout << "Do the players have existing scores (y/n)? " ;
cin >> yesNo ;
} while (yesNo != "y" && yesNo != "n" ) ;
if (yesNo == "n")
{
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
scoreArray[loopNum] = 1 ;
}
else
{
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
{
cout << nameArray[loopNum] << "'s score: " ;
cin >> scoreArray[loopNum] ;
}
}
cout << endl ;
displayScores () ;
//this loop generates an array, tierCount, that counts how many people are in each tier
for (int scoreLooking = 1; scoreLooking <= NumOfPlayers; scoreLooking++)
{
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
{
if (scoreArray[loopNum] == scoreLooking)
tierCount[scoreLooking - 1]++ ;
}
}
cout << endl ;
displayTierArray () ;
cout << "\nPlease enter a random seed number (an integer, but the date and time works well): " ;
cin >> seedNumber ;
//this loop inputs the tier number into the function displayTierBattle, up to the maximum tier
for (int loopNum = 1; loopNum <= NumOfPlayers; loopNum++ )
{
if (tierCount[loopNum - 1] > 0)
{
cout << "\nTier " << loopNum << " battles:" << endl ;
displayTierBattles (loopNum) ;
}
}
system ("pause") ;
return 0 ;
}
void displayScores ()
{
for (int loopNum = 0 ; loopNum < NumOfPlayers ; loopNum++)
{
cout << nameArray[loopNum] << ": " ;
cout << scoreArray[loopNum] ;
cout << endl ;
}
}
void displayTierArray ()
{
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
{
cout << loopNum + 1 << "-" << tierCount[loopNum] ;
if (loopNum < NumOfPlayers - 1)
cout << ", " ;
else
cout << endl ;
}
}
void displayTierBattles (int matchLevel )
{
for (tierCount[matchLevel - 1]; tierCount[matchLevel - 1] != 0;)
{
if (tierCount[matchLevel - 1] % 2 == 1) //if there's an odd number, one guy will be sitting it out.
{
cout << nameArray[findPlayer ( matchLevel)] << " will be sitting this one out." << endl ;
}
else //if there's an even number, the loop proceeds to match people up two at a time
{
cout << "*** " ;
cout << nameArray[findPlayer (matchLevel)] ;
cout << " shall hereby battle " ;
cout << nameArray[findPlayer (matchLevel)] ;
cout << "! ***" << endl ;
}
}
}
int findPlayer (int matchLevel)
{
int thisHereRandomNum = rand () % tierCount[matchLevel - 1] ;
for (int loopNum = 0; loopNum < NumOfPlayers; loopNum++)
{
if (scoreArray[loopNum] == matchLevel)
{
if (thisHereRandomNum == 0)
{
tierCount[matchLevel - 1]-- ;
scoreArray[loopNum] = 0 ;
return loopNum ;
}
thisHereRandomNum-- ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment