diff options
Diffstat (limited to 'kblackbox/util.cpp')
-rw-r--r-- | kblackbox/util.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/kblackbox/util.cpp b/kblackbox/util.cpp new file mode 100644 index 00000000..7282fe41 --- /dev/null +++ b/kblackbox/util.cpp @@ -0,0 +1,68 @@ +// +// +// KBlackBox +// +// A simple game inspired by an emacs module +// +// File: util.cpp +// +// The implementation of the RectOnArray class +// + +#include "util.h" + +RectOnArray::RectOnArray( int newWidth, int newHeight ) +{ + w = newWidth; + h = newHeight; + array = new ArrayType[w*h]; +} + +RectOnArray::~RectOnArray() +{ + delete[] array; +} + +/* + Size info... +*/ + +int RectOnArray::width() { return w; } +int RectOnArray::height() { return h; } + +/* + Utility function for mapping from 2D table to 1D array +*/ + +int RectOnArray::indexOf( int col, int row ) const +{ + return (row * w) + col; +} + +/* + Return content of cell +*/ + +ArrayType RectOnArray::get( int col, int row ) +{ + return array[indexOf( col, row )]; +} + +/* + Set content of cell +*/ + +void RectOnArray::set( int col, int row, ArrayType type ) +{ + array[indexOf( col, row )] = type; +} + +/* + Fill all cells witj type +*/ + +void RectOnArray::fill( ArrayType type ) +{ + int i; + for (i = 0; i < w*h; i++) array[i] = type; +} |