lis中的可变变量

2024-09-30 01:20:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个简单的浮桥游戏(类似于21点),我已经制作了一个牌组列表。如果我给ACE一个1或14的值,我当前的游戏版本可以运行,但是我需要它同时有两个值,所以如果一手牌超过21,ACE会返回1。你知道吗

deck = [ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

所以选卡的基本过程是这样的:

while True:
    hand = []
    hand.extend(random.sample(deck, 1))    
    print(hand)
    while True:
        hit = input("new card? (k/e)")
        if hit == "k":
            hand.extend(random.sample(deck, 1))
            print("")
            print("HAND:")
            print(hand)
            a = sum(hand)
            print("")
            print("SUM:")
            print(a)
            if a > 21:
                print("")
                print("OVER")
                break
            elif a == 21:
                print("")
                print("Pontoon")
                break
            else:
                continue

我试着把ACE作为一个函数,但是随机抽样这不管用

def ACE():
    if a > 21:
        A = 1
    else:
        A = 14
    return int(A)

那我怎么才能让王牌成为1和14的选手呢?你知道吗


Tags: sampletrue游戏ifrandomelseprintdeck
2条回答

不更改变量的值,而是调整求和函数:

import random

ACE = 14
deck = [ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
        ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
        ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
        ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

import copy

def sumHand(hand):
    """ calculates the sum of cards in hand. if over 21 it will substitute
    14 with 1 as ACE can be both. If no 14 in hand or below 21 will return value"""

    k = hand.copy() # copy hand into k, so it will still show 14 but 
                    # you can sum up as few ACE as needed to be 1 wehn 
                    # calculating this sum

    # maybe change 14th to 1 if neeed be
    while sum(k) > 21 and ACE in k:
        k.remove(ACE) # remove  a 14
        k.append(1)   # replace by 1
    return sum(k)

while True:
    hand = [ACE,ACE] # start with 2 aces already for testing purposes
    hand.extend(random.sample(deck, 1))    
    print(hand)
    while True:
        hit = input("new card? (k/e)")
        if hit == "k":
            hand.extend(random.sample(deck, 1))
            print("")
            print("HAND:")
            print(hand)
            a = sumHand(hand)
            print("")
            print("SUM:")
            print(a)
            if a > 21:
                print("")
                print("OVER")
                break
            elif a == 21:
                print("")
                print("Pontoon")
                break
            else:
                continue

输出:

[14, 14, 8]
new card? (k/e)
HAND:
[14, 14, 8, 7]

SUM:
17
new card? (k/e)
HAND:
[14, 14, 8, 7, 5]

SUM:
22

OVER

可以通过将ACE定义为类来实现这一点

class ACE():
    value = 1
    def set_value(self, value):
        self.value = value

然后你可以像这样改变数组元素的值

ace = ACE() # in this step you have ace value = 1
array = [ace, 1, 2, 3, ...]
for a in array:
    if type(a) == type(ACE):
        # Your desired conditions
        a.set_value(14)

相关问题 更多 >

    热门问题