-rw-r--r-- | noncore/games/oyatzee/oyatzee.cpp | 455 | ||||
-rw-r--r-- | noncore/games/oyatzee/oyatzee.h | 98 |
2 files changed, 403 insertions, 150 deletions
diff --git a/noncore/games/oyatzee/oyatzee.cpp b/noncore/games/oyatzee/oyatzee.cpp index d0e220c..0bd22f6 100644 --- a/noncore/games/oyatzee/oyatzee.cpp +++ b/noncore/games/oyatzee/oyatzee.cpp @@ -2,33 +2,14 @@ -#include <qpe/applnk.h> -#include <qpe/global.h> -#include <qpe/filemanager.h> -#include <qpe/resource.h> -#include <qpe/config.h> - -#include <qapplication.h> #include <qmessagebox.h> -#include <qcombobox.h> -#include <qdatetime.h> -#include <qfileinfo.h> -#include <qfile.h> +#include <qapplication.h> #include <qdir.h> -#include <qiconset.h> #include <qlabel.h> -#include <qlineedit.h> #include <qpushbutton.h> -#include <qtextstream.h> #include <qtimer.h> -#include <qpe/qpetoolbar.h> -#include <qtoolbutton.h> #include <qvbox.h> -#include <qwidgetstack.h> #include <qpainter.h> #include <qlayout.h> -#include <qregexp.h> +#include <qpoint.h> #include <stdlib.h> -#include <unistd.h> -#include <pwd.h> -#include <sys/types.h> @@ -38,3 +19,4 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( setCentralWidget( thing ); - + + setCaption( tr( "OYatzee" ) ); @@ -42,4 +24,6 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( setRoundsNumber( 1 ); + + lastPlayerFinished = false; + currentPlayer = 1; - playerList ps; ps.append( new Player( "Carsten" ) ); @@ -49,4 +33,2 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( -//X Game *g = new Game( ps ); -//X @@ -55,3 +37,6 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( sb = new Scoreboard( ps, thing , "sb" ); + connect( sb->pb , SIGNAL( item( int ) ), this , SLOT( slotEndRound( int ) ) ); + dw = new DiceWidget( thing , "dw" ); + dw->setMaximumHeight( this->height()/4 ); connect( dw->rollButton, SIGNAL( clicked() ), this , SLOT( slotRollDices() ) ); @@ -62,2 +47,112 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( +void OYatzee::slotEndRound( int item ) +{ + qDebug( "Der User hat Nummer %d ausgewählt" , item ); + + /* + * if the user clicked on Total, Bonus or Score and thus not on a + * selectable item return and do nothing + */ + if ( item == 7 || item == 8 || item == 16 ) return; + + /* + * check if the user can really click on that item + */ + if ( posibilities.find( item ) == posibilities.end() ) return; + + QValueListInt numbers; + + Dice *d = dw->diceList.first(); + for ( ; d != 0 ; d = dw->diceList.next() ) + { + numbers.append( d->hasValue() ); + } + + int points = 0; + + switch ( item ) + { + case Ones: + points = getPoints( 1 , numbers ); + break; + case Twos: + points = getPoints( 2 , numbers ); + break; + case Threes: + points = getPoints( 3 , numbers ); + break; + case Fours: + points = getPoints( 4 , numbers ); + break; + case Fives: + points = getPoints( 5 , numbers ); + break; + case Sixes: + points = getPoints( 6 , numbers ); + break; + case ThreeOfAKind: + points = oakPoints; + break; + case FourOfAKind: + points = oakPoints; + break; + case FullHouse: + points = 25; + break; + case SStraight: + points = 30; + break; + case LStraight: + points = 40; + break; + case Yatzee: + points = 50; + break; + case Chance: + points = getPoints ( Chance , numbers ); + } + + sb->nextRB(currentPlayer-1)->updateMap( item , points ); + nextPlayer(); + + qDebug( "Punkte: %d" , points ); +} + +void OYatzee::nextPlayer() +{ + currentPlayer++; + + if ( currentPlayer > numOfPlayers ) + { + currentPlayer = 1; + } + + ps.at(currentPlayer-1)->turn = 0; +} + +int OYatzee::getPoints( const int num , QValueListInt l) +{ + QValueListInt::Iterator it = l.begin(); + int c = 0; + + if ( num != Chance ) + { + for ( ; it != l.end() ; ++it ) + { + if ( *it == num ) + c++; + } + + return c * num; + } + else + { + for ( ; it != l.end() ; ++it ) + { + c += *it; + } + return c; + } +} + OYatzee::~OYatzee() @@ -77,3 +172,3 @@ void OYatzee::detectPosibilities() { - numbers.append( d->Value ); + numbers.append( d->hasValue() ); } @@ -100,3 +195,2 @@ void OYatzee::detectPosibilities() - //3er, 4er, Yatzee @@ -125,3 +219,3 @@ void OYatzee::detectPosibilities() { - posibilities.append( 7 ); + posibilities.append( 9 ); @@ -129,8 +223,10 @@ void OYatzee::detectPosibilities() if ( count == 3 && countFH == 2 ) //aka Full House - posibilities.append( 9 ); + posibilities.append( 11 ); + if ( count >= 4 ) + posibilities.append( 10 ); + if ( count == 5 ) //Yatzee + posibilities.append( 14 ); + + oakPoints = count * i; } - if ( count >= 4 ) - posibilities.append( 8 ); - if ( count == 5 ) //Yatzee - posibilities.append( 12 ); } @@ -159,5 +255,5 @@ void OYatzee::detectPosibilities() if ( isShort ) - posibilities.append( 10 ); + posibilities.append( 12 ); if ( isLong ) - posibilities.append( 11 ); + posibilities.append( 13 ); } @@ -171,50 +267,62 @@ void OYatzee::displayPossibilites() { - for ( QValueListInt::Iterator it = posibilities.begin() ; it != posibilities.end(); ++it ) - { - qDebug( QString::number( *it ) ); - switch ( *it ) - { - case Ones: - qDebug( "1er" ); - break; - case Twos: - qDebug( "2er" ); - break; - case Threes: - qDebug( "3er" ); - break; - case Fours: - qDebug( "4er" ); - break; - case Fives: - qDebug( "5er" ); - break; - case Sixes: - qDebug( "6er" ); - break; - case ThreeOfAKind: - qDebug( "3oaK" ); - break; - case FourOfAKind: - qDebug( "4oaK" ); - break; - case FullHouse: - qDebug( "Full House" ); - break; - case SStraight: - qDebug( "Short S" ); - break; - case LStraight: - qDebug( "Long S" ); - break; - case Yatzee: - qDebug( "Yatzee!" ); - break; - case Chance: - qDebug( "Chance" ); - break; - } - } +//X for ( QValueListInt::Iterator it = posibilities.begin() ; it != posibilities.end(); ++it ) +//X { +//X qDebug( QString::number( *it ) ); +//X switch ( *it ) +//X { +//X case Ones: +//X qDebug( "1er" ); +//X break; +//X case Twos: +//X qDebug( "2er" ); +//X break; +//X case Threes: +//X qDebug( "3er" ); +//X break; +//X case Fours: +//X qDebug( "4er" ); +//X break; +//X case Fives: +//X qDebug( "5er" ); +//X break; +//X case Sixes: +//X qDebug( "6er" ); +//X break; +//X case ThreeOfAKind: +//X qDebug( "3oaK" ); +//X break; +//X case FourOfAKind: +//X qDebug( "4oaK" ); +//X break; +//X case FullHouse: +//X qDebug( "Full House" ); +//X break; +//X case SStraight: +//X qDebug( "Short S" ); +//X break; +//X case LStraight: +//X qDebug( "Long S" ); +//X break; +//X case Yatzee: +//X qDebug( "Yatzee!" ); +//X break; +//X case Chance: +//X qDebug( "Chance" ); +//X break; +//X } +//X } + + sb->pb->setIntlist( posibilities ); + sb->pb->update(); +} + +void OYatzee::startGame() +{ + /* + * TODO + */ } +void OYatzee::stopGame(){} + void OYatzee::setPlayerNumber( const int num ) @@ -235,2 +343,12 @@ void OYatzee::slotRollDices() { + qDebug( "Roll nummer: %d" , ps.at( currentPlayer-1 )->turn ); + + if ( ps.at( currentPlayer-1 )->turn == 3 ) + { + QMessageBox::information( this, + "OYatzee", + tr( "Only three rolls per turn allowed." ) ); + return; + } + Dice *d = dw->diceList.first(); @@ -243,2 +361,6 @@ void OYatzee::slotRollDices() +// qDebug( "Roll nummer (vorher): %d" , ps.at( currentPlayer-1 )->turn ); + ps.at(currentPlayer-1)->turn++; +// qDebug( "Roll nummer (nachher): %d" , ps.at( currentPlayer-1 )->turn ); + detectPosibilities(); @@ -271,2 +393,13 @@ Scoreboard::Scoreboard( playerList ps, QWidget *parent, const char *name ) : QWi +Resultboard* Scoreboard::nextRB( int currentPlayer ) +{ + Resultboard *b; + + b = rbList.at( currentPlayer ); + + qDebug( "Anzahl: %d" , rbList.count() ); + + return b; +} + void Scoreboard::createResultboards(const int num) @@ -283,6 +416,6 @@ void Scoreboard::paintEvent( QPaintEvent * ) { - QPainter p; - p.begin( this ); - - p.drawRect( 0,0, this->width() , this->height() ); +//X QPainter p; +//X p.begin( this ); +//X +//X p.drawRect( 0,0, this->width() , this->height() ); } @@ -309,3 +442,3 @@ void Dice::slotSelected() -int Dice::hasValue() +const int Dice::hasValue() const { @@ -342,2 +475,9 @@ void Dice::paintNumber( QPainter *p ) { + p->setBrush( Qt::black ); + + int w = this->width(); + int h = this->height(); + int r = this->width(); + r /= 10; + switch ( Value ) @@ -345,18 +485,33 @@ void Dice::paintNumber( QPainter *p ) case 1: - p->drawText( 10,10,"1"); + p->drawEllipse( (int)( 0.5*w - r ) , (int)( 0.5*h - r ) , r , r ) ; break; case 2: - p->drawText( 10,10,"2"); + p->drawEllipse( (int)( 0.3*w - r ) , (int)( 0.3*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.7*w - r ) , (int)( 0.7*h - r ) , r , r ) ; break; case 3: - p->drawText( 10,10,"3"); + p->drawEllipse( (int)( 0.5*w - r ) , (int)( 0.5*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.8*h - r ) , r , r ) ; break; case 4: - p->drawText( 10,10,"4"); + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.8*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.8*h - r ) , r , r ) ; break; case 5: - p->drawText( 10,10,"5"); + p->drawEllipse( (int)( 0.5*w - r ) , (int)( 0.5*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.8*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.8*h - r ) , r , r ) ; break; case 6: - p->drawText( 10,10,"6"); + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.8*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.2*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.8*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.2*w - r ) , (int)( 0.5*h - r ) , r , r ) ; + p->drawEllipse( (int)( 0.8*w - r ) , (int)( 0.5*h - r ) , r , r ) ; break; @@ -396,2 +551,25 @@ Player::Player( QString name ) playerName = name; + setupResultList(); + turn = 0; +} + +void Player::setupResultList() +{ + for ( int i = 1 ; i < 14 ; ++i ) + { + pResults.append( 0 ); + } +} + +/* + * TODO: muss noch genutzt werden + */ +void Player::updateTotalPoints( pointMap m ) +{ + pointMap::Iterator it = m.begin(); + for ( ; it != m.end() ; ++it ) + { + totalPoints += it.data(); + } + } @@ -399,2 +577,8 @@ Player::Player( QString name ) +void Player::setResults( const int cat , const int points ) +{ + QValueListInt::Iterator it = pResults.at( cat ); + *it = points; +} + /* @@ -406,2 +590,7 @@ Board::Board( QWidget *parent , const char* name ) : QWidget ( parent , name ) +void Board::mousePressEvent( QMouseEvent *e ) +{ + emit clicked( e->pos() ); +} + /* @@ -419,5 +608,23 @@ void Resultboard::paintEvent( QPaintEvent* ) p.begin( this ); + + const int cell_width = this->width(); + const int cell_height = this->height()/17; + + pointMap::Iterator it = pMap.begin(); + for ( ; it != pMap.end() ; ++it ) + { + int i = it.key(); + qDebug( "ok: %d , %d" , i , it.data() ); + p.drawText( 0, i*cell_height , cell_width , cell_height , Qt::AlignCenter , QString::number( it.data() ) ); + } - p.drawText( 10,10, pName ); - p.drawRect( 0,0, this->width() , this->height() ); + p.drawText( 0,0,cell_width,cell_height, Qt::AlignCenter , pName ); //Playername +} + + +void Resultboard::updateMap( int item , int points ) +{ + pMap.insert( item , points ); + + update(); } @@ -430,14 +637,2 @@ Possibilityboard::Possibilityboard( QWidget *parent , const char* name ) : Board { -} - -void Possibilityboard::paintEvent( QPaintEvent* ) -{ - QPainter p; - p.begin( this ); - - const int cell_width = this->width(); - const int h = this->height(); - const int cell_height = h/17; - - QStringList begriffe; begriffe.append( "1er" ); @@ -458,10 +653,34 @@ void Possibilityboard::paintEvent( QPaintEvent* ) begriffe.append( "Score" ); - - QStringList::Iterator it = begriffe.begin(); + connect( this , SIGNAL( clicked( QPoint ) ), this , SLOT( slotClicked( QPoint ) ) ); +} + +void Possibilityboard::slotClicked( QPoint p) +{ + emit item( p.y()/(this->height()/17) ); +} + +void Possibilityboard::paintEvent( QPaintEvent* ) +{ + QPainter p; + p.begin( this ); + + const int cell_width = this->width(); + const int cell_height = this->height()/17; + + p.setBrush( Qt::blue ); + + QValueListInt::Iterator listIt = list.begin(); + for ( ; listIt != list.end() ; ++listIt ) + { + p.drawRect( 0 , (*listIt) * cell_height , cell_width , cell_height ); + } + + p.setBrush( Qt::black ); + p.setBrush( Qt::NoBrush ); + QStringList::Iterator begriffeIt = begriffe.begin(); for ( int i = 1 ; i < 18 ; ++i ) { - p.drawRect( 0 , i*cell_height , cell_width , cell_height ); - p.drawText( 0 , i*cell_height , cell_width , cell_height , Qt::AlignCenter , *it ); - ++it; + p.drawText( 0 , i*cell_height , cell_width , cell_height , Qt::AlignCenter , *begriffeIt ); + ++begriffeIt; } @@ -469,16 +688,6 @@ void Possibilityboard::paintEvent( QPaintEvent* ) -/* - * Game - */ - -Game::Game( playerList pla ) +void Possibilityboard::setIntlist( QValueListInt &l ) { - players = pla; + list = l; } -void Game::startGame() -{} - -void Game::stopGame() -{} - diff --git a/noncore/games/oyatzee/oyatzee.h b/noncore/games/oyatzee/oyatzee.h index 1074f1d..048a733 100644 --- a/noncore/games/oyatzee/oyatzee.h +++ b/noncore/games/oyatzee/oyatzee.h @@ -6,2 +6,4 @@ #include <qlist.h> +#include <qmap.h> +#include <qsplitter.h> @@ -17,2 +19,4 @@ class Player; +class QPoint; + typedef QList<Dice> dicesList; @@ -21,2 +25,3 @@ typedef QValueList<int> QValueListInt; typedef QList<Player> playerList; +typedef QMap<int,int> pointMap; @@ -28,3 +33,3 @@ class OYatzee : public QMainWindow { - Game *g; + Game *g(); DiceWidget *dw; @@ -33,2 +38,3 @@ class OYatzee : public QMainWindow { QValueListInt posibilities; + playerList ps; @@ -37,4 +43,3 @@ class OYatzee : public QMainWindow { - enum { - Ones=1, + enum { Ones = 1, Twos = 2, @@ -44,9 +49,9 @@ class OYatzee : public QMainWindow { Sixes = 6, - ThreeOfAKind = 7, //12444 - FourOfAKind = 8, //14444 - FullHouse = 9, //22555 - SStraight = 10, //13456 - LStraight = 11, //12345 - Yatzee = 12, //55555 - Chance = 13}; + ThreeOfAKind = 9, //12444 + FourOfAKind = 10, //14444 + FullHouse = 11, //22555 + SStraight = 12, //13456 + LStraight = 13, //12345 + Yatzee = 14, //55555 + Chance = 15}; @@ -55,2 +60,3 @@ class OYatzee : public QMainWindow { void slotRollDices(); + void slotEndRound( int ); @@ -59,2 +65,9 @@ class OYatzee : public QMainWindow { int numOfRounds; + int currentPlayer; /* the number of the current player */ + + int oakPoints; + + void nextPlayer(); + + bool lastPlayerFinished; @@ -66,2 +79,7 @@ class OYatzee : public QMainWindow { + int getPoints( const int , QValueListInt ); + + void startGame(); + void stopGame(); + }; @@ -74,8 +92,9 @@ class Dice : public QFrame - int Value; bool isSelected; - int hasValue(); + const int hasValue() const; void roll(); - virtual void mousePressEvent( QMouseEvent* ); + + private: + int Value; @@ -88,4 +107,5 @@ class Dice : public QFrame protected: - void paintEvent( QPaintEvent *e ); + void paintEvent( QPaintEvent *e ); void paintNumber( QPainter *p ); + virtual void mousePressEvent( QMouseEvent* ); }; @@ -103,3 +123,2 @@ class DiceWidget : public QWidget - class Board : public QWidget @@ -109,2 +128,9 @@ class Board : public QWidget Board( QWidget *parent = 0, const char* name = 0 ); + + signals: + void clicked( QPoint ); + void item( int ); + + protected: + virtual void mousePressEvent( QMouseEvent* ); }; @@ -117,5 +143,17 @@ class Possibilityboard : public Board Possibilityboard( QWidget *parent = 0, const char* name = 0 ); + + QValueListInt list; + void setIntlist( QValueListInt& ); + private: + QStringList begriffe; + + private slots: + /* + * this slot returns the item the user has selected + */ + virtual void slotClicked(QPoint); + protected: - virtual void paintEvent( QPaintEvent *e ); + virtual void paintEvent( QPaintEvent *e ); }; @@ -127,5 +165,9 @@ class Resultboard : public Board public: - Resultboard( QString playerName , QWidget *parent = 0, const char* name = 0 ); + Resultboard( QString playerName = 0 , QWidget *parent = 0, const char* name = 0 ); QString pName; + pointMap pMap; + + void updateMap( int, int ); + protected: @@ -144,4 +186,7 @@ class Scoreboard : public QWidget playerList ps_; - + void createResultboards(const int); + + Resultboard* nextRB(int); + @@ -159,14 +204,13 @@ class Player int totalPoints; -}; -class Game -{ - public: - Game( playerList pla ); + void setResults( const int , const int ); - playerList players; - int currentPlayer; + int turn; - void startGame(); - void stopGame(); + void updateTotalPoints( QMap<int,int> ); + + private: + QValueListInt pResults; /* the individual results of the player */ + + void setupResultList(); /* only in the ctor */ }; |