-4

I am writing a game simulation that tests if any piece is on the board. If a piece is not I would like the AI to place a piece on the board, for this I created a bool function to test if all the pieces are set to 0 which means they are yet to enter the board. The current function boots, but I feel there is a much simpler way to do this:

bool checkPiece(int a[])
{
int n = 0;
bool e = true;
while (e == true && n < 4)
{
    if (a[n] == 0 )
    {
        n++;
    }
    else
    {
        return false;
    }
}
return true;
}
Ravaen
  • 3
  • 2
  • 9
    What does `e` do? – François Andrieux Jun 18 '18 at 15:58
  • And why not the regular `for` loop? – SergeyA Jun 18 '18 at 16:01
  • Euler may have screamed a bit "Eeeeeee" when the decision was made to keep only the 'e' and start calling it Napier's constant. Otherwise it just puts an uncomfortable gap in the nut alphabet: Almonds, Brazil nuts, Cashews, Date, "eeeeek", Filberts, Ginko Nuts, Hazel Nuts, Indian Almond... – Dr t Jun 18 '18 at 17:44

4 Answers4

3

I'd use the standard library, something on this general order:

bool checkPiece(int const *a) { 
    return std::all_of(a, a+4, [](int i) { return i == 0; });
}

If you really wanted to do the job on your own, perhaps something on this order:

bool checkPiece(int const *a) { 
    for (int i=0; i<4; i++)
        if (a[i] != 0)
            return false;
    return true;
}

Most of the time, you'd also rather pass something collection-like, such as an std::array or std::vector (by const reference), or something range-like such as a gsl::span rather than a pointer though. This would (for one obvious example) make it trivial to get the size of what was passed instead of blindly assuming was 4 items.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Better yet: take an `std::array const&` (C++11) as argument, or `std::span` (C++20). – YSC Jun 18 '18 at 16:02
2

This is basically all you need:

for (size_t n = 0; n < 4; ++n) {
    if (a[n]) return false;
}
return true;

You dont need e and when iterating an array from begin till end (or return before) a for loop is easier to read and write than a while. You could use a algorithm, but I doubt that it will make your code more readable and you should avoid magic numbers like the plague (what if you ever change the size of the array to be something else than 4?).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

You can solve this with little code using std::count.

bool checkPiece(int const *a) { 
    return std::count(a, a+4, 0) == 4;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

First, don't use magic numbers instead of passing array sizes. If you change the size of your board you'll have to find every 4 in your program and decide whether it's actually the number 4 or the size of the array. Pass the size to your function.

Second, give the function a name that describes what it does. checkPiece doesn't tell me anything.

And when you've made those changes, use standard algorithms:

bool noPieces(int const *a, int size) {
    return std::all_of(a, a + size, [](int pc) { return pc == 0; }
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165