File: Well.cpp

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 (101 lines) | stat: -rw-r--r-- 2,656 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
/*
    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/>.
 */

#include "Well.hpp"
#include <cassert>
#include <cstring>
#include <boost/foreach.hpp>
#include <sstream>

namespace Bastet{

  std::string WellLine::PrettyPrint() const{
    std::string s;
    s.reserve(this->size());
    for(unsigned int i=0;i<this->size();++i)
      s.push_back(operator[](i)?'#':' ');
    return s;
  }

  Well::Well(){
    Clear();
  }
  
  Well::~Well(){
  }

  void Well::Clear(){
    BOOST_FOREACH(WellLine &l, _well){
      l.reset();
    }
  }

  bool Well::Accomodates(const DotMatrix &m) const{
    BOOST_FOREACH(const Dot &d, m){
      if(!d.IsValid() || _well[d.y+2][d.x]==true) return false;
    }
    return true;
  }

  bool Well::IsLineComplete(int y) const{
    for(int x=0;x<(int)WellWidth;++x)
      if(_well[y+2][x]==false)
	return false;
    return true;
  }

  LinesCompleted Well::Lock(BlockType t, const BlockPosition &p){
    if(p.IsOutOfScreen(t))
      throw(GameOver());
    BOOST_FOREACH(const Dot &d,p.GetDots(t)){
      _well[d.y+2][d.x]=true;
    }
    //checks for completedness
    LinesCompleted lc;
    lc._baseY=p.GetBaseY();
    for(int k=0;k<4;++k){
      int l=lc._baseY+k;
      if(IsValidLine(l) && IsLineComplete(l))
	lc._completed[k]=true;
    }
    return lc;
  }
  
  void Well::ClearLines(const LinesCompleted &completed){
    WellType::reverse_iterator it=completed.Clear(_well.rbegin(),_well.rend());
    for(;it!=_well.rend();++it){
      it->reset();
    }
  }

  int Well::LockAndClearLines(BlockType t, const BlockPosition &p){
    LinesCompleted lc=Lock(t,p);
    ClearLines(lc);
    return lc._completed.count();
  }
  
  std::string Well::PrettyPrint() const{
    std::ostringstream str;
    str<<std::string(WellWidth+2,'-')<<'\n';
    BOOST_FOREACH(const WellLine &l, _well)
      str<<'|'<<l.PrettyPrint()<< "|\n";
    str<<std::string(WellWidth+2,'-');
    return str.str();
  }
}