nsnake
Classic snake game for the terminal
Array2D.hpp
1 #ifndef ARRAY2D_H_DEFINED
2 #define ARRAY2D_H_DEFINED
3 
4 #include <vector>
5 #include <iostream> // size_t
6 
21 template<class T>
22 class Array2D
23 {
24 public:
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 
46 private:
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 
57 template <class T>
59 {
60  contents.resize(width);
61 
62  for (int i = 0; i < width; i++)
63  contents[i].resize(height);
64 }
65 
66 template <class T>
68 {
69  return contents.size();
70 }
71 
72 template <class T>
74 {
75  return contents[0].size();
76 }
77 
78 #endif //ARRAY2D_H_DEFINED
79 
size_t width()
Width size of the array.
Definition: Array2D.hpp:67
Two-dimensional array.
Definition: Array2D.hpp:22
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
Array2D(int width, int height)
Creates a 2D array with width and height.
Definition: Array2D.hpp:58