# Euchre机器人（纸牌游戏）

10

### 输入：

``````Ordering Trump:
js,ah,qc,ts,jc  // the cards in your hand
2               // number of points your team has
0               // number of tricks your team has taken
ordering        // the phase of the game
th              // the turned up card
p,p             // each previous player’s decision

Naming Trump:
js,ah,qc,ts,jc  // the cards in your hand
2               // number of points your team has
0               // number of tricks your team has taken
naming          // the phase of the game
p               // each previous player’s decision

js,ah,qc,ts,jc  // the cards in your hand
2               // number of points your team has
0               // number of tricks your team has taken
discard         // the phase of the game
th              // the card you will pick up

Going alone:
js,ah,qc,ts,jc  // the cards in your hand
2               // number of points your team has
0               // number of tricks your team has taken
alone           // the phase of the game
h               // the trump suit
n,n             // each previous player’s decision

js,ah,qc,ts,jc  // the cards in your hand
2               // number of points your team has
0               // number of tricks your team has taken
turn            // the phase of the game
h               // the trump suit
td,8h,p         // each previous player’s card

Trick data:
// the cards in your hand (none, since this happens at the end of a trick)
2               // number of points your team has
1               // number of tricks your team has taken
trick           // the phase of the game
0               // the index of the following list that is your card
js,tc,4d,js     // the cards played during the trick in the order they were played
``````

### 输出：

``````Ordering Trump:
p   //for pass
OR
o   //for order up

Naming Trump:
p           //for pass
OR ANY OF
c,s,h,d     //the suit you want to name

Going alone:
n   // no
OR
y   // yes

js  //the card you want to play
``````

### 笔记：

``````#!/usr/bin/python2.7
import sys

hand = data[0].strip().split(',')   # Hand as a list of strings
points = int(data[1])       # Number of points
tricks = int(data[2])       # Number of tricks

out = ''

if data[3] == 'ordering':
card = data[4]              # The upturn card
prev = data[5].strip().split(',')   # The previous player's decisions as a list
# Ordering logic
out =       # 'o' or 'p'
elif data[3] == 'naming':
prev = data[4].strip().split(',')   # The previous player's decisions as a list
# Naming logic
out =       # 'p', 'h', 's', 'c', or 'd'
card = data[4]              # The card you'll take
out =       # The card you want to discard
elif data[3] == 'alone':
trump = data[4]             # The trump suit
prev = data[5].strip().split(',')   # The previous player's decisions as a list
# Alone logic
out =       # 'y' for yes, 'n' for no
elif data[3] == 'turn':
trump = data[4]             # The trump suit
prev = data[5].strip().split(',')
# Turn logic
out =       # The card you want to play
elif data[3] == 'trick':
trump = data[5]
cards = data[6].strip().split(',')
my_card = cards[int(data[4])]
# Data logic

print(out)
``````
1. 总共有4个回应。如果某人一个人去，那么对方的回应将变成“ p”。

2. 我试图减少冗余输入的数量，因此要特别清楚：

2a。您相对于发牌人/领导者的位置以及您的伴侣所玩的纸牌都可以由先前输出的数量来确定。您和您的伴侣之间只有1个玩家。举例来说，如果您在回合的最后一行获得“ td，8h，p”，您可以看到您的伴侣玩了8h，而另一支球队有一个单独的玩家。

3. 如果您感到好奇，可以以传统方式完成交易（分两轮，每轮交替发送2张和3张卡），但这与您的机器人无关，所以...

4. 如果第二名选手决定在王牌阶段进行加码，则该阶段将继续进行，但他们的输出将几乎被忽略。换句话说，无论其他什么输出，谁先预定谁就是Namers团队。

5. 以下是各个游戏阶段的默认设置。如果您没有为该回合输出有效的答复，那么您的答复将更改为以下内容。

订购特朗普：p

命名特朗普：p

丢弃：（您手中的第一张卡片）

独自行动：n

轮到您了：（您手中的第一张合法卡）

6. 这是用于测试目的的控制器代码。

6a。请注意，您可以传入2个或4个漫游器名称，如果给它4个漫游器，则它们会随机配对，而与2个漫游器则是它们自己的副本配对。

6b。您需要与控制器代码位于同一目录中的“ bots”目录，并且您的bot代码必须位于bots目录中。

