繁体   English   中英

Python脚本测试游戏

[英]Python script to test game

我已经编写了一个名为reversi.py的游戏,我想编写一个脚本来帮助测试。 该游戏基于AI,需要大量时间才能运行。 我希望编写一个脚本来运行游戏并将其结果输出到文件中,这样我就可以在执行其他操作并返回到它的同时运行x次游戏。 我一直试图从脚本文件中调用游戏。 这是我到目前为止的内容:

from games import *
from reversi import *

def main():

    f = open('Reversi Test', 'w')


if __name__ == '__main__':
    main()

提前致谢!

如果程序写入标准输出,则只需将其重定向到其他文件。 类似于以下内容

import sys

from games import *
from reversi import *

def main():

    N = 100
    for i in range(N):
       sys.stdout = open('Reversi_Test_' + str(i), 'w')
       game() # call your method here
       sys.stdout.close()

if __name__ == '__main__':
    main()

您还可以使用with语句:

from future import with_statement
import sys

from games import *
from reversi import *

def main():

    N = 100
    for i in range(N):
       with open('Reversi_Test_' + str(i), 'w') as sys.stdout:
           game() # call your method here

if __name__ == '__main__':
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM