最中限オンライン自動対戦ボット

ついにできた!!なんか嬉しい。

# -*- encoding: utf-8 -*-
import mechanize
import re
from saichugenlib import *

# 履歴の読み取り用
def str2num(str):
    a = ['c', 'd', 'h', 's']
    return (int(str[1:]) - 1) * 4 + a.index(str[0])

# ブラウザ設定
br = mechanize.Browser()
br.set_handle_robots(False)

# トップページ
res = br.open('http://saichugen-online.appspot.com/')
# ログイン
link = br.find_link(text='ログイン')
res = br.follow_link(link)
br.select_form(nr = 0)
br['Email'] = 'ほげほげ@gmail.com'
br['Passwd'] = '********'
br.submit()
# 再びトップページ
res = br.open('http://saichugen-online.appspot.com/')
# 名前の変更
br.select_form(nr = 0)
br['new_nick'] = 'name'
br.submit()
# 新しいゲームの開始
res = br.open('http://saichugen-online.appspot.com/')
link = br.find_link(text='新しいゲームの開始')
url = link.url

# ゲーム開始準備
from myai import MyAI
ai = MyAI([])
unknowns = range(52)
info = dict(
    unknowns = unknowns
)
play = ''

for i in range(15):
    # ページの読み取り
    if play == '':
        res = br.follow_link(link)
    else:
        url = re.compile('(.*)\?.*')
        s = url.search(br.geturl())
        if s:
            res = br.open(s.group(1) + play)
        else:
            res = br.open(br.geturl() + play)

    lines = []
    while 1:
        line = res.readline()
        if line == "":
            break
        lines.append(line)

    # ページの解析
    play = re.compile('play/\d*')
    now = re.compile('第(\d)ラウンド, 第(\d)ターンです.' )
    game_score = re.compile('現在のゲームのスコア.*\[(\d*), (\d*), (\d*)\]')
    round_score = re.compile('現在のラウンドスコア.*\[(\d*), (\d*), (\d*)\]')
    result = re.compile('ターン\dの結果.*<br/>')
    card = re.compile('([cdhs]\d{2}).png')
    plays = []
    results = []
    for j in lines:
        # 現在の状況
        s = now.search(j)
        if s:
            print '第%sラウンド' % s.group(1)
            print '第%sターン' % s.group(2)
            info['iround'] = int(s.group(1))
            info['iturn'] = int(s.group(2))
        # 自分の手持ちカード
        s = play.search(j)
        if s:
            plays.append(s.group())
        # 現在のスコア
        s = game_score.search(j)
        if s:
            print '現在のゲームのスコア:[%s, %s, %s]' % (s.group(1), s.group(2), s.group(3))
            info['game_score'] = [int(s.group(1)), int(s.group(2)), int(s.group(3))]
        s = round_score.search(j)
        if s:
            print '現在のラウンドスコア:[%s, %s, %s]' % (s.group(1), s.group(2), s.group(3))
            info['round_score'] = [int(s.group(1)), int(s.group(2)), int(s.group(3))]
        # 結果の履歴
        s = result.search(j)
        if s:
            cards = card.findall(s.group())
            for k in [str2num(x) for x in cards]:
                if k in unknowns:
                    unknowns.remove(k)

    my_cards = []
    for j in plays:
        my_cards.append(int(j[5:]))
    print 'my_cards:', my_cards
    ai.hand = my_cards
    play = 'play/%d' % ai.play(info)
    print play

    print ""

# 結果の表示
url = re.compile('(.*)\?.*')
s = url.search(br.geturl())
if s:
    res = br.open(s.group(1) + play)
while 1:
    line = res.readline()
    if line == "":
        break
    point = re.compile('(.*: -?\d点)')
    s = point.search(line)
    if s:
        print s.group()

間違って「新しいゲームの開始」のリンクを2回押してしまうように書いてしまい、ゲーム開始状態で止まってるものがたくさんできてしまった。あとで消化しよう ><


名前をsugyan(bot)に変えてみたものの、結局アカウントは同じだから自分の成績と合算されることになるみたい。どっちにしろ良くない成績だ orz
とりあえずAIを改良していこう。。