7. 对于那些希望他们的机器人记住曾玩过哪些牌的人，在“技巧”阶段会给您机会，告诉您的机器人玩了哪些牌。您可以写入bots目录中的文件，只要该文件不超过1kb。

### 计分板：

``````Old Stager:  2
Marius:      1
Random 8020: 0
``````

2

3

1

plannapus 2015年

1
@Sleafar很好，如果有一种方法可以知道当前回合中播放的内容，则该机器人可以将其写入文件中以进行跟踪。
plannapus 2015年

1
@NotthatCharles我已经更新了规则，以明确允许写入文件
Beanstalk

2

# 马吕斯

``````#!/usr/bin/Rscript
options(warn=-1)
infile = file("stdin")
open(infile)
hand = strsplit(input[1],",")[[1]]
phase = input[4]
other_o = c("a","k","q","j","t","9")
alone = "n"
ord = "p"
trumpify = function(color){
tr_suit = switch(color,
"c" = c("c","s",rep("c",5)),
"s" = c("s","c",rep("s",5)),
"h" = c("h","d",rep("h",5)),
"d" = c("d","h",rep("d",5)))
paste(c("j","j","a","k","q","t","9"),tr_suit,sep="")
}

if(phase%in%c("ordering","alone")){
flip = input[5]
if(phase=="ordering") trump = trumpify(substr(flip,2,2))
if(phase=="alone") trump = trumpify(flip)
hand_value = sum((7:1)[trump%in%c(hand,flip)])
if(hand_value>13) ord = "o"
if(hand_value>18) alone = "y"
if(phase=="alone") cat(alone)
if(phase=="ordering") cat(ord)
}

if(phase=="naming"){
name = "p"
colors = unique(substr(hand,2,2))
col_values = sapply(colors,function(x)sum((7:1)[trumpify(x)%in%hand]))
if(any(col_values>13)){name = colors[which.max(col_values)]}
cat(name)
}

flip = input[5]
new_hand = c(hand,flip)
trump = trumpify(substr(flip,2,2))
}else{d = tail(trump[trump%in%new_hand],1)}
cat(d)
}

if(phase=="turn"){
trump = trumpify(input[5])
fold = strsplit(gsub("[[:punct:]]","",input[6]),",")[[1]]
if(length(fold)&!any(is.na(fold))){
fold_c = substr(fold[1],2,2)
f_suit = if(fold_c!=input[5]){paste(other_o,fold_c,sep="")}else{trump}
l = length(f_suit)
current = (l:1)[f_suit%in%fold]
if(any(hand%in%f_suit)){
playable = hand[hand%in%f_suit]
val = sapply(playable,function(x)(l:1)[f_suit==x])
if(all(max(val)>current)){
play = playable[which.max(val)]
}else{play = playable[which.min(val)]}
}else if(any(hand%in%trump)){
playable = hand[hand%in%trump]
val = sapply(playable,function(x)(7:1)[trump==x])
if(!any(fold%in%trump)){
play = playable[which.min(val)]
}else{
trumped = fold[fold%in%trump]
val_o = max((7:1)[trump%in%trumped])
play = ifelse(any(val>val_o), playable[which.min(val[val>val_o])], playable[which.min(val)])
}
}else{
val = sapply(substr(hand,1,1),function(x)(6:1)[other_o==x])
play = hand[which.min(val)]
}
}else{
col = c("c","s","h","d")
non_tr = col[col!=input[5]]
aces = paste("a",non_tr,sep="")
if(any(hand%in%aces)){
play = hand[hand%in%aces][1]
}else if(any(hand%in%trump)){
playable = hand[hand%in%trump]
val = sapply(playable,function(x)(7:1)[trump==x])
play = playable[which.max(val)]
}else{
val = sapply(substr(hand,1,1),function(x)(6:1)[other_o==x])
play = hand[which.max(val)]
}
}
cat(play)
}``````

1

# 老舞台

• 直观地为每张卡分配分数
• 如果手部得分足够好，请选择王牌
• 如果一个人的手非常好
• 首先玩的时候选择最好的卡
• 如果对手获胜，则选择比对手更好的牌
• 如果合作伙伴获胜或无法获胜，则选择最差卡

