All pastes #185623 Raw Edit

Stuff

public text v1 · immutable
#185623 ·published 2006-09-29 03:50 UTC
rendered paste body
template <class CellClass>
class BaseGrid
{public:
    /* snip snip snip */
    // A slew of functions allow us to probe the grid with ease
    template <bool(*F)(CellClass c)> bool HaveNeighbor(int x, int y) {
        return F(Get(x-1,y-1)) || F(Get(x  ,y-1)) || F(Get(x+1,y-1)) ||
               F(Get(x-1,y  ))          ||           F(Get(x-1,y  )) ||
               F(Get(x-1,y+1)) || F(Get(x  ,y+1)) || F(Get(x+1,y-1));
    }

    template <bool(*F)(CellClass c)> int CountNeighbors(int x, int y) {
        return F(Get(x-1,y-1)) + F(Get(x  ,y-1)) + F(Get(x+1,y-1)) +
               F(Get(x-1,y  ))          +          F(Get(x-1,y  )) +
               F(Get(x-1,y+1)) + F(Get(x  ,y+1)) + F(Get(x+1,y-1));
    }
};

class MachineGrid : public BaseGrid<char>
{public:
    /* snip snip snip */
    // Various functions for classifying cells and probing the grid
    bool IsSpark(char c) {return c == WireWorldSpark;}
    int CountSparks(char c) {return IsSpark(c)?1:0;}

    int CountNeigboringSparks(int x, int y)
@@    {return CountNeighbors<CountSparks>(x,y);}
};

/*
error:

src/machine.h: In member function `int Game::MachineGrid::CountNeigboringSparks(int, int)':
src/machine.h:17: error: 'int Game::MachineGrid::CountSparks(char)' cannot appear in a constant-expression

*/