avoid checking just the first char

This commit is contained in:
Sandipsinh Rathod 2024-11-19 22:34:14 -05:00
parent 45085e7434
commit e1cd179833
3 changed files with 27 additions and 3 deletions

@ -38,15 +38,26 @@ int main() {
// Read player 2 details
cin >> player2Name >> player2Type;
// Validate player types
if (!equalsIgnoreCase(player1Type, "Random") && !equalsIgnoreCase(player1Type, "Strategic")) {
cerr << "Error: Invalid player 1 type. Must be 'Random' or 'Strategic'." << endl;
return 1;
}
if (!equalsIgnoreCase(player2Type, "Random") && !equalsIgnoreCase(player2Type, "Strategic")) {
cerr << "Error: Invalid player 2 type. Must be 'Random' or 'Strategic'." << endl;
return 1;
}
// Determine player types and initialize gameplay
Player *randomPlayerName, *strategicPlayerName;
if (toLowerCase(player1Type[0]) == 'r' && toLowerCase(player2Type[0]) == 's') {
if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Strategic")) {
randomPlayerName = new RandomPlayer(player1Name);
strategicPlayerName = new StrategicPlayer(player2Name);
} else if (toLowerCase(player1Type[0]) == 's' && toLowerCase(player2Type[0]) == 'r') {
} else if (equalsIgnoreCase(player1Type, "Strategic") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name);
} else if (toLowerCase(player1Type[0]) == 'r' && toLowerCase(player2Type[0]) == 'r') {
} else if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new RandomPlayer(player1Name);
} else {

@ -1,3 +1,13 @@
#include <string>
char toLowerCase(const char c) {
return c | 32;
}
bool equalsIgnoreCase(const std::string &a, const std::string &b) {
if (a.size() != b.size()) return false;
for (size_t i = 0; i < a.size(); ++i) {
if (tolower(a[i]) != tolower(b[i])) return false;
}
return true;
}

@ -1,4 +1,7 @@
#ifndef UTILS_H
#define UTILS_H
#include <string>
char toLowerCase(char c);
bool equalsIgnoreCase(const std::string &a, const std::string &b);
#endif //UTILS_H