summaryrefslogtreecommitdiff
path: root/noncore/games/buzzword/buzzword.cpp
blob: 13eb481c663def9bc308832be3588a19a4bc0d77 (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
/*
 * Copyright (C) 2002 Martin Imobersteg <imm@gmx.ch>
 *
 * 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,Life or (at your option) any later version.
 *
 * This program 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <qlayout.h>
#include <qmainwindow.h>
#include <qlabel.h>
#include <qgrid.h>
#include <qcolor.h>
#include <qbutton.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qstringlist.h>
#include <qmessagebox.h>
#include <qdir.h>

#include <math.h>
#include <stdlib.h>

#include <qpe/qpeapplication.h>

#include "buzzword.h"

BuzzLabel::BuzzLabel( QWidget *parent, const char *name )
: QLabel( parent, name )
{
}

void BuzzLabel::mousePressEvent(QMouseEvent *e)
{
	if(e->button() == LeftButton)
	{
		emit clicked();
	}
}

BuzzItem::BuzzItem( int row, int column, QString text, QWidget *parent, const char *name )
: QVBox( parent, name ), _row(row), _column(column)
{
	setFrameStyle( QFrame::Panel | QFrame::Raised );
	setLineWidth( 1 );
	label = new BuzzLabel(this, "label");
	label->setText(text);
	label->setAlignment( int( QLabel::AlignCenter ) );

	connect( label, SIGNAL(clicked()), this, SLOT(flip()) );
}

void BuzzItem::flip()
{
	setLineWidth( 1 );
	label->setBackgroundColor(label->colorGroup().highlight());
	emit clicked(_row, _column);
}

BuzzWord::BuzzWord(QWidget* parent, const char* name, WFlags fl ) : QMainWindow( parent,  name, fl )
{
	setCaption(tr("buZzword"));

	menu = menuBar();
	game = new QPopupMenu;
	game->insertItem(tr("&New game"), this, SLOT(newGame()), Key_N );
	menu->insertItem( tr("&Game"), game );

	gridVal = 4;
	grid = NULL;
	gameOver = false;
	newGame();
}

void BuzzWord::drawGrid()
{
	QStringList l;

	QString path = QPEApplication::qpeDir()+"share/buzzword/";
	QFile f( path + "buzzwords" );
	if ( !f.open( IO_ReadOnly ) )
		return;

	QTextStream t( &f );

	while (!t.atEnd())
	{
		l << t.readLine();
	}

	f.close();

	grid = new QGrid(gridVal, this);
//	grid->setFixedSize( 480, 480 );

	for( int c = 0 ; c < gridVal ; c++ )
	{
		for( int r = 0 ; r < gridVal ; r++ )
		{
			uint pos = rand() % l. count();

			QString word = QStringList::split(" ", l[pos]).join("\n");
			BuzzItem* bi = new BuzzItem( c, r, word, grid );
			connect( bi, SIGNAL(clicked(int, int)), this, SLOT(clicked(int,int)) );
			map[c][r] = 0;

			l.remove( l.at( pos ));
		}
	}
}

void BuzzWord::clicked(int row, int column)
{
	if ( ! gameOver )
	{
		int rowTotal = 0;
		int columnTotal = 0;

		map[column][row] = 1;

		for( int c = 0 ; c < gridVal ; c++ )
		{
			for( int r = 0 ; r < gridVal ; r++ )
			{
				if ( map[c][r] == 1 )
					rowTotal++;

				if ( rowTotal == 4 )
				{
					bingo();
				}
			}
			rowTotal = 0;
		}

		for( int r = 0 ; r < gridVal ; r++ )
		{
			for( int c = 0 ; c < gridVal ; c++ )
			{
				if ( map[c][r] == 1 )
					columnTotal++;

				if ( columnTotal == 4 )
				{
					bingo();
				}
			}
			columnTotal = 0;
		}

		if ( map[0][0] && map[1][1] && map[2][2] && map[3][3] )
			bingo();

		if ( map[0][3] && map[1][2] && map[2][1] && map[3][0] )
            bingo();
	}
}

void BuzzWord::bingo()
{
	gameOver = true;
    QMessageBox::information( this, "BUZZWORD", tr("<h1><b>BINGO !</b></h1>"));
}

void BuzzWord::newGame()
{
	gameOver = false;
	delete grid;
	drawGrid();
	setCentralWidget(grid);
}