1 /*
2     Bastet - tetris clone with embedded bastard block chooser
3     (c) 2005-2009 Federico Poloni <f.polonithirtyseven@sns.it> minus 37
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef BLOCK_HPP
20 #define BLOCK_HPP
21 
22 #include <boost/array.hpp>
23 #include <curses.h>
24 
25 namespace Bastet{
26 
27   static const int WellHeight=20;
28   static const int WellWidth=10;
29   static const int RealWellHeight=WellHeight+2;
30 
31   typedef int Color; //to be given to wattrset
32 
33   class Orientation{
34   public:
Orientation(unsigned char o=0)35     Orientation(unsigned char o=0):_o(o){}
operator unsigned char() const36     operator unsigned char() const{
37       return _o;
38     }
operator ++()39     Orientation operator++(){
40       return (++_o & 3);
41     }
Next() const42     Orientation Next() const{
43       return ((_o+1) & 3);
44     }
operator --()45     Orientation operator--(){
46       return (--_o & 3);
47     }
Prior() const48     Orientation Prior() const{
49       return((_o-1) & 3);
50     }
51     const static size_t Number=4;
52   private:
53     unsigned char _o;
54   };
55 
56   enum BlockType{
57     O=0,
58     I=1,
59     Z=2,
60     T=3,
61     J=4,
62     S=5,
63     L=6
64   };
65   const size_t nBlockTypes=7;
66 
67   struct Dot;
68 
69   typedef boost::array<Dot,4> DotMatrix; //the four dots occupied by a tetromino
70   typedef boost::array<DotMatrix,4> OrientationMatrix; //the four orientations of a tetromino
71 
72   struct Dot{
73     int x;
74     int y;
IsValidBastet::Dot75     bool IsValid() const{
76       return (y>=-2) && y<WellHeight && (x>=0) && (x<WellWidth);
77     }
78 
operator +Bastet::Dot79     Dot operator +(const Dot &d) const{
80       return (Dot){x+d.x,y+d.y};
81     }
operator +=Bastet::Dot82     Dot &operator +=(const Dot &d){
83       x+=d.x;y+=d.y;
84       return *this;
85     }
operator +Bastet::Dot86     DotMatrix operator +(const DotMatrix &b) const{
87       return (DotMatrix){{*this+b[0],*this+b[1],*this+b[2],*this+b[3]}};
88     }
operator ==Bastet::Dot89     bool operator==(const Dot &other) const{
90       return (x==other.x) && (y==other.y);
91     }
operator <Bastet::Dot92     bool operator<(const Dot &other) const{
93       if(x==other.x)
94 	return y<other.y;
95       else return x<other.x;
96     }
97 
98     friend size_t hash_value(const Dot &d); //for use with boost::hash and unordered_set
99   };
100 
101   class BlockImpl{
102   private:
103     const OrientationMatrix _matrix;
104     const Color _color;
105   public:
BlockImpl(Color c,const OrientationMatrix & m)106     BlockImpl(Color c, const OrientationMatrix &m):_matrix(m),_color(c){};
~BlockImpl()107     ~BlockImpl(){};
108     /**
109      * returns an array of 4 (x,y) pair for the occupied dots
110      */
GetOrientationMatrix()111     const OrientationMatrix &GetOrientationMatrix(){return _matrix;}
GetColor() const112     Color GetColor() const {return _color;};
113   };
114 
115   typedef boost::array<BlockImpl,7> BlockArray;
116   extern BlockArray blocks;
117 
118   //should be members, but BlockType is an enum...
119   //  const DotMatrix GetDots(BlockType b, Dot position, Orientation o);
120   const Color GetColor(BlockType b);
121   const char GetChar(BlockType b);
122 
123 }
124 
125 #endif //BLOCK_HPP
126