-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
256 lines (223 loc) · 6.47 KB
/
player.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
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
/*
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 <cstdlib> // for rand() and srand()
#include <ctime> // time()
#include "player.hpp"
int GamePos::xgap = (MC_X_SIZE-9*CARDBMP_INCR-CARDBMP_W)/2;
int GamePos::ygap = (MC_Y_SIZE-9*CARDBMP_INCR-CARDBMP_H)/2;
void GamePos::ResetCheck()
{
if( i == MAX_CARDS )
i = 0;
}
// Posiotioning methods
GamePosP1::GamePosP1(): GamePos()
{
m_name = "bottom";
xstart = xgap;
ystart = pos.y = MC_Y_SIZE - CARDBMP_GAP - CARDBMP_H;
};
wxPoint& GamePosP1::NextPosition()
{
pos.x = xstart + ( i++ ) * CARDBMP_INCR;
ResetCheck();
return (wxPoint&)pos;
}
GamePosP2::GamePosP2(): GamePos()
{
m_name = "right";
xstart = pos.x = MC_X_SIZE - CARDBMP_GAP - CARDBMP_W;
ystart = MC_Y_SIZE - ygap - CARDBMP_H;
}
wxPoint& GamePosP2::NextPosition()
{
pos.y = ystart - (i++) * CARDBMP_INCR;
ResetCheck();
return (wxPoint&)pos;
}
GamePosP3::GamePosP3(): GamePos()
{
m_name = "top";
xstart = MC_X_SIZE - xgap - CARDBMP_W;
ystart = pos.y = CARDBMP_GAP;
};
wxPoint& GamePosP3::NextPosition()
{
pos.x = xstart - (i++) * CARDBMP_INCR;
ResetCheck();
return (wxPoint&)pos;
}
GamePosP4::GamePosP4(): GamePos()
{
m_name = "left";
xstart = pos.x = CARDBMP_GAP;
ystart = ygap;
};
wxPoint& GamePosP4::NextPosition()
{
pos.y = ystart + (i++) * CARDBMP_INCR;
ResetCheck();
return (wxPoint&)pos;
}
// Base player class implementation
Player::Player( GamePos* gamepos ):
m_gamepos( gamepos ), m_hidden_cards( true ), m_team( NULL ) {}
void Player::AddToHand( Card* newcard, MyCanvas* canvas )
{
m_hand.Append( newcard );
wxPoint pos = m_gamepos->NextPosition();
canvas->Add( newcard, pos.x, pos.y, AreCardsHidden() );
}
bool Player::IsValidMove( const Card* card, const CardList& played )
{
if( ! GetHand().Find( card ) )
return false; // Trying to play a card you don't have
if( played.GetCount() == 0 )
return true; // First to play
CardSuit current_suit = played.GetFirst()->GetData()->GetSuit();
if( card->GetSuit() == current_suit )
return true;
CardList::Node* node = GetHand().GetFirst();
while( node ) {
if( node->GetData()->GetSuit() == current_suit )
return false; // Trying to cheat :)
node = node->GetNext();
}
return true;
}
void Player::SetGamePos( GamePos* newpos )
{
delete m_gamepos;
m_gamepos = newpos;
}
// Team class implementation
Team::Team( Player* np1, Player* np2 ):
m_won( 0 ), m_points( 0 ), p1( np1 ), p2( np2 ), wonstr( "" )
{
p1->SetTeam( this );
p2->SetTeam( this );
}
void Team::AddToCapt( CardList& cards )
{
CardList::Node* node = cards.GetFirst();
while( node ) {
Card* crd = node->GetData();
m_points += crd->GetType().GetValue();
captured.Append( crd );
node = node->GetNext();
}
}
void Team::NewRound( Card* trumph, Player* owner )
{
m_points = 0;
captured.Clear();
p1->NewRound( trumph, owner );
p2->NewRound( trumph, owner );
}
bool Team::Replace( Player* oldplayer, Player* newplayer )
{
if( oldplayer == p1 )
p1 = newplayer;
else if( oldplayer == p2 )
p2 = newplayer;
else
return false;
newplayer->SetTeam( this );
return true;
}
// Human players class implementations
HumanPlayer::HumanPlayer( const wxString& name, GamePos* gamepos ):
Player( gamepos )
{
SetName( name );
}
LocalPlayer::LocalPlayer( const wxString& name, GamePos* gamepos ):
HumanPlayer( name, gamepos )
{
m_hidden_cards = false;
}
void LocalPlayer::AddToHand( Card* newcard, MyCanvas* canvas )
{
// Sort cards as we add them to the player's hand
CardList::Node *node = GetHand().GetFirst();
while( node && newcard->GetSuit() > node->GetData()->GetSuit() )
node = node->GetNext();
if ( !node )
GetHand().Append( newcard );
else {
while( node &&
newcard->GetSuit() == node->GetData()->GetSuit() &&
newcard->GetType() > node->GetData()->GetType() )
node = node->GetNext();
if ( !node )
GetHand().Append( newcard );
else
GetHand().Insert( node, newcard );
}
// Delay canvas placing until we reach the last card
// because of the sorting
if ( GetHand().GetCount() == 10 ) {
node = GetHand().GetFirst();
while( node ) {
Card* card = node->GetData();
wxPoint pos = m_gamepos->NextPosition();
canvas->Add( card, pos.x, pos.y, AreCardsHidden() );
card->SetPlayable();
node = node->GetNext();
}
}
}
// Computer players class implementations
char* BotPlayer::botnames[N_BOT_NAMES] = {
"George", "Tony", "Saddam", "Bin",
"Bill", "Steve", "Linus", "Alan",
"Lula", "Jorge", "Durao", "Ferro",
"Paulo", "Carlos"
};
bool BotPlayer::nameused[N_BOT_NAMES] = { false };
BotPlayer::BotPlayer( GamePos* gamepos ):
Player( gamepos )
{
// Random name, avoiding repeated ones
do {
nameind = (int)( (double)N_BOT_NAMES * rand() / ( RAND_MAX + 1.0 ) );
} while( nameused[nameind] ); // N_BOT_NAMES should be >= 4 ;)
nameused[nameind] = true;
SetName( botnames[nameind] );
}
BotPlayer::~BotPlayer()
{
// "Free" the name
nameused[nameind] = false;
}
void BotPlayer::OnMyTurn( Game* game, const CardList& played )
{
Card *toplay;
do {
toplay = PlayCard( & played );
} while( game->PlayMove( this, toplay ) != MOVE_OK );
}
// Dumb player implementation
DumbPlayer::DumbPlayer( GamePos* gamepos ):
BotPlayer( gamepos ) {}
Card* DumbPlayer::PlayCard( const CardList* played )
{
Card *toplay;
CardList::Node* node = GetHand().GetFirst();
while( !IsValidMove( ( toplay = node->GetData() ), *played ) )
node = node->GetNext();
return toplay;
}