summaryrefslogtreecommitdiff
path: root/noncore/games/zlines/field.cpp
blob: 0adf4acf5d7c7b69f9ff4e9a3d47f652e95242ac (plain)
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
126
127
128
129
130
131
/***************************************************************************
  field.cpp  -  description
  -------------------
begin                : Fri May 19 2000
copyright            : (C) 2000 by Roman Merzlyakov
email                : roman@sbrf.barrt.ru
copyright            : (C) 2000 by Roman Razilov
email                : Roman.Razilov@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <stdlib.h>
#include "field.h"

Field::Field(QWidget* parent, const char* name)
	: QWidget( parent, name )
{
	clearField();
}

Field::~Field()
{
}

void Field::clearField()
{
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
			field[y][x].clear();
}
void Field::clearAnim()
{
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
			field[y][x].setAnim( ANIM_NO );
}
int Field::deleteAnimatedBalls()
{
	int deleted = 0;
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
		{
			if ( field[y][x].getAnim() != ANIM_NO )
			{
				deleted++;
				field[y][x].clear();
			}
		}
	return deleted;
}

bool Field::checkBounds(int x, int y)
{
	return (x>=0) && (x<NUMCELLSW) && (y>=0) && (y<NUMCELLSH);
}

void Field::putBall(int x, int y, int color)
{
	if( checkBounds(x,y) )
		field[y][x].setColor(color);
}
/*
   void Field::putBall(int x, int y, char color)
   {
   if( checkBounds(x,y) ){
   field[y][x].setColor(color);
   erase5Balls();
   repaint(FALSE);
   }
   }*/
void Field::moveBall(int xa, int ya, int xb, int yb)
{
	if( checkBounds(xa,ya) && checkBounds(xb,yb) &&
			field[yb][xb].isFree() && ( xa != xb || ya != yb) ) {
		field[yb][xb].moveBall(field[ya][xa]);
	}
}

int Field::getBall(int x, int y)
{
	if( checkBounds(x,y) )
		return field[y][x].getColor();
	else 
		return NOBALL;
}
int Field::getAnim(int x, int y)
{
	if( checkBounds(x,y) )
		return field[y][x].getAnim();
	else 
		return NOBALL;
}
void Field::setAnim(int x, int y, int anim)
{
	if( checkBounds(x,y) )
		field[y][x].setAnim( anim );
}

void Field::removeBall(int x, int y )
{
	if( checkBounds(x,y) ) field[y][x].clear();
}

int Field::freeSpace()
{
	int s = 0;
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
			if(field[y][x].isFree()) s++;
	return s;
}
void Field::saveUndo()
{
	void clearAnim();
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
			field_undo[y][x] = field[y][x];
}
void Field::restoreUndo()
{
	for(int y=0; y<NUMCELLSH; y++)
		for(int x=0; x<NUMCELLSW; x++)
			field[y][x] = field_undo[y][x];
}