-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.py
30 lines (26 loc) · 852 Bytes
/
book.py
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
#!/usr/bin/env pypy
# -*- coding: utf-8 -*-
import arrows
from random import randint
class Book():
def __init__(self,path=""):
self.book={}
if path!="": self.import_book(path)
def import_book(self,path):
with open(path) as fp:
line=fp.readline()
while line:
line=line.strip()
line=line.split(" ")
move=line[-1]
line=line[:-1]
line=' '.join(line)
book_move=self.book.get(line)
if book_move: self.book[line].append(move)
else: self.book[line]=[move]
line=fp.readline()
fp.close()
def get_move(self,line):
book_move=self.book.get(line)
if book_move: return book_move[randint(0,len(book_move)-1)]
return ""