Skip to content

Instantly share code, notes, and snippets.

@eisterman
Created October 30, 2017 18:07
Show Gist options
  • Save eisterman/38bd4d9162e2abe7ef1535f9419a17bf to your computer and use it in GitHub Desktop.
Save eisterman/38bd4d9162e2abe7ef1535f9419a17bf to your computer and use it in GitHub Desktop.
GameOfLife_eisterman
#ifndef TABLE_H
#define TABLE_H
#include <vector>
template <class T>
class Table
{
public:
Table(); //Tavola 1x1
Table(int x, int y);
T get(int x, int y);
int getBoardX(){return BoardX;}
int getBoardY(){return BoardY;}
void set(int x, int y, T value);
void clear();
void resize(int x, int y); //ridimensiona e cancella tutto
private:
int BoardX;
int BoardY;
std::vector<std::vector<T> > m_table;
};
template <class T>
Table<T>::Table()
{
BoardX = 1;
BoardY = 1;
m_table.push_back(std::vector<T>(1));
}
template <class T>
Table<T>::Table(int x, int y)
{
if(x < 1 || y < 1) throw "Dimension must be greater than 1";
BoardX = x;
BoardY = y;
for (int i=0; i< BoardY; i++) //Se volevo potevo anche far fare a m_table
m_table.push_back(std::vector<T>(BoardX));
}
template <class T>
T Table<T>::get(int x, int y)
{
if(x < 0 || y < 0) throw "Coordinates must be positive";
if(x >= BoardX || y >= BoardY) throw "Coordinates exceede the table dimensions";
return m_table[x][y];
}
template <class T>
void Table<T>::set(int x, int y, T value)
{
if(x < 0 || y < 0) throw "Coordinates must be positive";
if(x >= BoardX || y >= BoardY) throw "Coordinates exceede the table dimensions";
m_table[x][y] = value;
}
template <class T>
void Table<T>::clear()
{
m_table.clear();
for (int i=0; i< BoardX; i++)
m_table.push_back(std::vector<T>(BoardY));
}
template <class T>
void Table<T>::resize(int x, int y)
{
if(x < 1 || y < 1) throw "Dimension must be greater than 1";
BoardX = x;
BoardY = y;
clear();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment