#include <iostream>
#include <algorithm>
#include <string>
std::string input;
std::getline(std::cin, input);
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
25/10/2019
[C++] Convert string to lower or upper case
While playing with the Rock, Paper, Scissors game, I had to convert a given string to lower case. Turns out, C++ does not have a very straightforward way of doing so, but I found this solution works well since I am not dealing with strange locales:
[C++] Validate string contains only digits and convert it to int
While playing with the Rock, Paper, Scissors game, I had to validate the user input to check only digits where entered at a particular moment. Turns out, C++ does not have a very straightforward way of doing so, but I found this solution works well:
#include <iostream>
#include <string>
std::string input;
std::getline(std::cin, input);
if(!(input.find_first_not_of("0123456789") == std::string::npos)){
cout << "ERROR" << endl;
cin.clear();
}
int entered = stoi(input);
[C++] Good random initialization and generator
While playing with the Rock, Paper, Scissors game, I had to pick a random move from three possible values for the AI. First attempt was:
But I noticed that playing many short games (3 to 5 rounds) in quick succession would yield a fairly common pattern where the AI move would not change often enough.
So I found there is a better way to do so:
#include <ctime>
srand(time(NULL));
int move = rand() % 3;
But I noticed that playing many short games (3 to 5 rounds) in quick succession would yield a fairly common pattern where the AI move would not change often enough.
So I found there is a better way to do so:
#include <random>
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng(seed);
//only 3 possible moves
std::uniform_int_distribution<> dist{0,2};
int move = dist(eng);
[C++] Rock! Paper! Scissors! Fight!
This C++ exercise implements a full game of Rock, Paper, Scissors against an AI opponent. Check the Rock! Paper! Scissors! Fight! project on GitHub.
17/10/2019
[Maven] Run HP Fortify scan during build
If you have Fortify static code analyzer (aka HP Fortify) and intend to use it with your builds, you can configure the sca-maven-plugin to perform the scan and produce a report during the build. It can also optionally upload it to your security server.
[git] Define code owners and enforce review checks
Git (both GitHub and Bitbucket) allows you to specify code owners for a particular repository. This allows you to set up checks that require an approval from any valid owner before the PR can be merged, it also is an easy place where to look up people working on the specific project.
To add code owners, simply check in a file named CODEOWNERS that includes owners in the format
filter owner
and remember that the LAST matching filter has precedence. For example:
* @someuser @someoneelse
*.java @anotheruser
means that someuser and someoneelse are requested to review any PR, while anotheruser is requested to review PRs that touch java files.
After this file is checked in, remember to also activate the PR check for your repo!
To add code owners, simply check in a file named CODEOWNERS that includes owners in the format
filter owner
and remember that the LAST matching filter has precedence. For example:
* @someuser @someoneelse
*.java @anotheruser
means that someuser and someoneelse are requested to review any PR, while anotheruser is requested to review PRs that touch java files.
After this file is checked in, remember to also activate the PR check for your repo!
[Maven] Code coverage during build
If you want to generate code coverage reports during your build, you can use an analyzer that will scan your code and your tests and give you a summary of the tested and untested lines of code.
[Maven] checkstyle during build
If you use autoformatting (and you should) for your code, you might also want to check for formatting violations as part of a build to ensure the style is consistent after every push. We will take a look at how to configure Google style, but this procedure works for any pair of files properly written.
Subscribe to:
Posts (Atom)