``````#!/usr/bin/python2.7
from __future__ import print_function
import sys, re, math

base = 1.2
playThreshold = 27.0
aloneThreshold = 36.0
sameColor = { 'd' : 'h', 'h' : 'd', 's' : 'c', 'c' : 's' , '' : '', 'n' : 'n' }
cardValue = { 'p' : 0, '9' : 1, 't' : 2, 'j' : 3, 'q' : 4, 'k' : 5, 'a' : 6 }

class Card(object):
def __init__(self, name, trump):
self.name = name
self.value = cardValue[name[0:1]]
self.suit = name[1:2]
self.trump = False
self.score = self.value
if self.suit == trump:
self.trump = True
self.score += 6
if self.value == 3:
if self.suit == trump:
self.score = 14
if self.suit == sameColor[trump]:
self.trump = True
self.score = 13

class Cards(object):
def __init__(self, cards, trump):
self.list = []
self.score = 0.0
if cards:
for c in cards.split(','):
self.append(Card(c, trump))
def append(self, card):
self.list.append(card)
self.score += math.pow(base, card.score)
self.score = 0.0
for card in self.list:
self.score += math.pow(base, card.score)
def best(self):
card = self.list[0]
for i in self.list[1:]:
if i.score > card.score:
card = i
return card
def worst(self):
card = self.list[0]
for i in self.list[1:]:
if i.score < card.score:
card = i
return card
def better(self, ref):
card = None
for i in self.list:
if i.score > ref.score and (card is None or i.score < card.score):
card = i
return card

def ordering(hand, card, decisions):
if len(decisions) == 3:
hand.append(card)
return 'o' if hand.score > playThreshold else 'p'

def naming(hand):
result = 'p'
score = playThreshold
for trump in ['d', 'h', 's', 'c']:
if hand.score > score:
result = trump
score = hand.score
return result

def turn(hand, decisions):
bestIndex = -1
for i, d in enumerate(decisions.list):
if d.suit:
bestIndex = i
break
if bestIndex == -1:
return hand.best()
else:
suit = decisions.list[bestIndex].suit
for i in range(2, len(decisions.list)):
if (decisions.list[i].suit == suit or decisions.list[i].trump) and decisions.list[i].score > decisions.list[bestIndex].score:
bestIndex = i
matching = Cards('', '')
for card in hand.list:
if card.suit == suit:
matching.append(card)
if not matching.list:
if bestIndex == len(decisions.list) - 2:
return hand.worst()
for card in hand.list:
if card.trump:
matching.append(card)
if not matching.list:
return hand.worst()
if bestIndex == len(decisions.list) - 2:
return matching.worst()
card = matching.better(decisions.list[bestIndex])
if card:
return card
return matching.worst()

output = ''
input = re.split('\n', re.sub(r'[^a-z0-9,\n]+', '', sys.stdin.read()))

if input[3] == 'ordering':
output = ordering(Cards(input[0], input[4][1:2]), Card(input[4], input[4][1:2]), input[5].split(','))
elif input[3] == 'naming':
output = naming(Cards(input[0], 'n'))
output = Cards(input[0], input[4][1:2]).worst().name
elif input[3] == 'alone':
output = 'y' if Cards(input[0], input[4]).score > aloneThreshold else 'n'
elif input[3] == 'turn':
output = turn(Cards(input[0], input[4]), Cards(input[5], input[4])).name

print(output)
``````

0

# 随机8020

``````#!/usr/bin/python2.7
from __future__ import print_function
import sys, re, random

output = ''
input = re.split('\n', re.sub(r'[^a-z0-9,\n]+', '', sys.stdin.read()))
hand = input[0].split(',')

if input[3] == 'ordering':
output = random.choice(['p', 'p', 'p', 'p', 'o'])
elif input[3] == 'naming':
output = random.choice(['p', 'p', 'p', 'p', random.choice(hand)[1:2]])
output = random.choice(hand)
elif input[3] == 'alone':
output = random.choice(['n', 'n', 'n', 'n', 'y'])
elif input[3] == 'turn':
output =  random.choice(hand)
if input[5]:
suited = filter(lambda x: input[5][1:2] in x, hand)
if suited:
output = random.choice(suited)

print(output)
#print(input, " --> ", output, file=sys.stderr)
``````