/
sgsai_AIGuanyu.cpp
158 lines (139 loc) · 4.55 KB
/
sgsai_AIGuanyu.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
/*
* sgsai_AIGuanyu.cpp
*
* Created on: May 15, 2012
* Author: latios
*/
#include "sgsai_AIGuanyu.h"
#include "sgsui_output.h"
#include "sgsdata_card.h"
#include "sgsdata_skill.h"
#include "sgsdata_player.h"
#include "sgsdata_msg.h"
#include "sgsui_PlayerSort.h"
#include "sgsui_output.h"
#include "sgsui_nameConvert.h"
namespace sgsai {
AIGuanyu::AIGuanyu(int seat) :
AICommon(seat) {
}
bool AIGuanyu::useSkillOrNot(sgs::ConstData::HeroSkill /*skillType*/,
sgs::DataType::Player * /*player*/) {
printDebug(
"AIGuanyu::useSkillOrNot: start, AI's seat = "
+ QString::number(mySeat()));
printDebug(
"<font color=red><b>Warning: </b></font>AIGuanyu::useSkillOrNot: shouldn't be called becase guanyu has no passive skill");
bool returnValue = true;
printDebug(
"AIGuanyu::useSkillOrNot: over, return "
+ QString(returnValue ? "true" : "false"));
return returnValue;
}
sgs::DataType::Message * AIGuanyu::useCardResponse() {
sgs::Derive::MSkill * wusheng = useWushengInUsecard();
if (wusheng) {
printDebug(sgsui::messageInterpret(wusheng));
printDebug(
"AIGuanyu::useCardResponse: use wusheng, AI's seat = "
+ QString::number(mySeat()));
return wusheng;
}
return AICommon::useCardResponse();
}
sgs::DataType::Message * AIGuanyu::shaOrNot(sgs::DataType::Player * from,
const sgs::DataType::Card * card) {
sgs::Derive::MSkill * wusheng = useWushengInShaOrNot();
if (wusheng) {
printDebug(sgsui::messageInterpret(wusheng));
printDebug(
"AIGuanyu::shaOrNot: use wusheng, AI's seat = "
+ QString::number(mySeat()));
return wusheng;
}
return AICommon::shaOrNot(from, card);
}
sgs::Derive::MSkill * AIGuanyu::useWushengInUsecard() {
sgs::Derive::MWuSheng * returnWusheng = 0;
if (!canUseSha())
return returnWusheng;
const CardVec& redShoupaiFound = redShoupai();
if (redShoupaiFound.empty()) {
printDebug("AIGuanyu::useWusheng: no red card");
return returnWusheng;
}
PlayerVec attackTargetVec = shaTarget();
if (attackTargetVec.empty()) {
for (CardVec::const_iterator cardIter = redShoupaiFound.begin();
cardIter != redShoupaiFound.end(); ++cardIter) {
if (canUseFangtian() && attackTargetVec.size() > 1) {
printDebug("AIGuanyu::useWusheng: using fangtian");
returnWusheng = new sgs::Derive::MWuSheng(myPlayer(),
sgs::ConstData::PHAND, (*cardIter).second);
// push in the fisrt 3 enemies;
int j = 0;
PlayerVec arrangedTargets;
for (PlayerVec::iterator iter = attackTargetVec.begin();
iter != attackTargetVec.end() && j < 3; ++iter, ++j) {
arrangedTargets.push_back(*iter);
}
// sort the targets by seat;
sgsui::PlayerSort arrangeSeat(mySeat(), sgsui::BySeat);
std::sort(arrangedTargets.begin(), arrangedTargets.end(),
arrangeSeat);
for (PlayerVec::iterator iter = arrangedTargets.begin();
iter != arrangedTargets.end(); ++iter) {
returnWusheng->addto(*iter);
}
} else {
printDebug("AIGuanyu::useWusheng: not using fangtian");
// Do not use black Sha to Renwang
// try each player in range
for (PlayerVec::reverse_iterator riter =
attackTargetVec.rbegin();
riter != attackTargetVec.rend(); ++riter) {
sgs::DataType::Player * target = *riter;
returnWusheng = new sgs::Derive::MWuSheng(myPlayer(),
sgs::ConstData::PHAND, (*cardIter).second, target);
printDebug(
"AIGuanyu::useWusheng: will attack player "
+ QString::number(target->seat())
+ ", break and stop searching for target");
break;
}
}
return returnWusheng;
}
}
return returnWusheng;
}
sgs::Derive::MSkill * AIGuanyu::useWushengInShaOrNot() {
sgs::Derive::MWuSheng * returnWusheng = 0;
const CardVec& redShoupaiFound = redShoupai();
if (redShoupaiFound.empty()) {
printDebug("AIGuanyu::useWushengInShaOrNot: no red card");
return returnWusheng;
}
for (CardVec::const_iterator cardIter = redShoupaiFound.begin();
cardIter != redShoupaiFound.end(); ++cardIter) {
returnWusheng = new sgs::Derive::MWuSheng(myPlayer(),
sgs::ConstData::PHAND, (*cardIter).second);
printDebug(sgsui::messageInterpret(returnWusheng));
printDebug("AIGuanyu::useWushengInShaOrNot: over");
return returnWusheng;
}
return returnWusheng;
}
const CardVec& AIGuanyu::redShoupai() {
redShoupaiVec.clear();
const sgs::DataType::Card * card = 0;
for (int i = 0; i < myPlayer()->handnum(); ++i) {
card = myPlayer()->hand(i);
if (card->color() == sgs::ConstData::HEART
|| card->color() == sgs::ConstData::DIAMOND) {
redShoupaiVec.push_back(cardPair(card, i));
}
}
return redShoupaiVec;
}
} /* namespace sgsai */