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

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 AI_INPUT_H
0009 #define AI_INPUT_H
0010 
0011 // KF includes
0012 #include "lskat_debug.h"
0013 
0014 // Local includes
0015 #include "abstractinput.h"
0016 #include "engine_two.h"
0017 #include "deck.h"
0018 
0019 /**
0020  * AI input device
0021  */
0022 class AiInput : public AbstractInput
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     /**
0028      * Constructor for the input
0029      * @param engine  The game engine
0030      * @param parent The parent object
0031      */
0032     AiInput(EngineTwo *engine, QObject *parent);
0033 
0034     /**
0035      * Allow or disallow input with this device
0036      * @param allowed True if input is allowed
0037      */
0038     void setInputAllowed(bool allowed) override;
0039 
0040     /**
0041      * Retrieve the type of device.
0042      * @return The device type.
0043      */
0044     InputDevice::InputDeviceType type() override {return InputDevice::TypeAiInput;}
0045 
0046 public Q_SLOTS:
0047     /**
0048      * AI turn is performed.
0049      */
0050     void aiTurn();
0051 
0052 private:
0053     /**
0054      * AI storage of the game board.
0055      */
0056     class Board
0057     {
0058     public:
0059         /**
0060          * Constructor
0061          */
0062         Board() {}
0063         /**
0064          * Copy constructor
0065          * @param board Another board
0066          */
0067         Board(const Board &board);
0068 
0069         /**
0070          * Retrieve card of given player on given position
0071          * @param p   player [0,1]
0072          * @param pos Position [0-7]
0073          * @return The card id.
0074          */
0075         int card(int p, int pos) const;
0076 
0077         /**
0078          * Take away card of given player on given position
0079          * @param p   player [0,1]
0080          * @param pos Position [0-7]
0081          * @return The card id.
0082          */
0083         int takeCard(int p, int pos);
0084 
0085         /**
0086          * Check amount of cards at position, that is
0087          * card on top (2), bottom (1), or none at all (0)
0088          * @param p   player [0,1]
0089          * @param pos Position [0-7]
0090          * @return The amount of cards.
0091          */
0092         int cardsAtPos(int p, int pos) const;
0093 
0094         /**
0095          * Analyze board, e.g. count how many of each suite.
0096          */
0097         void analyze();
0098 
0099         /** Cards of both players or -1 for used cards */
0100         int cards[2][16];
0101         /** Already played cards */
0102         int playedCards[32];
0103         /** How many cards of each suite (4==Trump color) */
0104         int amountOfSuite[2][5];
0105         /** How many cards of each type  */
0106         int amountOfCardType[2][8];
0107         /** Currently played card of first player or -1 */
0108         int playedCard;
0109         /** Points of both players */
0110         int points[2];
0111         /** Whose turn is it 0/1 UNUSED */
0112         int whoseTurn;
0113         /** True if first player movement phase UNUSED */
0114         bool firstPlay;
0115         /** Trump color */
0116         Suite trump;
0117     };
0118     /**
0119      * AI representation of a move.
0120      */
0121     class Move
0122     {
0123     public:
0124         /** The move value */
0125         double value;
0126         /** The move position */
0127         int move;
0128     };
0129 
0130 protected:
0131     /**
0132      * Extract the current game board from the engine.
0133      * @return The game board.
0134      */
0135     Board getBoardFromEngine();
0136 
0137     /**
0138      * Initiate a new move as first player.
0139      * @param p The current player number.
0140      * @param board The current game board.
0141      * @return The best move.
0142      */
0143     Move initiateMove(int p, const Board &board);
0144 
0145     /**
0146      * Answer a move as second player.
0147      * @param p The current player number.
0148      * @param board The current game board.
0149      * @return The best move.
0150      */
0151     Move answerMove(int p, const Board &board);
0152 
0153     /**
0154      * Evaluate the current game board and return a rating.
0155      * @param p The current player (we)
0156      * @param current The current game board
0157      * @return The rating for this situation.
0158      */
0159     double evaluteGame(int p, const AiInput::Board &current);
0160 
0161     int findCard(const AiInput::Board &current, Suite sSuite, CardType sCardType) const;
0162     int amountOfWinningCards(int p, Suite sSuite, const AiInput::Board &current) const;
0163     bool wouldWinMove(int p, int card, const AiInput::Board &current) const;
0164     double rulebaseFirstMover(int p, int card, const AiInput::Board &current) const;
0165     double rulebaseAnswerMover(int p, int card, const AiInput::Board &current) const;
0166     bool hasAmount(int player, Suite suite, int min, int max, const AiInput::Board &current) const;
0167     int amountOfOpenCards(int p, const AiInput::Board &current) const;
0168     bool isLegalMove(int card1, int card2, int pl, const AiInput::Board &current) const;
0169 
0170 private:
0171     /** The game engine used */
0172     EngineTwo *mEngine;
0173 };
0174 
0175 #endif