nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
src
Misc
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>
58
Array2D<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
66
template
<
class
T>
67
size_t
Array2D<T>::width
()
68
{
69
return
contents.size();
70
}
71
72
template
<
class
T>
73
size_t
Array2D<T>::height
()
74
{
75
return
contents[0].size();
76
}
77
78
#endif
//ARRAY2D_H_DEFINED
79
Array2D
Two-dimensional array.
Definition
Array2D.hpp:23
Array2D::height
size_t height()
Height size of the array.
Definition
Array2D.hpp:73
Array2D::at
T at(int x, int y)
Returns element at x y.
Definition
Array2D.hpp:30
Array2D::width
size_t width()
Width size of the array.
Definition
Array2D.hpp:67
Array2D::Array2D
Array2D(int width, int height)
Creates a 2D array with width and height.
Definition
Array2D.hpp:58
Generated on Thu Jul 20 2023 00:00:00 for nsnake by
1.12.0