summaryrefslogtreecommitdiffstats
path: root/kblackbox/util.cpp
blob: 7282fe41b6324316a902742772776e6df8c79eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}