0

我正在尝试构建一个战争纸牌游戏模拟器,但遇到了一些麻烦。出于某种原因,手牌总是赢。这是我的代码:

import random

def war(A,B):                                         ##determines which player A or B wins or if its a tie
    if A > B:
        return 'a'
    elif A < B:
        return 'b' 
    else:
        return 'tie'

def battle(frombefore,hand_a,hand_b):                       ##using winner, it transfers the cards, or if tie, it does another war and battelma
    winner = war(hand_a[0],hand_b[0])
    for k in frombefore:
        temp.append(k)
    if winner == 'a':
            hand_a.append(hand_a.pop(0))
            hand_a.append(hand_b.pop(0))
    elif winner == 'b':
            hand_b.append(hand_b.pop(0))
            hand_b.append(hand_a.pop(0))        
    else:
        if len(hand_a)>5 and len(hand_b)>5:
            temp = []
            temp.append(hand_a.pop(0))
            temp.append(hand_b.pop(0))
            temp.append(hand_a.pop(0))
            temp.append(hand_a.pop(0))
            temp.append(hand_a.pop(0))
            temp.append(hand_b.pop(0))
            temp.append(hand_b.pop(0))
            temp.append(hand_b.pop(0))
            winner2 = war(hand_a[0],hand_b[0])
            if winner2 == 'a':
                temp.append(hand_a.pop(0))
                temp.append(hand_b.pop(0))
                for j in temp:
                    hand_a.append(j)
            if winner2 == 'b':
                temp.append(hand_a.pop(0))
                temp.append(hand_b.pop(0))
                for j in temp:
                    hand_a.append(j)
            if winner2 == 'tie':
                battle(frombefore,hand_a,hand_b)

        else:
            if len(hand_a) > len(hand_b):
                   hand_b = []
            if len(hand_b) > len(hand_a):
                   hand_a = [] 

    return hand_a,hand_b


def playgame():
    handd_a = [2,3,4,5,6,7,8,9,10,11,12,13,14,2,3,4,5,6,7,8,9,10,11,12,13,14]
    handd_b = [2,3,4,5,6,7,8,9,10,11,12,13,14,2,3,4,5,6,7,8,9,10,11,12,13,14]
    random.shuffle(handd_a)
    random.shuffle(handd_b)

    while len(handd_a)>0 and len(handd_b)>0:
        handd_a,handd_b = battle([],handd_a,handd_b)

    if len(handd_a)>0:
        return 'a'
    elif len(handd_b)>0:
       return 'b'



##print(playgame())

Awins = 0
Bwins = 0
for i in range(5):
    current = playgame()
    print(current)
    if current == 'a':
        Awins = Awins + 1
    if current == 'b':
        Bwins = Bwins + 1
print(Awins)
print(Bwins)

我想我的问题可能是处理关系,也许是因为我在内部调用了一个函数?我对此有点迷茫。

4

1 回答 1

2

这看起来不对:

if current == 'b':
    Awins = Awins + 1

应该 :Bwins = Bwins + 1

于 2013-06-19T19:51:34.923 回答