首页 > 编程 > Python > 正文

Python版的文曲星猜数字游戏代码

2020-02-23 04:56:25
字体:
来源:转载
供稿:网友

代码如下:
# -*- coding: utf-8 -*-

import random

#数字类
class NumberItem:

    #数字个数
    _GUESS_NUMBER_COUNT_ = 4

    def __init__(self):
        self._num_ = []

    #长度是否标准   
    def IsFormat(self):
        return self._num_.__len__() == self._GUESS_NUMBER_COUNT_

    #生成随机数   
    def RestRandomNumber(self):
        allNum = range(10)
        for index in range(self._GUESS_NUMBER_COUNT_):
            self._num_.append(allNum.pop(random.randrange(10-index)))

    #校验输入数       
    def CheckNumber(self, input_number):
        result = NumberCheckResult()

        for index in range(self._GUESS_NUMBER_COUNT_):
            if (input_number._num_[index] == self._num_[index]):
                result.Add_A()
            elif(input_number._num_[index] in self._num_):
                result.Add_B()

        return result

    #返回字符串     
    def GetNumber(self):
        return self._num_

   
#竞猜结果 xAxB   
class NumberCheckResult:
    def __init__(self):
        self._guess_A_ = 0
        self._guess_B_ = 0

    def GetCheckResult(self):
        return '%d A %d B' % (self._guess_A_, self._guess_B_)

    def Add_A(self):
        self._guess_A_ += 1

    def Add_B(self):
        self._guess_B_ += 1

   
#竞猜历史项   
class GuessHisItem:
    def __init__(self):
        self._guessNum_ = NumberItem()
        self._guessResult_ = NumberCheckResult()

    def ShowItem(self):
        print self._guessNum_.GetNumber() , ' - ' , self._guessResult_.GetCheckResult()

#游戏类
class GuessNumberGame():

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表