Skip to content

Instantly share code, notes, and snippets.

@ArturTan
Last active April 13, 2018 13:33
Show Gist options
  • Save ArturTan/854d41368f112c8c627edbe2c887a12b to your computer and use it in GitHub Desktop.
Save ArturTan/854d41368f112c8c627edbe2c887a12b to your computer and use it in GitHub Desktop.
validCommand() - przykładowa implementacja
bool Saper::validCommand(const std::string& command)
{
//Sprawdź czy input ma maksymalnie 3 znaki (worst case: A7^)
if (command.size() > 3)
{
std::cout << "Wrong format of field\n";
return false;
}
//Sprawdź czy pierwszy znak jest literą pomiędzy A a Z
if (command[0] < 'A' || command [0] > 'Z')
{
std::cout << "Wrong letter of row" << std::endl;
return false;
}
std::string col_number;
//Sprawdź czy w nazwie są flagi
bool is_flagged = (command[command.size()-1] == '^');
if (is_flagged) //Jesli jest flaga, interesuje nas ciąg znaków pomiędy drugim znakiem a przedostatnim znakiem - jest to liczba (np. w przypadku A5^-> tylko 5)
std::copy(command.begin()+1, command.end()-1, col_number.begin());
else
std::copy(command.begin()+1, command.end(), col_number.begin());
;
//Sprawdź czy to co wzięliśmy wyżej jest liczbą
if(!(::isdigit(col_number[0])))
{
std::cout << "Wrong format of field\n";
return false;
}
int col_n = std::stoi(col_number); // Jeśli jest liczbą, to sprawdx czy jest istniejącą kolumną
if (col_n < 0 || col_n> size-1)
{
std::cout << "Wrong no of column" << std::endl;
return false;
}
//Tutaj mamy pewność, że wyrażenie jest poprawne
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment