# -*- coding: utf-8 -*-
import random

class cows_and_bulls():
    def gen_ans():
        ans = []
        while len(ans) != 4:
            n = random.randrange(0, 10)
            if not n in ans:
                ans.append(n)
        return ans
    def check_input(user_input):
        try: int(user_input)
        except: return False
        for x in user_input:
            if user_input.count(x)>1:
                return False
        if len(user_input) != 4: return False
        else: return True

    def check_ans(ans, nums):
        a, b = 0, 0
        for n in nums:
            if n in ans:
                if nums.index(n) == ans.index(n):
                    a += 1
                else:
                    b += 1
        return a, b

    def game1(answer,user_input):
        if check_input(user_input):
            num = []
            for x in user_input:
                num.append(int(x))
            a, b = check_ans(answer, num)
            if a == 4:
                return "4A0B，恭喜猜中數字囉！"
            else:
                return "%dA%dB，繼續加油吧！" % (a, b)
        else:
            return "你必須從0 ~ 9選出四個不重複數字的來玩。\n範例: !1234\n再嘗試一遍吧。"
# -*- coding: utf-8 -*-
import random

class cows_and_bulls():
    def gen_ans():
        ans = []
        while len(ans) != 4:
            n = random.randrange(0, 10)
            if not n in ans:
                ans.append(n)
        return ans
    def check_input(user_input):
        try: int(user_input)
        except: return False
        for x in user_input:
            if user_input.count(x)>1:
                return False
        if len(user_input) != 4: return False
        else: return True

    def check_ans(ans, nums):
        a, b = 0, 0
        for n in nums:
            if n in ans:
                if nums.index(n) == ans.index(n):
                    a += 1
                else:
                    b += 1
        return a, b

    def game1(answer,user_input):
        if check_input(user_input):
            num = []
            for x in user_input:
                num.append(int(x))
            a, b = check_ans(answer, num)
            if a == 4:
                return "4A0B，恭喜猜中數字囉！"
            else:
                return "%dA%dB，繼續加油吧！" % (a, b)
        else:
            return "你必須從0 ~ 9選出四個不重複數字的來玩。\n範例: !1234\n再嘗試一遍吧。"
# -*- coding: utf-8 -*-