Diffstat (limited to 'noncore/games/buzzword/buzzword.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/buzzword/buzzword.cpp | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/noncore/games/buzzword/buzzword.cpp b/noncore/games/buzzword/buzzword.cpp new file mode 100644 index 0000000..37032df --- a/dev/null +++ b/noncore/games/buzzword/buzzword.cpp | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2002 Martin Imobersteg <imm@gmx.ch> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Library General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2 of the License,Life or (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | * Library General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Library General Public | ||
15 | * License along with this program; if not, write to the Free | ||
16 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
17 | */ | ||
18 | |||
19 | #include <qlayout.h> | ||
20 | #include <qmessagebox.h> | ||
21 | #include <qmainwindow.h> | ||
22 | #include <qlabel.h> | ||
23 | #include <qgrid.h> | ||
24 | #include <qcolor.h> | ||
25 | #include <qbutton.h> | ||
26 | #include <qfile.h> | ||
27 | #include <qtextstream.h> | ||
28 | #include <qstringlist.h> | ||
29 | #include <qmessagebox.h> | ||
30 | #include <qdir.h> | ||
31 | |||
32 | #include <math.h> | ||
33 | #include <stdlib.h> | ||
34 | |||
35 | #include <list> | ||
36 | #include <string> | ||
37 | |||
38 | #include <qpe/qpeapplication.h> | ||
39 | |||
40 | #include "buzzword.h" | ||
41 | |||
42 | // sponsered by rikkus :) | ||
43 | bool random_compare(const QString &, const QString &) | ||
44 | { | ||
45 | return (rand() % 2) > 0.5; | ||
46 | } | ||
47 | |||
48 | BuzzLabel::BuzzLabel( QWidget *parent, const char *name ) | ||
49 | : QLabel( parent, name ) | ||
50 | { | ||
51 | } | ||
52 | |||
53 | void BuzzLabel::mousePressEvent(QMouseEvent *e) | ||
54 | { | ||
55 | if(e->button() == LeftButton) | ||
56 | { | ||
57 | emit clicked(); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | BuzzItem::BuzzItem( int row, int column, QString text, QWidget *parent, const char *name ) | ||
62 | : QVBox( parent, name ), _row(row), _column(column) | ||
63 | { | ||
64 | setFrameStyle( QFrame::Panel | QFrame::Raised ); | ||
65 | setLineWidth( 1 ); | ||
66 | label = new BuzzLabel(this, "label"); | ||
67 | label->setText(text); | ||
68 | label->setAlignment( int( QLabel::AlignCenter ) ); | ||
69 | |||
70 | connect( label, SIGNAL(clicked()), this, SLOT(flip()) ); | ||
71 | } | ||
72 | |||
73 | void BuzzItem::flip() | ||
74 | { | ||
75 | setLineWidth( 1 ); | ||
76 | label->setBackgroundColor(label->colorGroup().highlight()); | ||
77 | emit clicked(_row, _column); | ||
78 | } | ||
79 | |||
80 | BuzzWord::BuzzWord() : QMainWindow(0) | ||
81 | { | ||
82 | setCaption(tr("buZzword")); | ||
83 | |||
84 | menu = menuBar(); | ||
85 | game = new QPopupMenu; | ||
86 | game->insertItem(tr("&New game"), this, SLOT(newGame()), Key_N ); | ||
87 | game->insertSeparator(); | ||
88 | game->insertItem(tr("&About"), this, SLOT(about())); | ||
89 | game->insertItem(tr("&Quit"), qApp, SLOT(quit()), Key_Q ); | ||
90 | menu->insertItem( tr("&Game"), game ); | ||
91 | |||
92 | gridVal = 4; | ||
93 | grid = NULL; | ||
94 | gameOver = false; | ||
95 | newGame(); | ||
96 | } | ||
97 | |||
98 | void BuzzWord::drawGrid() | ||
99 | { | ||
100 | std::list<QString> l; | ||
101 | |||
102 | QString path = QPEApplication::qpeDir()+"share/buzzword/"; | ||
103 | QFile f( path + "buzzwords" ); | ||
104 | if ( !f.open( IO_ReadOnly ) ) | ||
105 | return; | ||
106 | |||
107 | QTextStream t( &f ); | ||
108 | |||
109 | while (!t.atEnd()) | ||
110 | { | ||
111 | l.push_back(t.readLine()); | ||
112 | } | ||
113 | |||
114 | f.close(); | ||
115 | |||
116 | l.sort(random_compare); | ||
117 | |||
118 | grid = new QGrid(gridVal, this); | ||
119 | grid->setFixedSize(240,240); | ||
120 | |||
121 | for( int c = 0 ; c < gridVal ; c++ ) | ||
122 | { | ||
123 | for( int r = 0 ; r < gridVal ; r++ ) | ||
124 | { | ||
125 | QString word = QStringList::split(" ", l.front()).join("\n"); | ||
126 | BuzzItem* bi = new BuzzItem( c, r, word, grid ); | ||
127 | connect( bi, SIGNAL(clicked(int, int)), this, SLOT(clicked(int,int)) ); | ||
128 | map[c][r] = 0; | ||
129 | |||
130 | l.pop_front(); | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | |||
135 | void BuzzWord::clicked(int row, int column) | ||
136 | { | ||
137 | if ( ! gameOver ) | ||
138 | { | ||
139 | int rowTotal = 0; | ||
140 | int columnTotal = 0; | ||
141 | |||
142 | map[column][row] = 1; | ||
143 | |||
144 | for( int c = 0 ; c < gridVal ; c++ ) | ||
145 | { | ||
146 | for( int r = 0 ; r < gridVal ; r++ ) | ||
147 | { | ||
148 | if ( map[c][r] == 1 ) | ||
149 | rowTotal++; | ||
150 | |||
151 | if ( rowTotal == 4 ) | ||
152 | { | ||
153 | bingo(); | ||
154 | } | ||
155 | } | ||
156 | rowTotal = 0; | ||
157 | } | ||
158 | |||
159 | for( int r = 0 ; r < gridVal ; r++ ) | ||
160 | { | ||
161 | for( int c = 0 ; c < gridVal ; c++ ) | ||
162 | { | ||
163 | if ( map[c][r] == 1 ) | ||
164 | columnTotal++; | ||
165 | |||
166 | if ( columnTotal == 4 ) | ||
167 | { | ||
168 | bingo(); | ||
169 | } | ||
170 | } | ||
171 | columnTotal = 0; | ||
172 | } | ||
173 | |||
174 | if ( map[0][0] && map[1][1] && map[2][2] && map[3][3] ) | ||
175 | bingo(); | ||
176 | |||
177 | if ( map[0][3] && map[1][2] && map[2][1] && map[3][0] ) | ||
178 | bingo(); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | void BuzzWord::bingo() | ||
183 | { | ||
184 | gameOver = true; | ||
185 | QMessageBox::information( this, "BUZZWORD", tr("<h1><b>BINGO !</b></h1>")); | ||
186 | } | ||
187 | |||
188 | void BuzzWord::about() | ||
189 | { | ||
190 | QMessageBox::information( this, "About", "buZzword 1.0\n(c) 2002 Martin Imobersteg\n\nThis program is distributed\nunder the terms of the GPL v2." ); | ||
191 | } | ||
192 | |||
193 | void BuzzWord::newGame() | ||
194 | { | ||
195 | gameOver = false; | ||
196 | delete grid; | ||
197 | drawGrid(); | ||
198 | setCentralWidget(grid); | ||
199 | } | ||