3 * Author: Petr Kubiznak
7 #include "../exceptions/GeneralException.h"
8 #include "../exceptions/AccessForbiddenException.h"
10 /* -------------------------------------------------------------------------- */
12 /* implicitni konstruktor (vytvori zakryte pole bez miny a oznaceni */
13 Field::Field(unsigned short int val
) {
17 /* -------------------------------------------------------------------------- */
22 /* -------------------------------------------------------------------------- */
24 /* vraci true, pokud je nastaven mine-bit */
25 bool Field::hasMine(void) const {
26 return data
& MASK_MINE
;
29 /* -------------------------------------------------------------------------- */
31 /* danemu poli nastavi mine-bit, zda-li je pritomna mina */
32 void Field::setMine(bool setVal
) {
33 data
= (data
& (~MASK_MINE
)) | (setVal
? MASK_MINE
: 0);
36 /* -------------------------------------------------------------------------- */
38 /* vraci true, pokud je nastaven mark-bit (pole oznaceno jako zaminovane) */
39 bool Field::hasMark(void) const {
40 return data
& MASK_MARK
;
43 /* -------------------------------------------------------------------------- */
45 /* danemu poli nastavi mark-bit podle hodnoty setVal */
46 void Field::setMark(bool setVal
) {
47 if(!isCovered()) throw AccessForbiddenException("Field is already uncovered, cannot be marked.");
48 data
= (data
& (~MASK_MARK
)) | (setVal
? MASK_MARK
: 0);
51 /* -------------------------------------------------------------------------- */
53 /* vraci true, pokud je zadane pole neodkryte - je nastaven covered-bit */
54 bool Field::isCovered(void) const {
55 return data
& MASK_COVERED
;
58 /* -------------------------------------------------------------------------- */
60 /* nastavi covered-bit */
61 void Field::setCovered(bool setVal
) {
62 data
= (data
& ~MASK_COVERED
) | (setVal
? MASK_COVERED
: 0);
65 /* -------------------------------------------------------------------------- */
67 int Field::getNeighboursCnt(void) const {
68 if(isCovered()) throw AccessForbiddenException("");
69 else return (int) ((data
& MASK_NEIGHBOURS
) >> MASK_NEIGHBOURS_SHIFT
);
72 /* -------------------------------------------------------------------------- */
74 void Field::setNeighboursCnt(int count
) {
75 count
= max(count
, 0);
76 data
= (data
& ~MASK_NEIGHBOURS
) | ((count
<< MASK_NEIGHBOURS_SHIFT
) & MASK_NEIGHBOURS
);
79 /* -------------------------------------------------------------------------- */
81 int Field::getRemainingNeighboursCntNE(void) const {
82 int sign
= (data
& MASK_REMAINING_SIGN
? -1 : 1);
83 return sign
* ((int) ((data
& MASK_REMAINING
) >> MASK_REMAINING_SHIFT
));
86 /* -------------------------------------------------------------------------- */
88 int Field::getRemainingNeighboursCnt(void) const {
89 if(isCovered()) throw AccessForbiddenException("");
90 else return getRemainingNeighboursCntNE();
93 /* -------------------------------------------------------------------------- */
95 void Field::setRemainingNeighboursCnt(int count
) {
96 data
= (data
& ~MASK_REMAINING_SIGN
) | (count
< 0 ? MASK_REMAINING_SIGN
: 0);
97 count
= (count
>= 0 ? count
: -count
);
98 data
= (data
& ~MASK_REMAINING
) | ((count
<< MASK_REMAINING_SHIFT
) & MASK_REMAINING
);
101 /* -------------------------------------------------------------------------- */
103 void Field::fixRemainingNeighboursCnt(int increment
) {
104 setRemainingNeighboursCnt(getRemainingNeighboursCntNE() + increment
);
107 /* -------------------------------------------------------------------------- */
109 /* odkryje zadane pole a vrati pocet min v jeho okoli */
110 int Field::uncover(void) {
112 return getNeighboursCnt();
115 /* -------------------------------------------------------------------------- */
117 /* pretypovani pole na int vraci vnitrni reprezentaci - hodnotu var "data" */
118 Field::operator int (void) const {