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