File indexing completed on 2024-04-21 04:05:24

0001 /*
0002     This file is part of the KDE games lskat program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef ENGINE_TWO_H
0009 #define ENGINE_TWO_H
0010 
0011 // Qt includes
0012 
0013 // KF includes
0014 
0015 // Local includes
0016 #include "abstractengine.h"
0017 #include "deck.h"
0018 #include "lskat_debug.h"
0019 
0020 // Forward declaration
0021 class DisplayTwo;
0022 
0023 using namespace CardDeck;
0024 
0025 /**
0026  * The game engine for two players
0027  */
0028 class EngineTwo : public AbstractEngine
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033     /**
0034      * Move phase states
0035      */
0036     enum MoveState {FirstPlayerTurn = 0, SecondPlayerTurn = 1};
0037 
0038     /**
0039      * Constructor for the game engine.
0040      * @param parent The parent window
0041      * @param deck   The card deck
0042      * @param display The display engine
0043      */
0044     EngineTwo(QWidget *parent, Deck *deck, DisplayTwo *display);
0045 
0046     /**
0047      * Start a new game.
0048      * @param trump The trump suite.
0049      * @param startPlayer The start player
0050      */
0051     void startGame(Suite trump, int startPlayer) override;
0052 
0053     /**
0054      * Stop a game.
0055      */
0056     void stopGame() override;
0057 
0058     /**
0059      * Retrieve the current move phase.
0060      * @return The move phase.
0061      */
0062     MoveState currentMovePhase() const {return mCurrentMovePhase;}
0063 
0064     /**
0065      * Retrieve the currently played card
0066      * @param no Which card (0,1)
0067      * @return The played card.
0068      */
0069     int playedCard(int no) const {return mCurrentMoveCards[no];}
0070 
0071     /**
0072      * Retrieve the current trump color.
0073      * @return The trump suite.
0074      */
0075     Suite trump() const {return mTrump;}
0076 
0077 public Q_SLOTS:
0078     /**
0079      * Player input available
0080      * @param inputId      The input device id
0081      * @param playerNumber The player number [0-1]
0082      * @param cardNumber   The card number [0-7]
0083      */
0084     void playerInput(int inputId, int playerNumber, int cardNumber) override;
0085 
0086     /**
0087      * Check whether the two cards played are legal, supposed the
0088      * given player is the second one.
0089      * @param card1  The card played by the first player
0090      * @param card2  The card played by the second player
0091      * @param playerNumber The id of the latter player (to access its cards)
0092      * @return true if the move is legal, false otherwise.
0093      */
0094     bool isLegalMove(int card1, int card2, int playerNumber);
0095 
0096     /**
0097      * Check who won a move, the first or the second card.
0098      * @param card1 Card value played by first mover
0099      * @param card2 Card value played by latter mover
0100      * @param trump Current trump suite
0101      * @return The winning player [0,1]
0102      */
0103     static int whoWonMove(int card1, int card2, Suite trump);
0104 
0105 protected Q_SLOTS:
0106     /**
0107      * First part of the game loop.
0108      */
0109     void gameLoopStart();
0110 
0111     /**
0112      * Second part of the game loop.
0113      */
0114     void gameLoopFinish();
0115 
0116 protected:
0117     /**
0118      * Active the current player to the given player number.
0119      * Enable inputs etc.
0120      * @param playerNumber The new player playerNumber
0121      */
0122     void activatePlayer(int playerNumber);
0123 
0124     /**
0125      * Check whether the game is over
0126      * @return True if the game is over, false otherwise
0127      */
0128     bool gameOver();
0129 
0130     /**
0131      * Called after game ends and then give points to players.
0132      * @return The winning player
0133      */
0134     int evaluateGame();
0135 
0136 private:
0137     // The display engine
0138     DisplayTwo *mDisplay;
0139     // The card deck
0140     Deck *mDeck;
0141     // Current move cards
0142     int mCurrentMoveCards[2];
0143     // Current cards to turn at end of move (or -1)
0144     int mCurrentTurnCards[2];
0145     // Move phase (1st player or 2nd player)
0146     MoveState mCurrentMovePhase;
0147     // Current move number (counting half moves)
0148     int mMoveNumber;
0149     // Trump color
0150     Suite mTrump;
0151 };
0152 
0153 #endif