1 /**********************************************************************
2  *
3  *   FreeDoko a Doppelkopf-Game
4  *
5  *   Copyright (C) 2001 – 2018 by Diether Knof and Borg Enders
6  *
7  *   This program is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU General Public License as
9  *   published by the Free Software Foundation; either version 2 of
10  *   the License, or (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *   You can find this license in the file 'gpl.txt'.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  *   MA  02111-1307  USA
22  *
23  *  Contact:
24  *    Diether Knof dknof@posteo.de
25  *
26  *********************************************************************/
27 
28 #pragma once
29 
30 #include "aiDb.h"
31 
32 #include "../player.h"
33 #include "../aiconfig.h"
34 #include "../../game/game.h"
35 
36 #include "../../basetypes.h"
37 
38 #include "cards_information.h"
39 #include "team_information.h"
40 #include "heuristic.h"
41 
42 class VirtualGamesInterface;
43 class HeuristicInterface;
44 class SoloDecision;
45 
46 class Ai: public Player, public Aiconfig {
47   friend class WVirtualGames;
48   friend class Gametree;
49   friend struct TrickWeighting;
50   friend class ::Heuristic;
51 
52   friend class VirtualGamesAi;
53   friend class HeuristicAi;
54   public:
55 
56   Ai();
57   Ai(Aiconfig const& aiconfig);
58   Ai(Player const& player);
59   Ai(Player const& player, Aiconfig const& aiconfig);
60   Ai(istream& istr);
61   Ai(Ai const& ai);
62   Ai& operator=(Ai const& ai) = delete;
63   // clone the player
64   unique_ptr<Player> clone() const override;
65 
66   ~Ai() override;
67 
68   virtual operator HeuristicInterface const&() const;
69   virtual operator VirtualGamesInterface const&() const;
70   TeamInformation const& team_information() const;
71   TeamInformation& team_information();
72   CardsInformation const& cards_information() const;
73   CardsInformation& cards_information();
74   AiDb const* db() const;
75   AiDb* db();
76 
77   // write the ai in the stream
78   ostream& write(ostream& ostr) const override;
79 
80   // read the config 'config' from 'istr'
81   bool read(Config const& config, istream& istr) override;
82 
83   // compares the ai
84   virtual bool isequal(Ai const& ai) const;
85 
86   AiType aitype() const;
87   unsigned future_limit() const;
88   Rating::Type rating() const;
89 
90   // set the cards information
91   void set_cards_information(CardsInformation const& cards_information);
92 
93   // set the game
94   void set_game(Game& game) override;
95   Hand const& set_hand(Hand const& hand) override;
96 
97   // h hands of players 0 till number_of_players number of player for Weighting
98   void new_game(Game& game) override;
99   Reservation const& reservation_get(bool isDuty = false ) override;
100   void rule_changed(int type, void const* old_value) override;
101   void game_open(Game& game) override;
102   void game_start() override;
103   void game_close(Game const& game) override;
104   HandCard card_get() override;
105   virtual HandCard card_suggestion();
106   void card_played(HandCard const& card) override;
107   void check_swines_announcement_at_game_start() override;
108   void swines_announced(Player const& player) override;
109   void hyperswines_announced(Player const& player) override;
110   void marriage(Player const& bridegroom, Player const& bride) override;
111   void genscher(Player const& genscher, Player const& partner) override;
112   Card nextcard(Trick const& trick);
113 
114   Heuristic last_heuristic() const;
115   Rationale const& last_heuristic_rationale() const;
116   void set_last_heuristic_to_manual();
117   void set_last_heuristic_to_bug_report();
118 
119   // the teaminfo of 'player'
120   Team teaminfo(Player const& player) const;
121   // update the teaminfo
122   // (compare with the teaminfo of the game and check the current trick
123   void teaminfo_update() override;
124   // the partner of the player
125   Player const* partner(Player const& player) const;
126   // the points the team has made
127   unsigned points_of_team() const;
128 
129   // set the teams
130   virtual void set_teams(vector<Team> const& teams);
131 
132   // the trick is opened
133   void trick_open(Trick const& trick) override;
134   // the trick is full
135   void trick_full(Trick const& trick) override;
136 
137   Announcement announcement_request() const override;
138   void check_and_make_first_announcement(HandCard const& handcard);
139   void announcement_made(Announcement announcement,
140                          Player const& player) override;
141 
142   bool colorjabbed(Card::Color c) const;
143 
144   unsigned color_runs(Card::Color color) const;
145   unsigned color_runs(Trick const& trick) const;
146 
147   bool jabbedbyownteam(Card::Color c) const;
148 
149   bool jabbedbyotherteam(Card::Color c) const;
150 
151 
152   bool next_low() const;
153 
154   void check_low_high(HandCard const& card);
155 
156   protected:
157   unsigned last_trick_to_calculate() const;
158   void set_last_trick_to_calculate(unsigned last_trick_to_calculate);
159 
160   // 'player' shifts cards
161   HandCards poverty_shift() override;
162   // returns whether 'player' accepts the poverty
163   bool poverty_take_accept(unsigned cardno) override;
164   // the player changes the cards of the poverty
165   HandCards poverty_cards_change(HandCards const& cards) override;
166   // the poverty player gets 'cards'
167   void poverty_cards_get_back(HandCards const& cards) override;
168 
169   public:
170   Card trump_card_limit() const;
171   Card lowest_trump_card_limit() const;
172 
173   private:
174 
175   MarriageSelector get_Marriage() const;
176 
177   public:
178   Player const* genscher_partner() override;
179 
180   private:
181   unique_ptr<TeamInformation> team_information_;
182   unique_ptr<CardsInformation> cards_information_;
183   // Team of all players known by this player
184   vector<Team> teaminfo_;
185   vector<Trick> trick_; // last played tricks
186   unsigned trickno_ = 0;
187   vector<vector<bool> > colorjabbed_; // remembering for each player if he has jabbed this color
188   bool low_ = false; // next card should be low or high trump
189   Card::Color meatlesscolor_ = Card::nocardcolor;
190   vector<bool> trumpless_; // players has no trumps
191 
192   unsigned last_trick_to_calculate_ = 12;
193 
194   Aiconfig::Heuristic last_heuristic_ = Aiconfig::Heuristic::no_heuristic;
195   Rationale last_heuristic_rationale_;
196 
197   public:
198   Hand const& handofplayer(Player const& player) const;
199   Team teamofplayer(Player const& player) const;
200 
201   protected:
202   unique_ptr<VirtualGamesInterface> vgi_;
203   unique_ptr<HeuristicInterface> hi_;
204   vector<unique_ptr<SoloDecision>> solo_decisions_;
205 
206   bool silent_marriage_ = false;
207 }; // class Ai
208