-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 @@ -1,35 +1,16 @@ #include "oyatzee.h" -#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> OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( parent , name , fl ) @@ -37,10 +18,13 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( QWidget *thing = new QWidget( this ); setCentralWidget( thing ); - + + setCaption( tr( "OYatzee" ) ); setPlayerNumber( 4 ); setRoundsNumber( 1 ); + + lastPlayerFinished = false; + currentPlayer = 1; - playerList ps; ps.append( new Player( "Carsten" ) ); ps.append( new Player( "Julia" ) ); @@ -48,11 +32,12 @@ OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( ps.append( new Player( "Stephan" ) ); -//X Game *g = new Game( ps ); -//X QVBoxLayout *vbox = new QVBoxLayout( thing ); 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() ) ); @@ -61,4 +46,114 @@ 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() { @@ -76,5 +171,5 @@ void OYatzee::detectPosibilities() for ( ; d != 0 ; d = dw->diceList.next() ) { - numbers.append( d->Value ); + numbers.append( d->hasValue() ); } @@ -99,5 +194,4 @@ void OYatzee::detectPosibilities() } - //3er, 4er, Yatzee it = numbers.begin(); @@ -124,14 +218,16 @@ void OYatzee::detectPosibilities() if ( count >= 3 ) { - posibilities.append( 7 ); + posibilities.append( 9 ); //now we check if it is a full house 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 ); } @@ -158,7 +254,7 @@ void OYatzee::detectPosibilities() if ( isShort ) - posibilities.append( 10 ); + posibilities.append( 12 ); if ( isLong ) - posibilities.append( 11 ); + posibilities.append( 13 ); } @@ -170,52 +266,64 @@ void OYatzee::detectPosibilities() 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 ) { @@ -234,4 +342,14 @@ void OYatzee::slotStartGame() 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(); @@ -242,4 +360,8 @@ 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(); } @@ -270,4 +392,15 @@ 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) { @@ -282,8 +415,8 @@ void Scoreboard::createResultboards(const int num) 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() ); } @@ -308,5 +441,5 @@ void Dice::slotSelected() } -int Dice::hasValue() +const int Dice::hasValue() const { return Value; @@ -341,23 +474,45 @@ void Dice::paintEvent( QPaintEvent * ) 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 ) { 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; } @@ -395,7 +550,36 @@ 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(); + } + } +void Player::setResults( const int cat , const int points ) +{ + QValueListInt::Iterator it = pResults.at( cat ); + *it = points; +} + /* * Board @@ -405,4 +589,9 @@ Board::Board( QWidget *parent , const char* name ) : QWidget ( parent , name ) } +void Board::mousePressEvent( QMouseEvent *e ) +{ + emit clicked( e->pos() ); +} + /* * Resultboard @@ -418,7 +607,25 @@ void Resultboard::paintEvent( QPaintEvent* ) QPainter p; 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(); } @@ -429,16 +636,4 @@ void Resultboard::paintEvent( QPaintEvent* ) Possibilityboard::Possibilityboard( QWidget *parent , const char* name ) : Board ( parent , name ) { -} - -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" ); begriffe.append( "2er" ); @@ -457,28 +652,42 @@ void Possibilityboard::paintEvent( QPaintEvent* ) begriffe.append( "Chance" ); 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; } } -/* - * 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 @@ -5,4 +5,6 @@ #include <qlabel.h> #include <qlist.h> +#include <qmap.h> +#include <qsplitter.h> #include <stdlib.h> // rand() function @@ -16,8 +18,11 @@ class Resultboard; class Player; +class QPoint; + typedef QList<Dice> dicesList; typedef QList<Resultboard> resultboardList; typedef QValueList<int> QValueListInt; typedef QList<Player> playerList; +typedef QMap<int,int> pointMap; class OYatzee : public QMainWindow { @@ -27,15 +32,15 @@ class OYatzee : public QMainWindow { ~OYatzee(); - Game *g; + Game *g(); DiceWidget *dw; Scoreboard *sb; QValueListInt posibilities; + playerList ps; void setPlayerNumber( const int num ); void setRoundsNumber( const int num ); - enum { - Ones=1, + enum { Ones = 1, Twos = 2, Threes = 3, @@ -43,19 +48,27 @@ class OYatzee : public QMainWindow { Fives = 5, 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}; public slots: void slotStartGame(); void slotRollDices(); + void slotEndRound( int ); private: int numOfPlayers; int numOfRounds; + int currentPlayer; /* the number of the current player */ + + int oakPoints; + + void nextPlayer(); + + bool lastPlayerFinished; /* @@ -65,4 +78,9 @@ class OYatzee : public QMainWindow { void displayPossibilites(); + int getPoints( const int , QValueListInt ); + + void startGame(); + void stopGame(); + }; @@ -73,10 +91,11 @@ class Dice : public QFrame Dice( QWidget* parent = 0, const char* name = 0 ); - int Value; bool isSelected; - int hasValue(); + const int hasValue() const; void roll(); - virtual void mousePressEvent( QMouseEvent* ); + + private: + int Value; private slots: @@ -87,6 +106,7 @@ class Dice : public QFrame protected: - void paintEvent( QPaintEvent *e ); + void paintEvent( QPaintEvent *e ); void paintNumber( QPainter *p ); + virtual void mousePressEvent( QMouseEvent* ); }; @@ -102,5 +122,4 @@ class DiceWidget : public QWidget }; - class Board : public QWidget { @@ -108,4 +127,11 @@ class Board : public QWidget public: Board( QWidget *parent = 0, const char* name = 0 ); + + signals: + void clicked( QPoint ); + void item( int ); + + protected: + virtual void mousePressEvent( QMouseEvent* ); }; @@ -116,7 +142,19 @@ class Possibilityboard : public Board public: 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 ); }; @@ -126,7 +164,11 @@ 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: virtual void paintEvent( QPaintEvent *e ); @@ -143,6 +185,9 @@ class Scoreboard : public QWidget resultboardList rbList; playerList ps_; - + void createResultboards(const int); + + Resultboard* nextRB(int); + protected: @@ -158,16 +203,15 @@ class Player QString playerName; 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 */ }; |