summaryrefslogtreecommitdiff
path: root/noncore/games/zlines/klines.cpp
blob: 76c94e2ce2cefccf09a39c16a181f1bf8ab08210 (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
195
196
197
198
199
200
201
202
203
204
205
/***************************************************************************
                          klines.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.                                   *
 *                                                                         *
 ***************************************************************************/
/* changes
21.05.2000    Roman Razilov     Menu game/Next
*/
//
// The implementation of the KLines widget
//

#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#include <qapplication.h>


#include "klines.h"


/*
   Creates the KLines widget and sets saved options (if any).
 */

KLines::KLines(QWidget *par, const char* n, WFlags fl) : QMainWindow(par,n,fl)
{
	time_t t;
	time(&t);
	srand((unsigned int)t + getpid());

	setCaption(QString("ZLines"));

	mwidget = new MainWidget(this);
	setCentralWidget( mwidget );

	lsb = mwidget->GetLsb();
	lPrompt = mwidget->GetPrompt();

	menu = menuBar();
	game = new QPopupMenu;
	edit = new QPopupMenu;

	game->insertItem(tr("&New game"), this, SLOT(stopGame()), CTRL+Key_N );
	game->insertSeparator();
	game->insertItem(tr("Ne&xt"), this, SLOT(makeTurn()), Key_N );
	game->insertSeparator();
	idMenuPrompt = game->insertItem( tr("&Show next"), this, SLOT(switchPrompt()), CTRL+Key_P );
	game->setCheckable(true);
	game->setItemChecked(idMenuPrompt, lPrompt->getState());
	game->insertSeparator();
	game->insertItem(tr("&Quit"), qApp, SLOT(quit()), CTRL+Key_Q );

	idMenuUndo = edit->insertItem(tr("Und&o"), this, SLOT(undo()), CTRL+Key_Z );

	menu->insertItem( tr("&Game"), game );
	menu->insertItem( tr("&Edit"), edit );

	menu->show();

	score = 0;
	prev_score = 0;

	mwidget->setMessage(tr("Points: 0"));

	startGame();
}

/*
   Saves the options and destroys the KLines widget.
 */

KLines::~KLines()
{
}

/*
   Resize event of the KLines widget.
 */

void KLines::resizeEvent( QResizeEvent *e )
{
	QMainWindow::resizeEvent(e);
}

void KLines::setMinSize()
{
	//  setMinimumSize( gr->wHint(), gr->hHint() + menu->height() + stat->height() +
	//      tool->height() );
}

void KLines::startGame()
{
	score = 0;
	prev_score = 0;
	bUndo=TRUE;

	lsb->clearField();
	generateRandomBalls();
	placeBalls();
	generateRandomBalls();
	edit->setItemEnabled(idMenuUndo, FALSE );
	updateStat();
}
void KLines::stopGame()
{
	debug("Klines::stopGame");
	endGame();
}

void KLines::searchBallsLine()
{
}

void KLines::generateRandomBalls()
{

	for( int i = 0 ; i < BALLSDROP ; i++ )
	{
		nextBalls_undo[i] = nextBalls[i];		
		nextBalls[i] = bUndo ?
			rand() % NCOLORS:
			nextBalls_redo[i];
	}
	lPrompt->SetBalls(nextBalls);
}

void KLines::placeBalls()
{
	lsb->placeBalls(nextBalls);
	debug("exit from placeBalls");
}

void KLines::undo()
{
	debug("Undo");
	if (!bUndo)
		return;
	for( int i = 0 ; i < BALLSDROP ; i++ )
	{
		nextBalls_redo[i] = nextBalls[i];		
		nextBalls[i] = nextBalls_undo[i];
	}
	lPrompt->SetBalls(nextBalls);
	lsb->undo();
	switchUndo(FALSE);
}

void KLines::makeTurn()
{
	placeBalls();
	generateRandomBalls();
	switchUndo(TRUE);
}

void KLines::addScore(int ballsErased)
{   if(ballsErased >= 5){
	score += 2*ballsErased*ballsErased - 20*ballsErased + 60 ;
	if( !lPrompt->getState() ) score+= 1;
	updateStat();
						};
}

void KLines::updateStat()
{
	mwidget->setMessage(tr(" Score: %1 ").arg(score));
}

void KLines::endGame()
{
	startGame();
}

void KLines::switchPrompt()
{
	lPrompt->setPrompt(!lPrompt->getState());
	game->setItemChecked(idMenuPrompt, lPrompt->getState());
}

void KLines::switchUndo(bool bu)
{
	bUndo = bu;
	edit->setItemEnabled(idMenuUndo, bUndo );
}

void KLines::help()
{
	//  KApplication::getKApplication()->invokeHTMLHelp("", "");
}