summaryrefslogtreecommitdiffstats
path: root/klines/field.cpp
blob: d8da8ec771411347f72939edfe3f6862470f8402 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
    begin                : Fri May 19 2000
    copyright            : (C) 2000 by Roman Merzlyakov
    email                : [email protected]
    copyright            : (C) 2000 by Roman Razilov
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "cfg.h"
#include "field.moc"

Field::Field(TQWidget* parent, const char* name)
    : TQWidget( 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];
}

int Field::calcRun(int sx, int sy, int dx, int dy)
{
  int ix = sx;
  int iy = sy;
  int run = 0;
  int score = 0;
  int current_color = NOBALL;
  int lpr = 0;
  int lprscore = 0;
  int empty = 0;
  for(int i = 0; i < 9; i++, ix += dx, iy += dy)
  {
    if ((ix < 0) || (iy < 0) || (ix >= NUMCELLSW) || (iy >= NUMCELLSH))
       continue;
    int b = field[iy][ix].getColor();
//printf("[%d,%d]%d", ix,iy,b);
    if (b == NOBALL)
    {
       run++;
       empty++;
    }
    else if (b == current_color)
    {
       run++;
       score++;
       empty = 0;
    }
    else
    {
       run = empty+1;
       score = 1;
       current_color = b;
       empty = 0;
    }
    if (run > lpr)
    {
       lpr = run;
       lprscore = score;
    }
  }
  if (lpr < 5)
    lprscore = -10;
  else 
    lprscore = lprscore*lprscore;
//  printf("= %d\n", lprscore); 
  return lprscore;
}

int Field::calcPosScore(int x, int y, int whatIf)
{
   int score = -10;
   int color = field[y][x].getColor();
   field[y][x].setColor(whatIf);
   score = QMAX(score, calcRun(x, y-4, 0, 1));
   score = QMAX(score, calcRun(x-4, y-4, 1, 1));
   score = QMAX(score, calcRun(x-4, y, 1, 0));
   score = QMAX(score, calcRun(x-4, y+4, 1, -1));
   field[y][x].setColor(color);
   return score;
}