File: Block.hpp

package info (click to toggle)
bastet 0.43.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: cpp: 2,105; sh: 45; makefile: 28; xml: 22
file content (125 lines) | stat: -rw-r--r-- 3,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    Bastet - tetris clone with embedded bastard block chooser
    (c) 2005-2009 Federico Poloni <f.polonithirtyseven@sns.it> minus 37

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef BLOCK_HPP
#define BLOCK_HPP

#include <boost/array.hpp>
#include <curses.h>

namespace Bastet{

  static const int WellHeight=20;
  static const int WellWidth=10;
  static const int RealWellHeight=WellHeight+2;
  
  typedef int Color; //to be given to wattrset
  
  class Orientation{
  public:
    Orientation(unsigned char o=0):_o(o){}
    operator unsigned char() const{
      return _o;
    }
    Orientation operator++(){
      return (++_o & 3);
    }
    Orientation Next() const{
      return ((_o+1) & 3);
    }
    Orientation operator--(){
      return (--_o & 3);
    }
    Orientation Prior() const{
      return((_o-1) & 3);
    }
    const static size_t Number=4;
  private:
    unsigned char _o;
  };

  enum BlockType{
    O=0,
    I=1,
    Z=2,
    T=3,
    J=4,
    S=5,
    L=6
  };
  const size_t nBlockTypes=7;

  struct Dot;

  typedef boost::array<Dot,4> DotMatrix; //the four dots occupied by a tetromino
  typedef boost::array<DotMatrix,4> OrientationMatrix; //the four orientations of a tetromino

  struct Dot{
    int x;
    int y;
    bool IsValid() const{
      return (y>=-2) && y<WellHeight && (x>=0) && (x<WellWidth);
    }

    Dot operator +(const Dot &d) const{
      return (Dot){x+d.x,y+d.y};
    }
    Dot &operator +=(const Dot &d){
      x+=d.x;y+=d.y;
      return *this;
    }
    DotMatrix operator +(const DotMatrix &b) const{
      return (DotMatrix){{*this+b[0],*this+b[1],*this+b[2],*this+b[3]}};
    }
    bool operator==(const Dot &other) const{
      return (x==other.x) && (y==other.y);
    }
    bool operator<(const Dot &other) const{
      if(x==other.x)
	return y<other.y;
      else return x<other.x;
    }

    friend size_t hash_value(const Dot &d); //for use with boost::hash and unordered_set
  };

  class BlockImpl{
  private:
    const OrientationMatrix _matrix;
    const Color _color;
  public:
    BlockImpl(Color c, const OrientationMatrix &m):_matrix(m),_color(c){};
    ~BlockImpl(){};
    /**
     * returns an array of 4 (x,y) pair for the occupied dots
     */
    const OrientationMatrix &GetOrientationMatrix(){return _matrix;}
    Color GetColor() const {return _color;};
  };

  typedef boost::array<BlockImpl,7> BlockArray;
  extern BlockArray blocks;

  //should be members, but BlockType is an enum...
  //  const DotMatrix GetDots(BlockType b, Dot position, Orientation o);
  const Color GetColor(BlockType b);
  const char GetChar(BlockType b);

}

#endif //BLOCK_HPP