summaryrefslogtreecommitdiffstats
path: root/kshisen/board.h
blob: 43706524960d622e7d1b9194a887ca44b28ee4d3 (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
/*
 *******************************************************************
 *******************************************************************
 *
 *
 * KSHISEN
 *
 *
 *******************************************************************
 *
 * A japanese game similar to mahjongg
 *
 *******************************************************************
 *
 * created 1997 by Mario Weilguni <[email protected]>
 *
 *******************************************************************
 *
 * This file is part of the KDE project "KSHISEN"
 *
 * KSHISEN 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, or (at your option)
 * any later version.
 *
 * KSHISEN 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 KSHISEN; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 *******************************************************************
 */

#ifndef __BOARD__H__
#define __BOARD__H__

// Should this get the whole HAVE_SYS_TIME_H TIME_WITH_SYS_TIME treatment?
#include <time.h>

#include <krandomsequence.h>
#include <list>
#include "tileset.h"
#include "debug.h"

struct Position
{
	Position() : x(0), y(0) { }
	Position(int _x, int _y) : x(_x), y(_y) { }
	int x;
	int y;
};

typedef std::list<Position> Path;

class Move
{
public:
	Move(int _x1, int _y1, int _x2, int _y2, int _tile) :
		x1(_x1), y1(_y1), x2(_x2), y2(_y2), tile(_tile) { }

	int x1, y1, x2, y2;
	int tile;
};

class Board : public TQWidget
{
	Q_OBJECT
  

public:
	Board(TQWidget *parent = 0, const char *name=0);
	~Board();

	virtual void paintEvent(TQPaintEvent *);
	virtual void mousePressEvent(TQMouseEvent *);
	virtual void resizeEvent(TQResizeEvent*);

	void setDelay(int);
	int  getDelay() const;

	bool canUndo() const;
	bool canRedo() const;
	void redo();
	void undo();

	void setSize(int x, int y);
	void resizeBoard();
	TQSize unscaledSize() const;
	void newGame();
	void setShuffle(int);
	int  getShuffle() const;

	void showHint();
	bool getHint_I(Path& p) const;

#ifdef DEBUGGING
	void makeHintMove();
	void finish();
	void dumpBoard() const;
#endif

	int tilesLeft() const;
	int getCurrentTime() const;
	int getTimeForGame() const;

	bool solvable(bool norestore = FALSE);

	bool getSolvableFlag() const;
	void setSolvableFlag(bool);
	bool gravityFlag() const;
	void setGravityFlag(bool);

	int x_tiles() const;
	int y_tiles() const;

	bool isPaused() const { return paused; }

signals:
	void markMatched();
	void changed();
	void endOfGame();
	void resized();

public slots:
	bool pause();
  void loadSettings();
	
private slots:
	void undrawConnection();
	void gravity(int, bool);

protected:
	virtual TQSize sizeHint() const;

private: // functions
	void initBoard();

	int xOffset() const;
	int yOffset() const;

	void setField(int x, int y, int value);
	int getField(int x, int y) const;
	void updateField(int, int, bool erase = true);
	void clearHighlight();
	bool canMakePath(int x1, int y1, int x2, int y2) const;
	bool findPath(int x1, int y1, int x2, int y2, Path& p) const;
	bool findSimplePath(int x1, int y1, int x2, int y2, Path& p) const;
	bool isTileHighlighted(int x, int y) const;
	void drawConnection(int timeout);
	TQPoint midCoord(int x, int y) const;
	void marked(int x, int y);
	void madeMove(int x1, int y1, int x2, int y2);

private:
	time_t starttime;
	time_t time_for_game;

	TileSet tiles;

	KRandomSequence random;

	TQPtrList<Move> _undo;
	TQPtrList<Move> _redo;

	int undraw_timer_id;
	int mark_x;
	int mark_y;
	Path connection;
	int *field;
	int _x_tiles;
	int _y_tiles;
	int _delay;
	int _shuffle;

	bool paused;
	time_t pause_start;

	bool gravity_flag;
	bool _solvable_flag;
	int grav_col_1, grav_col_2;

	int highlighted_tile;
};

#endif