![]() |
#2
libralibra2011-03-26 04:16
楼主加我q,790404545
代码写完了,测试如下: ![]() IDLE 2.6.6 ==== No Subprocess ==== >>> Would you like to play(Y/N):m # 测试错误输入 Wrong input! Would you like to play(Y/N):r # 测试错误输入 Wrong input! Would you like to play(Y/N):n # 测试n >>> Would you like to play(Y/N):y # 开始测试游戏流程 Great! What's you name:test Hi, test! ===========================Player START=========================== |--Dic1: 4; Dic2: 6 |---No ONEs! Score for this round: 10; Total score: 10 Do you want roll or hold?(r/h)h # 测试一次h,以后全部选择r ===========================Player END=========================== ===========================Computer START=========================== |--Dic1: 1; Dic2: 2 |---There's 1 ONE, round is ended! ===========================Computer END=========================== ===========================Player START=========================== |--Dic1: 2; Dic2: 3 |---No ONEs! Score for this round: 5; Total score: 15 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 1; Dic2: 2 |---There's 1 ONE, round is ended! ===========================Player END=========================== ===========================Computer START=========================== |--Dic1: 5; Dic2: 1 |---There's 1 ONE, round is ended! ===========================Computer END=========================== ===========================Player START=========================== |--Dic1: 2; Dic2: 6 |---No ONEs! Score for this round: 8; Total score: 23 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 6; Dic2: 3 |---No ONEs! Score for this round: 9; Total score: 32 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 2; Dic2: 6 |---No ONEs! Score for this round: 8; Total score: 40 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 3; Dic2: 4 |---No ONEs! Score for this round: 7; Total score: 47 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 6; Dic2: 6 |---No ONEs! Score for this round: 12; Total score: 59 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 3; Dic2: 5 |---No ONEs! Score for this round: 8; Total score: 67 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 4; Dic2: 3 |---No ONEs! Score for this round: 7; Total score: 74 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 1; Dic2: 4 |---There's 1 ONE, round is ended! ===========================Player END=========================== ===========================Computer START=========================== |--Dic1: 1; Dic2: 2 |---There's 1 ONE, round is ended! ===========================Computer END=========================== ===========================Player START=========================== |--Dic1: 4; Dic2: 2 |---No ONEs! Score for this round: 6; Total score: 80 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 3; Dic2: 5 |---No ONEs! Score for this round: 8; Total score: 88 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 6; Dic2: 3 |---No ONEs! Score for this round: 9; Total score: 97 Do you want roll or hold?(r/h)r ===========================Player START=========================== |--Dic1: 3; Dic2: 2 |---No ONEs! Score for this round: 5; Total score: 102 # 达到100分,胜利 *************Congratulations! You win!************* Would you like to play(Y/N):n # 询问是否重新开始 >>> [ 本帖最后由 libralibra 于 2011-3-26 04:26 编辑 ] |
规则是这样的:玩家和电脑,各两颗骰子,如果掷出的两颗骰子没有1,则分数累积,玩家可选择是否继续或结束回合,如果两颗骰子之中有一个1,则所得分数不予累计,强制结束回合,如果掷到两个1,累计的分数全部清零,并且强制结束回合,由电脑继续游戏。如此,直到有一方累计分数达到100或以上的,游戏胜利。题目中给予电脑的条件是只要在一回合中累计得分20点,则结束回合,由玩家继续游戏。

