战争牌游戏模拟器错误
问题描述:
我想建立一个战争牌游戏模拟器,但我有一些麻烦。出于某种原因,手总是赢。这里是我的代码:战争牌游戏模拟器错误
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)
我想可能我的问题是处理关系,也许因为我在调用自己的函数?我对此有点失落。
答
这看起来错:
if current == 'b':
Awins = Awins + 1
应该是:Bwins = Bwins + 1
是啊,这是错误的,但仍然问题是不固定的。不过谢谢 – user2502725