2012年8月11日 星期六

擲骰子遊戲

模擬 烤香腸路邊攤 擲骰子遊戲
規則:由雙方各擲一次骰子比大小,一次擲四顆,其中二顆需要同點數,計算另二顆相異點數總和,如四顆同點數,則為最大稱為「一色」,三點則為最小。

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
# -*- coding: UTF-8 -*-
import random

def score_count(point):
    score = 0
    check = len([i for i in set(point)])
    if check == 1:
        score = 19
    elif check == 2:
        if point.count(max(point)) == 2:
            score = max(point) * 2
        else:
            score = 0
    elif check == 3:
        for i in point:
            if point.count(i) == 1:
                score += i
    else:
        score = 0
    return score

def rand_dice(name):
    i = 1
    score = 0
    while not score:
        point = [random.choice(dice) for c in range(4)]
        score = -1
        score = score_count(point)
        print "%s第 %s 次,擲出:" % (name, i), point
        if score == 19:
            print "一色"
        else:
            print score, "點"
        i += 1
    return score

print "***擲骰子遊戲***"
win, lose = 0, 0
dice = [i for i in range(1,7)]

while 1:
    c_score = rand_dice('電腦')
    play = raw_input('請按Enter擲骰子')
    u_score = rand_dice('玩家')
    if c_score > u_score:
        print '你輸了!'
        lose += 1
    elif c_score < u_score:
        print '你贏了!'
        win += 1
    else:
        print '平手!'
    print '目前戰績%s勝%s負' % (win, lose)
    play = raw_input('是否繼續(y/n)?')
    if play in ('n', 'N'):
        print '目前戰績%s勝%s負' % (win, lose)
        print 'Good Bye!'
        break
    else:
        pass

沒有留言:

張貼留言