File: Well.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 (94 lines) | stat: -rw-r--r-- 3,051 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
/*
    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 WELL_HPP
#define WELL_HPP

#include "Block.hpp" //for Color
#include "BlockPosition.hpp"
#include <cstddef> //size_t
#include <vector>
#include <bitset>
#include <boost/array.hpp>

//DBG
#include <iostream>

namespace Bastet{
  
  class GameOver{}; //used as an exception

  class WellLine: public std::bitset<WellWidth>{
  public:
    std::string PrettyPrint() const;
  };

  ///complex type that holds which lines are completed
  ///if _completed[k]==true, then line _baseY+k exists and is completed
  class LinesCompleted{
  public:
    int _baseY;
    std::bitset<4> _completed;
    ///clear, returns iterator such that the segment [it, rend) is "new" (to be zeroed out by hand)
    template<typename Iterator> Iterator Clear(Iterator rbegin, Iterator rend) const;
  };
  

  /*
   * the real height of the well is _height+2, with the top two rows( -1 and -2) hidden (see guidelines)
   */
  class Well{
  private:
    typedef boost::array<WellLine,RealWellHeight> WellType;
    WellType _well;
  public:
    Well();
    ~Well();
    void Clear();
    bool Accomodates(const DotMatrix &d) const; //true if the given tetromino fits into the well
    bool IsValidLine(int y) const{return (y>=-2) && (y<WellHeight);};
    bool IsLineComplete(int y) const;
    LinesCompleted Lock(BlockType t, const BlockPosition &p); //permanently adds a tetromino to the well; returns a bitset of 4 bits where return[i]==1 iff line (start of fb)+i is complete
    void ClearLines(const LinesCompleted &lc); //removes the given lines from the well (whether they are completed or not)
    int LockAndClearLines(BlockType t, const BlockPosition &p); //locks, clear lines, returns number of lines cleared
    friend long Evaluate(const Well *w, int extralines); //for BastetBlockChooser
    std::string PrettyPrint() const;
  };
  
  template <typename Iterator> Iterator LinesCompleted::Clear(Iterator rbegin, Iterator rend) const{
    if(_completed.none()) return rend;
    Iterator orig=rbegin;
    Iterator dest=rbegin;
    int j=WellHeight-1;
    while(orig<rend){
      if(j-_baseY>=0 && j-_baseY<4 && _completed[j-_baseY]){
	//skip
      }
      else{
	*dest=*orig;
	dest++;
      }
      j--;
      orig++;
    }
    return dest++;
  }

}

#endif //WELL_HPP