-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMessageCardValue.kt
790 lines (763 loc) · 33.4 KB
/
MessageCardValue.kt
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package com.fengsheng
import com.fengsheng.RobotPlayer.Companion.sortCards
import com.fengsheng.ScoreFactory.logger
import com.fengsheng.card.Card
import com.fengsheng.card.count
import com.fengsheng.card.countTrueCard
import com.fengsheng.card.filter
import com.fengsheng.phase.FightPhaseIdle
import com.fengsheng.protos.Common.*
import com.fengsheng.protos.Common.card_type.*
import com.fengsheng.protos.Common.color.*
import com.fengsheng.protos.Common.direction.*
import com.fengsheng.protos.Common.secret_task.*
import com.fengsheng.skill.*
import com.fengsheng.skill.LengXueXunLian.MustLockOne
import kotlin.random.Random
/**
* 判断是否有玩家会死或者赢
*/
fun Iterable<Player?>.anyoneWillWinOrDie(e: FightPhaseIdle) = any {
it!!.willWin(e.whoseTurn, e.inFrontOfWhom, e.messageCard)
} || e.inFrontOfWhom.willDie(e.messageCard)
/**
* 判断玩家是否会死
*/
fun Player.willDie(card: Card) = messageCards.count(Black) >= 2 && card.isBlack()
/**
* 判断玩家是否会死
*/
fun Player.willDie(colors: List<color>) = messageCards.count(Black) >= 2 && Black in colors
/**
* 判断玩家是否能赢
*
* TODO: 这里没有判断传出者
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param card 情报牌
*/
fun Player.willWin(whoseTurn: Player, inFrontOfWhom: Player, card: Card) =
calculateMessageCardValue(whoseTurn, inFrontOfWhom, card) >= 600
/**
* 判断玩家是否能赢
*
* TODO: 这里没有判断传出者
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param colors 情报牌的颜色
*/
fun Player.willWin(whoseTurn: Player, inFrontOfWhom: Player, colors: List<color>) =
calculateMessageCardValue(whoseTurn, inFrontOfWhom, colors) >= 600
// TODO CP胜利条件太复杂了,还会引发一大堆bug,先不写了
// /**
// * 判断玩家是否赢了,考虑CP小九和CP韩梅的技能,不考虑簒夺者
// */
// private fun Player.willWinInternalCp(
// whoseTurn: Player,
// inFrontOfWhom: Player,
// colors: List<color>,
// partnerTogether: Boolean = true
// ): Boolean {
// if (!alive) return false
// if (willWinInternal(whoseTurn, inFrontOfWhom, colors, partnerTogether)) return true
// val g = whoseTurn.game!!
// fun isCpXiaoJiu(p: Player?) = p!!.alive && p.skills.any { it is YiZhongRen }
// fun isCpHanMei(p: Player?) = p!!.alive && p.skills.any { it is BaiYueGuang }
// fun isHanMei(p: Player) = p.alive && p.roleName.endsWith("韩梅")
// fun isXiaoJiu(p: Player) = p.alive && p.roleName.endsWith("小九")
// val counts = CountColors(inFrontOfWhom.messageCards)
// counts += colors
// if (isCpXiaoJiu(this) && isHanMei(inFrontOfWhom) && counts.red >= 3 &&
// !inFrontOfWhom.willWinInternal(whoseTurn, inFrontOfWhom, colors, false)
// ) return true
// if (isCpHanMei(this) && isXiaoJiu(inFrontOfWhom) && counts.blue >= 3 &&
// !inFrontOfWhom.willWinInternal(whoseTurn, inFrontOfWhom, colors, false)
// ) return true
// if (this === inFrontOfWhom) {
// if (isHanMei(this) && counts.red >= 3 && g.players.any(::isCpXiaoJiu)) return true
// if (isXiaoJiu(this) && counts.blue >= 3 && g.players.any(::isCpHanMei)) return true
// }
// return false
// }
/**
* 判断玩家是否赢了,不考虑簒夺者
*/
private fun Player.willWinInternal(
whoseTurn: Player,
inFrontOfWhom: Player,
colors: List<color>,
partnerTogether: Boolean = true
): Boolean {
if (!alive) return false
if (identity != Black) {
return (if (partnerTogether) isPartnerOrSelf(inFrontOfWhom) else this === inFrontOfWhom) &&
identity in colors && inFrontOfWhom.messageCards.count(identity) >= 2
} else {
return when (secretTask) {
Killer, Pioneer, Sweeper -> {
if (game!!.players.any {
(it!!.identity != Black || it.secretTask in listOf(Collector, Mutator)) &&
it.willWinInternal(whoseTurn, inFrontOfWhom, colors)
}) {
return false
}
val counts = CountColors(inFrontOfWhom.messageCards)
counts += colors
counts.black >= 3 || return false
when (secretTask) {
Killer -> this === whoseTurn && counts.trueCard >= 2
Pioneer -> this === inFrontOfWhom && counts.trueCard >= 1
Sweeper -> counts.red <= 1 && counts.blue <= 1
else -> false
}
}
Collector ->
this === inFrontOfWhom &&
if (Red in colors) messageCards.count(Red) >= 2
else if (Blue in colors) messageCards.count(Blue) >= 2
else false
Mutator -> {
if (inFrontOfWhom.let {
(it.identity != Black || it.secretTask == Collector) &&
it.willWinInternal(whoseTurn, it, colors)
}) {
return false
}
if (Red in colors && inFrontOfWhom.messageCards.count(Red) >= 2) return true
if (Blue in colors && inFrontOfWhom.messageCards.count(Blue) >= 2) return true
false
}
Disturber ->
if (Red in colors || Blue in colors)
game!!.players.all {
it === this || it === inFrontOfWhom || !it!!.alive ||
it.messageCards.countTrueCard() >= 2
} &&
inFrontOfWhom.messageCards.countTrueCard() >= 1
else
game!!.players.all { it === this || !it!!.alive || it.messageCards.countTrueCard() >= 2 }
else -> false
}
}
}
/**
* 计算随机颜色情报牌的平均价值
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param checkThreeSame 是否禁止三张同色。为`true`时,三张同色导致加入手牌认为是10分。为`false`时,三张同色就赢了/死了。默认为`false`。
*/
fun Player.calculateMessageCardValue(whoseTurn: Player, inFrontOfWhom: Player, checkThreeSame: Boolean = false) =
game!!.deck.colorRates.withIndex().sumOf { (i, rate) ->
val colors =
if (i % 3 == i / 3) listOf(color.forNumber(i / 3))
else listOf(color.forNumber(i / 3), color.forNumber(i % 3))
calculateMessageCardValue(whoseTurn, inFrontOfWhom, colors, checkThreeSame) * rate
}
/**
* 计算情报牌的价值
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param card 情报牌
* @param checkThreeSame 是否禁止三张同色。为`true`时,三张同色导致加入手牌认为是10分。为`false`时,三张同色就赢了/死了。默认为`false`。
* @param sender 情报传出者,null表示这并不是在计算待收情报
*/
fun Player.calculateMessageCardValue(
whoseTurn: Player,
inFrontOfWhom: Player,
card: Card,
checkThreeSame: Boolean = false,
sender: Player? = null
) = calculateMessageCardValue(whoseTurn, inFrontOfWhom, card.colors, checkThreeSame, sender)
/**
* 计算移除一张情报牌的价值
*/
fun Player.calculateRemoveCardValue(whoseTurn: Player, from: Player, card: Card): Int {
val index = from.messageCards.indexOfFirst { it.id == card.id }
from.messageCards.removeAt(index)
return -calculateMessageCardValue(whoseTurn, from, card).apply {
from.messageCards.add(index, card)
}
}
/**
* 计算情报牌的价值
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param colors 情报牌的颜色
* @param checkThreeSame 是否禁止三张同色。为`true`时,三张同色导致加入手牌认为是10分。为`false`时,三张同色就赢了/死了。默认为`false`。
* @param sender 情报传出者,null表示这并不是在计算待收情报
*/
fun Player.calculateMessageCardValue(
whoseTurn: Player,
inFrontOfWhom: Player,
colors: List<color>,
checkThreeSame: Boolean = false,
sender: Player? = null
): Int {
var v1 = calculateMessageCardValue(whoseTurn, inFrontOfWhom, colors, checkThreeSame)
if (sender != null) {
// TODO 临时这样写,后续应该改成调用Player.countMessageCard来计数
class TmpCard(colors: List<color>) : Card(999, colors, Up, false) {
override val type: card_type = card_type.UNRECOGNIZED
override fun canUse(g: Game, r: Player, vararg args: Any) = false
override fun execute(g: Game, r: Player, vararg args: Any) = Unit
}
if (sender.skills.any { it is MianLiCangZhen }) { // 邵秀
inFrontOfWhom.messageCards.add(TmpCard(colors))
var valueSender = -1
var valueMe = 0
for (c in sender.cards.filter(Card::isBlack)) {
val v = sender.calculateMessageCardValue(whoseTurn, inFrontOfWhom, c.colors, checkThreeSame)
if (v > valueSender) {
valueSender = v
valueMe = calculateMessageCardValue(whoseTurn, inFrontOfWhom, c.colors, checkThreeSame)
}
}
logger.debug("这是[邵秀]传出的情报,计算[绵里藏针]额外分数为$valueMe")
v1 += valueMe
inFrontOfWhom.messageCards.removeLast()
}
if (Black !in colors && sender.skills.any { it is ChiZiZhiXin } && sender !== inFrontOfWhom) { // 青年小九
inFrontOfWhom.messageCards.add(TmpCard(colors))
var valueSender = 30
var valueMe = 0
for (c in sender.cards.filter { it.colors.any { c -> c in colors } }) {
val v = sender.calculateMessageCardValue(whoseTurn, sender, c.colors, checkThreeSame)
if (v > valueSender) {
valueSender = v
valueMe = calculateMessageCardValue(whoseTurn, sender, c.colors, checkThreeSame)
}
}
logger.debug("这是[SP小九]传出的情报,计算[赤子之心]额外分数为$valueMe")
v1 += when {
valueSender > 30 -> valueMe
isPartnerOrSelf(sender) -> 20
else -> -20
}
inFrontOfWhom.messageCards.removeLast()
}
// TODO CP韩梅还要判断一下拿牌,这里就暂时先不写了
if (Blue in colors && sender.skills.any { it is AnCangShaJi }) { // SP韩梅
inFrontOfWhom.messageCards.add(TmpCard(colors))
if (sender.cards.any { it.isPureBlack() }) {
val v = sender.calculateMessageCardValue(whoseTurn, inFrontOfWhom, listOf(Black))
var valueMe = 0
if (v > 0) valueMe = calculateMessageCardValue(whoseTurn, inFrontOfWhom, listOf(Black))
logger.debug("这是[CP韩梅]传出的情报,计算[暗藏杀机]额外分数为$valueMe")
v1 += valueMe
}
inFrontOfWhom.messageCards.removeLast()
}
if (Black in colors && inFrontOfWhom.skills.any { it is ShiSi }) { // 老汉【视死】
if (isPartnerOrSelf(inFrontOfWhom)) {
v1 += 20
} else if (inFrontOfWhom.identity != Black && identity != Black) { // 自己是神秘人就无视,老汉是神秘人也无视
v1 -= 20
}
}
if (Black in colors && inFrontOfWhom.skills.any { it is YiXin } && inFrontOfWhom.messageCards.count(Black) == 2) {
// 李宁玉【遗信】
var liNingYuValue = -1
var myValue = 0
for (handCard in inFrontOfWhom.cards) {
for (p in game!!.players) {
if (!p!!.alive || p === inFrontOfWhom) continue
val v = inFrontOfWhom.calculateMessageCardValue(whoseTurn, p, handCard)
if (v > liNingYuValue) {
liNingYuValue = v
myValue = calculateMessageCardValue(whoseTurn, p, handCard)
}
}
}
v1 += myValue
}
if (Black in colors && inFrontOfWhom.skills.any { it is RuGui } && inFrontOfWhom.messageCards.count(Black) == 2) {
// 老汉【如归】
if (whoseTurn !== inFrontOfWhom && whoseTurn.alive) {
var laoHanValue = -1
var myValue = 0
for (mCard in inFrontOfWhom.messageCards + TmpCard(colors)) {
val v = inFrontOfWhom.calculateMessageCardValue(whoseTurn, whoseTurn, mCard)
if (v > laoHanValue) {
laoHanValue = v
myValue = calculateMessageCardValue(whoseTurn, whoseTurn, mCard)
}
}
v1 += myValue
}
}
}
return v1
}
/**
* 计算情报牌的价值
*
* @param whoseTurn 当前回合玩家
* @param inFrontOfWhom 情报在谁面前
* @param colors 情报牌的颜色
* @param checkThreeSame 是否禁止三张同色。为`true`时,三张同色导致加入手牌认为是10分。为`false`时,三张同色就赢了/死了。默认为`false`。
*/
fun Player.calculateMessageCardValue(
whoseTurn: Player,
inFrontOfWhom: Player,
colors: List<color>,
checkThreeSame: Boolean = false
): Int {
val disturber = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Disturber }
if (!checkThreeSame) {
if (whoseTurn.identity == Black && whoseTurn.secretTask == Stealer) {
if (this === whoseTurn) { // 簒夺者的回合,任何人赢了,簒夺者都会赢
if (game!!.players.any { it !== disturber && it!!.willWinInternal(whoseTurn, inFrontOfWhom, colors) })
return 600
} else { // 簒夺者的回合,任何人赢了,都算作输
if (game!!.players.any { it !== disturber && it!!.willWinInternal(whoseTurn, inFrontOfWhom, colors) })
return -600
}
} else if (whoseTurn.roleFaceUp && whoseTurn.skills.any { it is BiYiShuangFei }) {
if (this === whoseTurn) { // 秦圆圆的回合,任何男性角色赢了,秦圆圆都会赢
if (game!!.players.any {
it !== disturber && (isPartnerOrSelf(it!!) || it.isMale) &&
it.willWinInternal(whoseTurn, inFrontOfWhom, colors, false)
}) return 600
if (game!!.players.any {
it !== disturber && !(isPartnerOrSelf(it!!) || it.isMale) &&
it.willWinInternal(whoseTurn, inFrontOfWhom, colors, false)
}) return -600
} else if (identity == Black) { // 秦圆圆的回合,神秘人没关系,反正没有队友
if (game!!.players.any {
it !== disturber && !isEnemy(it!!) && it.willWinInternal(whoseTurn, inFrontOfWhom, colors)
}) return 600
if (game!!.players.any {
it !== disturber && isEnemy(it!!) && it.willWinInternal(whoseTurn, inFrontOfWhom, colors)
}) return -600
} else if (inFrontOfWhom.identity in colors && inFrontOfWhom.messageCards.count(inFrontOfWhom.identity) >= 2) {
return if (inFrontOfWhom === this || isPartner(inFrontOfWhom) &&
(!inFrontOfWhom.isMale || isPartnerOrSelf(whoseTurn))
) 600 else -600
}
} else {
if (game!!.players.any {
it !== disturber && !isEnemy(it!!) && it.willWinInternal(whoseTurn, inFrontOfWhom, colors)
}) return 600
if (game!!.players.any {
it !== disturber && isEnemy(it!!) && it.willWinInternal(whoseTurn, inFrontOfWhom, colors)
}) return -600
}
}
if (disturber != null && disturber.willWinInternal(whoseTurn, inFrontOfWhom, colors))
return if (disturber === this) 300 else -300
var value = 0
if (identity == Black) {
if (secretTask == Collector && this === inFrontOfWhom) {
if (Red in colors) {
value += when (messageCards.count(Red)) {
0 -> 10
1 -> 100
else -> if (checkThreeSame) return 10 else 1000
}
}
if (Blue in colors) {
value += when (messageCards.count(Blue)) {
0 -> 10
1 -> 100
else -> if (checkThreeSame) return 10 else 1000
}
}
}
if (secretTask == Mutator && this === inFrontOfWhom) {
if (Red in colors) {
value += when (messageCards.count(Red)) {
0, 1 -> 6
else -> if (checkThreeSame) return 10 else 1000
}
}
if (Blue in colors) {
value += when (messageCards.count(Blue)) {
0, 1 -> 6
else -> if (checkThreeSame) return 10 else 1000
}
}
}
if (secretTask == Disturber && this != inFrontOfWhom) {
val count = inFrontOfWhom.messageCards.countTrueCard()
if (inFrontOfWhom.willDie(colors))
value += (2 - count) * 5
else if (count < 2 && (Red in colors || Blue in colors))
value += 5
}
if (secretTask !in listOf(Killer, Pioneer, Sweeper)) {
if (this === inFrontOfWhom && Black in colors) {
value -= when (inFrontOfWhom.messageCards.count(Black)) {
0 -> 1
1 -> 11
else -> if (checkThreeSame) return 10 else 111
}
}
} else {
if (Black in colors) {
val useless = secretTask == Sweeper && inFrontOfWhom.messageCards.countTrueCard() > 1
value += when (inFrontOfWhom.messageCards.count(Black)) {
0 -> if (useless) 0 else 1
1 -> if (useless) 0 else 6
else -> if (checkThreeSame) return 10 else if (this === inFrontOfWhom) -112 else 0
}
if (secretTask == Pioneer && this === inFrontOfWhom)
value += 11
}
}
} else {
val myColor = identity
val enemyColor = (listOf(Red, Blue) - myColor).first()
if (inFrontOfWhom.identity == myColor) { // 队友
if (myColor in colors) {
value += when (inFrontOfWhom.messageCards.count(myColor)) {
0 -> 10
1 -> 100
else -> if (checkThreeSame) return 10 else 1000
}
}
if (Black in colors) {
value -= when (inFrontOfWhom.messageCards.count(Black)) {
0 -> 1
1 -> 11
else -> if (checkThreeSame) return 10 else 111
}
}
} else if (inFrontOfWhom.identity == enemyColor) { // 敌人
if (enemyColor in colors) {
value -= when (inFrontOfWhom.messageCards.count(enemyColor)) {
0 -> 10
1 -> 100
else -> if (checkThreeSame) return 10 else 1000
}
}
if (Black in colors) {
value += when (inFrontOfWhom.messageCards.count(Black)) {
0 -> 1
1 -> 11
else -> if (checkThreeSame) return 10 else 111
}
}
}
}
return value.coerceIn(-600..600)
}
/**
* 计算应该选择哪张情报传出的结果
*
* @param card 传出的牌
* @param target 传出的目标
* @param dir 传递方向
* @param lockedPlayers 被锁定的玩家
* @param value 价值
*/
class SendMessageCardResult(
val card: Card,
val target: Player,
val dir: direction,
var lockedPlayers: List<Player>,
val value: Double
)
/**
* 计算应该选择哪张情报传出
*
* @param availableCards 可以选择的牌,默认为该玩家的所有手牌
*/
fun Player.calSendMessageCard(
whoseTurn: Player = this,
availableCards: List<Card> = cards,
isYuQinGuZong: Boolean = false,
): SendMessageCardResult {
if (availableCards.isEmpty()) {
logger.error("没有可用的情报牌,玩家手牌:${cards.joinToString()}")
throw IllegalArgumentException("没有可用的情报牌")
}
var value = Double.NEGATIVE_INFINITY
// 先随便填一个,反正后面要替换
var result = SendMessageCardResult(availableCards[0], game!!.players[0]!!, Up, emptyList(), 0.0)
fun calAveValue(card: Card, attenuation: Double, nextPlayerFunc: Player.() -> Player): Double {
var sum = 0.0
var n = 0.0
var currentPlayer = nextPlayerFunc()
var currentPercent = 1.0
val canLock = card.canLock() || skills.any { it is MustLockOne || it is QiangYingXiaLing }
if (attenuation == 0.0) { // 对于向上的情报,单独处理
val nextPlayer = currentPlayer
val nextValue = calculateMessageCardValue(whoseTurn, nextPlayer, card)
val me = currentPlayer.nextPlayerFunc()
val myValue = calculateMessageCardValue(whoseTurn, me, card)
if (canLock && nextValue >= myValue) return nextValue.toDouble()
val nextValue2 = nextPlayer.calculateMessageCardValue(whoseTurn, nextPlayer, card)
val myValue2 = nextPlayer.calculateMessageCardValue(whoseTurn, me, card)
return (if (nextValue2 >= myValue2) nextValue else myValue).toDouble()
}
while (true) {
var m = currentPercent
if (canLock) m *= m
else if (isPartnerOrSelf(currentPlayer)) m *= 1.2
sum += calculateMessageCardValue(whoseTurn, currentPlayer, card) * m
n += m
if (currentPlayer === this) break
currentPlayer = currentPlayer.nextPlayerFunc()
currentPercent *= attenuation
}
return sum / n
}
val notUp = game!!.isEarly && !isYuQinGuZong && identity != Black && !skills.any { it is LianLuo } &&
availableCards.any { card ->
!card.isPureBlack() &&
when (card.direction) {
Left -> calAveValue(card, 0.7, Player::getNextLeftAlivePlayer) >= 0
Right -> calAveValue(card, 0.7, Player::getNextRightAlivePlayer) >= 0
else -> false
}
}
for (card in availableCards.sortCards(identity, true)) {
val removedCard = if (isYuQinGuZong) deleteMessageCard(card.id) else null
if (!notUp && (card.direction == Up || skills.any { it is LianLuo })) {
val (partner, enemy) = game!!.players.filter { it !== this && it!!.alive }.partition { isPartner(it!!) }
for (target in partner.shuffled() + enemy.shuffled()) {
val tmpValue = calAveValue(card, 0.0) { if (this === target) this@calSendMessageCard else target!! }
if (tmpValue > value) {
value = tmpValue
result = SendMessageCardResult(card, target!!, Up, emptyList(), value)
}
}
} else if (card.direction == Left) {
val tmpValue = calAveValue(card, 0.7, Player::getNextLeftAlivePlayer)
if (tmpValue > value) {
value = tmpValue
result = SendMessageCardResult(card, getNextLeftAlivePlayer(), Left, emptyList(), value)
}
} else if (card.direction == Right) {
val tmpValue = calAveValue(card, 0.7, Player::getNextRightAlivePlayer)
if (tmpValue > value) {
value = tmpValue
result = SendMessageCardResult(card, getNextRightAlivePlayer(), Right, emptyList(), value)
}
}
removedCard?.let { messageCards.add(it) }
}
if (result.card.canLock() || skills.any { it is MustLockOne || it is QiangYingXiaLing }) {
val removedCard = if (isYuQinGuZong) deleteMessageCard(result.card.id) else null
var maxValue = Int.MIN_VALUE
var lockTarget: Player? = null
when (result.dir) {
Left -> {
val targets = game!!.sortedFrom(game!!.players.filter { it!!.alive }, location)
for (i in listOf(0) + ((targets.size - 1) downTo 1)) {
val target = targets[i]
val v = calculateMessageCardValue(whoseTurn, target, result.card, sender = this)
if (v > maxValue) {
maxValue = v
lockTarget = target
}
}
}
Right -> {
val targets = game!!.sortedFrom(game!!.players.filter { it!!.alive }, location)
for (target in targets) {
val v = calculateMessageCardValue(whoseTurn, target, result.card, sender = this)
if (v > maxValue) {
maxValue = v
lockTarget = target
}
}
}
else -> {
for (player in listOf(this, result.target)) {
val v = calculateMessageCardValue(whoseTurn, player, result.card, sender = this)
if (v > maxValue) {
maxValue = v
lockTarget = player
}
}
}
}
lockTarget?.let { if (result.dir == Up && isPartner(result.target) || it !== this) result.lockedPlayers = listOf(it) }
removedCard?.let { messageCards.add(it) }
}
logger.debug("计算结果:${result.card}(cardId:${result.card.id})传递给${result.target},方向是${result.dir},分数为${result.value}")
return result
}
/**
* 是否要救人
*/
fun Player.wantToSave(whoseTurn: Player, whoDie: Player): Boolean {
// 如果死亡的是老汉且有情报
if (whoDie.skills.any { it is RuGui } && whoDie.messageCards.isNotEmpty()) {
// 如果老汉和当前回合角色是同一身份+老汉情报区有该颜色情报+当前回合角色听牌
if (whoDie !== whoseTurn && whoDie.identity == whoseTurn.identity &&
!whoDie.messageCards.filter(whoDie.identity).isEmpty() &&
whoseTurn.messageCards.count(whoseTurn.identity) == 2) {
// 如果自己也是同一阵营,则不救
if (isPartnerOrSelf(whoDie)) {
return false
}
// 如果自己不是同一阵营,则救(防止发动技能后敌方胜利)
return true
}
}
// 如果死亡的是李宁玉且有手牌
if (whoDie.skills.any { it is YiXin } && whoDie.cards.isNotEmpty()) {
// 如果李宁玉的队友听牌
if (whoDie.game!!.players.any {
it!!.alive && it !== whoDie && it.identity == whoDie.identity && it.messageCards.count(whoDie.identity) == 2
}) {
// 如果自己也是同一阵营,则不救
if (isPartnerOrSelf(whoDie)) {
val stealer = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Stealer }
// 特殊情况:当前回合是篡夺者,则救
return whoseTurn === stealer
}
// 如果自己不是同一阵营,则救(防止发动技能后敌方胜利)
return true
}
}
var save = isPartnerOrSelf(whoDie)
var notSave = false
val killer = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Killer }
val pioneer = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Pioneer }
val sweeper = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Sweeper }
val stealer = game!!.players.find { it!!.alive && it.identity == Black && it.secretTask == Stealer }
if (killer === whoseTurn && whoDie.messageCards.countTrueCard() >= 2) {
if (killer === this) notSave = true
save = save || killer !== this
}
if (pioneer === whoDie && whoDie.messageCards.countTrueCard() >= 1) {
if (pioneer === this && whoseTurn !== stealer) notSave = true
if (stealer === this && whoseTurn === this) notSave = true
save = save || pioneer !== this
}
if (sweeper != null && whoDie.messageCards.run { count(Red) <= 1 && count(Blue) <= 1 }) {
if (sweeper === this && whoseTurn !== stealer) notSave = true
if (stealer === this && whoseTurn === this) notSave = true
save = save || sweeper !== this
}
return !notSave && save
}
class FightPhaseResult(
val cardType: card_type,
val card: Card,
val wuDaoTarget: Player?,
val value: Int,
val deltaValue: Int,
val convertCardSkill: ConvertCardSkill?
)
fun Player.calFightPhase(e: FightPhaseIdle, whoUse: Player = this, availableCards: List<Card> = this.cards): FightPhaseResult? {
val order = mutableListOf(Wu_Dao, Jie_Huo, Diao_Bao)
if (skills.any { it is YouDao || it is JiangJiJiuJi }) {
if (roleFaceUp) {
order[order.indexOf(Wu_Dao)] = order[0]
order[0] = Wu_Dao
} else {
order[order.indexOf(Wu_Dao)] = order[2]
order[2] = Wu_Dao
}
} else if (skills.any { it is ShunShiErWei || it is ShenCang }) {
if (roleFaceUp) {
order[order.indexOf(Jie_Huo)] = order[0]
order[0] = Jie_Huo
} else {
order[order.indexOf(Jie_Huo)] = order[2]
order[2] = Jie_Huo
}
} else if (skills.any { it is HuanRi }) {
if (roleFaceUp) {
order[order.indexOf(Diao_Bao)] = order[0]
order[0] = Diao_Bao
} else {
order[order.indexOf(Diao_Bao)] = order[2]
order[2] = Diao_Bao
}
}
val oldValue = calculateMessageCardValue(e.whoseTurn, e.inFrontOfWhom, e.messageCard, sender = e.sender)
var value = oldValue
var result: FightPhaseResult? = null
val cards = availableCards.sortCards(identity)
for (cardType in order) {
!whoUse.cannotPlayCard(cardType) || continue
loop@ for (card in cards) {
val (ok, convertCardSkill) = whoUse.canUseCardTypes(cardType, card, whoUse !== this)
ok || continue
when (cardType) {
Jie_Huo -> {
val newValue = calculateMessageCardValue(e.whoseTurn, whoUse, e.messageCard, sender = e.sender)
if (newValue > value) {
result = FightPhaseResult(
cardType,
card,
null,
newValue,
newValue - oldValue,
convertCardSkill
)
value = newValue
}
break@loop
}
Diao_Bao -> {
val newValue = calculateMessageCardValue(e.whoseTurn, e.inFrontOfWhom, card, sender = e.sender)
if (newValue > value) {
result = FightPhaseResult(
cardType,
card,
null,
newValue,
newValue - oldValue,
convertCardSkill
)
value = newValue
}
}
else -> { // Wu_Dao
val calLeft = {
val left = e.inFrontOfWhom.getNextLeftAlivePlayer()
val newValueLeft =
calculateMessageCardValue(e.whoseTurn, left, e.messageCard, sender = e.sender)
if (newValueLeft > value) {
result = FightPhaseResult(
cardType,
card,
left,
newValueLeft,
newValueLeft - oldValue,
convertCardSkill
)
value = newValueLeft
}
}
val calRight = {
val right = e.inFrontOfWhom.getNextRightAlivePlayer()
val newValueRight =
calculateMessageCardValue(e.whoseTurn, right, e.messageCard, sender = e.sender)
if (newValueRight > value) {
result = FightPhaseResult(
cardType,
card,
right,
newValueRight,
newValueRight - oldValue,
convertCardSkill
)
value = newValueRight
}
}
if (Random.nextBoolean()) {
calLeft()
calRight()
} else {
calRight()
calLeft()
}
break@loop
}
}
}
}
return result
}