A very-very simple dice game:
two persons("You" and "CPU") are dropping dices.
If someone scores 17 during dropping dices, he wins.
Any doubts are interpreted to a humansside.
(i
m too lasy to add draw here)
And one more. Rules say- "if someone drops 3 same dices at the beginning, he wins."
this is not realized in game, too. (but the probability of this is just about 1.4%)
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/env python
# A simple game. auth: DR#m; last rev: 17-04
# Rules: CPU and man are dropping dices
# if someone scores > 17, he looses
import random
global manSum; manSum=0
global compSum; compSum=0
global manFinished; manFinished=False
global CPUfinished; CPUfinished=False
global gameover; gameover=False
global debug; debug=False # some additional info about game flow
def dice():
"""Number of points in dice"""
return random.randint(1,6)
def looser(who):
"""Finishes the game"""
global manSum, compSum, gameover
print
if who=="man":
print "LOOSER"
elif who=="CPU":
print "Congratulations!"
print
print "your score: ", manSum
print "CPU score: ", compSum
gameover = True;
def testWinLoose():
"""Complicated test- if somebody won?"""
global manSum, compSum, debug
if (compSum >= 18):
looser("CPU")
elif (manSum >= 18):
looser("man");
else:
if (manSum == 17):
looser("CPU")
elif (compSum == 17):
looser("man")
else: # nobody wants to play ?
if (manFinished and CPUfinished):
manCount = 17 - manSum
compCount = 17 - compSum
if debug:
print "score testing: "
if manCount > compCount:
looser("man")
else: looser("CPU")
def CPUdrop():
""" CPU drops his dice.
The algorithm is following:
guess the next number in dice (randomly)
and drop dice again to increase score
if you want to cheat, put "+1" somewhere here ;-)
"""
global CPUfinished, manSum, compSum, debug
if (not CPUfinished): # already refused
NeedPoints = 17 - compSum # how much we need?
if (dice() <= NeedPoints): # probability is well today
compSum += dice() # dice() is called twice, should return different values
if debug:
print "CPU played. His points: ", compSum
else: # too big score
CPUfinished = True # enough points
if debug:
print "CPU refused. His points: ", compSum
print "You are welcome!"
for a in xrange(3): # first tree drops
manSum+=dice()
compSum+=dice()
while (not gameover):
if (not manFinished):
# let`s print the menu for user
print "Ur score", manSum
if (debug): # usually, opponent`s score is unknown
print "CPU score", compSum
print "1: Drop!"
print "2: Enough!"
print "or give up"
inp=raw_input()
if inp == "1": # dice dropping
manSum += dice()
elif inp == "2":
manFinished = True # man- enough
else: # all other input is forbidden :))
if (debug):
print "Why`ve you gived up?"
looser("man")
else: #if (not manFinished)
if (debug):
print "Scipping menu..."
CPUdrop(); # CPU plays after
testWinLoose(); # after dice dropping
print "hit Enter to exit!"
raw_input()
|
i advise to run this from comand line, i saw IDLE problems with raw_input()