Abbiamo le migliori informazioni che abbiamo trovato online. Speriamo che lo troviate utile e se volete dirci qualcosa che possa aiutarci a migliorare, fatelo liberamente.
Soluzione:
Il codice di esempio non ha funzionato per me, che uso Python 3, quindi ho cambiato il parametro main.py
per farlo funzionare.
Ecco la mia versione corretta per Python 3, non ho mai programmato in Python prima d'ora quindi forse è un codice orribile ma funziona 🙂
run.sh
:
#!/bin/bash
./randy.py < from_server > to_server
randy.py
:
#!/usr/bin/env python3
import random
with open('players') as f:
p = f.read().split() + ['no one']
with open('from_server') as f:
fs = f.read().split()
msg = ""
day = True
try:
line = fs[0]
if line.endswith(('?', 'victim.')):
day = False
if not day:
msg = (random.choice(p))
else:
if random.random() > 0.5:
if random.random() > 0.5:
msg = ('vote {}'.format(random.choice(p)))
else:
id = random.randint(0, 17)
msg = ('say {}{}'.format(id, (' ' + random.choice(p)) if id > 4 else ''))
with open('to_server', 'w') as f:
f.write(msg)
print(msg)
except: pass
Alcune cose che ho imparato mentre lo facevo funzionare (e che non erano chiare per me nella descrizione)
print
non fa nulla con il gioco, è come unconsole.log
in jsinput()
blocca l'esecuzione del programma, può essere utile per il debug passo dopo passo.from_server
eto_server
viene cancellato a ogni giro.- È impossibile fermare lo script con
Ctrl+C
e questo è fastidioso.
Il Logico
#!/usr/bin/env python3
import sys
import os
import re
import random
from types import SimpleNamespace
def chooseSet(set):
return random.choice(list(set))
sys.stdin = open("from_server")
sys.stdout = open("to_server","w")
def saveData(data):
with open("gameData.txt", "w") as datafile:
datafile.write(repr(data.__dict__))
MY_NAME = os.path.basename(os.getcwd())
opener = input()
DATABASES = ("targets","herd","mafiosos","guilty","innocent","unlikely", "requests",
"selfvotes","players","used_roles")
ALLOW_SELF = ("players", "mafiosos")
LIESPERROLE = {"cop": ("I am the cop",
"I investigated this player and found that they were mafia-aligned",
"I investigated this player and found that they were village-aligned"),
"doctor": ("I am the doctor",
"I tried to save this player",
"I successfully saved this player"
)
}
#1: At the beginning of the game, parse beginning of day 0
if opener == "Rise and shine! Today is day 0.":
#Next two lines are completely predetermined and hold no data
assert input() == "No voting will occur today."
assert input() == "Be warned: Tonight the mafia will strike."
data = SimpleNamespace(cop=False, doctor=False, queued=[],askers={})
for datum in DATABASES:
setattr(data, datum, set())
try:
nextline = input()
if nextline == "You are a member of the mafia.":
data.mafiosos.add(MY_NAME)
assert input() == "Your allies are:"
while True:
data.mafiosos.add(input())
elif nextline == "You are the doctor":
data.doctor = True
data.used_roles.add("doctor")
elif nextline == "You are the cop":
data.cop = True
data.used_roles.add("cop")
except EOFError:
#villager, or ran out of mafiosos to add
pass
with open("players") as playersfile:
data.players = set(playersfile.read().strip().splitlines())
saveData(data)
exit()
with open("gameData.txt") as datafile:
data = SimpleNamespace(**eval(datafile.read().strip()))
#2: Beginning of day nonzero
if opener.startswith("Dawn of day"):
data.requests.clear()
data.selfvotes.clear()
data.askers.clear()
data.voted = False
try:
while True:
nextline = input()
victim = re.match("Last night, (.*) was killed. They were (?:a|the) (.*).", nextline)
if victim:
victim, role = victim.groups()
#remove dead people from lists
for datum in DATABASES:
getattr(data, datum).discard(victim)
if role == "cop" or role == "doctor":
data.used_roles.add(role)
continue
investigated = re.match("Investigations showed that (.*) is (.*)-aligned.", nextline)
if investigated:
assert data.cop
who = investigated.group(1)
if investigated.group(2) == "mafia":
data.guilty.add(who)
data.unlikely.discard(who)
else:
data.targets.discard(who)
data.herd.discard(who)
data.innocent.add(who)
data.unlikely.add(who)
continue
except EOFError:
pass
#3: We're being told some messages / news
elif " says " in opener or " voted " in opener:
message = opener
acted = question = False
try:
while True:
if " voted " in message:
message = ""
speaker, subject = re.match("(.*) has voted to lynch (.*)", message).groups()
target = None
else:
speaker, target, message, subject =
re.match("(.*) says "(?:(.*), )?([^:?]+)(?:[:?]s*(.*))?"",
message).groups()
if speaker == MY_NAME:
continue
BAD_MESSAGES = ("", "I think this player is mafia",
"I investigated this player and found that they were mafia-aligned",
"I think this player is suspicious")
GOOD_MESSAGES = ("I think this player is the cop",
"I think this player is the doctor",
"I think this player is a normal villager",
"I trust this player",
"I investigated this player and found that they were village-aligned")
OUTS = "I am the cop", "I am the doctor"
LIES = ()
for role in data.used_roles:
LIES += LIESPERROLE[role]
if message == "Yes" or message == "No":
if question and not target:
target = chooseSet(data.askers)
if target in data.askers:
BAD_MESSAGES += "Yes",
GOOD_MESSAGES += "No",
subject = data.askers[target]
if message in LIES and speaker not in data.mafiosos and speaker not in data.innocent:
# What you just said is false, and I know it!
data.unlikely.discard(speaker)
data.targets.add(speaker)
if subject and subject not in (data.unlikely.union(data.mafiosos)):
data.targets.add(subject)
elif message in BAD_MESSAGES:
if speaker in data.guilty:
#mafiosos rarely turn on eachother
data.unlikely.add(subject)
data.targets.discard(subject)
elif speaker in data.unlikely:
#believe the herd, especially people who we trust
data.herd.add(subject)
elif subject in data.unlikely:
#how dare you speak against players likely to be village-aligned!
data.targets.add(speaker)
elif subject == MY_NAME or subject in data.mafiosos:
#DON'T ATTACK ME (or my fellow mafiosos)
data.targets.add(speaker)
else:
#believe the herd
data.herd.add(subject)
if not acted and message == "":
if subject == MY_NAME:
data.selfvotes.add(speaker)
if len(data.selfvotes) >= (len(data.players)-len(data.mafiosos))/3:
if data.cop:
print("say 2")
#give a data point to prove it
if random.random() > .5 and data.guilty:
data.queued.append("say 14 %s" % chooseSet(data.guilty))
elif data.innocent:
data.queued.append("say 15 %s" % chooseSet(data.innocent))
else:
print("say 4") #Don't out myself if I'm the doctor
# and just lie if I'm a mafioso
acted = True
else:
data.selfvotes.discard(speaker)
elif message in OUTS and data.mafiosos and speaker not in data.unlikely:
data.targets.add(speaker) #Kill the fools who boast!
elif message in GOOD_MESSAGES:
chance = random.random() < .1 - (speaker in data.targets) / 20
if speaker in data.guilty: #Mafia liars
if subject not in data.unlikely:
data.targets.add(subject)
elif subject == MY_NAME and chance:
if speaker in data.targets:data.targets.remove(speaker)
data.unlikely.add(speaker)
elif speaker in data.unlikely or chance:
data.unlikely.add(subject)
elif message == "Do you think this player is mafia":
if subject == MY_NAME:
data.targets.append(speaker)
if target == MY_NAME or not target:
if speaker in data.guilty:
data.queued.append("say 14 %s %s" % (subject, speaker))
elif speaker in data.innocent:
data.queued.append("say 15 %s %s" % (subject, speaker))
elif subject in data.targets or subject in data.herd:
data.queued.append("say 1 %s" % (speaker))
elif subject in data.unlikely:
data.queued.append("say 0 %s" % (speaker))
if data.cop:
data.requests.add(subject)
data.askers[speaker] = subject
question = True
elif target == MY_NAME and message == "Will you please use your power on this player tonight":
data.requests.add(subject)
message = input()
except EOFError:
pass
for datum in DATABASES:
if datum in ALLOW_SELF: continue
getattr(data, datum).discard(MY_NAME)
chance = random.random()
if data.queued:
print(data.queued.pop())
elif chance < .1:
target = chooseSet(data.targets or data.players)
if target != MY_NAME:
print("say 10 %s" % target)
data.askers[MY_NAME] = target
elif chance < .3 and data.targets:
print("say 6 %s" % chooseSet(data.guilty or data.targets))
elif chance < .5 and data.unlikely:
print("say 5 %s" % chooseSet(data.innocent or data.unlikely))
elif chance < .6 and not data.voted:
target = chooseSet(data.guilty or data.targets or data.herd or data.players)
if target not in data.mafiosos and target != MY_NAME:
print("vote %s" % target)
data.voted = True
elif chance < .8:
#do nothing
pass
elif chance < .9:
#Confuse everybody
print("say 1")
data.queued.append("say 0")
######################
#4: End of day
elif "has killed" in opener:
victim = re.match("The town has killed (.*)!", opener)
if not victim:
exit()
victim = victim.group(1)
#remove dead people from lists
for datum in DATABASES:
getattr(data, datum).discard(victim)
role = input()
role = re.match("They were (?:a|the) (.*)", role).group(1)
if role == "cop" or role == "doctor":
data.used_roles.add(role)
#Misc: purge people from lists if too large
for list in data.unlikely, data.targets, data.herd:
while len(list) > len(data.players)/3:
list.pop()
for player in data.innocent:
data.unlikely.add(player)
elif opener == "The town opted to lynch no one today.":
#Do nothing
pass
#5: Night
elif "night" in opener:
if not data.mafiosos and data.requests and random.random() > .5:
print(chooseSet(data.requests))
if data.doctor:
print(chooseSet(data.unlikely or data.players))
else:
while True:
try:
target = (data.targets or data.herd).pop()
except KeyError:
target = chooseSet(data.players)
if target in data.mafiosos or target == MY_NAME:
continue
print(target)
break
else:
raise ValueError("Unknown message")
saveData(data)
Un lungo mucchio di codice python che non spiegherò (anche se non è un gioco di golf), se non che tiene elenchi di "amici" e "nemici" che vengono originariamente popolati in base al caso e/o alle indagini dei poliziotti. Attenzione: non mentire in presenza del logico.
Survivalist (v 1.0)
Sinossi
Il Survivalista sopravvive brutalmente al gioco rimproverando chiunque osi accusarlo, indipendentemente dal fatto che sia mafioso o meno.
Logica
Se si sopravvive fino alla fine del gioco, si vince a prescindere. Pertanto, si sopravvive a tutti i costi.
Retroscena
Le truppe marciavano attraverso la foresta buia e umida.
"Tenente, dove stiamo marciando?". A quanto pare la giovane recluta non si era ancora indurita alle atrocità, pensò il comandante. Oh, bene. Rispose con un brusco "per distruggere il nemico".
Al villaggio, il comandante nemico stava bevendo e ridendo con gli altri ufficiali al club quando un esploratore si precipitò con le notizie. "C'è una colonna, lunga diverse centinaia di metri, che sta marciando attraverso la foresta di Yulin per noi! Raduna le truppe!".
Il comandante nemico, evidentemente inebetito, disse inaspettatamente: "Non ho avuto notizie da altri esploratori". L'esploratore (poi Sopravvissuto) pensò, allora dovrò radunare le truppe io stesso.. Dopo aver raccontato la storia ai compagni scout, questi tornarono insieme, dicendo tutti di aver visto le truppe nemiche. Il comandante non ci credeva ancora, dicendo: "Sono ordinando di interrompere l'esplorazione. Ci sono no truppe nemiche".
Gli esploratori decisero di prendere le armi per salvare la comunità. Riuscirono a raggiungere le loro posizioni proprio quando il nemico arrivò in forze nel villaggio. "CARICA!" urlò il comandante dell'imboscata. "BRUCIATE LE CASE! BRUCIATE LE CASE! UCCIDETE TUTTI, COMPRESI LE DONNE E I BAMBINI!"
Gli esploratori salvarono l'intero esercito. Si aspettavano promozioni, premi e medaglie. Invece, ottennero una corte marziale truccata per ammutinamento, una condanna, 10 anni di prigione, il congedo con disonore dall'esercito e l'esilio.
C'è un anziano nel consiglio comunale di Salem, Massachusetts. La leggenda vuole che sia stato lui a fondare la città. Quando lo incontrate nel suo cottage isolato nella foresta, non lasciate che il luccichio dei suoi occhi vi faccia pensare che sia pacifico. Se lo accusate, vi rovinerà di fronte alla città.
Il Veterano rideva nell'oscurità. Paura del buio, assolutamente no. Paura dei mostri sotto il letto? L'uomo con la mano sul grilletto di una pistola rise nervosamente. Non aveva paura di nulla, si era detto. Certo, era un eroe di guerra del passato, ma era talmente abituato alle imboscate e alle situazioni di pericolo di vita che lo rendevano semplicemente nevrotico. Il suo dito sul grilletto si contraeva di fronte a semplici ombre; il suo battito cardiaco accelerava a ogni minimo rumore. Sì, era così spaventato dalla morte. Come poteva non averne, vedendo così tante persone morire in modi orribili? Tutto ciò che sapeva, essendo stato rapito e sfuggito miracolosamente ai suoi nemici, era che non c'era pietà.
Veterano
Codice (sono un principiante in python, non sono sicuro che il codice sia buono)
#!/bin/python2
import random
with open('players') as f:
p = f.read().split() + ['no one']
day = True
target = "survivalist"
role = "villager"
try:
line = raw_input()
if "You are the cop" in line:
role = "cop"
else if "You are the doctor" in line:
role = "doctor"
else if "You are a member of the mafia" in line:
role = "mafia"
if line.endswith(('?', 'victim.')):
day = False
if not day:
if target == "survivalist":
print random.choice(p)
else if role == mafia || role == sheriff:
print target
else if role == doctor:
print random.choice(p)
else:
if "survivalist" in line && ("I think this player is suspicious:" in line ||
"I think this player is mafia:" in line ||
"I investigated this player and found that they were mafia-aligned:")):
print 'say 0'
if role == "villager" || role == "mafia":
print 'say 4'
else if role == "cop":
print 'say 2'
else if role == "doctor"
print 'say 3'
target = line.split(" ")[0]
print 'vote ' + target
else if target != "survivalist":
print 'say 6 ' + target
print 'vote ' + target
else:
pass
except: pass
Ricorda che puoi permetterti di rivedere la tua esperienza se ti è stata utile.