summaryrefslogtreecommitdiffstats
path: root/kpoker/kpoker.h
blob: 94b82db25a0242a7880e59ca2e0ee64a97723f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#ifndef KPOKER_H
#define KPOKER_H


// QT includes
#include <tqwidget.h>
#include <tqptrlist.h>

// KDE includes
#include <krandomsequence.h>

// own includes
#include "poker.h"


// QT classes
class TQPushButton;
class TQLineEdit;
class TQLabel;
class TQFrame;  
class TQLineEdit;
class TQFrame;
class TQHBoxLayout;
class TQVBoxLayout;

// KDE classes
class KConfig;


// own classes
class BetBox;
class CardWidget;
class OptionsDlg;
class NewGameDlg;
class PokerPlayer;
class PlayerBox;


// ================================================================
//                            Poker Game


enum PokerGameType {
  SinglePlayer,
  MultiPlayer
};


enum PokerGameState {
  StateStartRound=0,		// Before dealing. Deal cards to switch state.

  StateBet1,
  StateRaise1,

  StateExchangeCards,

  StateBet2,
  StateRaise2,

  StateSee
};


// A PokerGame would be the main class in any poker game.  It controls
// the game flow and has control over all the players.
//
// FIXME: This is very much in flux right now.  It is in the process
//        of being separated from class kpok.  A lot of things need to 
//        be done.  Among them are:
//          - Finish separation from kpok.

class PokerGame {
public:
  PokerGame(KRandomSequence *random);
  ~PokerGame();

  PokerGameType   getType() const                { return m_type; }
  void            setType(PokerGameType type)    { m_type = type; }

  // The state of the current round.
  PokerGameState  getState() const               { return m_state; }
  void            setState(PokerGameState state) { m_state = state; }

  // Money in the game.
  void            clearPot()                     { m_pot = 0; }
  int             getPot() const                 { return m_pot; }
  void            bet(int amount)                { m_pot += amount; }
  void            bet(PokerPlayer * player, int amount);

  int             getMinBet() const              { return m_minBet; }
  int             getMaxBet() const              { return m_maxBet; }
  void            setBettingLimits(int minBet, int maxBet)
    { m_minBet = minBet;  m_maxBet = maxBet; }

  // Players
  int             getNumPlayers() const          { return m_numPlayers; }
  int             getNumActivePlayers() const    { return m_activePlayers.count(); }
  int             getNumInactivePlayers() const  { return m_removedPlayers.count(); }
  PokerPlayer *   getActivePlayer(unsigned int nr)  { return m_activePlayers.at(nr); }
  void            activatePlayer(PokerPlayer *player);
  void            inactivatePlayer(PokerPlayer *player);
  bool            isActivePlayer(PokerPlayer *player) const { return m_activePlayers.contains(player); }

  // Misc
  // FIXME: clearDirty should only be called by a save method
  //        The isDirty flag should only be set internally.
  void            setDirty()                     { m_isDirty = true; }
  void            clearDirty()                   { m_isDirty = false; }
  bool            isDirty() const                { return m_isDirty; }

  // Some more complex methods.  FIXME: These must be expanded!
  void  newGame(PokerGameType type, 
		int numPlayers, PokerPlayer *players,
		int minBet, int MaxBet);
  void  newRound();

  void  dealCards(PokerPlayer *player, bool skip[]);


 private:

  // ----------------------------------------------------------------
  // Properties of the entire game, not just one round:

  PokerGameType   m_type;	// The current type of game
  unsigned int    m_numPlayers; // Used for constructing and deleting only
  PokerPlayer    *m_players;	// The players (owned by kpok)

  int             m_minBet; // the money the player will bet if he wants or not
  int             m_maxBet; // max total bet including minBet.

  // True if we need to save before exiting.
  // This is the case if the cash has changed for any of the players.
  bool            m_isDirty;

  // ----------------------------------------------------------------
  // Properties of the current round:

  PokerGameState  m_state;	// The current phase of the game round
  CardDeck        m_deck;	// The card deck we are using
  int             m_pot; 	// The amount of money people have bet.

  // The players in the game.
  TQPtrList<PokerPlayer>   m_activePlayers;  // players still in the round
  TQPtrList<PokerPlayer>   m_removedPlayers; // players out of this round
};


// ================================================================
//                          Poker Game View


class kpok : public TQWidget
{
  Q_OBJECT
  TQ_OBJECT

 public:
  kpok(TQWidget * tqparent = 0, const char *name = 0);
  virtual ~kpok();
	
  TQString getName (int playerNr); 
  void paintCash();

  bool isDirty() const                          { return m_game.isDirty(); }
	
  void setBlinking(bool bl)			{ m_blinking = bl; }
  void setAdjust(bool ad);
  void setSound(bool s);
	
