RickWang522 2019-07-14 20:13 采纳率: 0%
浏览 1380

猜数字(Bulls and Cows)如何用python实现?

猜数字具体规则见:http://baike.baidu.com/subview/358630/11117097.htm

玩家和电脑对抗的模式,玩法为猜数字游戏标准玩法。

游戏流程:

执行guess.py文件开始游戏。

脚本随机生成一个没有重复的4位数字作为被猜数字。

玩家和电脑轮流猜数字。

玩家通过命令行输入数字,脚本打印出1A1B样式的测试结果。

脚本打印出电脑的猜测数字和结果。电脑不能使用玩家的猜测结果。

重复此过程直至一方猜中。

打印出胜利者的信息,脚本结束执行。

要求:

使用python2.7版本

使用一个py文件来实现,命名为guess.py,完成上述玩法需求。

guess.py文件可以作为模块被其他脚本调用,提供如下接口:

check:根据猜测数和预期数2个参数返回1A1B格式结果。

calculate:根据猜测数、该猜测数的1A1B格式结果,返回所有可能是正确答案的数据集。

在github上有一个比较完善的程序(python3.0)https://github.com/vpavlenko/bulls-and-cows/blob/master/solver.py

这两天我自己写好的python2.7代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-


import random


def random_num():
#生成4位无重复数字的随机数(首位不可为0)
    com_num = random.sample('123456789', 4)
    com_num = ''.join(com_num)
    return com_num

def player_input():
#判断玩家输入合法性
    while(True):
        try:
            global player_num
            player_num = str(raw_input('\nYour turn! Enter Your Guess Number:'))
            z = int(player_num)
            if(len(player_num) != 4):
                print('Please enter a 4 DIGIT number.')
                continue

        except ValueError:
            if player_num == 'quit':
                exit()
            print('Your input is not a number.')

        else:
            if(len(set(player_num)) != 4):
                print('Please enter a number with different digits.')
                continue
            elif player_num[0] == 0:
                print('Please enter a number that first number is not 0.')
                continue
            else:
                return

def check(number, question):
#根据猜测数和被猜数计算返回1A1B样式的结果"(1, 1)"
    number = str(number)
    question = str(question)
    global a_count
    global b_count
    a_count = 0
    b_count = 0

    for i in range(4):
        for j in range(4):
            if(number[i] == question[j]):
                if(i == j):
                    a_count += 1
                else:
                    b_count += 1

    return (a_count, b_count)   

def allowed_number(number):
#判断数字合法性
    return len(str(number)) == 4 and len(set(str(number))) == 4

all_numbers = [number for number in range(1234, 9877) if allowed_number(number)]
#所有可能结果

def calculate(number, answer):
#根据猜测数和1A1B结果计算返回所有可能结果
    number = str(number)
    global num_series
    num_series = []

    for possible_num in all_numbers:
        if not abs(cmp(check(possible_num, number), answer)):
            num_series.append(possible_num)

    return num_series

def history_rec(number):
#记录本轮游戏出现过的数字
    global history
    return history.append(int(number))

def del_history(num_series, history):
#删除本轮游戏中出现过的数字
    for number in history:
        if number in num_series:
            num_series.remove(number)  

    return num_series  

def ai_best():
#反馈个数指标评判答案最优解
    global best
    if not (best[0]+best[1] > answer[0]+answer[1]):
        if not (best[0] > answer[0]):
            best = answer

def history_area(num_series):
#计算保存最可能的值域,取历史值域及新值域的交集
    global area
    intersection = [i for i in area if i in num_series]
    intersection = list(set(area).intersection(set(num_series)))
    return intersection


print'''
----------Guess The Number Game: Player VS AI----------

You can input"quit"to exit

Rules:
1.Question number is a random 4 digit number without same digits (first digit can't be 0).

2.Player and AI guess the number one by one until Player Win or AI Win.

3.After every guess, program will return an answer of 1A1B like:"(1, 1)".
A value is the number of digit and position are all correct.
B value is just the number of digit is correct (position is wrong).'''

player_num = 0
ai_num = 0
random_number = random_num()
player_turn = 1
answer = (0, 0)
a_count = 0
best = (0, 0)
history = []
area = all_numbers

while(a_count != 4):
    question = random_number
    num_series = []
#玩家的回合
    if player_turn:
        player_input()
        history_rec(player_num)
        answer = check(player_num, question)
        a_count = answer[0]
        print "\nYour choice is", player_num
        print "Your answer is", answer
        ai_best()
#电脑的回合
    else:
        ai_num = random.choice(area)
        history_rec(ai_num)
        answer = check(ai_num, question)
        a_count = answer[0]
        print "\nAI's choice is", ai_num
        print "AI's answer is", answer
        ai_best()

    num_series = del_history(calculate(player_num, best), history)
    area = history_area(num_series)
    print num_series
    print area
    player_turn = not player_turn
#输出胜负结果    
if player_turn:
    print("\nYou Win!")      
else:
    print("\nAI Win!")

但是运行几次老是报错

Traceback (most recent call last):
  File "C:\Python27\guess.py", line 144, in <module>
    ai_num = random.choice(area)
  File "C:\Python27\lib\random.py", line 275, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range
  • 写回答

2条回答 默认 最新

  • threenewbee 2019-07-15 09:05
    关注

    nonlocal不支持python 2.x
    https://cndenis.iteye.com/blog/1754743

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