nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
Array2D.hpp
1#ifndef ARRAY2D_H_DEFINED
2#define ARRAY2D_H_DEFINED
3
4#include <vector>
5#include <iostream> // size_t
6
21template<class T>
23{
24public:
26 Array2D(int width, int height);
27 virtual ~Array2D() { };
28
30 T at(int x, int y)
31 {
32 return contents[x][y];
33 }
34
35 void set(int x, int y, const T& value)
36 {
37 contents[x][y] = value;
38 }
39
41 size_t width();
42
44 size_t height();
45
46private:
48 std::vector<std::vector<T> > contents;
49};
50
51// Damn you templates!
52//
53// I need to leave the function definitions on the header
54// since we need to tell the compiler to create any possible
55// templates for each type called on the whole program.
56
57template <class T>
58Array2D<T>::Array2D(int width, int height)
59{
60 contents.resize(width);
61
62 for (int i = 0; i < width; i++)
63 contents[i].resize(height);
64}
65
66template <class T>
68{
69 return contents.size();
70}
71
72template <class T>
74{
75 return contents[0].size();
76}
77
78#endif //ARRAY2D_H_DEFINED
79
Two-dimensional array.
Definition Array2D.hpp:23
size_t height()
Height size of the array.
Definition Array2D.hpp:73
T at(int x, int y)
Returns element at x y.
Definition Array2D.hpp:30
size_t width()
Width size of the array.
Definition Array2D.hpp:67
Array2D(int width, int height)
Creates a 2D array with width and height.
Definition Array2D.hpp:58