  void updateLHLabel();//temporary function, only called once

  bool getSound() const				{ return sound; }
  bool getBlinking() const			{ return m_blinking; }
  bool getAdjust() const			{ return adjust; }

 signals:
  void changeLastHand(const TQString &newHand, bool lastHand = true);
  void showClickToHold(bool show);
  void statusBarMessage(TQString);
  void clearStatusBar();

 protected:
  void initWindow(); // called only once 
  void readOptions();
  void drawCards(PokerPlayer* p, bool skip[]);
  void newRound();
  void noMoney();
  void paintEvent( TQPaintEvent * );	
  void playSound(const TQString &filename);
  void setBetButtonEnabled(bool enabled);
  void setHand(const TQString& newHand, bool lastHand = true);
  void setLastWinner(const TQString& lastWinner);
  void startBlinking();
  void stopBlinking();
  void stopDrawing();
  void result();
  void winner();
	
  void bet();

  void displayWin(const TQString& hand, int cashWon);

  /**
   * Displays the winner, adds the pot to his money
   *
   * othersPassed = true means all the other players didn't bet
   **/
  void displayWinner_Computer(PokerPlayer* winner, bool othersPassed);
	
  void removePlayerFromRound(PokerPlayer* removePlayer);
  void switchToOnePlayerRules();

  /**
   * @return The human player if he is in the game
   **/
  PokerPlayer* findHumanPlayer();

  /**
   * This method first reads the config file then starts a
   * newGame Dialog if it wasn't forbidden in config
   * 
   * After all options and defaults were set the method starts
   * @ref initPoker() with only the number of the players as an
   * argument or with a complete player class, depending on the
   * options the player chose @return True if successful false
   * if player clicked 'cancel' in the new game dialog
   **/
  bool readEntriesAndInitPoker();

  /**
   * This should only be done in the see phase and shows the
   * cards of the computer players
   **/
  void showComputerCards();

  /**
   * The main method for starting the game
   *
   * It constructs the players if only the number of players
   * are given or uses an existing array Then all player boxes
   * are being constructed as well as some smaller things like
   * the pot
   * @param players the number of the players 
   * @param ownAllPlayers This is used if there is already an array of players existing e.g. from @ref readEntriesAndInitPoker()
   **/
  void initPoker(unsigned int numPlayers);

  /**
   * Gives all players the deck as a card
   **/
  void drawAllDecks();


 public slots:
  void slotCardDeck();
  void toggleSound();
  void toggleAdjust();
  void toggleBlinking();
  void slotPreferences();
	
  bool initSound();

  /**
   * Just as the name says: This method/slot saves the current
   * game (to the config file)
   *
   * The game can be loaded on startup by activating the button
   * 'read from config'
   **/
  void saveGame(KConfig* conf);

  bool loadGame(KConfig* conf);
  bool loadGame();
  //	void commandCallback(int id);
  void newGame();
  void saveGame();

  void exchangeCard1();
  void exchangeCard2();
  void exchangeCard3();
  void exchangeCard4();
  void exchangeCard5();
  void drawClick();

  protected slots:
    void bTimerEvent();
  void drawCardsEvent();
  void waveTimerEvent();

  void betChange(int);
  void adjustBet();
  void out();

  void startWave();
  void stopWave();
  void toggleHeld(); // play a sound

 private:
  // The "document" - the game itself
  PokerGame       m_game;       // The game that this widget is showing.
  unsigned int    m_numPlayers;
  PokerPlayer    *m_players;	// The players

  int             cashPerRound;   // single player game: the ante
  int             currentMustBet; // the minimum bet amount
  int             oldBet_raise;   // used for raising only

  int             drawDelay;

  // Graphical tqlayout.
  TQVBoxLayout  *topLayout;
  TQHBoxLayout  *inputLayout;
  TQLabel       *potLabel;
  BetBox       *betBox;
  TQPushButton  *drawButton;    // the main Button
  TQLabel       *wonLabel;      // the winner
  TQLabel       *clickToHold; 
  TQWidget      *mWonWidget;

  PlayerBox   **playerBox;     //one box per player

  // Dialogs
  OptionsDlg* mOptions;
	
  // Other stuff
  KRandomSequence  m_random;

  TQTimer       *blinkTimer; // the winning cards will blink 
  TQTimer       *drawTimer;  // delay between drawing of the cards 
  TQTimer       *waveTimer;  // for displaying of the win (if winner == human)

  bool          adjust;     // allow user to adjust the bet.
  int           drawStat;   // status of drawing (which card already was drawn etc.

  bool          sound;

  bool          m_blinking;	// True if card should blink when winning.
  int           m_blinkStat;	// status of blinking
  int           m_blinkingBox;	// box of winning player
   
  bool          waveActive;
  int           fCount;

  TQString lastHandText;
};

#endif