avoid checking just the first char
This commit is contained in:
parent
45085e7434
commit
e1cd179833
17
main.cpp
17
main.cpp
@ -38,15 +38,26 @@ int main() {
|
|||||||
// Read player 2 details
|
// Read player 2 details
|
||||||
cin >> player2Name >> player2Type;
|
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
|
// Determine player types and initialize gameplay
|
||||||
Player *randomPlayerName, *strategicPlayerName;
|
Player *randomPlayerName, *strategicPlayerName;
|
||||||
if (toLowerCase(player1Type[0]) == 'r' && toLowerCase(player2Type[0]) == 's') {
|
if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Strategic")) {
|
||||||
randomPlayerName = new RandomPlayer(player1Name);
|
randomPlayerName = new RandomPlayer(player1Name);
|
||||||
strategicPlayerName = new StrategicPlayer(player2Name);
|
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);
|
randomPlayerName = new RandomPlayer(player2Name);
|
||||||
strategicPlayerName = new StrategicPlayer(player1Name);
|
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);
|
randomPlayerName = new RandomPlayer(player2Name);
|
||||||
strategicPlayerName = new RandomPlayer(player1Name);
|
strategicPlayerName = new RandomPlayer(player1Name);
|
||||||
} else {
|
} else {
|
||||||
|
10
utils.cpp
10
utils.cpp
@ -1,3 +1,13 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
char toLowerCase(const char c) {
|
char toLowerCase(const char c) {
|
||||||
return c | 32;
|
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;
|
||||||
|
}
|
3
utils.h
3
utils.h
@ -1,4 +1,7 @@
|
|||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
#include <string>
|
||||||
|
|
||||||
char toLowerCase(char c);
|
char toLowerCase(char c);
|
||||||
|
bool equalsIgnoreCase(const std::string &a, const std::string &b);
|
||||||
#endif //UTILS_H
|
#endif //UTILS_H
|
||||||
|
Loading…
Reference in New Issue
Block a user