-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartplayer.cpp
195 lines (181 loc) · 5.58 KB
/
smartplayer.cpp
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
/*
sueca - An implementation of the Portuguese game "Sueca" in C++ and wxWidgets
Copyright (C) 2003-2024 Rodrigo Araujo
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.
*/
#include <cstring> // For memset()
#include "smartplayer.hpp"
#include "main.hpp" // For wxGetApp()
// Smart player implementation
SmartPlayer::SmartPlayer( GamePos* gamepos ):
BotPlayer( gamepos ), thegame( NULL ), players( NULL ) {}
SmartPlayer::~SmartPlayer()
{
if( players )
delete players;
}
void SmartPlayer::NewGame( Game* game )
{
thegame = game;
if( players )
delete players;
// Meet the other players
players = game->GetPlayers();
players->SetCurrent( this );
right = players->GetNext();
partner = players->GetNext();
left = players->GetNext();
}
void SmartPlayer::NewRound( Card* newtrumph, Player* newowner )
{
trumph = newtrumph;
trumphowner = newowner;
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 4; j++ ) {
plhasnot[i][j] = false;
released[i][j] = 0;
}
out.Clear();
for( int i = SUITMIN; i <= SUITMAX; i++ )
n_out[i] = 0;
CardList::Node* node = GetHand().GetFirst();
// Group hand by suit, in ascending order
while( node ) {
Card* card = node->GetData();
CardList& suitlst = bysuit[card->GetSuit().GetId()];
CardList::Node* suitnode = suitlst.GetFirst();
while( suitnode && card->GetType() > suitnode->GetData()->GetType() )
suitnode = suitnode->GetNext();
if( suitnode )
suitlst.Insert( suitnode, card );
else
suitlst.Append( card );
node = node->GetNext();
}
}
void SmartPlayer::NewTurn( Player* starter )
{
turnstarter = starter;
}
void SmartPlayer::TurnEnd( const Player* winner, const CardList& played )
{
// Remove the card we played from our list
bysuit[played_card->GetSuit().GetId()].DeleteObject( played_card );
// Memorize cards that have been played
players->SetCurrent( turnstarter );
CardList::Node* node = played.GetFirst();
cardsuit_t firstsuit = node->GetData()->GetSuit().GetId();
while( node ) {
Card* card = node->GetData();
node = node->GetNext();
out.Append( card );
n_out[card->GetSuit().GetId()]++;
plindex_t pli;
if( ( pli = PlayerIndex( players->GetCurrent() ) ) != SBOT_THIS ) {
cardsuit_t suit_i = card->GetSuit().GetId();
released[pli][suit_i]++;
if( suit_i != firstsuit )
plhasnot[pli][suit_i] = true;
}
players->GetNext();
}
}
Card* SmartPlayer::PlayCard( const CardList* played )
{
Card *crdpartner, *crdleft, *crdright, *first;
cardsuit_t trumphsuit = trumph->GetSuit().GetId();
// See which card belongs to whom
CardList::Node* node = played->GetFirst();
if( !node ) {
// We are the first to play
// Start by trying our best non-trumph cards
for( int thissuit = SUITMIN; thissuit <= SUITMAX; thissuit++ ) {
if( thissuit == trumphsuit ||
released[SBOT_LEFT][thissuit] > 1 ||
released[SBOT_RIGHT][thissuit] > 1 ||
n_out[thissuit] > 4 )
continue;
if( ( node = bysuit[thissuit].GetLast() ) ) {
Card* card = node->GetData();
switch( cardtype_t cardtp = card->GetType().GetId() ) {
case ACE:
if( !plhasnot[SBOT_LEFT][cardtp] && !plhasnot[SBOT_RIGHT][cardtp] )
return ( played_card = card );
break;
case SEVEN:
if( !plhasnot[SBOT_LEFT][cardtp] && !plhasnot[SBOT_RIGHT][cardtp] &&
IsOut( ACE, thissuit ) )
;
return ( played_card = card );
break;
default:
break;
}
}
}
// Try a trumph card
if( ( node = bysuit[trumphsuit].GetLast() ) ) {
return ( played_card = node->GetData() );
}
}
else {
//Player *winner = thegame->TurnWinner();
first = node->GetData();
switch( played->GetCount() ) {
case 1:
crdleft = first;
crdpartner = crdright = NULL;
break;
case 2:
crdpartner = first;
crdleft = node->GetNext()->GetData();
crdright = NULL;
break;
case 3:
default:
crdright = first;
crdpartner = node->GetNext()->GetData();
crdleft = node->GetNext()->GetNext()->GetData();
break;
}
// TODO
}
// Awful game right now... play anything, like a dumb player
Card *toplay;
node = GetHand().GetFirst();
while( !IsValidMove( ( toplay = node->GetData() ), *played ) )
node = node->GetNext();
return ( played_card = toplay );
}
plindex_t SmartPlayer::PlayerIndex( Player* player )
{
if( player == left )
return SBOT_LEFT;
if( player == right )
return SBOT_RIGHT;
if( player == partner )
return SBOT_PARTNER;
return SBOT_THIS;
}
bool SmartPlayer::IsOut( int type_id, int suit_id )
{
CardList::Node* node = out.GetFirst();
while( node ) {
Card* card = node->GetData();
if( card->GetType().GetId() == type_id &&
card->GetSuit().GetId() == suit_id )
return true;
node = node->GetNext();
}
return false;
}