# Start
# Play control:
play = input('Would you like to play PIG (y/n)? ')# Play control
while play == 'y':
# Welcome:
username = input('Great! What is your name? ')
print('Hi',username)
# Game:
scoreP = 0# Player's potential score
scoreC = 0# Computer's potential score
turn = 0# Turn control(Player: 0, Computer:1)
# Player's turn:
print('|------------------- Start Player\'s Turn ---------------------')
# Roll or hold:
while turn == 0:
hold = input('| Would you like to roll or hold (r/h)? ')
while hold =='r':
totalP = 0# Player's turn total
import random
dicP1 = random.randint(1,6)# Player's dice 1
dicP2 = random.randint(1,6)# Player's dice 2
toP = 0# Total of this turn
# No 1
if dicP1 and dicP2 != 1:
stepP = dicP1 + dicP2# Step total
toP = totalP + stepP
scoreP = scoreP + toP
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('| Turn total:',toP,'| Potential score:',scoreP)
print('|')
hold = input('| Would you like to roll or hold (r/h)? ')
# If end
# Single 1
elif dicP1 or dicP2 == 1:
stepP = dicP1 + dicP2
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('|')
print('| *** You rolled a ONE - Turn over - No points for you!')
# Turn end show:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
turn = turn + 1
# Elif end
# Double 1s
else:
stepP = dicP1 + dicP2
totalP = 0
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('|')
print('| *** Ouch! That hurts! Two ONEs - You lose your entire score!')
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
turn = turn + 1
# Else end
# While End
else:
turn = turn+ 1
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
# while end
# Else end
# While end
# Player's Turn end
# Computer's turn:
else:
# Roll or hold:
totalC = 0
while totalC < 20:
import random
dicC1 = random.randint(1,6)# Computer's dice 1
dicC2 = random.randint(1,6)# Computer's dice 2
# No 1
if dicC1 and dicC2 != 1:
stepC = dicC1 + dicC2# Step total
toC = stepC + totalC
scoreC = scoreC + toC
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('| Turn total:',toC,'| Potential score:',scoreC)
print('|')
# If end
# Single 1
elif dicC1 or dicC2 == 1:
stepC = dicC1 + dicC2
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('|')
print('| *** Computer rolled a ONE - Turn over - No points for computer!')
# Turn end show:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Elif end
# Double 1s
else:
stepC = dicC1 + dicC2
totalC = 0
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('|')
print('| *** Ouch! That hurts! Two ONEs - Computer lose entire score!')
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Else end
# While end
else:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Else end
# Computer's turn end
# Replay control:
play = input('Would you like to play again (y/n)? ')
# While end
#
else:
print('Thanks for playing')
# Play control:
play = input('Would you like to play PIG (y/n)? ')# Play control
while play == 'y':
# Welcome:
username = input('Great! What is your name? ')
print('Hi',username)
# Game:
scoreP = 0# Player's potential score
scoreC = 0# Computer's potential score
turn = 0# Turn control(Player: 0, Computer:1)
# Player's turn:
print('|------------------- Start Player\'s Turn ---------------------')
# Roll or hold:
while turn == 0:
hold = input('| Would you like to roll or hold (r/h)? ')
while hold =='r':
totalP = 0# Player's turn total
import random
dicP1 = random.randint(1,6)# Player's dice 1
dicP2 = random.randint(1,6)# Player's dice 2
toP = 0# Total of this turn
# No 1
if dicP1 and dicP2 != 1:
stepP = dicP1 + dicP2# Step total
toP = totalP + stepP
scoreP = scoreP + toP
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('| Turn total:',toP,'| Potential score:',scoreP)
print('|')
hold = input('| Would you like to roll or hold (r/h)? ')
# If end
# Single 1
elif dicP1 or dicP2 == 1:
stepP = dicP1 + dicP2
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('|')
print('| *** You rolled a ONE - Turn over - No points for you!')
# Turn end show:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
turn = turn + 1
# Elif end
# Double 1s
else:
stepP = dicP1 + dicP2
totalP = 0
print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP)
print('|')
print('| *** Ouch! That hurts! Two ONEs - You lose your entire score!')
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
turn = turn + 1
# Else end
# While End
else:
turn = turn+ 1
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Player\'s Turn -----------------------')
print('|------------------- Start Computer\'s Turn -------------------')
# while end
# Else end
# While end
# Player's Turn end
# Computer's turn:
else:
# Roll or hold:
totalC = 0
while totalC < 20:
import random
dicC1 = random.randint(1,6)# Computer's dice 1
dicC2 = random.randint(1,6)# Computer's dice 2
# No 1
if dicC1 and dicC2 != 1:
stepC = dicC1 + dicC2# Step total
toC = stepC + totalC
scoreC = scoreC + toC
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('| Turn total:',toC,'| Potential score:',scoreC)
print('|')
# If end
# Single 1
elif dicC1 or dicC2 == 1:
stepC = dicC1 + dicC2
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('|')
print('| *** Computer rolled a ONE - Turn over - No points for computer!')
# Turn end show:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Elif end
# Double 1s
else:
stepC = dicC1 + dicC2
totalC = 0
print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC)
print('|')
print('| *** Ouch! That hurts! Two ONEs - Computer lose entire score!')
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Else end
# While end
else:
print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC)
print('|------------------- End Computer\'s Turn ---------------------')
turn = turn - 1
# Else end
# Computer's turn end
# Replay control:
play = input('Would you like to play again (y/n)? ')
# While end
#
else:
print('Thanks for playing')