Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

buzzword.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002 Martin Imobersteg <imm@gmx.ch>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License,Life or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this program; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include <qlayout.h>
00020 #include <qmainwindow.h>
00021 #include <qlabel.h>
00022 #include <qgrid.h>
00023 #include <qcolor.h>
00024 #include <qbutton.h>
00025 #include <qfile.h>
00026 #include <qtextstream.h>
00027 #include <qstringlist.h>
00028 #include <qmessagebox.h>
00029 #include <qdir.h>
00030 
00031 #include <math.h>
00032 #include <stdlib.h>
00033 
00034 #include <qpe/qpeapplication.h>
00035 
00036 #include "buzzword.h"
00037 
00038 BuzzLabel::BuzzLabel( QWidget *parent, const char *name )
00039 : QLabel( parent, name )
00040 {
00041 }
00042 
00043 void BuzzLabel::mousePressEvent(QMouseEvent *e)
00044 {
00045         if(e->button() == LeftButton)
00046         {
00047                 emit clicked();
00048         }
00049 }
00050 
00051 BuzzItem::BuzzItem( int row, int column, QString text, QWidget *parent, const char *name )
00052 : QVBox( parent, name ), _row(row), _column(column)
00053 {
00054         setFrameStyle( QFrame::Panel | QFrame::Raised );
00055         setLineWidth( 1 );
00056         label = new BuzzLabel(this, "label");
00057         label->setText(text);
00058         label->setAlignment( int( QLabel::AlignCenter ) );
00059 
00060         connect( label, SIGNAL(clicked()), this, SLOT(flip()) );
00061 }
00062 
00063 void BuzzItem::flip()
00064 {
00065         setLineWidth( 1 );
00066         label->setBackgroundColor(label->colorGroup().highlight());
00067         emit clicked(_row, _column);
00068 }
00069 
00070 BuzzWord::BuzzWord(QWidget* parent, const char* name, WFlags fl ) : QMainWindow( parent,  name, fl )
00071 {
00072         setCaption(tr("buZzword"));
00073 
00074         menu = menuBar();
00075         game = new QPopupMenu;
00076         game->insertItem(tr("&New game"), this, SLOT(newGame()), Key_N );
00077         menu->insertItem( tr("&Game"), game );
00078 
00079         gridVal = 4;
00080         grid = NULL;
00081         gameOver = false;
00082         newGame();
00083 }
00084 
00085 void BuzzWord::drawGrid()
00086 {
00087         QStringList l;
00088 
00089         QString path = QPEApplication::qpeDir()+"share/buzzword/";
00090         QFile f( path + "buzzwords" );
00091         if ( !f.open( IO_ReadOnly ) )
00092                 return;
00093 
00094         QTextStream t( &f );
00095 
00096         while (!t.atEnd())
00097         {
00098                 l << t.readLine();
00099         }
00100 
00101         f.close();
00102 
00103         grid = new QGrid(gridVal, this);
00104 //      grid->setFixedSize( 480, 480 );
00105 
00106         for( int c = 0 ; c < gridVal ; c++ )
00107         {
00108                 for( int r = 0 ; r < gridVal ; r++ )
00109                 {
00110                         uint pos = rand() % l. count();
00111 
00112                         QString word = QStringList::split(" ", l[pos]).join("\n");
00113                         BuzzItem* bi = new BuzzItem( c, r, word, grid );
00114                         connect( bi, SIGNAL(clicked(int,int)), this, SLOT(clicked(int,int)) );
00115                         map[c][r] = 0;
00116 
00117                         l.remove( l.at( pos ));
00118                 }
00119         }
00120 }
00121 
00122 void BuzzWord::clicked(int row, int column)
00123 {
00124         if ( ! gameOver )
00125         {
00126                 int rowTotal = 0;
00127                 int columnTotal = 0;
00128 
00129                 map[column][row] = 1;
00130 
00131                 for( int c = 0 ; c < gridVal ; c++ )
00132                 {
00133                         for( int r = 0 ; r < gridVal ; r++ )
00134                         {
00135                                 if ( map[c][r] == 1 )
00136                                         rowTotal++;
00137 
00138                                 if ( rowTotal == 4 )
00139                                 {
00140                                         bingo();
00141                                 }
00142                         }
00143                         rowTotal = 0;
00144                 }
00145 
00146                 for( int r = 0 ; r < gridVal ; r++ )
00147                 {
00148                         for( int c = 0 ; c < gridVal ; c++ )
00149                         {
00150                                 if ( map[c][r] == 1 )
00151                                         columnTotal++;
00152 
00153                                 if ( columnTotal == 4 )
00154                                 {
00155                                         bingo();
00156                                 }
00157                         }
00158                         columnTotal = 0;
00159                 }
00160 
00161                 if ( map[0][0] && map[1][1] && map[2][2] && map[3][3] )
00162                         bingo();
00163 
00164                 if ( map[0][3] && map[1][2] && map[2][1] && map[3][0] )
00165             bingo();
00166         }
00167 }
00168 
00169 void BuzzWord::bingo()
00170 {
00171         gameOver = true;
00172     QMessageBox::information( this, "BUZZWORD", tr("<h1><b>BINGO !</b></h1>"));
00173 }
00174 
00175 void BuzzWord::newGame()
00176 {
00177         gameOver = false;
00178         delete grid;
00179         drawGrid();
00180         setCentralWidget(grid);
00181 }

Generated on Sat Nov 5 16:17:19 2005 for OPIE by  doxygen 1.4.2