Added code
This commit is contained in:
77
Domain/Array2.cpp
Normal file
77
Domain/Array2.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Array2.cpp
|
||||
*
|
||||
* Created on: May 5, 2013
|
||||
* Author: chibi_000
|
||||
*/
|
||||
|
||||
#include "Array2.h"
|
||||
#include <cstring>
|
||||
|
||||
Array2::Array2(int w, int h)
|
||||
{
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
this->data = new int[w * h];
|
||||
|
||||
memset(this->data, 0, w * h * sizeof(int));
|
||||
}
|
||||
|
||||
Array2::Array2(int w, int h, const int* vals)
|
||||
{
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
this->data = new int[w * h];
|
||||
|
||||
memcpy(this->data, vals, w * h * sizeof(int));
|
||||
}
|
||||
|
||||
Array2::Array2(const Array2& a)
|
||||
{
|
||||
this->w = a.w;
|
||||
this->h = a.h;
|
||||
this->data = new int[w * h];
|
||||
|
||||
memcpy(this->data, a.data, w * h * sizeof(int));
|
||||
}
|
||||
|
||||
Array2& Array2::operator= (const Array2& a)
|
||||
{
|
||||
if (this != &a)
|
||||
{
|
||||
delete this->data;
|
||||
|
||||
this->w = a.w;
|
||||
this->h = a.h;
|
||||
this->data = new int[w * h];
|
||||
|
||||
memcpy(this->data, a.data, w * h * sizeof(int));
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Array2::~Array2()
|
||||
{
|
||||
delete this->data;
|
||||
}
|
||||
|
||||
int Array2::get(int x, int y) const
|
||||
{
|
||||
return this->data[y * this->w + x];
|
||||
}
|
||||
|
||||
void Array2::set(int x, int y, int value)
|
||||
{
|
||||
this->data[y * this->w + x] = value;
|
||||
}
|
||||
|
||||
int Array2::getWidth() const
|
||||
{
|
||||
return this->w;
|
||||
}
|
||||
|
||||
int Array2::getHeight() const
|
||||
{
|
||||
return this->h;
|
||||
}
|
37
Domain/Array2.h
Normal file
37
Domain/Array2.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Array2.h
|
||||
*
|
||||
* Created on: May 5, 2013
|
||||
* Author: chibi_000
|
||||
*/
|
||||
|
||||
#ifndef ARRAY2_H_
|
||||
#define ARRAY2_H_
|
||||
|
||||
class Array2 {
|
||||
|
||||
private:
|
||||
int* data;
|
||||
int w, h;
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
Array2(int w, int h);
|
||||
Array2(int w, int h, const int* vals);
|
||||
Array2(const Array2&);
|
||||
virtual ~Array2();
|
||||
|
||||
// Getters
|
||||
int get(int x, int y) const;
|
||||
int getWidth() const;
|
||||
int getHeight() const;
|
||||
|
||||
// Setters
|
||||
void set(int x, int y, int value);
|
||||
|
||||
// Assignment operator
|
||||
Array2& operator= (const Array2& a);
|
||||
};
|
||||
|
||||
#endif /* ARRAY2_H_ */
|
45
Domain/Piece.cpp
Normal file
45
Domain/Piece.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Piece.cpp
|
||||
*
|
||||
* Created on: May 5, 2013
|
||||
* Author: chibi_000
|
||||
*/
|
||||
|
||||
#include "Piece.h"
|
||||
|
||||
// Constructors
|
||||
Piece::Piece() : Array2(1, 1)
|
||||
{
|
||||
}
|
||||
|
||||
Piece::Piece(int w, int h) : Array2(w, h)
|
||||
{
|
||||
}
|
||||
|
||||
Piece::Piece(int w, int h, const int* vals) : Array2(w, h, vals)
|
||||
{
|
||||
}
|
||||
|
||||
Piece::Piece(const Piece& p) : Array2(p)
|
||||
{
|
||||
}
|
||||
|
||||
Piece::~Piece()
|
||||
{
|
||||
}
|
||||
|
||||
// Rotate
|
||||
Piece Piece::rotate() const
|
||||
{
|
||||
// Create a new piece
|
||||
Piece dest(getHeight(), getWidth());
|
||||
|
||||
// Rotate 90 degrees
|
||||
for (int x = 0; x < getWidth(); x++)
|
||||
for (int y = 0; y < getHeight(); y++)
|
||||
dest.set(getHeight() - y - 1, x, get(x, y));
|
||||
|
||||
// Return new piece
|
||||
return dest;
|
||||
}
|
||||
|
27
Domain/Piece.h
Normal file
27
Domain/Piece.h
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Piece.h
|
||||
*
|
||||
* Created on: May 5, 2013
|
||||
* Author: chibi_000
|
||||
*/
|
||||
|
||||
#ifndef PIECE_H_
|
||||
#define PIECE_H_
|
||||
|
||||
#include "Array2.h"
|
||||
|
||||
class Piece : public Array2 {
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
Piece();
|
||||
Piece(int w, int h);
|
||||
Piece(int w, int h, const int* vals);
|
||||
Piece(const Piece& p);
|
||||
virtual ~Piece();
|
||||
|
||||
// Rotate
|
||||
Piece rotate() const;
|
||||
};
|
||||
|
||||
#endif /* PIECE_H_ */
|
Reference in New Issue
Block a user