[[練習問題]] #!/usr/bin/python # -*- coding: utf-8 -*- import sys from random import shuffle, choice from itertools import count from collections import defaultdict try: input = raw_input except NameError: pass with(open(sys.argv[1], 'r')) as F: dic = F.read().strip().split('\n') dic = list(set(dic)) dic = list(set(F.read().strip().split('\n'))) first_words = [ e[0] for e in dic ] used = defaultdict(int) players = [ 'あなた', 'わたし' ] shuffle(players) startswith = None for i in count(1): cur = i % len(players) if players[cur] == 'あなた': if players[i % len(players)] == 'あなた': if startswith == None: startswith = choice(first_words) sys.stdout.write('{0:3}: あなたの番です。 "{1}": '.format(i, startswith)) s = raw_input() s = input() while not (s.startswith(startswith) and s in dic): if not s.startswith(startswith): print('"{0}"で始まっていません。 '.format(startswith)) else: print('辞書にありせん。') print('辞書にありません。') sys.stdout.write('{0:3}: あなたの番です。 "{1}": '.format(i, startswith)) s = raw_input() s = input() if s in dic: if s in used: print('"{0}"は{1}回目に{2}が使用しています。わたしの勝ちです。'.format(s, used[s], players[used[s]%len(players)])) break startswith = s[-1] used[s] = i else: msg = '{0:3}: わたしの番です。 '.format(i) if startswith == None: word = choice(dic) startswith = word[-1] used[word] = i print(msg + '{0}'.format(word)) else: words = [ e for e in dic if e.startswith(startswith) and e not in used ] if len(words) == 0: print('まいりました! あなたの勝ちです。') break else: word = choice(words) startswith = word[-1] used[word] = i print(msg + '{0}'.format(word)) print('今回のしりとりでは{0}個の単語を使用しました。'.format(len(used)))