1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream> //std::cout , std::cin , std::streamsize
#include <limits> //std::numeric_limits
#include <cstdlib> //rand() , srand()
#include <ctime> //time
#include <algorithm> //std::max_element()
#include <vector> //std::vector<>
//Defining the Gunman Structure
struct Gunman
{
/**Declaring new values for: *
*Acuracy , Kills , Deaths ,*
*Wins , KDR and Win Rate **/
unsigned short position ,accuracy ,
kills , deaths , wins , killDeathRate ,
winRatio;
bool alive;
/**Constructing a new gunman *
*Also setting default value**/
Gunman( void ) :
position( 0 ) , accuracy( 0 ) , kills( 0 ),
deaths( 0 ) , wins( 0 ) , killDeathRate( 0 ) ,
winRatio( 0 ) , alive( true ) { }
};
void clearBuff( void )
{
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max() , '\n' );
}
//Defining the MainMenu
const unsigned char mainMenu( void )
{
//Initializing invalid choice to true
bool invalid( true );
//initializing mode to an empty char
unsigned char selectedMode = char();
do
{
//setting invalid choice to false
//Getting input
invalid = false;
std::cout << "Welcome to the shooting statistics application.\n" << std::flush;
std::cout << "Which mode would you like?\n"
<< "1 - Duel between two people\n"
<< "2 - Multiple people battling\n> " << std::flush;
std::cin >> selectedMode;
clearBuff();
//invalid selection displaying message and restarting loop
if( selectedMode != '1' && selectedMode != '2' )
{
std::cout << "Invalid selection please make another.\n" << std::flush;
invalid = true;
}
} while( invalid );
//returning the selected mode
return( selectedMode );
}
//Setting options
const unsigned char settings( const unsigned int x )
{
//setting invalid choice to true
bool invalid( true );
//input character and short are set to empty
unsigned char inputChar = char();
do
{
//setting to valid choice and getting input
invalid = false;
switch( x )
{
//Gettting the way to assign accuracies
case 1:
std::cout << "How would you like accuracies assigned?\n"
<< "1 - Randomly\n"
<< "2 - Manually\n> " << std::flush;
std::cin >> inputChar;
clearBuff();
//invalid choice
if( inputChar != '1' && inputChar != '2' )
{
std::cout << "Invalid selection please make another\n" << std::flush;
invalid = true;
}
break;
//Getting the order to battle in
case 2:
std::cout << "Which order would you like them to shoot?\n"
<< "1 - Descending Accuracy\n"
<< "2 - Ascending Accuracy\n"
<< "3 - Descending Position\n"
<< "4 - Ascending Position\n> " << std::flush;
std::cin >> inputChar;
clearBuff();
//invalid choice
if( inputChar != '1' && inputChar != '2' && inputChar != '3' && inputChar != '4' )
{
std::cout << "Invalid selection please make another\n" << std::flush;
invalid = true;
}
break;
case 3:
std::cout << "Which order would you like them to target?\n"
<< "1 - Most Accurate\n"
<< "2 - Least Accurate\n"
<< "3 - Descending Position\n"
<< "4 - Ascending Position\n> " << std::flush;
std::cin >> inputChar;
clearBuff();
if( inputChar != '1' && inputChar != '2' && inputChar != '3' && inputChar != '4' )
{
std::cout << "Invalid selection please make another\n" << std::flush;
invalid = true;
}
}
} while( invalid );
//returning the settings
return( inputChar );
}
//setting up the stats for the gunman
void stats( std::vector<Gunman> &gMen , const unsigned short POPULATION , const unsigned short METHOD )
{
//setting input to an invalid choice , and the iterator to 0
unsigned short inputShort( 101 ) , i( 0 );
switch( METHOD )
{
//Assigning Random Accuracies
case 1:
for( i = 0; i < POPULATION; ++i )
{
//assigning accuracy
gMen[ i ].accuracy = rand() % 100 + 1;
}
break;
//Assigning Manual Accuracies
case 2:
for( i = 0; i < POPULATION; ++i )
{
do
{
std::cout << "Please enter the accuracy for Gunman " << i + 1 << "<1-100>\n> " << std::flush;
std::cin >> inputShort;
clearBuff();
//invalid
if( inputShort > 100 )
{
std::cout << "Invalid selection please make another\n" << std::flush;
}
} while( inputShort < 1 || inputShort > 100 );
//assigning accuracy
gMen[ i ].accuracy = inputShort;
}
break;
//assigning position
case 3:
for( i = 0; i < POPULATION; ++i )
{
gMen[ i ].position = i;
}
break;
}
}
void positionGunmen( std::vector<Gunman> &gMen , const unsigned short SHOOTORDER )
{
switch( SHOOTORDER )
{
case 1: //Descending Accuracy
std::stable_sort( gMen.begin() , gMen.end() , []( const Gunman &lhs , const Gunman &rhs ) { return( lhs.accuracy > rhs.accuracy ); } );
break;
case 2: //Ascending Accuacy
std::stable_sort( gMen.begin() , gMen.end() , []( const Gunman &lhs , const Gunman &rhs ) { return( lhs.accuracy < rhs.accuracy ); } );
break;
case 3: //Descending Position
std::stable_sort( gMen.begin() , gMen.end() , []( const Gunman &lhs , const Gunman &rhs ) { return( lhs.position > rhs.position ); } );
break;
case 4: //Ascending Position
//its already like this by default
break;
}
}
const unsigned short targetFunction( const unsigned short TARGET , const std::vector<Gunman> gMen )
{
unsigned short position = 0;
const auto cmp1 = []( const Gunman &lhs , const Gunman &rhs ){ return( lhs.accuracy < rhs.accuracy && lhs.alive && rhs.alive ); };
const auto cmp2 = [ position , cmp1 ]( const Gunman &lhs , const Gunman &rhs ){ return( cmp1( lhs , rhs ) && lhs.position != position && rhs.position != position ); };
switch( TARGET )
{
case 1: //least accurate
position = std::min_element( gMen.begin() , gMen.end() , cmp1 )->position;
break;
case 2: //most Accurate
position = std::max_element( gMen.begin() , gMen.end() , []( const Gunman &lhs , const Gunman &rhs ){ return( lhs.accuracy < rhs.accuracy && lhs.alive && rhs.alive ); } )->position;
break;
case 3:
//second least
position = std::min_element( gMen.begin() , gMen.end() , cmp1 )->position;
position = std::min_element( gMen.begin() , gMen.end() , cmp2 )->position;
}
return( position );
}
|